flutter/packages/flutter_web_plugins/test/plugin_registry_test.dart
Harry Terkelsen d33cf11556
Automatically generated registrants for web plugins (#39628)
* WIP on web plugin registry

* WIP on registering plugins

* WIP on web plugin registration

* Only generate `package:flutter_web_plugins` imports if plugins are
defined

* Add parsing test

* Add documentation

* Fix analyzer warnings

* add license headers

* Add tests for package:flutter_web_plugins

* Run `flutter update-packages --force-upgrade`

* Fix analyzer errors

* Fix analyzer error in test

* Update copyright and remove flutter SDK constraints

* Enable tests since engine has rolled

* add flutter_web_plugins tests to bots

* Create an empty .packages file for WebFs test
2019-09-03 10:37:34 -07:00

62 lines
1.8 KiB
Dart

// 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 '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() {
group('Plugin Registry', () {
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized();
webPluginRegistry.registerMessageHandler();
});
test('Can register a plugin', () {
TestPlugin.calledMethods.clear();
final Registrar registrar = webPluginRegistry.registrarFor(TestPlugin);
TestPlugin.registerWith(registrar);
const MethodChannel frameworkChannel =
MethodChannel('test_plugin', StandardMethodCodec());
frameworkChannel.invokeMethod<void>('test1');
expect(TestPlugin.calledMethods, <String>['test1']);
});
test('Throws when trying to send a platform message to the framework', () {
expect(() => pluginBinaryMessenger.send('test', ByteData(0)),
throwsFlutterError);
});
test('Throws when trying to set a mock handler', () {
expect(
() => pluginBinaryMessenger.setMockMessageHandler(
'test', (ByteData data) async => ByteData(0)),
throwsFlutterError);
});
});
}