diff --git a/dev/automated_tests/flutter_test/ticker_test.dart b/dev/automated_tests/flutter_test/ticker_test.dart index 3d819ab6ca6..50f6d963cf8 100644 --- a/dev/automated_tests/flutter_test/ticker_test.dart +++ b/dev/automated_tests/flutter_test/ticker_test.dart @@ -11,6 +11,6 @@ void main() { Ticker((Duration duration) { })..start(); final ByteData message = const StringCodec().encodeMessage('AppLifecycleState.paused'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {}); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) {}); }); } diff --git a/packages/flutter/lib/services.dart b/packages/flutter/lib/services.dart index 4da9445952c..84e9b07f384 100644 --- a/packages/flutter/lib/services.dart +++ b/packages/flutter/lib/services.dart @@ -11,6 +11,7 @@ library services; export 'src/services/asset_bundle.dart'; +export 'src/services/binary_messenger.dart'; export 'src/services/binding.dart'; export 'src/services/clipboard.dart'; export 'src/services/font_loader.dart'; diff --git a/packages/flutter/lib/src/services/asset_bundle.dart b/packages/flutter/lib/src/services/asset_bundle.dart index a89b2f56d85..e6921c0e775 100644 --- a/packages/flutter/lib/src/services/asset_bundle.dart +++ b/packages/flutter/lib/src/services/asset_bundle.dart @@ -9,7 +9,7 @@ import 'dart:typed_data'; import 'package:flutter/foundation.dart'; -import 'platform_messages.dart'; +import 'binary_messenger.dart'; /// A collection of resources used by the application. /// @@ -216,7 +216,7 @@ class PlatformAssetBundle extends CachingAssetBundle { Future load(String key) async { final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull(key)).path); final ByteData asset = - await BinaryMessages.send('flutter/assets', encoded.buffer.asByteData()); + await defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData()); if (asset == null) throw FlutterError('Unable to load asset: $key'); return asset; diff --git a/packages/flutter/lib/src/services/binary_messenger.dart b/packages/flutter/lib/src/services/binary_messenger.dart new file mode 100644 index 00000000000..b246f0530dd --- /dev/null +++ b/packages/flutter/lib/src/services/binary_messenger.dart @@ -0,0 +1,154 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'dart:typed_data'; +import 'dart:ui' as ui; + +import 'package:flutter/foundation.dart'; + +/// A function which takes a platform message and asynchronously returns an encoded response. +typedef MessageHandler = Future Function(ByteData message); + +/// A messenger which sends binary data across the Flutter platform barrier. +/// +/// This class also registers handlers for incoming messages. +abstract class BinaryMessenger { + /// A const constructor to allow subclasses to be const. + const BinaryMessenger(); + + /// Calls the handler registered for the given channel. + /// + /// Typically called by [ServicesBinding] to handle platform messages received + /// from [Window.onPlatformMessage]. + /// + /// To register a handler for a given message channel, see [setMessageHandler]. + Future handlePlatformMessage(String channel, ByteData data, ui.PlatformMessageResponseCallback callback); + + /// Send a binary message to the platform plugins on the given channel. + /// + /// Returns a [Future] which completes to the received response, undecoded, + /// in binary form. + Future send(String channel, ByteData message); + + /// Set a callback for receiving messages from the platform plugins on the + /// given channel, without decoding them. + /// + /// The given callback will replace the currently registered callback for that + /// channel, if any. To remove the handler, pass null as the [handler] + /// argument. + /// + /// The handler's return value, if non-null, is sent as a response, unencoded. + void setMessageHandler(String channel, Future handler(ByteData message)); + + /// Set a mock callback for intercepting messages from the [send] method on + /// this class, on the given channel, without decoding them. + /// + /// The given callback will replace the currently registered mock callback for + /// that channel, if any. To remove the mock handler, pass null as the + /// [handler] argument. + /// + /// The handler's return value, if non-null, is used as a response, unencoded. + /// + /// This is intended for testing. Messages intercepted in this manner are not + /// sent to platform plugins. + void setMockMessageHandler(String channel, Future handler(ByteData message)); +} + +/// The default implementation of [BinaryMessenger]. +/// +/// This messenger sends messages from the app-side to the platform-side and +/// dispatches incoming messages from the platform-side to the appropriate +/// handler. +class _DefaultBinaryMessenger extends BinaryMessenger { + const _DefaultBinaryMessenger._(); + + // Handlers for incoming messages from platform plugins. + // This is static so that this class can have a const constructor. + static final Map _handlers = + {}; + + // Mock handlers that intercept and respond to outgoing messages. + // This is static so that this class can have a const constructor. + static final Map _mockHandlers = + {}; + + Future _sendPlatformMessage(String channel, ByteData message) { + final Completer completer = Completer(); + // ui.window is accessed directly instead of using ServicesBinding.instance.window + // because this method might be invoked before any binding is initialized. + // This issue was reported in #27541. It is not ideal to statically access + // ui.window because the Window may be dependency injected elsewhere with + // a different instance. However, static access at this location seems to be + // the least bad option. + ui.window.sendPlatformMessage(channel, message, (ByteData reply) { + try { + completer.complete(reply); + } catch (exception, stack) { + FlutterError.reportError(FlutterErrorDetails( + exception: exception, + stack: stack, + library: 'services library', + context: ErrorDescription('during a platform message response callback'), + )); + } + }); + return completer.future; + } + + @override + Future handlePlatformMessage( + String channel, + ByteData data, + ui.PlatformMessageResponseCallback callback, + ) async { + ByteData response; + try { + final MessageHandler handler = _handlers[channel]; + if (handler != null) + response = await handler(data); + } catch (exception, stack) { + FlutterError.reportError(FlutterErrorDetails( + exception: exception, + stack: stack, + library: 'services library', + context: ErrorDescription('during a platform message callback'), + )); + } finally { + callback(response); + } + } + + + @override + Future send(String channel, ByteData message) { + final MessageHandler handler = _mockHandlers[channel]; + if (handler != null) + return handler(message); + return _sendPlatformMessage(channel, message); + } + + @override + void setMessageHandler(String channel, MessageHandler handler) { + if (handler == null) + _handlers.remove(channel); + else + _handlers[channel] = handler; + } + + @override + void setMockMessageHandler(String channel, MessageHandler handler) { + if (handler == null) + _mockHandlers.remove(channel); + else + _mockHandlers[channel] = handler; + } +} + +/// The default instance of [BinaryMessenger]. +/// +/// This is used to send messages from the application to the platform, and +/// keeps track of which handlers have been registered on each channel so +/// it may dispatch incoming messages to the registered handler. +const BinaryMessenger defaultBinaryMessenger = _DefaultBinaryMessenger._(); diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index a1e5554dd3d..7cc1f3525ca 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -7,9 +7,9 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; import 'asset_bundle.dart'; -import 'platform_messages.dart'; +import 'binary_messenger.dart'; -/// Listens for platform messages and directs them to [BinaryMessages]. +/// Listens for platform messages and directs them to the [defaultBinaryMessenger]. /// /// The [ServicesBinding] also registers a [LicenseEntryCollector] that exposes /// the licenses found in the `LICENSE` file stored at the root of the asset @@ -21,7 +21,7 @@ mixin ServicesBinding on BindingBase { super.initInstances(); _instance = this; window - ..onPlatformMessage = BinaryMessages.handlePlatformMessage; + ..onPlatformMessage = defaultBinaryMessenger.handlePlatformMessage; initLicenses(); } diff --git a/packages/flutter/lib/src/services/platform_channel.dart b/packages/flutter/lib/src/services/platform_channel.dart index 80e233b0ac7..b5f98ea4849 100644 --- a/packages/flutter/lib/src/services/platform_channel.dart +++ b/packages/flutter/lib/src/services/platform_channel.dart @@ -7,9 +7,9 @@ import 'dart:typed_data'; import 'package:flutter/foundation.dart'; +import 'binary_messenger.dart'; import 'message_codec.dart'; import 'message_codecs.dart'; -import 'platform_messages.dart'; /// A named channel for communicating with platform plugins using asynchronous /// message passing. @@ -29,10 +29,13 @@ import 'platform_messages.dart'; /// /// See: class BasicMessageChannel { - /// Creates a [BasicMessageChannel] with the specified [name] and [codec]. + /// Creates a [BasicMessageChannel] with the specified [name], [codec] and [binaryMessenger]. /// - /// Neither [name] nor [codec] may be null. - const BasicMessageChannel(this.name, this.codec); + /// None of [name], [codec], or [binaryMessenger] may be null. + const BasicMessageChannel(this.name, this.codec, { this.binaryMessenger = defaultBinaryMessenger }) + : assert(name != null), + assert(codec != null), + assert(binaryMessenger != null); /// The logical channel on which communication happens, not null. final String name; @@ -40,12 +43,15 @@ class BasicMessageChannel { /// The message codec used by this channel, not null. final MessageCodec codec; + /// The messenger which sends the bytes for this channel, not null. + final BinaryMessenger binaryMessenger; + /// Sends the specified [message] to the platform plugins on this channel. /// /// Returns a [Future] which completes to the received response, which may /// be null. Future send(T message) async { - return codec.decodeMessage(await BinaryMessages.send(name, codec.encodeMessage(message))); + return codec.decodeMessage(await binaryMessenger.send(name, codec.encodeMessage(message))); } /// Sets a callback for receiving messages from the platform plugins on this @@ -59,9 +65,9 @@ class BasicMessageChannel { /// message reply. It may be null. void setMessageHandler(Future handler(T message)) { if (handler == null) { - BinaryMessages.setMessageHandler(name, null); + binaryMessenger.setMessageHandler(name, null); } else { - BinaryMessages.setMessageHandler(name, (ByteData message) async { + binaryMessenger.setMessageHandler(name, (ByteData message) async { return codec.encodeMessage(await handler(codec.decodeMessage(message))); }); } @@ -80,9 +86,9 @@ class BasicMessageChannel { /// sent to platform plugins. void setMockMessageHandler(Future handler(T message)) { if (handler == null) { - BinaryMessages.setMockMessageHandler(name, null); + binaryMessenger.setMockMessageHandler(name, null); } else { - BinaryMessages.setMockMessageHandler(name, (ByteData message) async { + binaryMessenger.setMockMessageHandler(name, (ByteData message) async { return codec.encodeMessage(await handler(codec.decodeMessage(message))); }); } @@ -112,8 +118,11 @@ class MethodChannel { /// The [codec] used will be [StandardMethodCodec], unless otherwise /// specified. /// - /// Neither [name] nor [codec] may be null. - const MethodChannel(this.name, [this.codec = const StandardMethodCodec()]); + /// None of [name], [binaryMessenger], or [codec] may be null. + const MethodChannel(this.name, [this.codec = const StandardMethodCodec(), this.binaryMessenger = defaultBinaryMessenger ]) + : assert(name != null), + assert(binaryMessenger != null), + assert(codec != null); /// The logical channel on which communication happens, not null. final String name; @@ -121,6 +130,11 @@ class MethodChannel { /// The message codec used by this channel, not null. final MethodCodec codec; + /// The messenger used by this channel to send platform messages. + /// + /// The messenger may not be null. + final BinaryMessenger binaryMessenger; + /// Invokes a [method] on this channel with the specified [arguments]. /// /// The static type of [arguments] is `dynamic`, but only values supported by @@ -287,12 +301,12 @@ class MethodChannel { /// [StandardMethodCodec]. /// * [JSONMessageCodec] which defines the payload values supported by /// [JSONMethodCodec]. - /// * + /// * /// for how to access method call arguments on Android. @optionalTypeArgs Future invokeMethod(String method, [ dynamic arguments ]) async { assert(method != null); - final ByteData result = await BinaryMessages.send( + final ByteData result = await binaryMessenger.send( name, codec.encodeMethodCall(MethodCall(method, arguments)), ); @@ -346,7 +360,7 @@ class MethodChannel { /// similarly to what happens if no method call handler has been set. /// Any other exception results in an error envelope being sent. void setMethodCallHandler(Future handler(MethodCall call)) { - BinaryMessages.setMessageHandler( + binaryMessenger.setMessageHandler( name, handler == null ? null : (ByteData message) => _handleAsMethodCall(message, handler), ); @@ -371,7 +385,7 @@ class MethodChannel { /// [MethodCodec.encodeSuccessEnvelope], to act as if platform plugin had /// returned that value. void setMockMethodCallHandler(Future handler(MethodCall call)) { - BinaryMessages.setMockMessageHandler( + binaryMessenger.setMockMessageHandler( name, handler == null ? null : (ByteData message) => _handleAsMethodCall(message, handler), ); @@ -474,7 +488,7 @@ class EventChannel { final MethodChannel methodChannel = MethodChannel(name, codec); StreamController controller; controller = StreamController.broadcast(onListen: () async { - BinaryMessages.setMessageHandler(name, (ByteData reply) async { + defaultBinaryMessenger.setMessageHandler(name, (ByteData reply) async { if (reply == null) { controller.close(); } else { @@ -497,7 +511,7 @@ class EventChannel { )); } }, onCancel: () async { - BinaryMessages.setMessageHandler(name, null); + defaultBinaryMessenger.setMessageHandler(name, null); try { await methodChannel.invokeMethod('cancel', arguments); } catch (exception, stack) { diff --git a/packages/flutter/lib/src/services/platform_messages.dart b/packages/flutter/lib/src/services/platform_messages.dart index 2f0b05f3bbf..43fa779f890 100644 --- a/packages/flutter/lib/src/services/platform_messages.dart +++ b/packages/flutter/lib/src/services/platform_messages.dart @@ -6,17 +6,18 @@ import 'dart:async'; import 'dart:typed_data'; import 'dart:ui' as ui; -import 'package:flutter/foundation.dart'; - +import 'binary_messenger.dart'; import 'binding.dart' show ServicesBinding; import 'platform_channel.dart'; -typedef _MessageHandler = Future Function(ByteData message); - /// Sends binary messages to and receives binary messages from platform plugins. /// +/// This class has been deprecated in favor of [defaultBinaryMessenger]. New +/// code should not use [BinaryMessages]. +/// /// See also: /// +/// * [BinaryMessenger], the interface which has replaced this class. /// * [BasicMessageChannel], which provides basic messaging services similar to /// `BinaryMessages`, but with pluggable message codecs in support of sending /// strings or semi-structured messages. @@ -24,77 +25,34 @@ typedef _MessageHandler = Future Function(ByteData message); /// method calls. /// * [EventChannel], which provides platform communication using event streams. /// * +@Deprecated('This class, which was just a collection of static methods, has been ' + 'deprecated in favor of BinaryMessenger, and its default ' + 'implementation, defaultBinaryMessenger.') class BinaryMessages { BinaryMessages._(); - // Handlers for incoming messages from platform plugins. - static final Map _handlers = - {}; - - // Mock handlers that intercept and respond to outgoing messages. - static final Map _mockHandlers = - {}; - - static Future _sendPlatformMessage(String channel, ByteData message) { - final Completer completer = Completer(); - // ui.window is accessed directly instead of using ServicesBinding.instance.window - // because this method might be invoked before any binding is initialized. - // This issue was reported in #27541. It is not ideal to statically access - // ui.window because the Window may be dependency injected elsewhere with - // a different instance. However, static access at this location seems to be - // the least bad option. - ui.window.sendPlatformMessage(channel, message, (ByteData reply) { - try { - completer.complete(reply); - } catch (exception, stack) { - FlutterError.reportError(FlutterErrorDetails( - exception: exception, - stack: stack, - library: 'services library', - context: ErrorDescription('during a platform message response callback'), - )); - } - }); - return completer.future; - } - /// Calls the handler registered for the given channel. /// /// Typically called by [ServicesBinding] to handle platform messages received /// from [Window.onPlatformMessage]. /// /// To register a handler for a given message channel, see [setMessageHandler]. + @Deprecated('Use defaultBinaryMessenger.handlePlatformMessage instead.') static Future handlePlatformMessage( String channel, ByteData data, ui.PlatformMessageResponseCallback callback, - ) async { - ByteData response; - try { - final _MessageHandler handler = _handlers[channel]; - if (handler != null) - response = await handler(data); - } catch (exception, stack) { - FlutterError.reportError(FlutterErrorDetails( - exception: exception, - stack: stack, - library: 'services library', - context: ErrorDescription('during a platform message callback'), - )); - } finally { - callback(response); - } + ) { + return defaultBinaryMessenger.handlePlatformMessage(channel, data, callback); } /// Send a binary message to the platform plugins on the given channel. /// /// Returns a [Future] which completes to the received response, undecoded, in /// binary form. + @Deprecated('Use defaultBinaryMessenger.send instead.') static Future send(String channel, ByteData message) { - final _MessageHandler handler = _mockHandlers[channel]; - if (handler != null) - return handler(message); - return _sendPlatformMessage(channel, message); + return defaultBinaryMessenger.send(channel, message); } /// Set a callback for receiving messages from the platform plugins on the @@ -105,11 +63,9 @@ class BinaryMessages { /// argument. /// /// The handler's return value, if non-null, is sent as a response, unencoded. + @Deprecated('Use defaultBinaryMessenger.setMessageHandler instead.') static void setMessageHandler(String channel, Future handler(ByteData message)) { - if (handler == null) - _handlers.remove(channel); - else - _handlers[channel] = handler; + defaultBinaryMessenger.setMessageHandler(channel, handler); } /// Set a mock callback for intercepting messages from the `send*` methods on @@ -123,10 +79,8 @@ class BinaryMessages { /// /// This is intended for testing. Messages intercepted in this manner are not /// sent to platform plugins. + @Deprecated('Use defaultBinaryMessenger.setMockMessageHandler instead.') static void setMockMessageHandler(String channel, Future handler(ByteData message)) { - if (handler == null) - _mockHandlers.remove(channel); - else - _mockHandlers[channel] = handler; + defaultBinaryMessenger.setMockMessageHandler(channel, handler); } } diff --git a/packages/flutter/test/foundation/service_extensions_test.dart b/packages/flutter/test/foundation/service_extensions_test.dart index 2970d198a95..f15bc5fef63 100644 --- a/packages/flutter/test/foundation/service_extensions_test.dart +++ b/packages/flutter/test/foundation/service_extensions_test.dart @@ -417,7 +417,7 @@ void main() { bool completed; completed = false; - BinaryMessages.setMockMessageHandler('flutter/assets', (ByteData message) async { + defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData message) async { expect(utf8.decode(message.buffer.asUint8List()), 'test'); completed = true; return ByteData(5); // 0x0000000000 @@ -436,7 +436,7 @@ void main() { data = await rootBundle.loadStructuredData('test', (String value) async { expect(value, '\x00\x00\x00\x00\x00'); return false; }); expect(data, isFalse); expect(completed, isTrue); - BinaryMessages.setMockMessageHandler('flutter/assets', null); + defaultBinaryMessenger.setMockMessageHandler('flutter/assets', null); }); test('Service extensions - exit', () async { diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index 52e85f694ce..caec4278e6c 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -72,7 +72,7 @@ void main() { // Simulate system back button final ByteData message = const JSONMethodCodec().encodeMethodCall(const MethodCall('popRoute')); - await BinaryMessages.handlePlatformMessage('flutter/navigation', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { }); await tester.pumpAndSettle(); expect(selectedResults, [null]); diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 4f7021fbf77..86a7879bd7f 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -2957,7 +2957,7 @@ void main() { }); void sendFakeKeyEvent(Map data) { - BinaryMessages.handlePlatformMessage( + defaultBinaryMessenger.handlePlatformMessage( SystemChannels.keyEvent.name, SystemChannels.keyEvent.codec.encodeMessage(data), (ByteData data) { }, diff --git a/packages/flutter/test/scheduler/ticker_test.dart b/packages/flutter/test/scheduler/ticker_test.dart index 6133c46471b..1f16b8193f1 100644 --- a/packages/flutter/test/scheduler/ticker_test.dart +++ b/packages/flutter/test/scheduler/ticker_test.dart @@ -129,7 +129,7 @@ void main() { expect(tickCount, equals(0)); final ByteData message = const StringCodec().encodeMessage('AppLifecycleState.paused'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(ticker.isTicking, isFalse); expect(ticker.isActive, isTrue); @@ -138,7 +138,7 @@ void main() { testWidgets('Ticker can be created before application unpauses', (WidgetTester tester) async { final ByteData pausedMessage = const StringCodec().encodeMessage('AppLifecycleState.paused'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', pausedMessage, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', pausedMessage, (_) { }); int tickCount = 0; void handleTick(Duration duration) { @@ -157,7 +157,7 @@ void main() { expect(ticker.isTicking, isFalse); final ByteData resumedMessage = const StringCodec().encodeMessage('AppLifecycleState.resumed'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', resumedMessage, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', resumedMessage, (_) { }); await tester.pump(const Duration(milliseconds: 10)); diff --git a/packages/flutter/test/services/fake_platform_views.dart b/packages/flutter/test/services/fake_platform_views.dart index 83f81bc64b5..b066348ed47 100644 --- a/packages/flutter/test/services/fake_platform_views.dart +++ b/packages/flutter/test/services/fake_platform_views.dart @@ -35,7 +35,7 @@ class FakeAndroidPlatformViewsController { void invokeViewFocused(int viewId) { final MethodCodec codec = SystemChannels.platform_views.codec; final ByteData data = codec.encodeMethodCall(MethodCall('viewFocused', viewId)); - BinaryMessages.handlePlatformMessage(SystemChannels.platform_views.name, data, (ByteData data) {}); + defaultBinaryMessenger.handlePlatformMessage(SystemChannels.platform_views.name, data, (ByteData data) {}); } Future _onMethodCall(MethodCall call) { diff --git a/packages/flutter/test/services/platform_channel_test.dart b/packages/flutter/test/services/platform_channel_test.dart index 9b1108073e4..8e7586a69e8 100644 --- a/packages/flutter/test/services/platform_channel_test.dart +++ b/packages/flutter/test/services/platform_channel_test.dart @@ -13,7 +13,7 @@ void main() { const MessageCodec string = StringCodec(); const BasicMessageChannel channel = BasicMessageChannel('ch', string); test('can send string message and get reply', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch', (ByteData message) async => string.encodeMessage(string.decodeMessage(message) + ' world'), ); @@ -23,7 +23,7 @@ void main() { test('can receive string message and send reply', () async { channel.setMessageHandler((String message) async => message + ' world'); String reply; - await BinaryMessages.handlePlatformMessage( + await defaultBinaryMessenger.handlePlatformMessage( 'ch', const StringCodec().encodeMessage('hello'), (ByteData replyBinary) { @@ -39,7 +39,7 @@ void main() { const MethodCodec jsonMethod = JSONMethodCodec(); const MethodChannel channel = MethodChannel('ch7', jsonMethod); test('can invoke method and get result', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch7', (ByteData message) async { final Map methodCall = jsonMessage.decodeMessage(message); @@ -54,7 +54,7 @@ void main() { expect(result, equals('hello world')); }); test('can invoke list method and get result', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch7', (ByteData message) async { final Map methodCall = jsonMessage.decodeMessage(message); @@ -70,7 +70,7 @@ void main() { }); test('can invoke list method and get null result', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch7', (ByteData message) async { final Map methodCall = jsonMessage.decodeMessage(message); @@ -86,7 +86,7 @@ void main() { test('can invoke map method and get result', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch7', (ByteData message) async { final Map methodCall = jsonMessage.decodeMessage(message); @@ -102,7 +102,7 @@ void main() { }); test('can invoke map method and get null result', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch7', (ByteData message) async { final Map methodCall = jsonMessage.decodeMessage(message); @@ -117,7 +117,7 @@ void main() { }); test('can invoke method and get error', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch7', (ByteData message) async { return jsonMessage.encodeMessage([ @@ -139,7 +139,7 @@ void main() { } }); test('can invoke unimplemented method', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch7', (ByteData message) async => null, ); @@ -157,7 +157,7 @@ void main() { channel.setMethodCallHandler(null); final ByteData call = jsonMethod.encodeMethodCall(const MethodCall('sayHello', 'hello')); ByteData envelope; - await BinaryMessages.handlePlatformMessage('ch7', call, (ByteData result) { + await defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData result) { envelope = result; }); expect(envelope, isNull); @@ -168,7 +168,7 @@ void main() { }); final ByteData call = jsonMethod.encodeMethodCall(const MethodCall('sayHello', 'hello')); ByteData envelope; - await BinaryMessages.handlePlatformMessage('ch7', call, (ByteData result) { + await defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData result) { envelope = result; }); expect(envelope, isNull); @@ -177,7 +177,7 @@ void main() { channel.setMethodCallHandler((MethodCall call) async => '${call.arguments}, world'); final ByteData call = jsonMethod.encodeMethodCall(const MethodCall('sayHello', 'hello')); ByteData envelope; - await BinaryMessages.handlePlatformMessage('ch7', call, (ByteData result) { + await defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData result) { envelope = result; }); expect(jsonMethod.decodeEnvelope(envelope), equals('hello, world')); @@ -188,7 +188,7 @@ void main() { }); final ByteData call = jsonMethod.encodeMethodCall(const MethodCall('sayHello', 'hello')); ByteData envelope; - await BinaryMessages.handlePlatformMessage('ch7', call, (ByteData result) { + await defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData result) { envelope = result; }); try { @@ -207,7 +207,7 @@ void main() { }); final ByteData call = jsonMethod.encodeMethodCall(const MethodCall('sayHello', 'hello')); ByteData envelope; - await BinaryMessages.handlePlatformMessage('ch7', call, (ByteData result) { + await defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData result) { envelope = result; }); try { @@ -226,7 +226,7 @@ void main() { const MethodCodec jsonMethod = JSONMethodCodec(); const EventChannel channel = EventChannel('ch', jsonMethod); void emitEvent(dynamic event) { - BinaryMessages.handlePlatformMessage( + defaultBinaryMessenger.handlePlatformMessage( 'ch', event, (ByteData reply) { }, @@ -234,7 +234,7 @@ void main() { } test('can receive event stream', () async { bool canceled = false; - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch', (ByteData message) async { final Map methodCall = jsonMessage.decodeMessage(message); @@ -258,7 +258,7 @@ void main() { expect(canceled, isTrue); }); test('can receive error event', () async { - BinaryMessages.setMockMessageHandler( + defaultBinaryMessenger.setMockMessageHandler( 'ch', (ByteData message) async { final Map methodCall = jsonMessage.decodeMessage(message); diff --git a/packages/flutter/test/services/platform_messages_test.dart b/packages/flutter/test/services/platform_messages_test.dart index 14b590553be..0e97dc214d6 100644 --- a/packages/flutter/test/services/platform_messages_test.dart +++ b/packages/flutter/test/services/platform_messages_test.dart @@ -9,23 +9,23 @@ import 'package:flutter_test/flutter_test.dart'; void main() { test('Mock binary message handler control test', () async { - // Initialize all bindings because BinaryMessages.send() needs a window. + // Initialize all bindings because defaultBinaryMessenger.send() needs a window. TestWidgetsFlutterBinding.ensureInitialized(); final List log = []; - BinaryMessages.setMockMessageHandler('test1', (ByteData message) async { + defaultBinaryMessenger.setMockMessageHandler('test1', (ByteData message) async { log.add(message); return null; }); final ByteData message = ByteData(2)..setUint16(0, 0xABCD); - await BinaryMessages.send('test1', message); + await defaultBinaryMessenger.send('test1', message); expect(log, equals([message])); log.clear(); - BinaryMessages.setMockMessageHandler('test1', null); - await BinaryMessages.send('test1', message); + defaultBinaryMessenger.setMockMessageHandler('test1', null); + await defaultBinaryMessenger.send('test1', message); expect(log, isEmpty); }); } diff --git a/packages/flutter/test/services/system_chrome_test.dart b/packages/flutter/test/services/system_chrome_test.dart index abeb5dca0d8..ac02009a31a 100644 --- a/packages/flutter/test/services/system_chrome_test.dart +++ b/packages/flutter/test/services/system_chrome_test.dart @@ -61,7 +61,7 @@ void main() { test('setApplicationSwitcherDescription missing plugin', () async { final List log = []; - BinaryMessages.setMockMessageHandler('flutter/platform', (ByteData message) { + defaultBinaryMessenger.setMockMessageHandler('flutter/platform', (ByteData message) { log.add(message); return null; }); diff --git a/packages/flutter/test/widgets/binding_test.dart b/packages/flutter/test/widgets/binding_test.dart index fdcc37ba4eb..4991e9e350e 100644 --- a/packages/flutter/test/widgets/binding_test.dart +++ b/packages/flutter/test/widgets/binding_test.dart @@ -46,7 +46,7 @@ void main() { WidgetsBinding.instance.addObserver(observer); final ByteData message = const JSONMessageCodec().encodeMessage( {'type': 'memoryPressure'}); - await BinaryMessages.handlePlatformMessage('flutter/system', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/system', message, (_) { }); expect(observer.sawMemoryPressure, true); WidgetsBinding.instance.removeObserver(observer); }); @@ -56,19 +56,19 @@ void main() { WidgetsBinding.instance.addObserver(observer); ByteData message = const StringCodec().encodeMessage('AppLifecycleState.paused'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(observer.lifecycleState, AppLifecycleState.paused); message = const StringCodec().encodeMessage('AppLifecycleState.resumed'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(observer.lifecycleState, AppLifecycleState.resumed); message = const StringCodec().encodeMessage('AppLifecycleState.inactive'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(observer.lifecycleState, AppLifecycleState.inactive); message = const StringCodec().encodeMessage('AppLifecycleState.suspending'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(observer.lifecycleState, AppLifecycleState.suspending); }); @@ -79,7 +79,7 @@ void main() { const String testRouteName = 'testRouteName'; final ByteData message = const JSONMethodCodec().encodeMethodCall( const MethodCall('pushRoute', testRouteName)); - await BinaryMessages.handlePlatformMessage('flutter/navigation', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { }); expect(observer.pushedRoute, testRouteName); WidgetsBinding.instance.removeObserver(observer); @@ -90,31 +90,31 @@ void main() { expect(tester.binding.hasScheduledFrame, isFalse); message = const StringCodec().encodeMessage('AppLifecycleState.paused'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(tester.binding.hasScheduledFrame, isFalse); message = const StringCodec().encodeMessage('AppLifecycleState.resumed'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(tester.binding.hasScheduledFrame, isTrue); await tester.pump(); expect(tester.binding.hasScheduledFrame, isFalse); message = const StringCodec().encodeMessage('AppLifecycleState.inactive'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(tester.binding.hasScheduledFrame, isFalse); message = const StringCodec().encodeMessage('AppLifecycleState.suspending'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(tester.binding.hasScheduledFrame, isFalse); message = const StringCodec().encodeMessage('AppLifecycleState.inactive'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(tester.binding.hasScheduledFrame, isTrue); await tester.pump(); expect(tester.binding.hasScheduledFrame, isFalse); message = const StringCodec().encodeMessage('AppLifecycleState.paused'); - await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) { }); + await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { }); expect(tester.binding.hasScheduledFrame, isFalse); tester.binding.scheduleFrame(); diff --git a/packages/flutter/test/widgets/focus_manager_test.dart b/packages/flutter/test/widgets/focus_manager_test.dart index 3bb4d7ce7b9..7bb4033b5b4 100644 --- a/packages/flutter/test/widgets/focus_manager_test.dart +++ b/packages/flutter/test/widgets/focus_manager_test.dart @@ -10,7 +10,7 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; void sendFakeKeyEvent(Map data) { - BinaryMessages.handlePlatformMessage( + defaultBinaryMessenger.handlePlatformMessage( SystemChannels.keyEvent.name, SystemChannels.keyEvent.codec.encodeMessage(data), (ByteData data) {}, diff --git a/packages/flutter/test/widgets/raw_keyboard_listener_test.dart b/packages/flutter/test/widgets/raw_keyboard_listener_test.dart index ddff589c136..41cd8493314 100644 --- a/packages/flutter/test/widgets/raw_keyboard_listener_test.dart +++ b/packages/flutter/test/widgets/raw_keyboard_listener_test.dart @@ -9,7 +9,7 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; void sendFakeKeyEvent(Map data) { - BinaryMessages.handlePlatformMessage( + defaultBinaryMessenger.handlePlatformMessage( SystemChannels.keyEvent.name, SystemChannels.keyEvent.codec.encodeMessage(data), (ByteData data) {}, diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index ec2db8b5c7d..7d450be427a 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -757,7 +757,7 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { _ensureInitialized(assetFolderPath); if (_allowedAssetKeys.isNotEmpty) { - BinaryMessages.setMockMessageHandler('flutter/assets', (ByteData message) { + defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData message) { final String key = utf8.decode(message.buffer.asUint8List()); if (_allowedAssetKeys.contains(key)) { final File asset = File(path.join(assetFolderPath, key)); diff --git a/packages/flutter_test/lib/src/test_text_input.dart b/packages/flutter_test/lib/src/test_text_input.dart index c6c1dbc7b43..15472cbd69e 100644 --- a/packages/flutter_test/lib/src/test_text_input.dart +++ b/packages/flutter_test/lib/src/test_text_input.dart @@ -105,7 +105,7 @@ class TestTextInput { // test this code does not run in a package:test test zone. if (_client == 0) throw TestFailure('Tried to use TestTextInput with no keyboard attached. You must use WidgetTester.showKeyboard() first.'); - BinaryMessages.handlePlatformMessage( + defaultBinaryMessenger.handlePlatformMessage( SystemChannels.textInput.name, SystemChannels.textInput.codec.encodeMethodCall( MethodCall( @@ -137,7 +137,7 @@ class TestTextInput { final Completer completer = Completer(); - BinaryMessages.handlePlatformMessage( + defaultBinaryMessenger.handlePlatformMessage( SystemChannels.textInput.name, SystemChannels.textInput.codec.encodeMethodCall( MethodCall(