Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Migrate these tests to the "new" API #7189

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/camera/camera_android/test/method_channel_mock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class MethodChannelMock {
this.delay,
required this.methods,
}) : methodChannel = MethodChannel(channelName) {
methodChannel.setMockMethodCallHandler(_handler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, _handler);
}

final Duration? delay;
Expand All @@ -37,3 +39,9 @@ class MethodChannelMock {
});
}
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class MethodChannelMock {
this.delay,
required this.methods,
}) : methodChannel = MethodChannel(channelName) {
methodChannel.setMockMethodCallHandler(_handler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, _handler);
}

final Duration? delay;
Expand All @@ -37,3 +39,9 @@ class MethodChannelMock {
});
}
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class MethodChannelMock {
this.delay,
required this.methods,
}) : methodChannel = MethodChannel(channelName) {
methodChannel.setMockMethodCallHandler(_handler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, _handler);
}

final Duration? delay;
Expand All @@ -37,3 +39,9 @@ class MethodChannelMock {
});
}
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class MethodChannelMock {
this.delay,
required this.methods,
}) : methodChannel = MethodChannel(channelName) {
methodChannel.setMockMethodCallHandler(_handler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, _handler);
}

final Duration? delay;
Expand All @@ -43,3 +45,9 @@ class MethodChannelMock {
});
}
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ void main() {
setUp(() {
plugin = FileSelectorLinux();
log = <MethodCall>[];
plugin.channel.setMockMethodCallHandler((MethodCall methodCall) async {
log.add(methodCall);
return null;
});
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
plugin.channel,
(MethodCall methodCall) async {
log.add(methodCall);
return null;
},
);
Comment on lines +19 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what most of the changes on this PR look like:

- x.y.setMockMethodCallHandler(handler);
+ _ambiguate(TestDefaultBinaryMessengerBinding.instance)!
+  .defaultBinaryMessenger
+  .setMockMethodCallHandler(x.y, handler)

});

