10 #include "flutter/shell/platform/windows/testing/test_binary_messenger.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
18 static constexpr
char kScanCodeKey[] =
"scanCode";
19 static constexpr
char kCharacterCodePointKey[] =
"characterCodePoint";
20 static constexpr
int kHandledScanCode = 0x14;
21 static constexpr
int kUnhandledScanCode = 0x15;
22 static constexpr
int kUnhandledScanCodeExtended = 0xe015;
24 static std::unique_ptr<std::vector<uint8_t>> CreateResponse(
bool handled) {
26 std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
27 auto& allocator = response_doc->GetAllocator();
28 response_doc->AddMember(
"handled", handled, allocator);
33 TEST(KeyboardKeyChannelHandlerTest, KeyboardHookHandling) {
34 auto handled_message = CreateResponse(
true);
35 auto unhandled_message = CreateResponse(
false);
36 int received_scancode = 0;
38 TestBinaryMessenger messenger(
39 [&received_scancode, &handled_message, &unhandled_message](
40 const std::string& channel,
const uint8_t*
message,
42 if (channel ==
"flutter/keyevent") {
45 received_scancode = (*message_doc)[kScanCodeKey].GetInt();
46 if (received_scancode == kHandledScanCode) {
47 reply(handled_message->data(), handled_message->size());
49 reply(unhandled_message->data(), unhandled_message->size());
55 bool last_handled =
false;
58 64, kHandledScanCode, WM_KEYDOWN, L
'a',
false,
false,
59 [&last_handled](
bool handled) { last_handled = handled; });
60 EXPECT_EQ(received_scancode, kHandledScanCode);
61 EXPECT_EQ(last_handled,
true);
63 received_scancode = 0;
66 64, kUnhandledScanCode, WM_KEYDOWN, L
'b',
false,
false,
67 [&last_handled](
bool handled) { last_handled = handled; });
68 EXPECT_EQ(received_scancode, kUnhandledScanCode);
69 EXPECT_EQ(last_handled,
false);
71 received_scancode = 0;
74 64, kHandledScanCode, WM_SYSKEYDOWN, L
'a',
false,
false,
75 [&last_handled](
bool handled) { last_handled = handled; });
76 EXPECT_EQ(received_scancode, kHandledScanCode);
77 EXPECT_EQ(last_handled,
true);
79 received_scancode = 0;
82 64, kUnhandledScanCode, WM_SYSKEYDOWN, L
'c',
false,
false,
83 [&last_handled](
bool handled) { last_handled = handled; });
84 EXPECT_EQ(received_scancode, kUnhandledScanCode);
85 EXPECT_EQ(last_handled,
false);
88 TEST(KeyboardKeyChannelHandlerTest, ExtendedKeysAreSentToRedispatch) {
89 auto handled_message = CreateResponse(
true);
90 auto unhandled_message = CreateResponse(
false);
91 int received_scancode = 0;
93 TestBinaryMessenger messenger(
94 [&received_scancode, &handled_message, &unhandled_message](
95 const std::string& channel,
const uint8_t*
message,
97 if (channel ==
"flutter/keyevent") {
100 received_scancode = (*message_doc)[kScanCodeKey].GetInt();
101 if (received_scancode == kHandledScanCode) {
102 reply(handled_message->data(), handled_message->size());
104 reply(unhandled_message->data(), unhandled_message->size());
110 bool last_handled =
true;
113 handler.KeyboardHook(
114 64, kUnhandledScanCode, WM_KEYDOWN, L
'b',
true,
false,
115 [&last_handled](
bool handled) { last_handled = handled; });
116 EXPECT_EQ(last_handled,
false);
117 EXPECT_EQ(received_scancode, kUnhandledScanCodeExtended);
121 handler.KeyboardHook(
122 64, kUnhandledScanCode, WM_KEYDOWN, L
'b',
false,
false,
123 [&last_handled](
bool handled) { last_handled = handled; });
124 EXPECT_EQ(last_handled,
false);
125 EXPECT_EQ(received_scancode, kUnhandledScanCode);
128 TEST(KeyboardKeyChannelHandlerTest, DeadKeysDoNotCrash) {
129 bool received =
false;
130 TestBinaryMessenger messenger(
131 [&received](
const std::string& channel,
const uint8_t*
message,
133 if (channel ==
"flutter/keyevent") {
136 uint32_t
character = (*message_doc)[kCharacterCodePointKey].GetUint();
145 handler.KeyboardHook(0xDD, 0x1a, WM_KEYDOWN, 0x8000005E,
false,
false,
146 [](
bool handled) {});
149 EXPECT_TRUE(received);
152 TEST(KeyboardKeyChannelHandlerTest, EmptyResponsesDoNotCrash) {
153 bool received =
false;
154 TestBinaryMessenger messenger(
155 [&received](
const std::string& channel,
const uint8_t*
message,
157 if (channel ==
"flutter/keyevent") {
158 std::string empty_message =
"";
159 std::vector<uint8_t> empty_response(empty_message.begin(),
160 empty_message.end());
161 reply(empty_response.data(), empty_response.size());
168 handler.KeyboardHook(64, kUnhandledScanCode, WM_KEYDOWN, L
'b',
false,
false,
169 [](
bool handled) {});
172 EXPECT_TRUE(received);