flutter_flutter/packages/flutter_web_plugins/test/plugin_registry_test.dart
Mouad Debbar caf0c82b82
[web] More granular configuration of the test environment (#168767)
There's today a single boolean `debugEmulateFlutterTesterEnvironment`
that determines certain behaviors in the engine to emulate how the
Flutter Tester runs (more details at
https://github.com/flutter/flutter/issues/145779).

With this PR:
- Run all engine unit tests in production mode by default.
- Give tests more granular control over which test behaviors to
enable/disable (e.g. Ahem font).
- Keep it easy for framework tests to enable all emulation behaviors.

Fixes https://github.com/flutter/flutter/issues/145779
2025-06-24 19:45:19 +00:00

76 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() {
setUp(() {
// Disabling tester emulation because this test relies on real message channel communication.
ui_web.TestEnvironment.setUp(const ui_web.TestEnvironment.production());
});
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);
});
});
}