1package io.flutter.embedding.engine.systemchannels;
3import static io.flutter.Build.API_LEVELS;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertTrue;
7import static org.mockito.Mockito.any;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.times;
10import static org.mockito.Mockito.verify;
11import static org.mockito.Mockito.when;
13import android.annotation.TargetApi;
17import androidx.test.ext.junit.runners.AndroidJUnit4;
18import io.flutter.plugin.common.BinaryMessenger;
19import io.flutter.plugin.common.JSONMessageCodec;
20import io.flutter.util.FakeKeyEvent;
21import java.nio.ByteBuffer;
22import org.json.JSONException;
23import org.json.JSONObject;
24import org.junit.After;
25import org.junit.Before;
27import org.junit.runner.RunWith;
28import org.mockito.ArgumentCaptor;
29import org.mockito.Mock;
30import org.mockito.MockitoAnnotations;
31import org.robolectric.annotation.Config;
32import org.robolectric.annotation.Implementation;
33import org.robolectric.annotation.Implements;
34import org.robolectric.annotation.Resetter;
35import org.robolectric.shadow.api.Shadow;
38@RunWith(AndroidJUnit4.class)
39@TargetApi(API_LEVELS.API_24)
48 throws JSONException {
49 JSONObject reply =
new JSONObject();
50 reply.put(
"handled",
true);
52 assertNotNull(binaryReply);
54 messengerReply.reply(binaryReply);
59 MockitoAnnotations.openMocks(
this);
61 handled =
new boolean[] {
false};
71 @Config(shadows = {ShadowInputDevice.class})
73 final InputDevice
device = mock(InputDevice.class);
74 when(
device.isVirtual()).thenReturn(
false);
75 when(
device.getName()).thenReturn(
"keyboard");
76 ShadowInputDevice.sDeviceIds =
new int[] {0};
79 KeyEventChannel.FlutterKeyEvent flutterKeyEvent =
85 handled[0] = isHandled;
88 ArgumentCaptor<ByteBuffer> byteBufferArgumentCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
91 verify(fakeMessenger,
times(1))
92 .send(
any(), byteBufferArgumentCaptor.capture(), replyArgumentCaptor.capture());
93 ByteBuffer capturedMessage = byteBufferArgumentCaptor.getValue();
94 capturedMessage.rewind();
97 assertEquals(
"keydown",
message.get(
"type"));
100 sendReply(
true, replyArgumentCaptor.getValue());
101 assertTrue(handled[0]);
105 @Config(shadows = {ShadowInputDevice.class})
107 final InputDevice
device = mock(InputDevice.class);
108 when(
device.isVirtual()).thenReturn(
false);
109 when(
device.getName()).thenReturn(
"keyboard");
110 ShadowInputDevice.sDeviceIds =
new int[] {0};
114 KeyEventChannel.FlutterKeyEvent flutterKeyEvent =
120 handled[0] = isHandled;
123 ArgumentCaptor<ByteBuffer> byteBufferArgumentCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
126 verify(fakeMessenger,
times(1))
127 .send(
any(), byteBufferArgumentCaptor.capture(), replyArgumentCaptor.capture());
128 ByteBuffer capturedMessage = byteBufferArgumentCaptor.getValue();
129 capturedMessage.rewind();
132 assertEquals(
"keydown",
message.get(
"type"));
135 sendReply(
true, replyArgumentCaptor.getValue());
136 assertTrue(handled[0]);
139 @Implements(InputDevice.class)
142 private static SparseArray<InputDevice> sDeviceMap =
new SparseArray<>();
144 private int mDeviceId;
153 return sDeviceMap.get(
id);
157 sDeviceMap.append(
id,
device);
172 final InputDevice inputDevice = Shadow.newInstanceOf(InputDevice.class);
174 shadowInputDevice.
setId(
id);
static SkISize times(const SkISize &size, float factor)
void keyDownEventIsSentToFramework()
void keyUpEventIsSentToFramework()
KeyEventChannel keyEventChannel
BinaryMessenger fakeMessenger
void sendFlutterKeyEvent( @NonNull FlutterKeyEvent keyEvent, boolean isKeyUp, @NonNull EventResponseHandler responseHandler)
Object decodeMessage(@Nullable ByteBuffer message)
static final JSONMessageCodec INSTANCE
ByteBuffer encodeMessage(@Nullable Object message)
SIT bool any(const Vec< 1, T > &x)