| Flutter Linux Embedder |

fl_keyboard_handler_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_keyboard_handler.h"
6
7 #include "flutter/shell/platform/embedder/test_utils/key_codes.g.h"
8 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
9 #include "flutter/shell/platform/linux/fl_engine_private.h"
10 #include "flutter/shell/platform/linux/fl_method_codec_private.h"
11 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
12 #include "flutter/shell/platform/linux/testing/fl_mock_binary_messenger.h"
13 #include "flutter/shell/platform/linux/testing/mock_gtk.h"
14
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17
18 static constexpr char kKeyboardChannelName[] = "flutter/keyboard";
19 static constexpr char kGetKeyboardStateMethod[] = "getKeyboardState";
20
21 using ::flutter::testing::keycodes::kLogicalKeyA;
22 using ::flutter::testing::keycodes::kPhysicalKeyA;
23
24 constexpr guint16 kKeyCodeKeyA = 0x26u;
25
26 TEST(FlKeyboardHandlerTest, KeyboardChannelGetPressedState) {
27 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
28
29 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
30 g_autoptr(FlEngine) engine =
31 fl_engine_new_with_binary_messenger(FL_BINARY_MESSENGER(messenger));
32 g_autoptr(FlKeyboardManager) manager = fl_keyboard_manager_new(engine);
33
34 EXPECT_TRUE(fl_engine_start(engine, nullptr));
35
36 g_autoptr(FlKeyboardHandler) handler =
37 fl_keyboard_handler_new(FL_BINARY_MESSENGER(messenger), manager);
38 EXPECT_NE(handler, nullptr);
39
40 // Send key event to set pressed state.
41 fl_mock_binary_messenger_set_json_message_channel(
42 messenger, "flutter/keyevent",
43 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
44 gpointer user_data) {
45 FlValue* response = fl_value_new_map();
46 fl_value_set_string_take(response, "handled", fl_value_new_bool(FALSE));
47 return response;
48 },
49 nullptr);
50 fl_engine_get_embedder_api(engine)->SendKeyEvent = MOCK_ENGINE_PROC(
51 SendKeyEvent, ([](auto engine, const FlutterKeyEvent* event,
52 FlutterKeyEventCallback callback, void* user_data) {
53 callback(false, user_data);
54 return kSuccess;
55 }));
56 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
57 g_autoptr(FlKeyEvent) event = fl_key_event_new(
58 0, TRUE, kKeyCodeKeyA, GDK_KEY_a, static_cast<GdkModifierType>(0), 0);
59 fl_keyboard_manager_handle_event(
60 manager, event, nullptr,
61 [](GObject* object, GAsyncResult* result, gpointer user_data) {
62 g_autoptr(FlKeyEvent) redispatched_event = nullptr;
63 EXPECT_TRUE(fl_keyboard_manager_handle_event_finish(
64 FL_KEYBOARD_MANAGER(object), result, &redispatched_event, nullptr));
65 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
66 },
67 loop);
68 g_main_loop_run(loop);
69
70 gboolean called = FALSE;
71 fl_mock_binary_messenger_invoke_standard_method(
72 messenger, kKeyboardChannelName, kGetKeyboardStateMethod, nullptr,
73 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
74 gpointer user_data) {
75 gboolean* called = static_cast<gboolean*>(user_data);
76 *called = TRUE;
77
78 EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
79
80 g_autoptr(FlValue) expected_result = fl_value_new_map();
81 fl_value_set_take(expected_result, fl_value_new_int(kPhysicalKeyA),
82 fl_value_new_int(kLogicalKeyA));
83 EXPECT_TRUE(fl_value_equal(fl_method_success_response_get_result(
84 FL_METHOD_SUCCESS_RESPONSE(response)),
85 expected_result));
86 },
87 &called);
88
89 EXPECT_TRUE(called);
90 }
g_autoptr
g_autoptr(GMutexLocker) locker
TRUE
return TRUE
Definition: fl_compositor_software.cc:68
fl_engine_get_embedder_api
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:864
fl_engine_start
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:726
fl_engine_new_with_binary_messenger
FlEngine * fl_engine_new_with_binary_messenger(FlBinaryMessenger *binary_messenger)
Definition: fl_engine.cc:701
fl_engine_private.h
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
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_keyboard_handler_new
FlKeyboardHandler * fl_keyboard_handler_new(FlBinaryMessenger *messenger, FlKeyboardManager *keyboard_manager)
Definition: fl_keyboard_handler.cc:63
fl_keyboard_handler.h
kKeyboardChannelName
static constexpr char kKeyboardChannelName[]
Definition: fl_keyboard_handler_test.cc:18
kKeyCodeKeyA
constexpr guint16 kKeyCodeKeyA
Definition: fl_keyboard_handler_test.cc:24
kGetKeyboardStateMethod
static constexpr char kGetKeyboardStateMethod[]
Definition: fl_keyboard_handler_test.cc:19
TEST
TEST(FlKeyboardHandlerTest, KeyboardChannelGetPressedState)
Definition: fl_keyboard_handler_test.cc:26
fl_keyboard_manager_new
FlKeyboardManager * fl_keyboard_manager_new(FlEngine *engine)
Definition: fl_keyboard_manager.cc:356
fl_keyboard_manager_handle_event
void fl_keyboard_manager_handle_event(FlKeyboardManager *self, FlKeyEvent *event, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_keyboard_manager.cc:374
fl_keyboard_manager_handle_event_finish
gboolean fl_keyboard_manager_handle_event_finish(FlKeyboardManager *self, GAsyncResult *result, FlKeyEvent **redispatched_event, GError **error)
Definition: fl_keyboard_manager.cc:410
fl_method_codec_private.h
fl_method_success_response_get_result
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
Definition: fl_method_response.cc:138
fl_standard_method_codec.h
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
fl_value_set_take
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:618
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42