Flutter Engine
The Flutter Engine
PlatformChannelTest.java
Go to the documentation of this file.
1package io.flutter.embedding.engine.systemchannels;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.ArgumentMatchers.refEq;
5import static org.mockito.Mockito.doNothing;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.verify;
8import static org.mockito.Mockito.when;
9
10import android.content.res.AssetManager;
11import androidx.test.ext.junit.runners.AndroidJUnit4;
12import io.flutter.embedding.engine.FlutterJNI;
13import io.flutter.embedding.engine.dart.DartExecutor;
14import io.flutter.plugin.common.MethodCall;
15import io.flutter.plugin.common.MethodChannel;
16import org.json.JSONException;
17import org.json.JSONObject;
18import org.junit.Test;
19import org.junit.runner.RunWith;
20import org.mockito.ArgumentCaptor;
21import org.robolectric.annotation.Config;
22
23@Config(manifest = Config.NONE)
24@RunWith(AndroidJUnit4.class)
25public class PlatformChannelTest {
26 @Test
28 MethodChannel rawChannel = mock(MethodChannel.class);
29 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
30 DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class));
31 PlatformChannel fakePlatformChannel = new PlatformChannel(dartExecutor);
32 PlatformChannel.PlatformMessageHandler mockMessageHandler =
34 fakePlatformChannel.setPlatformMessageHandler(mockMessageHandler);
35 Boolean returnValue = true;
36 when(mockMessageHandler.clipboardHasStrings()).thenReturn(returnValue);
37 MethodCall methodCall = new MethodCall("Clipboard.hasStrings", null);
38 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
39 fakePlatformChannel.parsingMethodCallHandler.onMethodCall(methodCall, mockResult);
40
41 JSONObject expected = new JSONObject();
42 try {
43 expected.put("value", returnValue);
44 } catch (JSONException e) {
45 }
46 verify(mockResult).success(refEq(expected));
47 }
48
49 @Test
51 MethodChannel rawChannel = mock(MethodChannel.class);
52 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
53 DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class));
54 PlatformChannel fakePlatformChannel = new PlatformChannel(dartExecutor);
55 PlatformChannel.PlatformMessageHandler mockMessageHandler =
57 fakePlatformChannel.setPlatformMessageHandler(mockMessageHandler);
58
59 ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class);
60 doNothing().when(mockMessageHandler).share(valueCapture.capture());
61
62 final String expectedContent = "Flutter";
63 MethodCall methodCall = new MethodCall("Share.invoke", expectedContent);
64 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
65 fakePlatformChannel.parsingMethodCallHandler.onMethodCall(methodCall, mockResult);
66
67 assertEquals(valueCapture.getValue(), expectedContent);
68 verify(mockResult).success(null);
69 }
70}
void setPlatformMessageHandler(@Nullable PlatformMessageHandler platformMessageHandler)
final MethodChannel.MethodCallHandler parsingMethodCallHandler
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)