Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
MethodChannelTest.java
Go to the documentation of this file.
1package io.flutter.plugin.common;
2
3import static org.mockito.ArgumentMatchers.anyInt;
4import static org.mockito.ArgumentMatchers.argThat;
5import static org.mockito.ArgumentMatchers.eq;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.times;
8import static org.mockito.Mockito.verify;
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 java.nio.ByteBuffer;
15import org.junit.Test;
16import org.junit.runner.RunWith;
17import org.mockito.ArgumentMatcher;
18import org.robolectric.annotation.Config;
19
20@Config(manifest = Config.NONE)
21@RunWith(AndroidJUnit4.class)
22public class MethodChannelTest {
23 @Test
25 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
26 DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class));
27 String channel = "flutter/test";
28 MethodChannel rawChannel = new MethodChannel(dartExecutor, channel);
29
30 int newSize = 3;
31 rawChannel.resizeChannelBuffer(newSize);
32
33 // Created from the following Dart code:
34 // MethodCall methodCall = const MethodCall('resize', ['flutter/test', 3]);
35 // const StandardMethodCodec().encodeMethodCall(methodCall).buffer.asUint8List();
36 final byte[] expected = {
37 7, 6, 114, 101, 115, 105, 122, 101, 12, 2, 7, 12, 102, 108, 117, 116, 116, 101, 114, 47, 116,
38 101, 115, 116, 3, 3, 0, 0, 0
39 };
40
41 // Verify that the correct message was sent to FlutterJNI.
42 ArgumentMatcher<ByteBuffer> packetMatcher =
43 new ByteBufferContentMatcher(ByteBuffer.wrap(expected));
44 verify(mockFlutterJNI, times(1))
45 .dispatchPlatformMessage(
47 argThat(packetMatcher),
48 anyInt(),
49 anyInt());
50 }
51
52 @Test
54 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
55 DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class));
56 String channel = "flutter/test";
57 MethodChannel rawChannel = new MethodChannel(dartExecutor, channel);
58
59 rawChannel.setWarnsOnChannelOverflow(false);
60
61 // Created from the following Dart code:
62 // MethodCall methodCall = const MethodCall('overflow', ['flutter/test', true]);
63 // const StandardMethodCodec().encodeMethodCall(methodCall).buffer.asUint8List();
64 final byte[] expected = {
65 7, 8, 111, 118, 101, 114, 102, 108, 111, 119, 12, 2, 7, 12, 102, 108, 117, 116, 116, 101, 114,
66 47, 116, 101, 115, 116, 1
67 };
68
69 // Verify that the correct message was sent to FlutterJNI.
70 ArgumentMatcher<ByteBuffer> packetMatcher =
71 new ByteBufferContentMatcher(ByteBuffer.wrap(expected));
72 verify(mockFlutterJNI, times(1))
73 .dispatchPlatformMessage(
75 argThat(packetMatcher),
76 anyInt(),
77 anyInt());
78 }
79}
80
81// Custom ByteBuffer matcher which calls rewind on both buffers before calling equals.
82// ByteBuffer.equals might return true when comparing byte buffers with different content if
83// both have no remaining elements.
84class ByteBufferContentMatcher implements ArgumentMatcher<ByteBuffer> {
85 private ByteBuffer expected;
86
87 public ByteBufferContentMatcher(ByteBuffer expected) {
88 this.expected = expected;
89 }
90
91 @Override
92 public boolean matches(ByteBuffer received) {
93 expected.rewind();
94 received.rewind();
95 return received.equals(expected);
96 }
97}
static SkISize times(const SkISize &size, float factor)
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Definition M44Test.cpp:18