| 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/linux/fl_binary_messenger_private.h"
8 #include "flutter/shell/platform/linux/fl_method_codec_private.h"
9 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
10 #include "flutter/shell/platform/linux/testing/fl_mock_binary_messenger.h"
11
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
14
15 static constexpr char kKeyboardChannelName[] = "flutter/keyboard";
16 static constexpr char kGetKeyboardStateMethod[] = "getKeyboardState";
17 static constexpr uint64_t kMockPhysicalKey = 42;
18 static constexpr uint64_t kMockLogicalKey = 42;
19
20 G_BEGIN_DECLS
21
22 G_DECLARE_FINAL_TYPE(FlMockKeyboardHandlerDelegate,
23 fl_mock_keyboard_handler_delegate,
24 FL,
25 MOCK_KEYBOARD_HANDLER_DELEGATE,
26 GObject);
27
28 G_END_DECLS
29
30 struct _FlMockKeyboardHandlerDelegate {
31 GObject parent_instance;
32 };
33
34 static void fl_mock_keyboard_handler_delegate_keyboard_view_delegate_iface_init(
35 FlKeyboardViewDelegateInterface* iface);
36
37 G_DEFINE_TYPE_WITH_CODE(
38 FlMockKeyboardHandlerDelegate,
39 fl_mock_keyboard_handler_delegate,
40 G_TYPE_OBJECT,
41 G_IMPLEMENT_INTERFACE(
42 fl_keyboard_view_delegate_get_type(),
43 fl_mock_keyboard_handler_delegate_keyboard_view_delegate_iface_init))
44
45 static void fl_mock_keyboard_handler_delegate_init(
46 FlMockKeyboardHandlerDelegate* self) {}
47
48 static void fl_mock_keyboard_handler_delegate_class_init(
49 FlMockKeyboardHandlerDelegateClass* klass) {}
50
51 static void fl_mock_keyboard_handler_delegate_keyboard_view_delegate_iface_init(
52 FlKeyboardViewDelegateInterface* iface) {}
53
54 static FlMockKeyboardHandlerDelegate* fl_mock_keyboard_handler_delegate_new() {
55 FlMockKeyboardHandlerDelegate* self = FL_MOCK_KEYBOARD_HANDLER_DELEGATE(
56 g_object_new(fl_mock_keyboard_handler_delegate_get_type(), nullptr));
57
58 // Added to stop compiler complaining about an unused function.
59 FL_IS_MOCK_KEYBOARD_HANDLER_DELEGATE(self);
60
61 return self;
62 }
63
64 TEST(FlKeyboardHandlerTest, KeyboardChannelGetPressedState) {
65 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
66 g_autoptr(FlEngine) engine =
67 FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
68 FL_BINARY_MESSENGER(messenger), nullptr));
69 g_autoptr(FlMockKeyboardHandlerDelegate) view_delegate =
70 fl_mock_keyboard_handler_delegate_new();
71 g_autoptr(FlKeyboardManager) manager =
72 fl_keyboard_manager_new(engine, FL_KEYBOARD_VIEW_DELEGATE(view_delegate));
73 fl_keyboard_manager_set_get_pressed_state_handler(
74 manager,
75 [](gpointer user_data) {
76 GHashTable* result = g_hash_table_new(g_direct_hash, g_direct_equal);
77 g_hash_table_insert(result,
78 reinterpret_cast<gpointer>(kMockPhysicalKey),
79 reinterpret_cast<gpointer>(kMockLogicalKey));
80
81 return result;
82 },
83 nullptr);
84 g_autoptr(FlKeyboardHandler) handler =
85 fl_keyboard_handler_new(FL_BINARY_MESSENGER(messenger), manager);
86 EXPECT_NE(handler, nullptr);
87
88 gboolean called = FALSE;
89 fl_mock_binary_messenger_invoke_standard_method(
90 messenger, kKeyboardChannelName, kGetKeyboardStateMethod, nullptr,
91 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
92 gpointer user_data) {
93 gboolean* called = static_cast<gboolean*>(user_data);
94 *called = TRUE;
95
96 EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
97
98 g_autoptr(FlValue) expected_result = fl_value_new_map();
99 fl_value_set_take(expected_result, fl_value_new_int(kMockPhysicalKey),
100 fl_value_new_int(kMockLogicalKey));
101 EXPECT_TRUE(fl_value_equal(fl_method_success_response_get_result(
102 FL_METHOD_SUCCESS_RESPONSE(response)),
103 expected_result));
104 },
105 &called);
106 EXPECT_TRUE(called);
107
108 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
109 }
fl_binary_messenger_shutdown
void fl_binary_messenger_shutdown(FlBinaryMessenger *self)
Definition: fl_binary_messenger.cc:500
kMockPhysicalKey
static constexpr uint64_t kMockPhysicalKey
Definition: fl_keyboard_handler_test.cc:17
fl_mock_keyboard_handler_delegate_class_init
static void fl_mock_keyboard_handler_delegate_class_init(FlMockKeyboardHandlerDelegateClass *klass)
Definition: fl_keyboard_handler_test.cc:48
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_value_set_take
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:618
_FlMockKeyboardHandlerDelegate::parent_instance
GObject parent_instance
Definition: fl_keyboard_handler_test.cc:31
G_DEFINE_TYPE_WITH_CODE
G_DEFINE_TYPE_WITH_CODE(FlMockKeyboardHandlerDelegate, fl_mock_keyboard_handler_delegate, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(fl_keyboard_view_delegate_get_type(), fl_mock_keyboard_handler_delegate_keyboard_view_delegate_iface_init)) static void fl_mock_keyboard_handler_delegate_init(FlMockKeyboardHandlerDelegate *self)
Definition: fl_keyboard_handler_test.cc:37
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
fl_keyboard_handler.h
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
_FlMockKeyboardHandlerDelegate
Definition: fl_keyboard_handler_test.cc:30
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
kKeyboardChannelName
static constexpr char kKeyboardChannelName[]
Definition: fl_keyboard_handler_test.cc:15
fl_keyboard_handler_new
FlKeyboardHandler * fl_keyboard_handler_new(FlBinaryMessenger *messenger, FlKeyboardManager *keyboard_manager)
Definition: fl_keyboard_handler.cc:62
fl_method_success_response_get_result
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
Definition: fl_method_response.cc:138
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
FL
FL
Definition: fl_binary_messenger.cc:27
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
fl_standard_method_codec.h
kGetKeyboardStateMethod
static constexpr char kGetKeyboardStateMethod[]
Definition: fl_keyboard_handler_test.cc:16
fl_keyboard_manager_set_get_pressed_state_handler
void fl_keyboard_manager_set_get_pressed_state_handler(FlKeyboardManager *self, FlKeyboardManagerGetPressedStateHandler get_pressed_state_handler, gpointer user_data)
Definition: fl_keyboard_manager.cc:604
fl_binary_messenger_private.h
fl_mock_keyboard_handler_delegate_new
static FlMockKeyboardHandlerDelegate * fl_mock_keyboard_handler_delegate_new()
Definition: fl_keyboard_handler_test.cc:54
TEST
TEST(FlKeyboardHandlerTest, KeyboardChannelGetPressedState)
Definition: fl_keyboard_handler_test.cc:64
kMockLogicalKey
static constexpr uint64_t kMockLogicalKey
Definition: fl_keyboard_handler_test.cc:18
fl_method_codec_private.h
fl_keyboard_manager_new
FlKeyboardManager * fl_keyboard_manager_new(FlEngine *engine, FlKeyboardViewDelegate *view_delegate)
Definition: fl_keyboard_manager.cc:462
G_DECLARE_FINAL_TYPE
G_BEGIN_DECLS G_DECLARE_FINAL_TYPE(FlMockKeyboardHandlerDelegate, fl_mock_keyboard_handler_delegate, FL, MOCK_KEYBOARD_HANDLER_DELEGATE, GObject)
fl_mock_keyboard_handler_delegate_keyboard_view_delegate_iface_init
static void fl_mock_keyboard_handler_delegate_keyboard_view_delegate_iface_init(FlKeyboardViewDelegateInterface *iface)
Definition: fl_keyboard_handler_test.cc:51