Flutter Linux Embedder
fl_key_channel_responder_test.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include "gtest/gtest.h"
8 
11 #include "flutter/shell/platform/linux/testing/fl_test.h"
12 
13 static const char* expected_value = nullptr;
14 static gboolean expected_handled = FALSE;
15 
16 static FlValue* echo_response_cb(FlValue* echoed_value) {
17  gchar* text = fl_value_to_string(echoed_value);
18  EXPECT_STREQ(text, expected_value);
19  g_free(text);
20 
24  return value;
25 }
26 
27 static void responder_callback(bool handled, gpointer user_data) {
28  EXPECT_EQ(handled, expected_handled);
29  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
30 }
31 
32 // Test sending a letter "A";
33 TEST(FlKeyChannelResponderTest, SendKeyEvent) {
34  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
35 
36  g_autoptr(FlEngine) engine = make_mock_engine();
37  g_autoptr(FlBinaryMessenger) messenger = fl_binary_messenger_new(engine);
40  .channel_name = "test/echo",
41  };
42  g_autoptr(FlKeyResponder) responder =
43  FL_KEY_RESPONDER(fl_key_channel_responder_new(messenger, &mock));
44 
45  g_autoptr(FlKeyEvent) event1 = fl_key_event_new(
46  12345, TRUE, 0x04, GDK_KEY_A, static_cast<GdkModifierType>(0), 0);
47  fl_key_responder_handle_event(responder, event1, responder_callback, loop);
49  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, keyCode: 65, "
50  "modifiers: 0, unicodeScalarValues: 65}";
51  expected_handled = FALSE;
52 
53  // Blocks here until echo_response_cb is called.
54  g_main_loop_run(loop);
55 
56  g_autoptr(FlKeyEvent) event2 = fl_key_event_new(
57  23456, FALSE, 0x04, GDK_KEY_A, static_cast<GdkModifierType>(0), 0);
58  fl_key_responder_handle_event(responder, event2, responder_callback, loop);
60  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, keyCode: 65, "
61  "modifiers: 0, unicodeScalarValues: 65}";
62  expected_handled = FALSE;
63 
64  // Blocks here until echo_response_cb is called.
65  g_main_loop_run(loop);
66 }
67 
68 void test_lock_event(guint key_code,
69  const char* down_expected,
70  const char* up_expected) {
71  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
72 
73  g_autoptr(FlEngine) engine = make_mock_engine();
74  g_autoptr(FlBinaryMessenger) messenger = fl_binary_messenger_new(engine);
77  .channel_name = "test/echo",
78  };
79  g_autoptr(FlKeyResponder) responder =
80  FL_KEY_RESPONDER(fl_key_channel_responder_new(messenger, &mock));
81 
82  g_autoptr(FlKeyEvent) event1 = fl_key_event_new(
83  12345, TRUE, 0x04, key_code, static_cast<GdkModifierType>(0), 0);
84  fl_key_responder_handle_event(responder, event1, responder_callback, loop);
85  expected_value = down_expected;
86  expected_handled = FALSE;
87 
88  // Blocks here until echo_response_cb is called.
89  g_main_loop_run(loop);
90 
91  expected_value = up_expected;
92  expected_handled = FALSE;
93  g_autoptr(FlKeyEvent) event2 = fl_key_event_new(
94  12346, FALSE, 0x04, key_code, static_cast<GdkModifierType>(0), 0);
95  fl_key_responder_handle_event(responder, event2, responder_callback, loop);
96 
97  // Blocks here until echo_response_cb is called.
98  g_main_loop_run(loop);
99 }
100 
101 // Test sending a "NumLock" keypress.
102 TEST(FlKeyChannelResponderTest, SendNumLockKeyEvent) {
103  test_lock_event(GDK_KEY_Num_Lock,
104  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
105  "keyCode: 65407, modifiers: 16}",
106  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
107  "keyCode: 65407, modifiers: 0}");
108 }
109 
110 // Test sending a "CapsLock" keypress.
111 TEST(FlKeyChannelResponderTest, SendCapsLockKeyEvent) {
112  test_lock_event(GDK_KEY_Caps_Lock,
113  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
114  "keyCode: 65509, modifiers: 2}",
115  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
116  "keyCode: 65509, modifiers: 0}");
117 }
118 
119 // Test sending a "ShiftLock" keypress.
120 TEST(FlKeyChannelResponderTest, SendShiftLockKeyEvent) {
121  test_lock_event(GDK_KEY_Shift_Lock,
122  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
123  "keyCode: 65510, modifiers: 2}",
124  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
125  "keyCode: 65510, modifiers: 0}");
126 }
127 
128 TEST(FlKeyChannelResponderTest, TestKeyEventHandledByFramework) {
129  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
130 
131  g_autoptr(FlEngine) engine = make_mock_engine();
132  g_autoptr(FlBinaryMessenger) messenger = fl_binary_messenger_new(engine);
135  .channel_name = "test/echo",
136  };
137  g_autoptr(FlKeyResponder) responder =
138  FL_KEY_RESPONDER(fl_key_channel_responder_new(messenger, &mock));
139 
140  g_autoptr(FlKeyEvent) event = fl_key_event_new(
141  12345, TRUE, 0x04, GDK_KEY_A, static_cast<GdkModifierType>(0), 0);
145  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
146  "keyCode: 65, modifiers: 0, unicodeScalarValues: 65}";
147 
148  // Blocks here until echo_response_cb is called.
149  g_main_loop_run(loop);
150 }
151 
152 TEST(FlKeyChannelResponderTest, UseSpecifiedLogicalKey) {
153  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
154 
155  g_autoptr(FlEngine) engine = make_mock_engine();
156  g_autoptr(FlBinaryMessenger) messenger = fl_binary_messenger_new(engine);
159  .channel_name = "test/echo",
160  };
161  g_autoptr(FlKeyResponder) responder =
162  FL_KEY_RESPONDER(fl_key_channel_responder_new(messenger, &mock));
163 
164  g_autoptr(FlKeyEvent) event = fl_key_event_new(
165  12345, TRUE, 0x04, GDK_KEY_A, static_cast<GdkModifierType>(0), 0);
167  888);
170  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
171  "keyCode: 65, modifiers: 0, unicodeScalarValues: 65, "
172  "specifiedLogicalKey: 888}";
173 
174  // Blocks here until echo_response_cb is called.
175  g_main_loop_run(loop);
176 }
event
FlKeyEvent * event
Definition: fl_key_channel_responder.cc:118
responder_callback
static void responder_callback(bool handled, gpointer user_data)
Definition: fl_key_channel_responder_test.cc:27
fl_value_set_string_take
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:650
fl_value_new_bool
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
user_data
FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data
Definition: fl_key_channel_responder.cc:121
expected_value
static const char * expected_value
Definition: fl_key_channel_responder_test.cc:13
fl_binary_messenger_new
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)
Definition: fl_binary_messenger.cc:399
fl_key_channel_responder_new
FlKeyChannelResponder * fl_key_channel_responder_new(FlBinaryMessenger *messenger, FlKeyChannelResponderMock *mock)
Definition: fl_key_channel_responder.cc:186
make_mock_engine
static FlEngine * make_mock_engine()
Definition: fl_event_channel_test.cc:24
_FlKeyChannelResponderMock::value_converter
FlValueConverter value_converter
Definition: fl_key_channel_responder.h:29
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
fl_key_responder_handle_event
void fl_key_responder_handle_event(FlKeyResponder *self, FlKeyEvent *event, FlKeyResponderAsyncCallback callback, gpointer user_data, uint64_t specified_logical_key)
Definition: fl_key_responder.cc:11
fl_engine_private.h
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_binary_messenger_private.h
TEST
TEST(FlKeyChannelResponderTest, SendKeyEvent)
Definition: fl_key_channel_responder_test.cc:33
echo_response_cb
static FlValue * echo_response_cb(FlValue *echoed_value)
Definition: fl_key_channel_responder_test.cc:16
_FlKeyChannelResponderMock
Definition: fl_key_channel_responder.h:23
fl_key_event_new
FlKeyEvent * fl_key_event_new(guint32 time, gboolean is_press, guint16 keycode, guint keyval, GdkModifierType state, guint8 group)
Definition: fl_key_event.cc:34
fl_key_channel_responder.h
test_lock_event
void test_lock_event(guint key_code, const char *down_expected, const char *up_expected)
Definition: fl_key_channel_responder_test.cc:68
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_value_to_string
G_MODULE_EXPORT gchar * fl_value_to_string(FlValue *value)
Definition: fl_value.cc:846
expected_handled
static gboolean expected_handled
Definition: fl_key_channel_responder_test.cc:14