Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
shell
platform
android
test
io
flutter
plugin
common
MethodChannelTest.java
Go to the documentation of this file.
1
package
io.flutter.plugin.common;
2
3
import
static
org.mockito.ArgumentMatchers.anyInt;
4
import
static
org.mockito.ArgumentMatchers.argThat;
5
import
static
org.mockito.ArgumentMatchers.eq;
6
import
static
org.mockito.Mockito.mock;
7
import
static
org.mockito.Mockito.times;
8
import
static
org.mockito.Mockito.verify;
9
10
import
android
.content.res.AssetManager;
11
import
androidx.test.ext.junit.runners.AndroidJUnit4;
12
import
io.flutter.embedding.engine.FlutterJNI;
13
import
io.flutter.embedding.engine.dart.DartExecutor;
14
import
java.nio.ByteBuffer;
15
import
org.junit.Test;
16
import
org.junit.runner.RunWith;
17
import
org.mockito.ArgumentMatcher;
18
import
org.robolectric.annotation.Config;
19
20
@
Config
(manifest =
Config
.NONE)
21
@RunWith(AndroidJUnit4.class)
22
public class
MethodChannelTest
{
23
@Test
24
public
void
resizeChannelBufferMessageIsWellformed
() {
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(
46
eq
(
BasicMessageChannel
.
CHANNEL_BUFFERS_CHANNEL
),
47
argThat(packetMatcher),
48
anyInt(),
49
anyInt());
50
}
51
52
@Test
53
public
void
overflowChannelBufferMessageIsWellformed
() {
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(
74
eq
(
BasicMessageChannel
.
CHANNEL_BUFFERS_CHANNEL
),
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.
84
class
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
}
times
static SkISize times(const SkISize &size, float factor)
Definition
AndroidCodecTest.cpp:29
eq
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Definition
M44Test.cpp:18
io.flutter.plugin.common.BasicMessageChannel
Definition
BasicMessageChannel.java:31
io.flutter.plugin.common.BasicMessageChannel.CHANNEL_BUFFERS_CHANNEL
static final String CHANNEL_BUFFERS_CHANNEL
Definition
BasicMessageChannel.java:33
io.flutter.plugin.common.ByteBufferContentMatcher
Definition
MethodChannelTest.java:84
io.flutter.plugin.common.ByteBufferContentMatcher.matches
boolean matches(ByteBuffer received)
Definition
MethodChannelTest.java:92
io.flutter.plugin.common.ByteBufferContentMatcher.ByteBufferContentMatcher
ByteBufferContentMatcher(ByteBuffer expected)
Definition
MethodChannelTest.java:87
io.flutter.plugin.common.MethodChannelTest
Definition
MethodChannelTest.java:22
io.flutter.plugin.common.MethodChannelTest.resizeChannelBufferMessageIsWellformed
void resizeChannelBufferMessageIsWellformed()
Definition
MethodChannelTest.java:24
io.flutter.plugin.common.MethodChannelTest.overflowChannelBufferMessageIsWellformed
void overflowChannelBufferMessageIsWellformed()
Definition
MethodChannelTest.java:53
io.flutter.plugin.common.MethodChannel
Definition
MethodChannel.java:29
io.flutter.plugin.common.MethodChannel.setWarnsOnChannelOverflow
void setWarnsOnChannelOverflow(boolean warns)
Definition
MethodChannel.java:165
io.flutter.plugin.common.MethodChannel.resizeChannelBuffer
void resizeChannelBuffer(int newSize)
Definition
MethodChannel.java:156
android
Definition
BitmapRegionDecoder.cpp:14
Config
Definition
nanobench.h:24
Generated on Fri Apr 26 2024 06:14:22 for Flutter Engine by
1.9.8