|
| 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/mock_renderer.h" |
| 15 | + |
| 16 | +// Creates a mock engine that responds to platform messages. |
| 17 | +static FlEngine* make_mock_engine() { |
| 18 | + g_autoptr(FlDartProject) project = fl_dart_project_new(); |
| 19 | + g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new(); |
| 20 | + g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer)); |
| 21 | + g_autoptr(GError) engine_error = nullptr; |
| 22 | + EXPECT_TRUE(fl_engine_start(engine, &engine_error)); |
| 23 | + EXPECT_EQ(engine_error, nullptr); |
| 24 | + |
| 25 | + return static_cast<FlEngine*>(g_object_ref(engine)); |
| 26 | +} |
| 27 | + |
| 28 | +const char* expected_value = nullptr; |
| 29 | + |
| 30 | +// Called when the message response is received in the send_key_event test. |
| 31 | +static void echo_response_cb(GObject* object, |
| 32 | + GAsyncResult* result, |
| 33 | + gpointer user_data) { |
| 34 | + g_autoptr(GError) error = nullptr; |
| 35 | + g_autoptr(FlValue) message = fl_basic_message_channel_send_finish( |
| 36 | + FL_BASIC_MESSAGE_CHANNEL(object), result, &error); |
| 37 | + EXPECT_NE(message, nullptr); |
| 38 | + EXPECT_EQ(error, nullptr); |
| 39 | + |
| 40 | + EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP); |
| 41 | + EXPECT_STREQ(fl_value_to_string(message), expected_value); |
| 42 | + g_main_loop_quit(static_cast<GMainLoop*>(user_data)); |
| 43 | +} |
| 44 | + |
| 45 | +// Test sending a letter "A"; |
| 46 | +TEST(FlKeyEventPluginTest, SendKeyEvent) { |
| 47 | + g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0); |
| 48 | + |
| 49 | + g_autoptr(FlEngine) engine = make_mock_engine(); |
| 50 | + FlBinaryMessenger* messenger = fl_binary_messenger_new(engine); |
| 51 | + g_autoptr(FlKeyEventPlugin) plugin = |
| 52 | + fl_key_event_plugin_new(messenger, echo_response_cb, "test/echo"); |
| 53 | + |
| 54 | + char string[] = "A"; |
| 55 | + GdkEventKey key_event = GdkEventKey{ |
| 56 | + GDK_KEY_PRESS, // event type |
| 57 | + nullptr, // window (not needed) |
| 58 | + FALSE, // event was sent explicitly |
| 59 | + 12345, // time |
| 60 | + 0x0, // modifier state |
| 61 | + GDK_KEY_A, // key code |
| 62 | + 1, // length of string representation |
| 63 | + reinterpret_cast<gchar*>(&string[0]), // string representation |
| 64 | + 0x04, // scan code |
| 65 | + 0, // keyboard group |
| 66 | + 0, // is a modifier |
| 67 | + }; |
| 68 | + |
| 69 | + expected_value = |
| 70 | + "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, keyCode: 65, " |
| 71 | + "modifiers: 0, unicodeScalarValues: 65}"; |
| 72 | + fl_key_event_plugin_send_key_event(plugin, &key_event, loop); |
| 73 | + |
| 74 | + // Blocks here until echo_response_cb is called. |
| 75 | + g_main_loop_run(loop); |
| 76 | + |
| 77 | + key_event = GdkEventKey{ |
| 78 | + GDK_KEY_RELEASE, // event type |
| 79 | + nullptr, // window (not needed) |
| 80 | + FALSE, // event was sent explicitly |
| 81 | + 12345, // time |
| 82 | + 0x0, // modifier state |
| 83 | + GDK_KEY_A, // key code |
| 84 | + 1, // length of string representation |
| 85 | + reinterpret_cast<gchar*>(&string[0]), // string representation |
| 86 | + 0x04, // scan code |
| 87 | + 0, // keyboard group |
| 88 | + 0, // is a modifier |
| 89 | + }; |
| 90 | + |
| 91 | + expected_value = |
| 92 | + "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, keyCode: 65, " |
| 93 | + "modifiers: 0, unicodeScalarValues: 65}"; |
| 94 | + fl_key_event_plugin_send_key_event(plugin, &key_event, loop); |
| 95 | + |
| 96 | + // Blocks here until echo_response_cb is called. |
| 97 | + g_main_loop_run(loop); |
| 98 | +} |
| 99 | + |
| 100 | +void test_lock_event(guint key_code, |
| 101 | + const char* down_expected, |
| 102 | + const char* up_expected) { |
| 103 | + g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0); |
| 104 | + |
| 105 | + g_autoptr(FlEngine) engine = make_mock_engine(); |
| 106 | + FlBinaryMessenger* messenger = fl_binary_messenger_new(engine); |
| 107 | + g_autoptr(FlKeyEventPlugin) plugin = |
| 108 | + fl_key_event_plugin_new(messenger, echo_response_cb, "test/echo"); |
| 109 | + |
| 110 | + GdkEventKey key_event = GdkEventKey{ |
| 111 | + GDK_KEY_PRESS, // event type |
| 112 | + nullptr, // window (not needed) |
| 113 | + FALSE, // event was sent explicitly |
| 114 | + 12345, // time |
| 115 | + 0x10, // modifier state |
| 116 | + key_code, // key code |
| 117 | + 1, // length of string representation |
| 118 | + nullptr, // string representation |
| 119 | + 0x04, // scan code |
| 120 | + 0, // keyboard group |
| 121 | + 0, // is a modifier |
| 122 | + }; |
| 123 | + |
| 124 | + expected_value = down_expected; |
| 125 | + fl_key_event_plugin_send_key_event(plugin, &key_event, loop); |
| 126 | + |
| 127 | + // Blocks here until echo_response_cb is called. |
| 128 | + g_main_loop_run(loop); |
| 129 | + |
| 130 | + key_event.type = GDK_KEY_RELEASE; |
| 131 | + |
| 132 | + expected_value = up_expected; |
| 133 | + fl_key_event_plugin_send_key_event(plugin, &key_event, loop); |
| 134 | + |
| 135 | + // Blocks here until echo_response_cb is called. |
| 136 | + g_main_loop_run(loop); |
| 137 | +} |
| 138 | + |
| 139 | +// Test sending a "NumLock" keypress. |
| 140 | +TEST(FlKeyEventPluginTest, SendNumLockKeyEvent) { |
| 141 | + test_lock_event(GDK_KEY_Num_Lock, |
| 142 | + "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, " |
| 143 | + "keyCode: 65407, modifiers: 16}", |
| 144 | + "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, " |
| 145 | + "keyCode: 65407, modifiers: 0}"); |
| 146 | +} |
| 147 | + |
| 148 | +// Test sending a "CapsLock" keypress. |
| 149 | +TEST(FlKeyEventPluginTest, SendCapsLockKeyEvent) { |
| 150 | + test_lock_event(GDK_KEY_Caps_Lock, |
| 151 | + "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, " |
| 152 | + "keyCode: 65509, modifiers: 2}", |
| 153 | + "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, " |
| 154 | + "keyCode: 65509, modifiers: 0}"); |
| 155 | +} |
| 156 | + |
| 157 | +// Test sending a "ShiftLock" keypress. |
| 158 | +TEST(FlKeyEventPluginTest, SendShiftLockKeyEvent) { |
| 159 | + test_lock_event(GDK_KEY_Shift_Lock, |
| 160 | + "{type: keydown, keymap: linux, scanCode: 4, toolkit: gtk, " |
| 161 | + "keyCode: 65510, modifiers: 2}", |
| 162 | + "{type: keyup, keymap: linux, scanCode: 4, toolkit: gtk, " |
| 163 | + "keyCode: 65510, modifiers: 0}"); |
| 164 | +} |
0 commit comments