// Copyright 2014 The Flutter 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 'dart:ui' as ui; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; void main() { // Disabling tester emulation because this test relies on real message channel communication. ui.debugEmulateFlutterTesterEnvironment = false; // ignore: undefined_prefixed_name group('Plugin Event Channel', () { setUp(() { TestWidgetsFlutterBinding.ensureInitialized(); webPluginRegistry.registerMessageHandler(); }); test('can send events to an $EventChannel (deprecated API)', () async { const EventChannel listeningChannel = EventChannel('test'); const PluginEventChannel sendingChannel = PluginEventChannel('test'); final StreamController controller = StreamController(); sendingChannel.setController(controller); expect(listeningChannel.receiveBroadcastStream(), emitsInOrder(['hello', 'world'])); controller.add('hello'); controller.add('world'); await controller.close(); }); test('can send events to an $EventChannel', () async { const EventChannel listeningChannel = EventChannel('test'); const PluginEventChannel sendingChannel = PluginEventChannel('test'); final StreamController controller = StreamController(); sendingChannel.setController(controller); expect(listeningChannel.receiveBroadcastStream(), emitsInOrder(['hello', 'world'])); controller.add('hello'); controller.add('world'); await controller.close(); }); test('can send errors to an $EventChannel (deprecated API)', () async { const EventChannel listeningChannel = EventChannel('test2'); const PluginEventChannel sendingChannel = PluginEventChannel('test2'); final StreamController controller = StreamController(); sendingChannel.setController(controller); expect( listeningChannel.receiveBroadcastStream(), emitsError(predicate((dynamic e) => e is PlatformException && e.message == 'Test error'))); controller.addError('Test error'); await controller.close(); }); test('can send errors to an $EventChannel', () async { const EventChannel listeningChannel = EventChannel('test2'); const PluginEventChannel sendingChannel = PluginEventChannel('test2'); final StreamController controller = StreamController(); sendingChannel.setController(controller); expect( listeningChannel.receiveBroadcastStream(), emitsError(predicate((dynamic e) => e is PlatformException && e.message == 'Test error'))); controller.addError('Test error'); await controller.close(); }); test('receives a listen event (deprecated API)', () async { const EventChannel listeningChannel = EventChannel('test3'); const PluginEventChannel sendingChannel = PluginEventChannel('test3'); final StreamController controller = StreamController( onListen: expectAsync0(() {}, count: 1)); sendingChannel.setController(controller); expect(listeningChannel.receiveBroadcastStream(), emitsInOrder(['hello'])); controller.add('hello'); await controller.close(); }); test('receives a listen event', () async { const EventChannel listeningChannel = EventChannel('test3'); const PluginEventChannel sendingChannel = PluginEventChannel('test3'); final StreamController controller = StreamController( onListen: expectAsync0(() {}, count: 1)); sendingChannel.setController(controller); expect(listeningChannel.receiveBroadcastStream(), emitsInOrder(['hello'])); controller.add('hello'); await controller.close(); }); test('receives a cancel event (deprecated API)', () async { const EventChannel listeningChannel = EventChannel('test4'); const PluginEventChannel sendingChannel = PluginEventChannel('test4'); final StreamController controller = StreamController(onCancel: expectAsync0(() {})); sendingChannel.setController(controller); final Stream eventStream = listeningChannel.receiveBroadcastStream(); late StreamSubscription subscription; subscription = eventStream.listen(expectAsync1((dynamic x) { expect(x, equals('hello')); subscription.cancel(); })); controller.add('hello'); }); test('receives a cancel event', () async { const EventChannel listeningChannel = EventChannel('test4'); const PluginEventChannel sendingChannel = PluginEventChannel('test4'); final StreamController controller = StreamController(onCancel: expectAsync0(() {})); sendingChannel.setController(controller); final Stream eventStream = listeningChannel.receiveBroadcastStream(); late StreamSubscription subscription; subscription = eventStream.listen(expectAsync1((dynamic x) { expect(x, equals('hello')); subscription.cancel(); })); controller.add('hello'); }); }); }