plugin_registry_test.dart 2.67 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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

7 8
import 'dart:ui' as ui;

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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() {
32 33 34
  // Disabling tester emulation because this test relies on real message channel communication.
  ui.debugEmulateFlutterTesterEnvironment = false; // ignore: undefined_prefixed_name

35 36 37 38
  group('Plugin Registry', () {
    setUp(() {
      TestWidgetsFlutterBinding.ensureInitialized();
      webPluginRegistry.registerMessageHandler();
39 40
      final Registrar registrar = webPluginRegistry.registrarFor(TestPlugin);
      TestPlugin.registerWith(registrar);
41 42
    });

43
    test('can register a plugin', () {
44 45 46 47 48 49
      TestPlugin.calledMethods.clear();

      const MethodChannel frameworkChannel =
          MethodChannel('test_plugin', StandardMethodCodec());
      frameworkChannel.invokeMethod<void>('test1');

50
      expect(TestPlugin.calledMethods, equals(<String>['test1']));
51 52
    });

53 54 55 56 57 58
    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) {
59
        loggedMessages.add(codec.decodeMessage(data) as String);
60 61 62 63 64 65 66 67 68 69 70 71 72
        return null;
      });

      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);
73 74
    });

75
    test('throws when trying to set a mock handler', () {
76 77 78 79 80 81 82
      expect(
          () => pluginBinaryMessenger.setMockMessageHandler(
              'test', (ByteData data) async => ByteData(0)),
          throwsFlutterError);
    });
  });
}