flutter/packages/flutter_web_plugins/test/plugin_event_channel_test.dart
Michael Goderbauer 5491c8c146
Auto-format Framework (#160545)
This auto-formats all *.dart files in the repository outside of the
`engine` subdirectory and enforces that these files stay formatted with
a presubmit check.

**Reviewers:** Please carefully review all the commits except for the
one titled "formatted". The "formatted" commit was auto-generated by
running `dev/tools/format.sh -a -f`. The other commits were hand-crafted
to prepare the repo for the formatting change. I recommend reviewing the
commits one-by-one via the "Commits" tab and avoiding Github's "Files
changed" tab as it will likely slow down your browser because of the
size of this PR.

---------

Co-authored-by: Kate Lovett <katelovett@google.com>
Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
2024-12-19 20:06:21 +00:00

162 lines
5.7 KiB
Dart

// 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
library;
import 'dart:async';
import 'dart:ui_web' as ui_web;
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_web.debugEmulateFlutterTesterEnvironment = false;
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<String> sendingChannel = PluginEventChannel<String>('test');
final StreamController<String> controller = StreamController<String>();
sendingChannel.setController(controller);
expect(listeningChannel.receiveBroadcastStream(), emitsInOrder(<String>['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<String> sendingChannel = PluginEventChannel<String>('test');
final StreamController<String> controller = StreamController<String>();
sendingChannel.setController(controller);
expect(listeningChannel.receiveBroadcastStream(), emitsInOrder(<String>['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<String> sendingChannel = PluginEventChannel<String>('test2');
final StreamController<String> controller = StreamController<String>();
sendingChannel.setController(controller);
expect(
listeningChannel.receiveBroadcastStream(),
emitsError(
predicate<dynamic>((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<String> sendingChannel = PluginEventChannel<String>('test2');
final StreamController<String> controller = StreamController<String>();
sendingChannel.setController(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 (deprecated API)', () async {
const EventChannel listeningChannel = EventChannel('test3');
const PluginEventChannel<String> sendingChannel = PluginEventChannel<String>('test3');
final StreamController<String> controller = StreamController<String>(
onListen: expectAsync0<void>(() {}),
);
sendingChannel.setController(controller);
expect(listeningChannel.receiveBroadcastStream(), emitsInOrder(<String>['hello']));
controller.add('hello');
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>(() {}),
);
sendingChannel.setController(controller);
expect(listeningChannel.receiveBroadcastStream(), emitsInOrder(<String>['hello']));
controller.add('hello');
await controller.close();
});
test('receives a cancel event (deprecated API)', () async {
const EventChannel listeningChannel = EventChannel('test4');
const PluginEventChannel<String> sendingChannel = PluginEventChannel<String>('test4');
final StreamController<String> controller = StreamController<String>(
onCancel: expectAsync0<void>(() {}),
);
sendingChannel.setController(controller);
final Stream<dynamic> eventStream = listeningChannel.receiveBroadcastStream();
late StreamSubscription<dynamic> subscription;
subscription = eventStream.listen(
expectAsync1<void, dynamic>((dynamic x) {
expect(x, equals('hello'));
subscription.cancel();
}),
);
controller.add('hello');
});
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.setController(controller);
final Stream<dynamic> eventStream = listeningChannel.receiveBroadcastStream();
late StreamSubscription<dynamic> subscription;
subscription = eventStream.listen(
expectAsync1<void, dynamic>((dynamic x) {
expect(x, equals('hello'));
subscription.cancel();
}),
);
controller.add('hello');
});
});
}