Flutter Engine
fl_key_event_plugin_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 
5 #include "flutter/shell/platform/linux/fl_key_event_plugin.h"
6 
7 #include <iostream>
8 #include "gtest/gtest.h"
9 
10 #include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
11 #include "flutter/shell/platform/linux/fl_engine_private.h"
12 #include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
13 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
14 #include "flutter/shell/platform/linux/testing/fl_test.h"
15 #include "flutter/shell/platform/linux/testing/mock_renderer.h"
16 
17 const char* expected_value = nullptr;
18 
19 // Called when the message response is received in the send_key_event test.
20 static void echo_response_cb(GObject* object,
21  GAsyncResult* result,
22  gpointer user_data) {
23  g_autoptr(GError) error = nullptr;
24  g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
25  FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
26  EXPECT_NE(message, nullptr);
27  EXPECT_EQ(error, nullptr);
28 
29  EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP);
30  EXPECT_STREQ(fl_value_to_string(message), expected_value);
31  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
32 }
33 
34 // Test sending a letter "A";
35 TEST(FlKeyEventPluginTest, SendKeyEvent) {
36  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
37 
38  g_autoptr(FlEngine) engine = make_mock_engine();
39  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
40  g_autoptr(FlKeyEventPlugin) plugin =
41  fl_key_event_plugin_new(messenger, echo_response_cb, "test/echo");
42 
43  char string[] = "A";
44  GdkEventKey key_event = GdkEventKey{
45  GDK_KEY_PRESS, // event type
46  nullptr, // window (not needed)
47  FALSE, // event was sent explicitly
48  12345, // time
49  0x0, // modifier state
50  GDK_KEY_A, // key code
51  1, // length of string representation
52  reinterpret_cast<gchar*>(&string[0]), // string representation
53  0x04, // scan code
54  0, // keyboard group
55  0, // is a modifier
56  };
57 
59  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, keyCode: 65, "
60  "modifiers: 0, unicodeScalarValues: 65}";
61  fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
62 
63  // Blocks here until echo_response_cb is called.
64  g_main_loop_run(loop);
65 
66  key_event = GdkEventKey{
67  GDK_KEY_RELEASE, // event type
68  nullptr, // window (not needed)
69  FALSE, // event was sent explicitly
70  12345, // time
71  0x0, // modifier state
72  GDK_KEY_A, // key code
73  1, // length of string representation
74  reinterpret_cast<gchar*>(&string[0]), // string representation
75  0x04, // scan code
76  0, // keyboard group
77  0, // is a modifier
78  };
79 
80  expected_value =
81  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, keyCode: 65, "
82  "modifiers: 0, unicodeScalarValues: 65}";
83  fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
84 
85  // Blocks here until echo_response_cb is called.
86  g_main_loop_run(loop);
87 }
88 
89 void test_lock_event(guint key_code,
90  const char* down_expected,
91  const char* up_expected) {
92  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
93 
94  g_autoptr(FlEngine) engine = make_mock_engine();
95  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
96  g_autoptr(FlKeyEventPlugin) plugin =
97  fl_key_event_plugin_new(messenger, echo_response_cb, "test/echo");
98 
99  GdkEventKey key_event = GdkEventKey{
100  GDK_KEY_PRESS, // event type
101  nullptr, // window (not needed)
102  FALSE, // event was sent explicitly
103  12345, // time
104  0x10, // modifier state
105  key_code, // key code
106  1, // length of string representation
107  nullptr, // string representation
108  0x04, // scan code
109  0, // keyboard group
110  0, // is a modifier
111  };
112 
113  expected_value = down_expected;
114  fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
115 
116  // Blocks here until echo_response_cb is called.
117  g_main_loop_run(loop);
118 
119  key_event.type = GDK_KEY_RELEASE;
120 
121  expected_value = up_expected;
122  fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
123 
124  // Blocks here until echo_response_cb is called.
125  g_main_loop_run(loop);
126 }
127 
128 // Test sending a "NumLock" keypress.
129 TEST(FlKeyEventPluginTest, SendNumLockKeyEvent) {
130  test_lock_event(GDK_KEY_Num_Lock,
131  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
132  "keyCode: 65407, modifiers: 16}",
133  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
134  "keyCode: 65407, modifiers: 0}");
135 }
136 
137 // Test sending a "CapsLock" keypress.
138 TEST(FlKeyEventPluginTest, SendCapsLockKeyEvent) {
139  test_lock_event(GDK_KEY_Caps_Lock,
140  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
141  "keyCode: 65509, modifiers: 2}",
142  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
143  "keyCode: 65509, modifiers: 0}");
144 }
145 
146 // Test sending a "ShiftLock" keypress.
147 TEST(FlKeyEventPluginTest, SendShiftLockKeyEvent) {
148  test_lock_event(GDK_KEY_Shift_Lock,
149  "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, "
150  "keyCode: 65510, modifiers: 2}",
151  "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, "
152  "keyCode: 65510, modifiers: 0}");
153 }
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:395
FlKeyEventPlugin * fl_key_event_plugin_new(FlBinaryMessenger *messenger, GAsyncReadyCallback response_callback, const char *channel_name)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
void test_lock_event(guint key_code, const char *down_expected, const char *up_expected)
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
static FlEngine * make_mock_engine()
G_BEGIN_DECLS FlValue gpointer user_data
TEST(FlKeyEventPluginTest, SendKeyEvent)
G_MODULE_EXPORT gchar * fl_value_to_string(FlValue *value)
Definition: fl_value.cc:723
void fl_key_event_plugin_send_key_event(FlKeyEventPlugin *self, GdkEventKey *event, gpointer user_data)
const char * expected_value
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)