88#include < vector>
99
1010#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"
1113#include " flutter/shell/platform/linux/key_mapping.h"
1214#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"
1319#include " flutter/shell/platform/linux/testing/mock_text_input_plugin.h"
20+ #include " flutter/testing/testing.h"
21+
22+ #include " gmock/gmock.h"
1423#include " gtest/gtest.h"
1524
1625// Define compound `expect` in macros. If they were defined in functions, the
@@ -101,6 +110,10 @@ constexpr guint16 kKeyCodeSemicolon = 0x2fu;
101110constexpr guint16 kKeyCodeKeyLeftBracket = 0x22u ;
102111
103112static 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 ;
104117
105118// All key clues for a keyboard layout.
106119//
@@ -130,6 +143,19 @@ G_DECLARE_FINAL_TYPE(FlMockKeyBinaryMessenger,
130143
131144G_END_DECLS
132145
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+
133159/* **** FlMockKeyBinaryMessenger *****/
134160/* Mock a binary messenger that only processes messages from the embedding on
135161 * 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,
323349 return (*group_layout)[key->keycode * 2 + shift];
324350}
325351
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+
326361static void fl_mock_view_keyboard_delegate_iface_init (
327362 FlKeyboardViewDelegateInterface* iface) {
328363 iface->send_key_event = fl_mock_view_keyboard_send_key_event;
@@ -332,6 +367,7 @@ static void fl_mock_view_keyboard_delegate_iface_init(
332367 iface->subscribe_to_layout_change =
333368 fl_mock_view_keyboard_subscribe_to_layout_change;
334369 iface->lookup_key = fl_mock_view_keyboard_lookup_key;
370+ iface->get_keyboard_state = fl_mock_view_keyboard_get_keyboard_state;
335371}
336372
337373static FlMockViewDelegate* fl_mock_view_delegate_new () {
@@ -407,13 +443,16 @@ static FlKeyEvent* fl_key_event_new_by_mock(bool is_press,
407443class KeyboardTester {
408444 public:
409445 KeyboardTester () {
446+ ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
447+
410448 view_ = fl_mock_view_delegate_new ();
411449 respondToEmbedderCallsWith (false );
412450 respondToChannelCallsWith (false );
413451 respondToTextInputWith (false );
414452 setLayout (kLayoutUs );
415453
416- manager_ = fl_keyboard_manager_new (FL_KEYBOARD_VIEW_DELEGATE (view_));
454+ manager_ =
455+ fl_keyboard_manager_new (messenger, FL_KEYBOARD_VIEW_DELEGATE (view_));
417456 }
418457
419458 ~KeyboardTester () {
@@ -945,6 +984,29 @@ TEST(FlKeyboardManagerTest, GetPressedState) {
945984 EXPECT_EQ (gpointer_to_uint64 (physical_key), kLogicalKeyA );
946985}
947986
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+
9481010// The following layout data is generated using DEBUG_PRINT_LAYOUT.
9491011
9501012const MockGroupLayoutData kLayoutUs0 {{
0 commit comments