Allow sending platform messages from plugins to the framework and implement EventChannel (#40638)
* Allow sending messages from the platform to the framework * Add ability to send messages from the plugin to the platform. Also adds [PluginEventChannel] which is the plugin counterpart to EventChannel. Fixes https://github.com/flutter/flutter/issues/39981 * fix analyzer errors * Enhance doc comments * Remove dead code in test
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
// Copyright 2019 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.
|
||||
|
||||
@TestOn('chrome') // Uses web-only Flutter SDK
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
||||
|
||||
void main() {
|
||||
group('Plugin Event Channel', () {
|
||||
setUp(() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
webPluginRegistry.registerMessageHandler();
|
||||
});
|
||||
|
||||
test('can send events to an $EventChannel', () async {
|
||||
const EventChannel listeningChannel = EventChannel('test');
|
||||
const PluginEventChannel<String> sendingChannel =
|
||||
PluginEventChannel<String>('test');
|
||||
|
||||
final StreamController<String> controller = StreamController<String>();
|
||||
sendingChannel.controller = controller;
|
||||
|
||||
expect(listeningChannel.receiveBroadcastStream(),
|
||||
emitsInOrder(<String>['hello', 'world']));
|
||||
|
||||
controller.add('hello');
|
||||
controller.add('world');
|
||||
await controller.close();
|
||||
});
|
||||
|
||||
test('can send errors to an $EventChannel', () async {
|
||||
const EventChannel listeningChannel = EventChannel('test2');
|
||||
const PluginEventChannel<String> sendingChannel =
|
||||
PluginEventChannel<String>('test2');
|
||||
|
||||
final StreamController<String> controller = StreamController<String>();
|
||||
sendingChannel.controller = controller;
|
||||
|
||||
expect(
|
||||
listeningChannel.receiveBroadcastStream(),
|
||||
emitsError(predicate<dynamic>((dynamic e) =>
|
||||
e is PlatformException && e.message == 'Test error')));
|
||||
|
||||
controller.addError('Test error');
|
||||
await controller.close();
|
||||
});
|
||||
|
||||
test('receives a listen event', () async {
|
||||
const EventChannel listeningChannel = EventChannel('test3');
|
||||
const PluginEventChannel<String> sendingChannel =
|
||||
PluginEventChannel<String>('test3');
|
||||
|
||||
final StreamController<String> controller = StreamController<String>(
|
||||
onListen: expectAsync0<void>(() {}, count: 1));
|
||||
sendingChannel.controller = controller;
|
||||
|
||||
expect(listeningChannel.receiveBroadcastStream(),
|
||||
emitsInOrder(<String>['hello']));
|
||||
|
||||
controller.add('hello');
|
||||
await controller.close();
|
||||
});
|
||||
|
||||
test('receives a cancel event', () async {
|
||||
const EventChannel listeningChannel = EventChannel('test4');
|
||||
const PluginEventChannel<String> sendingChannel =
|
||||
PluginEventChannel<String>('test4');
|
||||
|
||||
final StreamController<String> controller =
|
||||
StreamController<String>(onCancel: expectAsync0<void>(() {}));
|
||||
sendingChannel.controller = controller;
|
||||
|
||||
final Stream<dynamic> eventStream =
|
||||
listeningChannel.receiveBroadcastStream();
|
||||
StreamSubscription<dynamic> subscription;
|
||||
subscription =
|
||||
eventStream.listen(expectAsync1<void, dynamic>((dynamic x) {
|
||||
expect(x, equals('hello'));
|
||||
subscription.cancel();
|
||||
}));
|
||||
|
||||
controller.add('hello');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -31,27 +31,43 @@ void main() {
|
||||
setUp(() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
webPluginRegistry.registerMessageHandler();
|
||||
});
|
||||
|
||||
test('Can register a plugin', () {
|
||||
TestPlugin.calledMethods.clear();
|
||||
|
||||
final Registrar registrar = webPluginRegistry.registrarFor(TestPlugin);
|
||||
TestPlugin.registerWith(registrar);
|
||||
});
|
||||
|
||||
test('can register a plugin', () {
|
||||
TestPlugin.calledMethods.clear();
|
||||
|
||||
const MethodChannel frameworkChannel =
|
||||
MethodChannel('test_plugin', StandardMethodCodec());
|
||||
frameworkChannel.invokeMethod<void>('test1');
|
||||
|
||||
expect(TestPlugin.calledMethods, <String>['test1']);
|
||||
expect(TestPlugin.calledMethods, equals(<String>['test1']));
|
||||
});
|
||||
|
||||
test('Throws when trying to send a platform message to the framework', () {
|
||||
expect(() => pluginBinaryMessenger.send('test', ByteData(0)),
|
||||
throwsFlutterError);
|
||||
test('can send a message from the plugin to the framework', () async {
|
||||
const StandardMessageCodec codec = StandardMessageCodec();
|
||||
|
||||
final List<String> loggedMessages = <String>[];
|
||||
ServicesBinding.instance.defaultBinaryMessenger
|
||||
.setMessageHandler('test_send', (ByteData data) {
|
||||
loggedMessages.add(codec.decodeMessage(data));
|
||||
return null;
|
||||
});
|
||||
|
||||
await pluginBinaryMessenger.send(
|
||||
'test_send', codec.encodeMessage('hello'));
|
||||
expect(loggedMessages, equals(<String>['hello']));
|
||||
|
||||
await pluginBinaryMessenger.send(
|
||||
'test_send', codec.encodeMessage('world'));
|
||||
expect(loggedMessages, equals(<String>['hello', 'world']));
|
||||
|
||||
ServicesBinding.instance.defaultBinaryMessenger
|
||||
.setMessageHandler('test_send', null);
|
||||
});
|
||||
|
||||
test('Throws when trying to set a mock handler', () {
|
||||
test('throws when trying to set a mock handler', () {
|
||||
expect(
|
||||
() => pluginBinaryMessenger.setMockMessageHandler(
|
||||
'test', (ByteData data) async => ByteData(0)),
|
||||
|
||||
Reference in New Issue
Block a user