public void Ensure_Handler_Is_Not_Null()
            {
                var messenger = Substitute.For <IBinaryMessenger>();
                var channel   = new MethodChannel(TEST_CHANNEL_NAME, StandardMethodCodec.Instance, messenger);

                Assert.Throws <ArgumentNullException>(() => channel.SetMethodCallHandler(null as Func <MethodCall, Task <object> >));
                Assert.Throws <ArgumentNullException>(() => channel.SetMethodCallHandler(null as Func <MethodCall, object>));
            }
Ejemplo n.º 2
0
            public void Unregisters_Message_Handler()
            {
                var messenger = Substitute.For <IBinaryMessenger>();
                var channel   = new MethodChannel(TEST_CHANNEL_NAME, StandardMethodCodec.Instance, messenger);

                channel.SetMethodCallHandler(null);

                messenger.Received().SetMessageHandler(Arg.Is <string>(x => x == TEST_CHANNEL_NAME), null);
            }
            public void Registers_Async_MethodCallHandler()
            {
                var messenger = Substitute.For <IBinaryMessenger>();
                var channel   = new MethodChannel(TEST_CHANNEL_NAME, StandardMethodCodec.Instance, messenger);

                channel.SetMethodCallHandler((call) => Task.FromResult <object>(string.Empty));

                messenger.Received().SetMessageHandler(Arg.Is <string>(x => x == TEST_CHANNEL_NAME),
                                                       Arg.Any <BinaryMessageHandler>());
            }
Ejemplo n.º 4
0
        private void FabOnClick(object sender, EventArgs eventArgs)
        {
            if (flutterEngine == null)
            {
                flutterEngine = new FlutterEngine(this);

                flutterEngine.DartExecutor.ExecuteDartEntrypoint(
                    DartEntrypoint.CreateDefault()
                    );

                FlutterEngineCache.Instance.Put("my_engine", flutterEngine);

                var channel = new MethodChannel(flutterEngine.DartExecutor.BinaryMessenger, "diversido.io/main");
                channel.SetMethodCallHandler(this);
            }

            StartActivity(FlutterActivity.WithCachedEngine("my_engine").Build(this));
        }
Ejemplo n.º 5
0
            public void Ensures_Empty_Response_When_MissingPluginException_Is_Thrown()
            {
                var messenger = Substitute.For <IBinaryMessenger>();
                var channel   = new MethodChannel(TEST_CHANNEL_NAME, StandardMethodCodec.Instance, messenger);

                byte[] testCall = StandardMethodCodec.Instance.EncodeMethodCall(new MethodCall(TEST_METHOD_NAME, 1));
                messenger.When(x => x.SetMessageHandler(TEST_CHANNEL_NAME, Arg.Any <BinaryMessageHandler>()))
                .Do(async x =>
                {
                    var binaryHandler = x[1] as BinaryMessageHandler;
                    byte[] result     = await binaryHandler(testCall);
                    Assert.Null(result);
                });
                channel.SetMethodCallHandler((call) =>
                {
                    throw new MissingPluginException();
                });
            }
Ejemplo n.º 6
0
            public void Ensures_Error_Response_When_FlutterException_Is_Thrown()
            {
                var messenger = Substitute.For <IBinaryMessenger>();
                var codec     = StandardMethodCodec.Instance;
                var channel   = new MethodChannel(TEST_CHANNEL_NAME, StandardMethodCodec.Instance, messenger);

                byte[] testCall = codec.EncodeMethodCall(new MethodCall(TEST_METHOD_NAME, 1));
                byte[] expected = codec.EncodeErrorEnvelope("E0001", "TEST_MESSAGE", "TEST_DETAIL");
                messenger.When(x => x.SetMessageHandler(TEST_CHANNEL_NAME, Arg.Any <BinaryMessageHandler>()))
                .Do(async x =>
                {
                    var binaryHandler = x[1] as BinaryMessageHandler;
                    byte[] result     = await binaryHandler(testCall);
                    for (int i = 0; i < expected.Length; i++)
                    {
                        Assert.Equal(expected[i], result[i]);
                    }
                });
                channel.SetMethodCallHandler((call) =>
                {
                    throw new FlutterException("E0001", "TEST_MESSAGE", "TEST_DETAIL");
                });
            }
        /// <summary>
        /// Initializes a new instance of the <see cref="FlutnetBridge"/> class
        /// specifying how platform code and Flutter code communicate.
        /// </summary>
        public FlutnetBridge(FlutterEngine engine, Android.Content.Context context, FlutnetBridgeMode mode)
        {
            // Create the named channel for communicating with Flutter module using asynchronous method calls
            // NOTE: This channel is used to RECEIVE messages/requests FROM Flutter
            _methodChannelIncoming     = new MethodChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.incoming");
            _methodCallHandlerIncoming = new MethodCallHandler(HandleMethodCall);
            _methodChannelIncoming.SetMethodCallHandler(_methodCallHandlerIncoming);

            // Create a second named channel for diagnostic use only.
            // This channel is used, for example, to test if Flutter module is running
            // embedded into a native Xamarin app or as a standalone app
            _methodChannelTest     = new MethodChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.support");
            _methodCallHandlerTest = new MethodCallHandler(HandleMethodCallTest);
            _methodChannelTest.SetMethodCallHandler(_methodCallHandlerTest);

            // Create the named channel for communicating with Flutter module using event streams
            // NOTE: This channel is used to SEND messages/notifications TO Flutter

            // An event channel is a specialized platform channel intended for the use case of exposing platform events to Flutter as a Dart stream.
            // The Flutter SDK currently has no support for the symmetrical case of exposing Dart streams to platform code, though that could be built, if the need arises.
            // see: https://medium.com/flutter/flutter-platform-channels-ce7f540a104e

            _streamHandler = new StreamHandler(this);
            _eventChannel  = new EventChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.outgoing");
            _eventChannel.SetStreamHandler(_streamHandler);

            _context = context;
            Mode     = mode;

            FlutnetRuntime.OnPlatformEvent += FlutnetRuntimeOnPlatformEvent;

            if (Mode == FlutnetBridgeMode.WebSocket)
            {
                _context.StartService(new Android.Content.Intent(_context, typeof(FlutnetWebSocketService)));
            }
        }
        public override void Start(FlutterView messenger)
        {
            foreach (var serviceHandler in ServiceHandlers)
            {
                // Create the channel
                var methodChannel = new MethodChannel(messenger, serviceHandler.Key);

                // setup a message handler
                var messageHandler = new MessageHandler(serviceHandler.Value);

                // create our local callback handler invoked from Flutter
                var callHandler = new CallHandler(messageHandler);

                methodChannel.SetMethodCallHandler(callHandler);

                // record the handler, this isn't used anywhere
                _methodCallHandlers[serviceHandler.Key] = callHandler;

                var eventStreamFactory = new EventStreamFactory(messenger, serviceHandler.Key);
                //_factories[serviceHandler.Key] = eventStreamFactory.CreateFor(ServiceKey.Create("bongo"));

                serviceHandler.Value.SetEventStreamFactory(eventStreamFactory);
            }
        }