flutter/packages/flutter_web_plugins/test/plugin_registry_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

74 lines
2.4 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: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';
class TestPlugin {
static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel(
'test_plugin',
const StandardMethodCodec(),
registrar.messenger,
);
final TestPlugin testPlugin = TestPlugin();
channel.setMethodCallHandler(testPlugin.handleMethodCall);
}
static final List<String> calledMethods = <String>[];
Future<void> handleMethodCall(MethodCall call) async {
calledMethods.add(call.method);
}
}
void main() {
// Disabling tester emulation because this test relies on real message channel communication.
ui_web.debugEmulateFlutterTesterEnvironment = false;
group('Plugin Registry', () {
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized();
webPluginRegistry.registerMessageHandler();
final Registrar registrar = webPluginRegistry.registrarFor(TestPlugin);
TestPlugin.registerWith(registrar);
});
test('can register a plugin', () {
TestPlugin.calledMethods.clear();
const MethodChannel frameworkChannel = MethodChannel('test_plugin');
frameworkChannel.invokeMethod<void>('test1');
expect(TestPlugin.calledMethods, equals(<String>['test1']));
});
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)! as String);
return Future<ByteData?>.value();
});
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);
});
});
}