8
8
#include < vector>
9
9
10
10
#include " flutter/shell/platform/embedder/test_utils/key_codes.g.h"
11
+ #include " flutter/shell/platform/linux/fl_binary_messenger_private.h"
12
+ #include " flutter/shell/platform/linux/fl_method_codec_private.h"
11
13
#include " flutter/shell/platform/linux/key_mapping.h"
12
14
#include " flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
15
+ #include " flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h"
16
+ #include " flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
17
+ #include " flutter/shell/platform/linux/testing/fl_test.h"
18
+ #include " flutter/shell/platform/linux/testing/mock_binary_messenger.h"
13
19
#include " flutter/shell/platform/linux/testing/mock_text_input_plugin.h"
20
+ #include " flutter/testing/testing.h"
21
+
22
+ #include " gmock/gmock.h"
14
23
#include " gtest/gtest.h"
15
24
16
25
// Define compound `expect` in macros. If they were defined in functions, the
@@ -101,6 +110,10 @@ constexpr guint16 kKeyCodeSemicolon = 0x2fu;
101
110
constexpr guint16 kKeyCodeKeyLeftBracket = 0x22u ;
102
111
103
112
static constexpr char kKeyEventChannelName [] = " flutter/keyevent" ;
113
+ static constexpr char kKeyboardChannelName [] = " flutter/keyboard" ;
114
+ static constexpr char kGetKeyboardStateMethod [] = " getKeyboardState" ;
115
+ static constexpr uint64_t kMockPhysicalKey = 42 ;
116
+ static constexpr uint64_t kMockLogicalKey = 42 ;
104
117
105
118
// All key clues for a keyboard layout.
106
119
//
@@ -130,6 +143,19 @@ G_DECLARE_FINAL_TYPE(FlMockKeyBinaryMessenger,
130
143
131
144
G_END_DECLS
132
145
146
+ MATCHER_P (MethodSuccessResponse, result, " " ) {
147
+ g_autoptr (FlStandardMethodCodec) codec = fl_standard_method_codec_new ();
148
+ g_autoptr (FlMethodResponse) response =
149
+ fl_method_codec_decode_response (FL_METHOD_CODEC (codec), arg, nullptr );
150
+ fl_method_response_get_result (response, nullptr );
151
+ if (fl_value_equal (fl_method_response_get_result (response, nullptr ),
152
+ result)) {
153
+ return true ;
154
+ }
155
+ *result_listener << ::testing::PrintToString (response);
156
+ return false ;
157
+ }
158
+
133
159
/* **** FlMockKeyBinaryMessenger *****/
134
160
/* Mock a binary messenger that only processes messages from the embedding on
135
161
* the key event channel, and does so according to the callback set by
@@ -323,6 +349,15 @@ static guint fl_mock_view_keyboard_lookup_key(FlKeyboardViewDelegate* delegate,
323
349
return (*group_layout)[key->keycode * 2 + shift];
324
350
}
325
351
352
+ static GHashTable* fl_mock_view_keyboard_get_keyboard_state (
353
+ FlKeyboardViewDelegate* view_delegate) {
354
+ GHashTable* result = g_hash_table_new (g_direct_hash, g_direct_equal);
355
+ g_hash_table_insert (result, reinterpret_cast <gpointer>(kMockPhysicalKey ),
356
+ reinterpret_cast <gpointer>(kMockLogicalKey ));
357
+
358
+ return result;
359
+ }
360
+
326
361
static void fl_mock_view_keyboard_delegate_iface_init (
327
362
FlKeyboardViewDelegateInterface* iface) {
328
363
iface->send_key_event = fl_mock_view_keyboard_send_key_event;
@@ -332,6 +367,7 @@ static void fl_mock_view_keyboard_delegate_iface_init(
332
367
iface->subscribe_to_layout_change =
333
368
fl_mock_view_keyboard_subscribe_to_layout_change;
334
369
iface->lookup_key = fl_mock_view_keyboard_lookup_key;
370
+ iface->get_keyboard_state = fl_mock_view_keyboard_get_keyboard_state;
335
371
}
336
372
337
373
static FlMockViewDelegate* fl_mock_view_delegate_new () {
@@ -407,13 +443,16 @@ static FlKeyEvent* fl_key_event_new_by_mock(bool is_press,
407
443
class KeyboardTester {
408
444
public:
409
445
KeyboardTester () {
446
+ ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
447
+
410
448
view_ = fl_mock_view_delegate_new ();
411
449
respondToEmbedderCallsWith (false );
412
450
respondToChannelCallsWith (false );
413
451
respondToTextInputWith (false );
414
452
setLayout (kLayoutUs );
415
453
416
- manager_ = fl_keyboard_manager_new (FL_KEYBOARD_VIEW_DELEGATE (view_));
454
+ manager_ =
455
+ fl_keyboard_manager_new (messenger, FL_KEYBOARD_VIEW_DELEGATE (view_));
417
456
}
418
457
419
458
~KeyboardTester () {
@@ -945,6 +984,29 @@ TEST(FlKeyboardManagerTest, GetPressedState) {
945
984
EXPECT_EQ (gpointer_to_uint64 (physical_key), kLogicalKeyA );
946
985
}
947
986
987
+ TEST (FlKeyboardPluginTest, KeyboardChannelGetPressedState) {
988
+ ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
989
+
990
+ g_autoptr (FlKeyboardManager) manager = fl_keyboard_manager_new (
991
+ messenger, FL_KEYBOARD_VIEW_DELEGATE (fl_mock_view_delegate_new ()));
992
+ EXPECT_NE (manager, nullptr );
993
+
994
+ g_autoptr (FlStandardMethodCodec) codec = fl_standard_method_codec_new ();
995
+ g_autoptr (GBytes) message = fl_method_codec_encode_method_call (
996
+ FL_METHOD_CODEC (codec), kGetKeyboardStateMethod , nullptr , nullptr );
997
+
998
+ g_autoptr (FlValue) response = fl_value_new_map ();
999
+ fl_value_set_take (response, fl_value_new_int (kMockPhysicalKey ),
1000
+ fl_value_new_int (kMockLogicalKey ));
1001
+ EXPECT_CALL (messenger,
1002
+ fl_binary_messenger_send_response (
1003
+ ::testing::Eq<FlBinaryMessenger*>(messenger), ::testing::_,
1004
+ MethodSuccessResponse (response), ::testing::_))
1005
+ .WillOnce (::testing::Return (true ));
1006
+
1007
+ messenger.ReceiveMessage (kKeyboardChannelName , message);
1008
+ }
1009
+
948
1010
// The following layout data is generated using DEBUG_PRINT_LAYOUT.
949
1011
950
1012
const MockGroupLayoutData kLayoutUs0 {{
0 commit comments