forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fl_keyboard_manager_test.cc
1261 lines (1071 loc) · 51.7 KB
/
fl_keyboard_manager_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/linux/fl_keyboard_manager.h"
#include <cstring>
#include <vector>
#include "flutter/shell/platform/embedder/test_utils/key_codes.g.h"
#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
#include "flutter/shell/platform/linux/fl_method_codec_private.h"
#include "flutter/shell/platform/linux/key_mapping.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_binary_messenger.h"
#include "flutter/shell/platform/linux/testing/mock_text_input_plugin.h"
#include "flutter/testing/testing.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
// Define compound `expect` in macros. If they were defined in functions, the
// stacktrace wouldn't print where the function is called in the unit tests.
#define EXPECT_KEY_EVENT(RECORD, TYPE, PHYSICAL, LOGICAL, CHAR, SYNTHESIZED) \
EXPECT_EQ((RECORD).type, CallRecord::kKeyCallEmbedder); \
EXPECT_EQ((RECORD).event->type, (TYPE)); \
EXPECT_EQ((RECORD).event->physical, (PHYSICAL)); \
EXPECT_EQ((RECORD).event->logical, (LOGICAL)); \
EXPECT_STREQ((RECORD).event->character, (CHAR)); \
EXPECT_EQ((RECORD).event->synthesized, (SYNTHESIZED));
#define VERIFY_DOWN(OUT_LOGICAL, OUT_CHAR) \
EXPECT_EQ(call_records[0].type, CallRecord::kKeyCallEmbedder); \
EXPECT_EQ(call_records[0].event->type, kFlutterKeyEventTypeDown); \
EXPECT_EQ(call_records[0].event->logical, (OUT_LOGICAL)); \
EXPECT_STREQ(call_records[0].event->character, (OUT_CHAR)); \
EXPECT_EQ(call_records[0].event->synthesized, false); \
call_records.clear()
namespace {
using ::flutter::testing::keycodes::kLogicalAltLeft;
using ::flutter::testing::keycodes::kLogicalBracketLeft;
using ::flutter::testing::keycodes::kLogicalComma;
using ::flutter::testing::keycodes::kLogicalControlLeft;
using ::flutter::testing::keycodes::kLogicalDigit1;
using ::flutter::testing::keycodes::kLogicalKeyA;
using ::flutter::testing::keycodes::kLogicalKeyB;
using ::flutter::testing::keycodes::kLogicalKeyM;
using ::flutter::testing::keycodes::kLogicalKeyQ;
using ::flutter::testing::keycodes::kLogicalMetaLeft;
using ::flutter::testing::keycodes::kLogicalMinus;
using ::flutter::testing::keycodes::kLogicalParenthesisRight;
using ::flutter::testing::keycodes::kLogicalSemicolon;
using ::flutter::testing::keycodes::kLogicalShiftLeft;
using ::flutter::testing::keycodes::kLogicalUnderscore;
using ::flutter::testing::keycodes::kPhysicalAltLeft;
using ::flutter::testing::keycodes::kPhysicalControlLeft;
using ::flutter::testing::keycodes::kPhysicalKeyA;
using ::flutter::testing::keycodes::kPhysicalKeyB;
using ::flutter::testing::keycodes::kPhysicalMetaLeft;
using ::flutter::testing::keycodes::kPhysicalShiftLeft;
// Hardware key codes.
typedef std::function<void(bool handled)> AsyncKeyCallback;
typedef std::function<void(AsyncKeyCallback callback)> ChannelCallHandler;
typedef std::function<void(const FlutterKeyEvent* event,
AsyncKeyCallback callback)>
EmbedderCallHandler;
typedef std::function<void(std::unique_ptr<FlKeyEvent>)> RedispatchHandler;
// A type that can record all kinds of effects that the keyboard manager
// triggers.
//
// An instance of `CallRecord` might not have all the fields filled.
typedef struct {
enum {
kKeyCallEmbedder,
kKeyCallChannel,
} type;
AsyncKeyCallback callback;
std::unique_ptr<FlutterKeyEvent> event;
std::unique_ptr<char[]> event_character;
} CallRecord;
// Clone a C-string.
//
// Must be deleted by delete[].
char* cloneString(const char* source) {
if (source == nullptr) {
return nullptr;
}
size_t charLen = strlen(source);
char* target = new char[charLen + 1];
strncpy(target, source, charLen + 1);
return target;
}
constexpr guint16 kKeyCodeKeyA = 0x26u;
constexpr guint16 kKeyCodeKeyB = 0x38u;
constexpr guint16 kKeyCodeKeyM = 0x3au;
constexpr guint16 kKeyCodeDigit1 = 0x0au;
constexpr guint16 kKeyCodeMinus = 0x14u;
constexpr guint16 kKeyCodeSemicolon = 0x2fu;
constexpr guint16 kKeyCodeKeyLeftBracket = 0x22u;
static constexpr char kKeyEventChannelName[] = "flutter/keyevent";
static constexpr char kKeyboardChannelName[] = "flutter/keyboard";
static constexpr char kGetKeyboardStateMethod[] = "getKeyboardState";
static constexpr uint64_t kMockPhysicalKey = 42;
static constexpr uint64_t kMockLogicalKey = 42;
// All key clues for a keyboard layout.
//
// The index is (keyCode * 2 + hasShift), where each value is the character for
// this key (GTK only supports UTF-16.) Since the maximum keycode of interest
// is 128, it has a total of 256 entries..
typedef std::array<uint32_t, 256> MockGroupLayoutData;
typedef std::vector<const MockGroupLayoutData*> MockLayoutData;
extern const MockLayoutData kLayoutUs;
extern const MockLayoutData kLayoutRussian;
extern const MockLayoutData kLayoutFrench;
G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE(FlMockViewDelegate,
fl_mock_view_delegate,
FL,
MOCK_VIEW_DELEGATE,
GObject);
G_DECLARE_FINAL_TYPE(FlMockKeyBinaryMessenger,
fl_mock_key_binary_messenger,
FL,
MOCK_KEY_BINARY_MESSENGER,
GObject)
G_END_DECLS
MATCHER_P(MethodSuccessResponse, result, "") {
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
g_autoptr(FlMethodResponse) response =
fl_method_codec_decode_response(FL_METHOD_CODEC(codec), arg, nullptr);
fl_method_response_get_result(response, nullptr);
if (fl_value_equal(fl_method_response_get_result(response, nullptr),
result)) {
return true;
}
*result_listener << ::testing::PrintToString(response);
return false;
}
/***** FlMockKeyBinaryMessenger *****/
/* Mock a binary messenger that only processes messages from the embedding on
* the key event channel, and does so according to the callback set by
* fl_mock_key_binary_messenger_set_callback_handler */
struct _FlMockKeyBinaryMessenger {
GObject parent_instance;
};
struct FlMockKeyBinaryMessengerPrivate {
ChannelCallHandler callback_handler;
};
static void fl_mock_key_binary_messenger_iface_init(
FlBinaryMessengerInterface* iface);
G_DEFINE_TYPE_WITH_CODE(
FlMockKeyBinaryMessenger,
fl_mock_key_binary_messenger,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(fl_binary_messenger_get_type(),
fl_mock_key_binary_messenger_iface_init);
G_ADD_PRIVATE(FlMockKeyBinaryMessenger))
#define FL_MOCK_KEY_BINARY_MESSENGER_GET_PRIVATE(obj) \
static_cast<FlMockKeyBinaryMessengerPrivate*>( \
fl_mock_key_binary_messenger_get_instance_private( \
FL_MOCK_KEY_BINARY_MESSENGER(obj)))
static void fl_mock_key_binary_messenger_init(FlMockKeyBinaryMessenger* self) {
FlMockKeyBinaryMessengerPrivate* priv =
FL_MOCK_KEY_BINARY_MESSENGER_GET_PRIVATE(self);
new (priv) FlMockKeyBinaryMessengerPrivate();
}
static void fl_mock_key_binary_messenger_finalize(GObject* object) {
FL_MOCK_KEY_BINARY_MESSENGER_GET_PRIVATE(object)
->~FlMockKeyBinaryMessengerPrivate();
}
static void fl_mock_key_binary_messenger_class_init(
FlMockKeyBinaryMessengerClass* klass) {
G_OBJECT_CLASS(klass)->finalize = fl_mock_key_binary_messenger_finalize;
}
static void fl_mock_key_binary_messenger_send_on_channel(
FlBinaryMessenger* messenger,
const gchar* channel,
GBytes* message,
GCancellable* cancellable,
GAsyncReadyCallback callback,
gpointer user_data) {
FlMockKeyBinaryMessenger* self = FL_MOCK_KEY_BINARY_MESSENGER(messenger);
if (callback != nullptr) {
EXPECT_STREQ(channel, kKeyEventChannelName);
FL_MOCK_KEY_BINARY_MESSENGER_GET_PRIVATE(self)->callback_handler(
[self, cancellable, callback, user_data](bool handled) {
g_autoptr(GTask) task =
g_task_new(self, cancellable, callback, user_data);
g_autoptr(FlValue) result = fl_value_new_map();
fl_value_set_string_take(result, "handled",
fl_value_new_bool(handled));
g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
g_autoptr(GError) error = nullptr;
GBytes* data = fl_message_codec_encode_message(
FL_MESSAGE_CODEC(codec), result, &error);
g_task_return_pointer(
task, data, reinterpret_cast<GDestroyNotify>(g_bytes_unref));
});
}
}
static GBytes* fl_mock_key_binary_messenger_send_on_channel_finish(
FlBinaryMessenger* messenger,
GAsyncResult* result,
GError** error) {
return static_cast<GBytes*>(g_task_propagate_pointer(G_TASK(result), error));
}
static void fl_mock_binary_messenger_resize_channel(
FlBinaryMessenger* messenger,
const gchar* channel,
int64_t new_size) {
// Mock implementation. Do nothing.
}
static void fl_mock_binary_messenger_set_warns_on_channel_overflow(
FlBinaryMessenger* messenger,
const gchar* channel,
bool warns) {
// Mock implementation. Do nothing.
}
static void fl_mock_key_binary_messenger_iface_init(
FlBinaryMessengerInterface* iface) {
iface->set_message_handler_on_channel =
[](FlBinaryMessenger* messenger, const gchar* channel,
FlBinaryMessengerMessageHandler handler, gpointer user_data,
GDestroyNotify destroy_notify) {
EXPECT_STREQ(channel, kKeyEventChannelName);
// No need to mock. The key event channel expects no incoming messages
// from the framework.
};
iface->send_response = [](FlBinaryMessenger* messenger,
FlBinaryMessengerResponseHandle* response_handle,
GBytes* response, GError** error) -> gboolean {
// The key event channel expects no incoming messages from the framework,
// hence no responses either.
g_return_val_if_reached(TRUE);
return TRUE;
};
iface->send_on_channel = fl_mock_key_binary_messenger_send_on_channel;
iface->send_on_channel_finish =
fl_mock_key_binary_messenger_send_on_channel_finish;
iface->resize_channel = fl_mock_binary_messenger_resize_channel;
iface->set_warns_on_channel_overflow =
fl_mock_binary_messenger_set_warns_on_channel_overflow;
}
static FlMockKeyBinaryMessenger* fl_mock_key_binary_messenger_new() {
FlMockKeyBinaryMessenger* self = FL_MOCK_KEY_BINARY_MESSENGER(
g_object_new(fl_mock_key_binary_messenger_get_type(), NULL));
// Added to stop compiler complaining about an unused function.
FL_IS_MOCK_KEY_BINARY_MESSENGER(self);
return self;
}
static void fl_mock_key_binary_messenger_set_callback_handler(
FlMockKeyBinaryMessenger* self,
ChannelCallHandler handler) {
FL_MOCK_KEY_BINARY_MESSENGER_GET_PRIVATE(self)->callback_handler =
std::move(handler);
}
/***** FlMockViewDelegate *****/
struct _FlMockViewDelegate {
GObject parent_instance;
};
struct FlMockViewDelegatePrivate {
FlMockKeyBinaryMessenger* messenger;
EmbedderCallHandler embedder_handler;
bool text_filter_result;
RedispatchHandler redispatch_handler;
KeyboardLayoutNotifier layout_notifier;
const MockLayoutData* layout_data;
};
static void fl_mock_view_keyboard_delegate_iface_init(
FlKeyboardViewDelegateInterface* iface);
G_DEFINE_TYPE_WITH_CODE(
FlMockViewDelegate,
fl_mock_view_delegate,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(fl_keyboard_view_delegate_get_type(),
fl_mock_view_keyboard_delegate_iface_init);
G_ADD_PRIVATE(FlMockViewDelegate))
#define FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(obj) \
static_cast<FlMockViewDelegatePrivate*>( \
fl_mock_view_delegate_get_instance_private(FL_MOCK_VIEW_DELEGATE(obj)))
static void fl_mock_view_delegate_init(FlMockViewDelegate* self) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(self);
new (priv) FlMockViewDelegatePrivate();
}
static void fl_mock_view_delegate_finalize(GObject* object) {
FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(object)->~FlMockViewDelegatePrivate();
}
static void fl_mock_view_delegate_dispose(GObject* object) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(object);
g_clear_object(&priv->messenger);
G_OBJECT_CLASS(fl_mock_view_delegate_parent_class)->dispose(object);
}
static void fl_mock_view_delegate_class_init(FlMockViewDelegateClass* klass) {
G_OBJECT_CLASS(klass)->dispose = fl_mock_view_delegate_dispose;
G_OBJECT_CLASS(klass)->finalize = fl_mock_view_delegate_finalize;
}
static void fl_mock_view_keyboard_send_key_event(
FlKeyboardViewDelegate* view_delegate,
const FlutterKeyEvent* event,
FlutterKeyEventCallback callback,
void* user_data) {
FlMockViewDelegatePrivate* priv =
FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(view_delegate);
priv->embedder_handler(event, [callback, user_data](bool handled) {
if (callback != nullptr) {
callback(handled, user_data);
}
});
}
static gboolean fl_mock_view_keyboard_text_filter_key_press(
FlKeyboardViewDelegate* view_delegate,
FlKeyEvent* event) {
FlMockViewDelegatePrivate* priv =
FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(view_delegate);
return priv->text_filter_result;
}
static FlBinaryMessenger* fl_mock_view_keyboard_get_messenger(
FlKeyboardViewDelegate* view_delegate) {
FlMockViewDelegatePrivate* priv =
FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(view_delegate);
return FL_BINARY_MESSENGER(priv->messenger);
}
static void fl_mock_view_keyboard_redispatch_event(
FlKeyboardViewDelegate* view_delegate,
std::unique_ptr<FlKeyEvent> event) {
FlMockViewDelegatePrivate* priv =
FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(view_delegate);
if (priv->redispatch_handler) {
priv->redispatch_handler(std::move(event));
}
}
static void fl_mock_view_keyboard_subscribe_to_layout_change(
FlKeyboardViewDelegate* delegate,
KeyboardLayoutNotifier notifier) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(delegate);
priv->layout_notifier = std::move(notifier);
}
static guint fl_mock_view_keyboard_lookup_key(FlKeyboardViewDelegate* delegate,
const GdkKeymapKey* key) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(delegate);
guint8 group = static_cast<guint8>(key->group);
EXPECT_LT(group, priv->layout_data->size());
const MockGroupLayoutData* group_layout = (*priv->layout_data)[group];
EXPECT_TRUE(group_layout != nullptr);
EXPECT_TRUE(key->level == 0 || key->level == 1);
bool shift = key->level == 1;
return (*group_layout)[key->keycode * 2 + shift];
}
static GHashTable* fl_mock_view_keyboard_get_keyboard_state(
FlKeyboardViewDelegate* view_delegate) {
GHashTable* result = g_hash_table_new(g_direct_hash, g_direct_equal);
g_hash_table_insert(result, reinterpret_cast<gpointer>(kMockPhysicalKey),
reinterpret_cast<gpointer>(kMockLogicalKey));
return result;
}
static void fl_mock_view_keyboard_delegate_iface_init(
FlKeyboardViewDelegateInterface* iface) {
iface->send_key_event = fl_mock_view_keyboard_send_key_event;
iface->text_filter_key_press = fl_mock_view_keyboard_text_filter_key_press;
iface->get_messenger = fl_mock_view_keyboard_get_messenger;
iface->redispatch_event = fl_mock_view_keyboard_redispatch_event;
iface->subscribe_to_layout_change =
fl_mock_view_keyboard_subscribe_to_layout_change;
iface->lookup_key = fl_mock_view_keyboard_lookup_key;
iface->get_keyboard_state = fl_mock_view_keyboard_get_keyboard_state;
}
static FlMockViewDelegate* fl_mock_view_delegate_new() {
FlMockViewDelegate* self = FL_MOCK_VIEW_DELEGATE(
g_object_new(fl_mock_view_delegate_get_type(), nullptr));
// Added to stop compiler complaining about an unused function.
FL_IS_MOCK_VIEW_DELEGATE(self);
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(self);
priv->messenger = fl_mock_key_binary_messenger_new();
return self;
}
static void fl_mock_view_set_embedder_handler(FlMockViewDelegate* self,
EmbedderCallHandler handler) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(self);
priv->embedder_handler = std::move(handler);
}
static void fl_mock_view_set_text_filter_result(FlMockViewDelegate* self,
bool result) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(self);
priv->text_filter_result = result;
}
static void fl_mock_view_set_redispatch_handler(FlMockViewDelegate* self,
RedispatchHandler handler) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(self);
priv->redispatch_handler = std::move(handler);
}
static void fl_mock_view_set_layout(FlMockViewDelegate* self,
const MockLayoutData* layout) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(self);
priv->layout_data = layout;
if (priv->layout_notifier != nullptr) {
priv->layout_notifier();
}
}
/***** End FlMockViewDelegate *****/
// Create a new #FlKeyEvent with the given information.
static FlKeyEvent* fl_key_event_new_by_mock(bool is_press,
guint keyval,
guint16 keycode,
GdkModifierType state,
gboolean is_modifier,
guint8 group = 0) {
FlKeyEvent* event = g_new0(FlKeyEvent, 1);
event->is_press = is_press;
event->time = 0;
event->state = state;
event->keyval = keyval;
event->group = group;
event->keycode = keycode;
return event;
}
class KeyboardTester {
public:
KeyboardTester() {
::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
view_ = fl_mock_view_delegate_new();
respondToEmbedderCallsWith(false);
respondToChannelCallsWith(false);
respondToTextInputWith(false);
setLayout(kLayoutUs);
manager_ =
fl_keyboard_manager_new(messenger, FL_KEYBOARD_VIEW_DELEGATE(view_));
}
~KeyboardTester() {
g_clear_object(&view_);
g_clear_object(&manager_);
}
FlKeyboardManager* manager() { return manager_; }
// Block until all GdkMainLoop messages are processed, which is basically
// used only for channel messages.
void flushChannelMessages() {
GMainLoop* loop = g_main_loop_new(nullptr, 0);
g_idle_add(_flushChannelMessagesCb, loop);
g_main_loop_run(loop);
}
// Dispatch each of the given events, expect their results to be false
// (unhandled), and clear the event array.
//
// Returns the number of events redispatched. If any result is unexpected
// (handled), return a minus number `-x` instead, where `x` is the index of
// the first unexpected redispatch.
int redispatchEventsAndClear(
std::vector<std::unique_ptr<FlKeyEvent>>& events) {
size_t event_count = events.size();
int first_error = -1;
during_redispatch_ = true;
for (size_t event_id = 0; event_id < event_count; event_id += 1) {
bool handled = fl_keyboard_manager_handle_event(
manager_, events[event_id].release());
EXPECT_FALSE(handled);
if (handled) {
first_error = first_error == -1 ? event_id : first_error;
}
}
during_redispatch_ = false;
events.clear();
return first_error < 0 ? event_count : -first_error;
}
void respondToEmbedderCallsWith(bool response) {
fl_mock_view_set_embedder_handler(
view_, [response, this](const FlutterKeyEvent* event,
const AsyncKeyCallback& callback) {
EXPECT_FALSE(during_redispatch_);
callback(response);
});
}
void recordEmbedderCallsTo(std::vector<CallRecord>& storage) {
fl_mock_view_set_embedder_handler(
view_, [&storage, this](const FlutterKeyEvent* event,
AsyncKeyCallback callback) {
EXPECT_FALSE(during_redispatch_);
auto new_event = std::make_unique<FlutterKeyEvent>(*event);
char* new_event_character = cloneString(event->character);
new_event->character = new_event_character;
storage.push_back(CallRecord{
.type = CallRecord::kKeyCallEmbedder,
.callback = std::move(callback),
.event = std::move(new_event),
.event_character = std::unique_ptr<char[]>(new_event_character),
});
});
}
void respondToEmbedderCallsWithAndRecordsTo(
bool response,
std::vector<CallRecord>& storage) {
fl_mock_view_set_embedder_handler(
view_, [&storage, response, this](const FlutterKeyEvent* event,
const AsyncKeyCallback& callback) {
EXPECT_FALSE(during_redispatch_);
auto new_event = std::make_unique<FlutterKeyEvent>(*event);
char* new_event_character = cloneString(event->character);
new_event->character = new_event_character;
storage.push_back(CallRecord{
.type = CallRecord::kKeyCallEmbedder,
.event = std::move(new_event),
.event_character = std::unique_ptr<char[]>(new_event_character),
});
callback(response);
});
}
void respondToChannelCallsWith(bool response) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(view_);
fl_mock_key_binary_messenger_set_callback_handler(
priv->messenger, [response, this](const AsyncKeyCallback& callback) {
EXPECT_FALSE(during_redispatch_);
callback(response);
});
}
void recordChannelCallsTo(std::vector<CallRecord>& storage) {
FlMockViewDelegatePrivate* priv = FL_MOCK_VIEW_DELEGATE_GET_PRIVATE(view_);
fl_mock_key_binary_messenger_set_callback_handler(
priv->messenger, [&storage, this](AsyncKeyCallback callback) {
EXPECT_FALSE(during_redispatch_);
storage.push_back(CallRecord{
.type = CallRecord::kKeyCallChannel,
.callback = std::move(callback),
});
});
}
void respondToTextInputWith(bool response) {
fl_mock_view_set_text_filter_result(view_, response);
}
void recordRedispatchedEventsTo(
std::vector<std::unique_ptr<FlKeyEvent>>& storage) {
fl_mock_view_set_redispatch_handler(
view_, [&storage](std::unique_ptr<FlKeyEvent> key) {
storage.push_back(std::move(key));
});
}
void setLayout(const MockLayoutData& layout) {
fl_mock_view_set_layout(view_, &layout);
}
private:
FlMockViewDelegate* view_;
FlKeyboardManager* manager_;
bool during_redispatch_ = false;
static gboolean _flushChannelMessagesCb(gpointer data) {
g_autoptr(GMainLoop) loop = reinterpret_cast<GMainLoop*>(data);
g_main_loop_quit(loop);
return FALSE;
}
};
// Make sure that the keyboard can be disposed without crashes when there are
// unresolved pending events.
TEST(FlKeyboardManagerTest, DisposeWithUnresolvedPends) {
KeyboardTester tester;
std::vector<CallRecord> call_records;
// Record calls so that they aren't responded.
tester.recordEmbedderCallsTo(call_records);
fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(true, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.respondToEmbedderCallsWith(true);
fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(false, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
// Passes if the cleanup does not crash.
}
TEST(FlKeyboardManagerTest, SingleDelegateWithAsyncResponds) {
KeyboardTester tester;
std::vector<CallRecord> call_records;
std::vector<std::unique_ptr<FlKeyEvent>> redispatched;
gboolean manager_handled = false;
/// Test 1: One event that is handled by the framework
tester.recordEmbedderCallsTo(call_records);
tester.recordRedispatchedEventsTo(redispatched);
// Dispatch a key event
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(true, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
EXPECT_EQ(manager_handled, true);
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_EQ(call_records.size(), 1u);
EXPECT_KEY_EVENT(call_records[0], kFlutterKeyEventTypeDown, kPhysicalKeyA,
kLogicalKeyA, "a", false);
call_records[0].callback(true);
tester.flushChannelMessages();
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
call_records.clear();
/// Test 2: Two events that are unhandled by the framework
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(false, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
EXPECT_EQ(manager_handled, true);
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_EQ(call_records.size(), 1u);
EXPECT_KEY_EVENT(call_records[0], kFlutterKeyEventTypeUp, kPhysicalKeyA,
kLogicalKeyA, nullptr, false);
// Dispatch another key event
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(true, GDK_KEY_b, kKeyCodeKeyB,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
EXPECT_EQ(manager_handled, true);
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_EQ(call_records.size(), 2u);
EXPECT_KEY_EVENT(call_records[1], kFlutterKeyEventTypeDown, kPhysicalKeyB,
kLogicalKeyB, "b", false);
// Resolve the second event first to test out-of-order response
call_records[1].callback(false);
EXPECT_EQ(redispatched.size(), 1u);
EXPECT_EQ(redispatched[0]->keyval, 0x62u);
call_records[0].callback(false);
tester.flushChannelMessages();
EXPECT_EQ(redispatched.size(), 2u);
EXPECT_EQ(redispatched[1]->keyval, 0x61u);
EXPECT_FALSE(fl_keyboard_manager_is_state_clear(tester.manager()));
call_records.clear();
// Resolve redispatches
EXPECT_EQ(tester.redispatchEventsAndClear(redispatched), 2);
tester.flushChannelMessages();
EXPECT_EQ(call_records.size(), 0u);
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
/// Test 3: Dispatch the same event again to ensure that prevention from
/// redispatching only works once.
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(false, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
EXPECT_EQ(manager_handled, true);
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_EQ(call_records.size(), 1u);
call_records[0].callback(true);
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
}
TEST(FlKeyboardManagerTest, SingleDelegateWithSyncResponds) {
KeyboardTester tester;
gboolean manager_handled = false;
std::vector<CallRecord> call_records;
std::vector<std::unique_ptr<FlKeyEvent>> redispatched;
/// Test 1: One event that is handled by the framework
tester.respondToEmbedderCallsWithAndRecordsTo(true, call_records);
tester.recordRedispatchedEventsTo(redispatched);
// Dispatch a key event
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(true, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
EXPECT_EQ(manager_handled, true);
EXPECT_EQ(call_records.size(), 1u);
EXPECT_KEY_EVENT(call_records[0], kFlutterKeyEventTypeDown, kPhysicalKeyA,
kLogicalKeyA, "a", false);
EXPECT_EQ(redispatched.size(), 0u);
call_records.clear();
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
redispatched.clear();
/// Test 2: An event unhandled by the framework
tester.respondToEmbedderCallsWithAndRecordsTo(false, call_records);
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(false, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
EXPECT_EQ(manager_handled, true);
EXPECT_EQ(call_records.size(), 1u);
EXPECT_KEY_EVENT(call_records[0], kFlutterKeyEventTypeUp, kPhysicalKeyA,
kLogicalKeyA, nullptr, false);
EXPECT_EQ(redispatched.size(), 1u);
call_records.clear();
EXPECT_FALSE(fl_keyboard_manager_is_state_clear(tester.manager()));
EXPECT_EQ(tester.redispatchEventsAndClear(redispatched), 1);
EXPECT_EQ(call_records.size(), 0u);
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
}
TEST(FlKeyboardManagerTest, WithTwoAsyncDelegates) {
KeyboardTester tester;
std::vector<CallRecord> call_records;
std::vector<std::unique_ptr<FlKeyEvent>> redispatched;
gboolean manager_handled = false;
tester.recordEmbedderCallsTo(call_records);
tester.recordChannelCallsTo(call_records);
tester.recordRedispatchedEventsTo(redispatched);
/// Test 1: One delegate responds true, the other false
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(true, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
EXPECT_EQ(manager_handled, true);
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_EQ(call_records.size(), 2u);
EXPECT_EQ(call_records[0].type, CallRecord::kKeyCallEmbedder);
EXPECT_EQ(call_records[1].type, CallRecord::kKeyCallChannel);
call_records[0].callback(true);
call_records[1].callback(false);
tester.flushChannelMessages();
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
call_records.clear();
/// Test 2: All delegates respond false
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(false, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
EXPECT_EQ(manager_handled, true);
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_EQ(call_records.size(), 2u);
EXPECT_EQ(call_records[0].type, CallRecord::kKeyCallEmbedder);
EXPECT_EQ(call_records[1].type, CallRecord::kKeyCallChannel);
call_records[0].callback(false);
call_records[1].callback(false);
call_records.clear();
// Resolve redispatch
tester.flushChannelMessages();
EXPECT_EQ(redispatched.size(), 1u);
EXPECT_EQ(tester.redispatchEventsAndClear(redispatched), 1);
EXPECT_EQ(call_records.size(), 0u);
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
}
TEST(FlKeyboardManagerTest, TextInputPluginReturnsFalse) {
KeyboardTester tester;
std::vector<std::unique_ptr<FlKeyEvent>> redispatched;
gboolean manager_handled = false;
tester.recordRedispatchedEventsTo(redispatched);
tester.respondToTextInputWith(false);
// Dispatch a key event.
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(true, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
EXPECT_EQ(manager_handled, true);
// The event was redispatched because no one handles it.
EXPECT_EQ(redispatched.size(), 1u);
// Resolve redispatched event.
EXPECT_EQ(tester.redispatchEventsAndClear(redispatched), 1);
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
}
TEST(FlKeyboardManagerTest, TextInputPluginReturnsTrue) {
KeyboardTester tester;
std::vector<std::unique_ptr<FlKeyEvent>> redispatched;
gboolean manager_handled = false;
tester.recordRedispatchedEventsTo(redispatched);
tester.respondToTextInputWith(true);
// Dispatch a key event.
manager_handled = fl_keyboard_manager_handle_event(
tester.manager(),
fl_key_event_new_by_mock(true, GDK_KEY_a, kKeyCodeKeyA,
static_cast<GdkModifierType>(0), false));
tester.flushChannelMessages();
EXPECT_EQ(manager_handled, true);
// The event was not redispatched because text input plugin handles it.
EXPECT_EQ(redispatched.size(), 0u);
EXPECT_TRUE(fl_keyboard_manager_is_state_clear(tester.manager()));
}
TEST(FlKeyboardManagerTest, CorrectLogicalKeyForLayouts) {
KeyboardTester tester;
std::vector<CallRecord> call_records;
tester.recordEmbedderCallsTo(call_records);
auto sendTap = [&](guint8 keycode, guint keyval, guint8 group) {
fl_keyboard_manager_handle_event(
tester.manager(), fl_key_event_new_by_mock(
true, keyval, keycode,
static_cast<GdkModifierType>(0), false, group));
fl_keyboard_manager_handle_event(
tester.manager(), fl_key_event_new_by_mock(
false, keyval, keycode,
static_cast<GdkModifierType>(0), false, group));
};
/* US keyboard layout */
sendTap(kKeyCodeKeyA, GDK_KEY_a, 0); // KeyA
VERIFY_DOWN(kLogicalKeyA, "a");
sendTap(kKeyCodeKeyA, GDK_KEY_A, 0); // Shift-KeyA
VERIFY_DOWN(kLogicalKeyA, "A");
sendTap(kKeyCodeDigit1, GDK_KEY_1, 0); // Digit1
VERIFY_DOWN(kLogicalDigit1, "1");
sendTap(kKeyCodeDigit1, GDK_KEY_exclam, 0); // Shift-Digit1
VERIFY_DOWN(kLogicalDigit1, "!");
sendTap(kKeyCodeMinus, GDK_KEY_minus, 0); // Minus
VERIFY_DOWN(kLogicalMinus, "-");
sendTap(kKeyCodeMinus, GDK_KEY_underscore, 0); // Shift-Minus
VERIFY_DOWN(kLogicalUnderscore, "_");
/* French keyboard layout, group 3, which is when the input method is showing
* "Fr" */
tester.setLayout(kLayoutFrench);
sendTap(kKeyCodeKeyA, GDK_KEY_q, 3); // KeyA
VERIFY_DOWN(kLogicalKeyQ, "q");
sendTap(kKeyCodeKeyA, GDK_KEY_Q, 3); // Shift-KeyA
VERIFY_DOWN(kLogicalKeyQ, "Q");
sendTap(kKeyCodeSemicolon, GDK_KEY_m, 3); // ; but prints M
VERIFY_DOWN(kLogicalKeyM, "m");
sendTap(kKeyCodeKeyM, GDK_KEY_comma, 3); // M but prints ,
VERIFY_DOWN(kLogicalComma, ",");
sendTap(kKeyCodeDigit1, GDK_KEY_ampersand, 3); // Digit1
VERIFY_DOWN(kLogicalDigit1, "&");
sendTap(kKeyCodeDigit1, GDK_KEY_1, 3); // Shift-Digit1
VERIFY_DOWN(kLogicalDigit1, "1");
sendTap(kKeyCodeMinus, GDK_KEY_parenright, 3); // Minus
VERIFY_DOWN(kLogicalParenthesisRight, ")");
sendTap(kKeyCodeMinus, GDK_KEY_degree, 3); // Shift-Minus
VERIFY_DOWN(static_cast<uint32_t>(L'°'), "°");
/* French keyboard layout, group 0, which is pressing the "extra key for
* triggering input method" key once after switching to French IME. */
sendTap(kKeyCodeKeyA, GDK_KEY_a, 0); // KeyA
VERIFY_DOWN(kLogicalKeyA, "a");
sendTap(kKeyCodeDigit1, GDK_KEY_1, 0); // Digit1
VERIFY_DOWN(kLogicalDigit1, "1");
/* Russian keyboard layout, group 2 */
tester.setLayout(kLayoutRussian);
sendTap(kKeyCodeKeyA, GDK_KEY_Cyrillic_ef, 2); // KeyA
VERIFY_DOWN(kLogicalKeyA, "ф");
sendTap(kKeyCodeDigit1, GDK_KEY_1, 2); // Shift-Digit1
VERIFY_DOWN(kLogicalDigit1, "1");
sendTap(kKeyCodeKeyLeftBracket, GDK_KEY_Cyrillic_ha, 2);
VERIFY_DOWN(kLogicalBracketLeft, "х");
/* Russian keyboard layout, group 0 */
sendTap(kKeyCodeKeyA, GDK_KEY_a, 0); // KeyA
VERIFY_DOWN(kLogicalKeyA, "a");
sendTap(kKeyCodeKeyLeftBracket, GDK_KEY_bracketleft, 0);
VERIFY_DOWN(kLogicalBracketLeft, "[");
}
TEST(FlKeyboardManagerTest, SynthesizeModifiersIfNeeded) {
KeyboardTester tester;
std::vector<CallRecord> call_records;
tester.recordEmbedderCallsTo(call_records);
auto verifyModifierIsSynthesized = [&](GdkModifierType mask,
uint64_t physical, uint64_t logical) {
// Modifier is pressed.