test('registers instance', () {
Expand Down Expand Up @@ -424,3 +429,9 @@ void expectMethodCall(
}) {
expect(log, <Matcher>[isMethodCall(methodName, arguments: arguments)]);
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ void main() {
final List<MethodCall> log = <MethodCall>[];

setUp(() {
plugin.channel.setMockMethodCallHandler((MethodCall methodCall) async {
log.add(methodCall);
return null;
});
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
plugin.channel,
(MethodCall methodCall) async {
log.add(methodCall);
return null;
},
);

log.clear();
});
Expand Down Expand Up @@ -274,3 +279,9 @@ void expectMethodCall(
}) {
expect(log, <Matcher>[isMethodCall(methodName, arguments: arguments)]);
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ void main() {
FakePlatformViewsController();

setUpAll(() {
SystemChannels.platform_views.setMockMethodCallHandler(
fakePlatformViewsController.fakePlatformViewsMethodHandler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
SystemChannels.platform_views,
fakePlatformViewsController.fakePlatformViewsMethodHandler,
);
});

setUp(() {
Expand Down Expand Up @@ -194,3 +198,9 @@ void main() {
expect(platformGoogleMap.circlesToAdd.isEmpty, true);
});
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class FakePlatformGoogleMap {
: cameraPosition =
CameraPosition.fromMap(params['initialCameraPosition']),
channel = MethodChannel('plugins.flutter.io/google_maps_$id') {
channel.setMockMethodCallHandler(onMethodCall);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(channel, onMethodCall);
updateOptions(params['options'] as Map<dynamic, dynamic>);
updateMarkers(params);
updatePolygons(params);
Expand Down Expand Up @@ -478,3 +480,9 @@ Map<dynamic, dynamic>? _decodeParams(Uint8List paramsMessage) {
return const StandardMessageCodec().decodeMessage(messageBytes)
as Map<dynamic, dynamic>?;
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ void main() {
FakePlatformViewsController();

setUpAll(() {
SystemChannels.platform_views.setMockMethodCallHandler(
fakePlatformViewsController.fakePlatformViewsMethodHandler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
SystemChannels.platform_views,
fakePlatformViewsController.fakePlatformViewsMethodHandler,
);
});

setUp(() {
Expand Down Expand Up @@ -576,3 +580,9 @@ void main() {
expect(platformGoogleMap.buildingsEnabled, true);
});
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ void main() {
FakePlatformViewsController();

setUpAll(() {
SystemChannels.platform_views.setMockMethodCallHandler(
fakePlatformViewsController.fakePlatformViewsMethodHandler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
SystemChannels.platform_views,
fakePlatformViewsController.fakePlatformViewsMethodHandler,
);
});

setUp(() {
Expand Down Expand Up @@ -200,3 +204,9 @@ void main() {
expect(platformGoogleMap.markersToAdd.isEmpty, true);
});
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ void main() {
FakePlatformViewsController();

setUpAll(() {
SystemChannels.platform_views.setMockMethodCallHandler(
fakePlatformViewsController.fakePlatformViewsMethodHandler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
SystemChannels.platform_views,
fakePlatformViewsController.fakePlatformViewsMethodHandler,
);
});

setUp(() {
Expand Down Expand Up @@ -403,3 +407,9 @@ void main() {
expect(platformGoogleMap.polygonsToAdd.isEmpty, true);
});
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ void main() {
FakePlatformViewsController();

setUpAll(() {
SystemChannels.platform_views.setMockMethodCallHandler(
fakePlatformViewsController.fakePlatformViewsMethodHandler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
SystemChannels.platform_views,
fakePlatformViewsController.fakePlatformViewsMethodHandler,
);
});

setUp(() {
Expand Down Expand Up @@ -216,3 +220,9 @@ void main() {
expect(platformGoogleMap.polylinesToAdd.isEmpty, true);
});
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ void main() {
FakePlatformViewsController();

setUpAll(() {
SystemChannels.platform_views.setMockMethodCallHandler(
fakePlatformViewsController.fakePlatformViewsMethodHandler);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
SystemChannels.platform_views,
fakePlatformViewsController.fakePlatformViewsMethodHandler,
);
});

setUp(() {
Expand Down Expand Up @@ -198,3 +202,9 @@ void main() {
expect(platformGoogleMap.tileOverlaysToAdd.isEmpty, true);
});
}

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
T? _ambiguate<T>(T? value) => value;
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ void main() {
required int mapId,
required Future<dynamic>? Function(MethodCall call) handler,
}) {
maps
.ensureChannelInitialized(mapId)
.setMockMethodCallHandler((MethodCall methodCall) {
log.add(methodCall.method);
return handler(methodCall);
});
final MethodChannel channel = maps.ensureChannelInitialized(mapId);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
channel,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight deviation of the pattern here, expected:

_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
    .defaultBinaryMessenger
    .setMockMethodCallhandler(
  maps.ensureChannelInitialized(mapId), (MethodCall methodCall) {...

(I think it's better extracting the channel to its own local, tbh)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird, i wonder why that happened.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i think this was the file i ended up looking at first, before i automated it, so this was done by hand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to clarify, did you want me to make the changes consistent, or should i just land it as-is?
(this code will all get refactored in a few months anyway when the _ambiguate is removed.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i'm going to mark this autosubmit for now given the repo merge, happy to land a separate PR if you think it's worth making the call sites more consistent)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hixie nah, I just left these comments in case others wanted to look at what was "different" from the more automatic/mechanic changes. Thanks for landing this!

(MethodCall methodCall) {
log.add(methodCall.method);
return handler(methodCall);
},
);
}

Future<void> sendPlatformMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ void main() {
required int mapId,
required Future<dynamic>? Function(MethodCall call) handler,
}) {
maps
.ensureChannelInitialized(mapId)
.setMockMethodCallHandler((MethodCall methodCall) {
log.add(methodCall.method);
return handler(methodCall);
});
final MethodChannel channel = maps.ensureChannelInitialized(mapId);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
channel,
(MethodCall methodCall) {
log.add(methodCall.method);
return handler(methodCall);
},
);
}

Future<void> sendPlatformMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ void main() {
required int mapId,
required Future<dynamic>? Function(MethodCall call) handler,
}) {
maps
.ensureChannelInitialized(mapId)
.setMockMethodCallHandler((MethodCall methodCall) {
log.add(methodCall.method);
return handler(methodCall);
});
final MethodChannel channel = maps.ensureChannelInitialized(mapId);
_ambiguate(TestDefaultBinaryMessengerBinding.instance)!
.defaultBinaryMessenger
.setMockMethodCallHandler(
channel,
(MethodCall methodCall) {
log.add(methodCall.method);
return handler(methodCall);
},
);
}

Future<void> sendPlatformMessage(
Expand Down
Loading