4#include "flutter/shell/platform/windows/keyboard_key_channel_handler.h"
8#include "flutter/shell/platform/common/json_message_codec.h"
9#include "flutter/shell/platform/windows/flutter_windows_view.h"
10#include "flutter/shell/platform/windows/testing/test_binary_messenger.h"
11#include "gmock/gmock.h"
12#include "gtest/gtest.h"
19static constexpr char kCharacterCodePointKey[] =
"characterCodePoint";
20static constexpr int kHandledScanCode = 0x14;
21static constexpr int kUnhandledScanCode = 0x15;
22static constexpr int kUnhandledScanCodeExtended = 0xe015;
24static 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);
33TEST(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);
88TEST(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);
128TEST(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) {});
152TEST(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) {});
static const JsonMessageCodec & GetInstance()
std::unique_ptr< T > DecodeMessage(const uint8_t *binary_message, const size_t message_size) const
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
static constexpr char kScanCodeKey[]
TEST(DisplayListComplexity, EmptyDisplayList)
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
#define EXPECT_TRUE(handle)