Flutter Engine
The Flutter Engine
KeyboardChannelTest.java
Go to the documentation of this file.
1package io.flutter.embedding.android;
2
3import static org.mockito.Mockito.any;
4import static org.mockito.Mockito.mock;
5import static org.mockito.Mockito.times;
6import static org.mockito.Mockito.verify;
7
8import androidx.test.ext.junit.runners.AndroidJUnit4;
9import io.flutter.embedding.engine.dart.DartExecutor;
10import io.flutter.embedding.engine.systemchannels.KeyboardChannel;
11import io.flutter.plugin.common.BinaryMessenger;
12import io.flutter.plugin.common.MethodCall;
13import io.flutter.plugin.common.MethodChannel;
14import io.flutter.plugin.common.StandardMethodCodec;
15import java.nio.ByteBuffer;
16import java.util.HashMap;
17import org.junit.Test;
18import org.junit.runner.RunWith;
19import org.mockito.ArgumentCaptor;
20import org.robolectric.annotation.Config;
21
22@Config(manifest = Config.NONE)
23@RunWith(AndroidJUnit4.class)
24public class KeyboardChannelTest {
25
26 private static BinaryMessenger.BinaryReply sendToBinaryMessageHandler(
27 BinaryMessenger.BinaryMessageHandler binaryMessageHandler, String method, Object args) {
28 MethodCall methodCall = new MethodCall(method, args);
29 ByteBuffer encodedMethodCall = StandardMethodCodec.INSTANCE.encodeMethodCall(methodCall);
30 BinaryMessenger.BinaryReply reply = mock(BinaryMessenger.BinaryReply.class);
31 binaryMessageHandler.onMessage((ByteBuffer) encodedMethodCall.flip(), reply);
32 return reply;
33 }
34
35 @SuppressWarnings("deprecation")
36 // setMessageHandler is deprecated.
37 @Test
39 ArgumentCaptor<BinaryMessenger.BinaryMessageHandler> binaryMessageHandlerCaptor =
40 ArgumentCaptor.forClass(BinaryMessenger.BinaryMessageHandler.class);
41 DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
42 KeyboardChannel.KeyboardMethodHandler mockHandler =
44 KeyboardChannel keyboardChannel = new KeyboardChannel(mockBinaryMessenger);
45
46 verify(mockBinaryMessenger, times(1))
47 .setMessageHandler(any(String.class), binaryMessageHandlerCaptor.capture());
48
49 BinaryMessenger.BinaryMessageHandler binaryMessageHandler =
50 binaryMessageHandlerCaptor.getValue();
51
52 keyboardChannel.setKeyboardMethodHandler(mockHandler);
53 sendToBinaryMessageHandler(binaryMessageHandler, "getKeyboardState", null);
54
55 verify(mockHandler, times(1)).getKeyboardState();
56 }
57
58 @Test
60 // Regression test for https://github.com/flutter/flutter/issues/122441#issuecomment-1582052616.
61
62 KeyboardChannel keyboardChannel = new KeyboardChannel(mock(DartExecutor.class));
63 MethodCall methodCall = new MethodCall("getKeyboardState", null);
64 MethodChannel.Result result = mock(MethodChannel.Result.class);
65 keyboardChannel.parsingMethodHandler.onMethodCall(methodCall, result);
66
67 verify(result).success(new HashMap());
68 }
69}
static SkISize times(const SkISize &size, float factor)
final MethodChannel.MethodCallHandler parsingMethodHandler
void setKeyboardMethodHandler(@Nullable KeyboardMethodHandler keyboardMethodHandler)
ByteBuffer encodeMethodCall(@NonNull MethodCall methodCall)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)
GAsyncResult * result
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530