Flutter macOS Embedder
FlutterKeyboardManagerTest.mm
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 <Carbon/Carbon.h>
6 #import <Foundation/Foundation.h>
7 #import <OCMock/OCMock.h>
8 
12 #include "flutter/shell/platform/embedder/test_utils/key_codes.g.h"
13 #import "flutter/testing/testing.h"
14 #include "third_party/googletest/googletest/include/gtest/gtest.h"
15 
16 namespace {
17 
18 using flutter::testing::keycodes::kLogicalBracketLeft;
19 using flutter::testing::keycodes::kLogicalDigit1;
20 using flutter::testing::keycodes::kLogicalDigit2;
21 using flutter::testing::keycodes::kLogicalKeyA;
22 using flutter::testing::keycodes::kLogicalKeyM;
23 using flutter::testing::keycodes::kLogicalKeyQ;
24 using flutter::testing::keycodes::kLogicalKeyT;
25 using flutter::testing::keycodes::kPhysicalKeyA;
26 
28 
29 typedef BOOL (^BoolGetter)();
30 typedef void (^AsyncKeyCallbackHandler)(FlutterAsyncKeyCallback callback);
31 typedef void (^AsyncEmbedderCallbackHandler)(const FlutterKeyEvent* event,
32  FlutterAsyncKeyCallback callback);
33 typedef BOOL (^TextInputCallback)(NSEvent*);
34 
35 // When the Vietnamese IME converts messages into "pure text" messages, their
36 // key codes are set to "empty".
37 //
38 // The 0 also happens to be the key code for key A.
39 constexpr uint16_t kKeyCodeEmpty = 0x00;
40 
41 // Constants used for `recordCallTypesTo:forTypes:`.
42 constexpr uint32_t kEmbedderCall = 0x1;
43 constexpr uint32_t kChannelCall = 0x2;
44 constexpr uint32_t kTextCall = 0x4;
45 
46 // All key clues for a keyboard layout.
47 //
48 // The index is (keyCode * 2 + hasShift). The value is 0xMNNNN, where:
49 //
50 // - M is whether the key is a dead key (0x1 for true, 0x0 for false).
51 // - N is the character for this key. (It only supports UTF-16, but we don't
52 // need full UTF-32 support for unit tests. Moreover, Carbon's UCKeyTranslate
53 // only returns UniChar (UInt16) anyway.)
54 typedef const std::array<uint32_t, 256> MockLayoutData;
55 
56 // The following layout data is generated using DEBUG_PRINT_LAYOUT.
57 
58 MockLayoutData kUsLayout = {
59  // +0x0 Shift +0x1 Shift +0x2 Shift +0x3 Shift
60  /* 0x00 */ 0x00061, 0x00041, 0x00073, 0x00053, 0x00064, 0x00044, 0x00066, 0x00046,
61  /* 0x04 */ 0x00068, 0x00048, 0x00067, 0x00047, 0x0007a, 0x0005a, 0x00078, 0x00058,
62  /* 0x08 */ 0x00063, 0x00043, 0x00076, 0x00056, 0x000a7, 0x000b1, 0x00062, 0x00042,
63  /* 0x0c */ 0x00071, 0x00051, 0x00077, 0x00057, 0x00065, 0x00045, 0x00072, 0x00052,
64  /* 0x10 */ 0x00079, 0x00059, 0x00074, 0x00054, 0x00031, 0x00021, 0x00032, 0x00040,
65  /* 0x14 */ 0x00033, 0x00023, 0x00034, 0x00024, 0x00036, 0x0005e, 0x00035, 0x00025,
66  /* 0x18 */ 0x0003d, 0x0002b, 0x00039, 0x00028, 0x00037, 0x00026, 0x0002d, 0x0005f,
67  /* 0x1c */ 0x00038, 0x0002a, 0x00030, 0x00029, 0x0005d, 0x0007d, 0x0006f, 0x0004f,
68  /* 0x20 */ 0x00075, 0x00055, 0x0005b, 0x0007b, 0x00069, 0x00049, 0x00070, 0x00050,
69  /* 0x24 */ 0x00000, 0x00000, 0x0006c, 0x0004c, 0x0006a, 0x0004a, 0x00027, 0x00022,
70  /* 0x28 */ 0x0006b, 0x0004b, 0x0003b, 0x0003a, 0x0005c, 0x0007c, 0x0002c, 0x0003c,
71  /* 0x2c */ 0x0002f, 0x0003f, 0x0006e, 0x0004e, 0x0006d, 0x0004d, 0x0002e, 0x0003e,
72  /* 0x30 */ 0x00000, 0x00000, 0x00020, 0x00020, 0x00060, 0x0007e,
73 };
74 
75 MockLayoutData kFrenchLayout = {
76  // +0x0 Shift +0x1 Shift +0x2 Shift +0x3 Shift
77  /* 0x00 */ 0x00071, 0x00051, 0x00073, 0x00053, 0x00064, 0x00044, 0x00066, 0x00046,
78  /* 0x04 */ 0x00068, 0x00048, 0x00067, 0x00047, 0x00077, 0x00057, 0x00078, 0x00058,
79  /* 0x08 */ 0x00063, 0x00043, 0x00076, 0x00056, 0x00040, 0x00023, 0x00062, 0x00042,
80  /* 0x0c */ 0x00061, 0x00041, 0x0007a, 0x0005a, 0x00065, 0x00045, 0x00072, 0x00052,
81  /* 0x10 */ 0x00079, 0x00059, 0x00074, 0x00054, 0x00026, 0x00031, 0x000e9, 0x00032,
82  /* 0x14 */ 0x00022, 0x00033, 0x00027, 0x00034, 0x000a7, 0x00036, 0x00028, 0x00035,
83  /* 0x18 */ 0x0002d, 0x0005f, 0x000e7, 0x00039, 0x000e8, 0x00037, 0x00029, 0x000b0,
84  /* 0x1c */ 0x00021, 0x00038, 0x000e0, 0x00030, 0x00024, 0x0002a, 0x0006f, 0x0004f,
85  /* 0x20 */ 0x00075, 0x00055, 0x1005e, 0x100a8, 0x00069, 0x00049, 0x00070, 0x00050,
86  /* 0x24 */ 0x00000, 0x00000, 0x0006c, 0x0004c, 0x0006a, 0x0004a, 0x000f9, 0x00025,
87  /* 0x28 */ 0x0006b, 0x0004b, 0x0006d, 0x0004d, 0x10060, 0x000a3, 0x0003b, 0x0002e,
88  /* 0x2c */ 0x0003d, 0x0002b, 0x0006e, 0x0004e, 0x0002c, 0x0003f, 0x0003a, 0x0002f,
89  /* 0x30 */ 0x00000, 0x00000, 0x00020, 0x00020, 0x0003c, 0x0003e,
90 };
91 
92 MockLayoutData kRussianLayout = {
93  // +0x0 Shift +0x1 Shift +0x2 Shift +0x3 Shift
94  /* 0x00 */ 0x00444, 0x00424, 0x0044b, 0x0042b, 0x00432, 0x00412, 0x00430, 0x00410,
95  /* 0x04 */ 0x00440, 0x00420, 0x0043f, 0x0041f, 0x0044f, 0x0042f, 0x00447, 0x00427,
96  /* 0x08 */ 0x00441, 0x00421, 0x0043c, 0x0041c, 0x0003e, 0x0003c, 0x00438, 0x00418,
97  /* 0x0c */ 0x00439, 0x00419, 0x00446, 0x00426, 0x00443, 0x00423, 0x0043a, 0x0041a,
98  /* 0x10 */ 0x0043d, 0x0041d, 0x00435, 0x00415, 0x00031, 0x00021, 0x00032, 0x00022,
99  /* 0x14 */ 0x00033, 0x02116, 0x00034, 0x00025, 0x00036, 0x0002c, 0x00035, 0x0003a,
100  /* 0x18 */ 0x0003d, 0x0002b, 0x00039, 0x00028, 0x00037, 0x0002e, 0x0002d, 0x0005f,
101  /* 0x1c */ 0x00038, 0x0003b, 0x00030, 0x00029, 0x0044a, 0x0042a, 0x00449, 0x00429,
102  /* 0x20 */ 0x00433, 0x00413, 0x00445, 0x00425, 0x00448, 0x00428, 0x00437, 0x00417,
103  /* 0x24 */ 0x00000, 0x00000, 0x00434, 0x00414, 0x0043e, 0x0041e, 0x0044d, 0x0042d,
104  /* 0x28 */ 0x0043b, 0x0041b, 0x00436, 0x00416, 0x00451, 0x00401, 0x00431, 0x00411,
105  /* 0x2c */ 0x0002f, 0x0003f, 0x00442, 0x00422, 0x0044c, 0x0042c, 0x0044e, 0x0042e,
106  /* 0x30 */ 0x00000, 0x00000, 0x00020, 0x00020, 0x0005d, 0x0005b,
107 };
108 
109 MockLayoutData kKhmerLayout = {
110  // +0x0 Shift +0x1 Shift +0x2 Shift +0x3 Shift
111  /* 0x00 */ 0x017b6, 0x017ab, 0x0179f, 0x017c3, 0x0178a, 0x0178c, 0x01790, 0x01792,
112  /* 0x04 */ 0x017a0, 0x017c7, 0x01784, 0x017a2, 0x0178b, 0x0178d, 0x01781, 0x01783,
113  /* 0x08 */ 0x01785, 0x01787, 0x0179c, 0x017c8, 0x00000, 0x00000, 0x01794, 0x01796,
114  /* 0x0c */ 0x01786, 0x01788, 0x017b9, 0x017ba, 0x017c1, 0x017c2, 0x0179a, 0x017ac,
115  /* 0x10 */ 0x01799, 0x017bd, 0x0178f, 0x01791, 0x017e1, 0x00021, 0x017e2, 0x017d7,
116  /* 0x14 */ 0x017e3, 0x00022, 0x017e4, 0x017db, 0x017e6, 0x017cd, 0x017e5, 0x00025,
117  /* 0x18 */ 0x017b2, 0x017ce, 0x017e9, 0x017b0, 0x017e7, 0x017d0, 0x017a5, 0x017cc,
118  /* 0x1c */ 0x017e8, 0x017cf, 0x017e0, 0x017b3, 0x017aa, 0x017a7, 0x017c4, 0x017c5,
119  /* 0x20 */ 0x017bb, 0x017bc, 0x017c0, 0x017bf, 0x017b7, 0x017b8, 0x01795, 0x01797,
120  /* 0x24 */ 0x00000, 0x00000, 0x0179b, 0x017a1, 0x017d2, 0x01789, 0x017cb, 0x017c9,
121  /* 0x28 */ 0x01780, 0x01782, 0x017be, 0x017d6, 0x017ad, 0x017ae, 0x017a6, 0x017b1,
122  /* 0x2c */ 0x017ca, 0x017af, 0x01793, 0x0178e, 0x01798, 0x017c6, 0x017d4, 0x017d5,
123  /* 0x30 */ 0x00000, 0x00000, 0x00020, 0x0200b, 0x000ab, 0x000bb,
124 };
125 
126 NSEvent* keyDownEvent(unsigned short keyCode, NSString* chars = @"", NSString* charsUnmod = @"") {
127  return [NSEvent keyEventWithType:NSEventTypeKeyDown
128  location:NSZeroPoint
129  modifierFlags:0x100
130  timestamp:0
131  windowNumber:0
132  context:nil
133  characters:chars
134  charactersIgnoringModifiers:charsUnmod
135  isARepeat:NO
136  keyCode:keyCode];
137 }
138 
139 NSEvent* keyUpEvent(unsigned short keyCode) {
140  return [NSEvent keyEventWithType:NSEventTypeKeyUp
141  location:NSZeroPoint
142  modifierFlags:0x100
143  timestamp:0
144  windowNumber:0
145  context:nil
146  characters:@""
147  charactersIgnoringModifiers:@""
148  isARepeat:NO
149  keyCode:keyCode];
150 }
151 
152 id checkKeyDownEvent(unsigned short keyCode) {
153  return [OCMArg checkWithBlock:^BOOL(id value) {
154  if (![value isKindOfClass:[NSEvent class]]) {
155  return NO;
156  }
157  NSEvent* event = value;
158  return event.keyCode == keyCode;
159  }];
160 }
161 
162 // Clear a list of `FlutterKeyEvent` whose `character` is dynamically allocated.
163 void clearEvents(std::vector<FlutterKeyEvent>& events) {
164  for (FlutterKeyEvent& event : events) {
165  if (event.character != nullptr) {
166  delete[] event.character;
167  }
168  }
169  events.clear();
170 }
171 
172 #define VERIFY_DOWN(OUT_LOGICAL, OUT_CHAR) \
173  EXPECT_EQ(events[0].type, kFlutterKeyEventTypeDown); \
174  EXPECT_EQ(events[0].logical, static_cast<uint64_t>(OUT_LOGICAL)); \
175  EXPECT_STREQ(events[0].character, (OUT_CHAR)); \
176  clearEvents(events);
177 
178 } // namespace
179 
180 @interface KeyboardTester : NSObject
181 - (nonnull instancetype)init;
182 
183 // Set embedder calls to respond immediately with the given response.
184 - (void)respondEmbedderCallsWith:(BOOL)response;
185 
186 // Record embedder calls to the given storage.
187 //
188 // They are not responded to until the stored callbacks are manually called.
189 - (void)recordEmbedderCallsTo:(nonnull NSMutableArray<FlutterAsyncKeyCallback>*)storage;
190 
191 - (void)recordEmbedderEventsTo:(nonnull std::vector<FlutterKeyEvent>*)storage
192  returning:(bool)handled;
193 
194 // Set channel calls to respond immediately with the given response.
195 - (void)respondChannelCallsWith:(BOOL)response;
196 
197 // Record channel calls to the given storage.
198 //
199 // They are not responded to until the stored callbacks are manually called.
200 - (void)recordChannelCallsTo:(nonnull NSMutableArray<FlutterAsyncKeyCallback>*)storage;
201 
202 // Set text calls to respond with the given response.
203 - (void)respondTextInputWith:(BOOL)response;
204 
205 // At the start of any kind of call, record the call type to the given storage.
206 //
207 // Only calls that are included in `typeMask` will be added. Options are
208 // kEmbedderCall, kChannelCall, and kTextCall.
209 //
210 // This method does not conflict with other call settings, and the recording
211 // takes place before the callbacks are (or are not) invoked.
212 - (void)recordCallTypesTo:(nonnull NSMutableArray<NSNumber*>*)typeStorage
213  forTypes:(uint32_t)typeMask;
214 
216 
217 - (void)sendKeyboardChannelMessage:(NSData* _Nullable)message;
218 
219 @property(readonly, nonatomic, strong) FlutterKeyboardManager* manager;
220 @property(nonatomic, nullable, strong) NSResponder* nextResponder;
221 
222 #pragma mark - Private
223 
224 - (void)handleEmbedderEvent:(const FlutterKeyEvent&)event
225  callback:(nullable FlutterKeyEventCallback)callback
226  userData:(nullable void*)userData;
227 
228 - (void)handleChannelMessage:(NSString*)channel
229  message:(NSData* _Nullable)message
230  binaryReply:(FlutterBinaryReply _Nullable)callback;
231 
232 - (BOOL)handleTextInputKeyEvent:(NSEvent*)event;
233 @end
234 
235 @implementation KeyboardTester {
236  AsyncEmbedderCallbackHandler _embedderHandler;
237  AsyncKeyCallbackHandler _channelHandler;
238  TextInputCallback _textCallback;
239 
240  NSMutableArray<NSNumber*>* _typeStorage;
242 
244  const MockLayoutData* _currentLayout;
245 
247  NSObject<FlutterBinaryMessenger>* _messengerMock;
249 }
250 
251 - (nonnull instancetype)init {
252  self = [super init];
253  if (self == nil) {
254  return nil;
255  }
256 
257  _nextResponder = OCMClassMock([NSResponder class]);
258  [self respondChannelCallsWith:FALSE];
259  [self respondEmbedderCallsWith:FALSE];
260  [self respondTextInputWith:FALSE];
261 
262  _currentLayout = &kUsLayout;
263 
264  _messengerMock = OCMStrictProtocolMock(@protocol(FlutterBinaryMessenger));
265  OCMStub([_messengerMock sendOnChannel:@"flutter/keyevent"
266  message:[OCMArg any]
267  binaryReply:[OCMArg any]])
268  .andCall(self, @selector(handleChannelMessage:message:binaryReply:));
269  OCMStub([_messengerMock setMessageHandlerOnChannel:@"flutter/keyboard"
270  binaryMessageHandler:[OCMArg any]])
271  .andCall(self, @selector(setKeyboardChannelHandler:handler:));
272  OCMStub([_messengerMock sendOnChannel:@"flutter/keyboard" message:[OCMArg any]])
273  .andCall(self, @selector(handleKeyboardChannelMessage:message:));
274  id viewDelegateMock = OCMStrictProtocolMock(@protocol(FlutterKeyboardViewDelegate));
275  OCMStub([viewDelegateMock nextResponder]).andReturn(_nextResponder);
276  OCMStub([viewDelegateMock onTextInputKeyEvent:[OCMArg any]])
277  .andCall(self, @selector(handleTextInputKeyEvent:));
278  OCMStub([viewDelegateMock getBinaryMessenger]).andReturn(_messengerMock);
279  OCMStub([viewDelegateMock sendKeyEvent:FlutterKeyEvent {} callback:nil userData:nil])
280  .ignoringNonObjectArgs()
281  .andCall(self, @selector(handleEmbedderEvent:callback:userData:));
282  OCMStub([viewDelegateMock subscribeToKeyboardLayoutChange:[OCMArg any]])
283  .andCall(self, @selector(onSetKeyboardLayoutNotifier:));
284  OCMStub([viewDelegateMock lookUpLayoutForKeyCode:0 shift:false])
285  .ignoringNonObjectArgs()
286  .andCall(self, @selector(lookUpLayoutForKeyCode:shift:));
287 
288  _manager = [[FlutterKeyboardManager alloc] initWithViewDelegate:viewDelegateMock];
289  return self;
290 }
291 
292 - (id)lastKeyboardChannelResult {
293  return _keyboardChannelResult;
294 }
295 
296 - (void)respondEmbedderCallsWith:(BOOL)response {
297  _embedderHandler = ^(const FlutterKeyEvent* event, FlutterAsyncKeyCallback callback) {
298  callback(response);
299  };
300 }
301 
302 - (void)recordEmbedderCallsTo:(nonnull NSMutableArray<FlutterAsyncKeyCallback>*)storage {
303  _embedderHandler = ^(const FlutterKeyEvent* event, FlutterAsyncKeyCallback callback) {
304  [storage addObject:callback];
305  };
306 }
307 
308 - (void)recordEmbedderEventsTo:(nonnull std::vector<FlutterKeyEvent>*)storage
309  returning:(bool)handled {
310  _embedderHandler = ^(const FlutterKeyEvent* event, FlutterAsyncKeyCallback callback) {
311  FlutterKeyEvent newEvent = *event;
312  if (event->character != nullptr) {
313  size_t charLen = strlen(event->character);
314  char* newCharacter = new char[charLen + 1];
315  strlcpy(newCharacter, event->character, charLen + 1);
316  newEvent.character = newCharacter;
317  }
318  storage->push_back(newEvent);
319  callback(handled);
320  };
321 }
322 
323 - (void)respondChannelCallsWith:(BOOL)response {
325  callback(response);
326  };
327 }
328 
329 - (void)recordChannelCallsTo:(nonnull NSMutableArray<FlutterAsyncKeyCallback>*)storage {
331  [storage addObject:callback];
332  };
333 }
334 
335 - (void)respondTextInputWith:(BOOL)response {
336  _textCallback = ^(NSEvent* event) {
337  return response;
338  };
339 }
340 
341 - (void)recordCallTypesTo:(nonnull NSMutableArray<NSNumber*>*)typeStorage
342  forTypes:(uint32_t)typeMask {
343  _typeStorage = typeStorage;
344  _typeStorageMask = typeMask;
345 }
346 
347 - (void)sendKeyboardChannelMessage:(NSData* _Nullable)message {
348  [_messengerMock sendOnChannel:@"flutter/keyboard" message:message];
349 }
350 
351 - (void)setLayout:(const MockLayoutData&)layout {
352  _currentLayout = &layout;
353  if (_keyboardLayoutNotifier != nil) {
355  }
356 }
357 
358 #pragma mark - Private
359 
360 - (void)handleEmbedderEvent:(const FlutterKeyEvent&)event
361  callback:(nullable FlutterKeyEventCallback)callback
362  userData:(nullable void*)userData {
363  if (_typeStorage != nil && (_typeStorageMask & kEmbedderCall) != 0) {
364  [_typeStorage addObject:@(kEmbedderCall)];
365  }
366  if (callback != nullptr) {
367  _embedderHandler(&event, ^(BOOL handled) {
368  callback(handled, userData);
369  });
370  }
371 }
372 
373 - (void)handleChannelMessage:(NSString*)channel
374  message:(NSData* _Nullable)message
375  binaryReply:(FlutterBinaryReply _Nullable)callback {
376  if (_typeStorage != nil && (_typeStorageMask & kChannelCall) != 0) {
377  [_typeStorage addObject:@(kChannelCall)];
378  }
379  _channelHandler(^(BOOL handled) {
380  NSDictionary* result = @{
381  @"handled" : @(handled),
382  };
383  NSData* encodedKeyEvent = [[FlutterJSONMessageCodec sharedInstance] encode:result];
384  callback(encodedKeyEvent);
385  });
386 }
387 
388 - (void)handleKeyboardChannelMessage:(NSString*)channel message:(NSData* _Nullable)message {
389  _keyboardHandler(message, ^(id result) {
390  _keyboardChannelResult = result;
391  });
392 }
393 
394 - (BOOL)handleTextInputKeyEvent:(NSEvent*)event {
395  if (_typeStorage != nil && (_typeStorageMask & kTextCall) != 0) {
396  [_typeStorage addObject:@(kTextCall)];
397  }
398  return _textCallback(event);
399 }
400 
401 - (void)onSetKeyboardLayoutNotifier:(nullable flutter::KeyboardLayoutNotifier)callback {
402  _keyboardLayoutNotifier = callback;
403 }
404 
405 - (LayoutClue)lookUpLayoutForKeyCode:(uint16_t)keyCode shift:(BOOL)shift {
406  uint32_t cluePair = (*_currentLayout)[(keyCode * 2) + (shift ? 1 : 0)];
407  const uint32_t kCharMask = 0xffff;
408  const uint32_t kDeadKeyMask = 0x10000;
409  return LayoutClue{cluePair & kCharMask, (cluePair & kDeadKeyMask) != 0};
410 }
411 
412 - (void)setKeyboardChannelHandler:(NSString*)channel handler:(FlutterBinaryMessageHandler)handler {
413  _keyboardHandler = handler;
414 }
415 
416 @end
417 
419 - (bool)singlePrimaryResponder;
420 - (bool)doublePrimaryResponder;
421 - (bool)textInputPlugin;
422 - (bool)emptyNextResponder;
423 - (bool)getPressedState;
427 @end
428 
429 namespace flutter::testing {
430 TEST(FlutterKeyboardManagerUnittests, SinglePrimaryResponder) {
431  ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] singlePrimaryResponder]);
432 }
433 
434 TEST(FlutterKeyboardManagerUnittests, DoublePrimaryResponder) {
435  ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] doublePrimaryResponder]);
436 }
437 
438 TEST(FlutterKeyboardManagerUnittests, SingleFinalResponder) {
439  ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] textInputPlugin]);
440 }
441 
442 TEST(FlutterKeyboardManagerUnittests, EmptyNextResponder) {
443  ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] emptyNextResponder]);
444 }
445 
446 TEST(FlutterKeyboardManagerUnittests, GetPressedState) {
447  ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] getPressedState]);
448 }
449 
450 TEST(FlutterKeyboardManagerUnittests, KeyboardChannelGetPressedState) {
451  ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] keyboardChannelGetPressedState]);
452 }
453 
454 TEST(FlutterKeyboardManagerUnittests, RacingConditionBetweenKeyAndText) {
455  ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] racingConditionBetweenKeyAndText]);
456 }
457 
458 TEST(FlutterKeyboardManagerUnittests, CorrectLogicalKeyForLayouts) {
459  ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] correctLogicalKeyForLayouts]);
460 }
461 
462 } // namespace flutter::testing
463 
465 
467  KeyboardTester* tester = [[KeyboardTester alloc] init];
468  NSMutableArray<FlutterAsyncKeyCallback>* embedderCallbacks =
469  [NSMutableArray<FlutterAsyncKeyCallback> array];
470  [tester recordEmbedderCallsTo:embedderCallbacks];
471 
472  // Case: The responder reports FALSE
473  [tester.manager handleEvent:keyDownEvent(0x50)];
474  EXPECT_EQ([embedderCallbacks count], 1u);
475  embedderCallbacks[0](FALSE);
476  OCMVerify([tester.nextResponder keyDown:checkKeyDownEvent(0x50)]);
477  [embedderCallbacks removeAllObjects];
478 
479  // Case: The responder reports TRUE
480  [tester.manager handleEvent:keyUpEvent(0x50)];
481  EXPECT_EQ([embedderCallbacks count], 1u);
482  embedderCallbacks[0](TRUE);
483  // [owner.nextResponder keyUp:] should not be called, otherwise an error will be thrown.
484 
485  return true;
486 }
487 
489  KeyboardTester* tester = [[KeyboardTester alloc] init];
490 
491  // Send a down event first so we can send an up event later.
492  [tester respondEmbedderCallsWith:false];
493  [tester respondChannelCallsWith:false];
494  [tester.manager handleEvent:keyDownEvent(0x50)];
495 
496  NSMutableArray<FlutterAsyncKeyCallback>* embedderCallbacks =
497  [NSMutableArray<FlutterAsyncKeyCallback> array];
498  NSMutableArray<FlutterAsyncKeyCallback>* channelCallbacks =
499  [NSMutableArray<FlutterAsyncKeyCallback> array];
500  [tester recordEmbedderCallsTo:embedderCallbacks];
501  [tester recordChannelCallsTo:channelCallbacks];
502 
503  // Case: Both responders report TRUE.
504  [tester.manager handleEvent:keyUpEvent(0x50)];
505  EXPECT_EQ([embedderCallbacks count], 1u);
506  EXPECT_EQ([channelCallbacks count], 1u);
507  embedderCallbacks[0](TRUE);
508  channelCallbacks[0](TRUE);
509  EXPECT_EQ([embedderCallbacks count], 1u);
510  EXPECT_EQ([channelCallbacks count], 1u);
511  // [tester.nextResponder keyUp:] should not be called, otherwise an error will be thrown.
512  [embedderCallbacks removeAllObjects];
513  [channelCallbacks removeAllObjects];
514 
515  // Case: One responder reports TRUE.
516  [tester respondEmbedderCallsWith:false];
517  [tester respondChannelCallsWith:false];
518  [tester.manager handleEvent:keyDownEvent(0x50)];
519 
520  [tester recordEmbedderCallsTo:embedderCallbacks];
521  [tester recordChannelCallsTo:channelCallbacks];
522  [tester.manager handleEvent:keyUpEvent(0x50)];
523  EXPECT_EQ([embedderCallbacks count], 1u);
524  EXPECT_EQ([channelCallbacks count], 1u);
525  embedderCallbacks[0](FALSE);
526  channelCallbacks[0](TRUE);
527  EXPECT_EQ([embedderCallbacks count], 1u);
528  EXPECT_EQ([channelCallbacks count], 1u);
529  // [tester.nextResponder keyUp:] should not be called, otherwise an error will be thrown.
530  [embedderCallbacks removeAllObjects];
531  [channelCallbacks removeAllObjects];
532 
533  // Case: Both responders report FALSE.
534  [tester.manager handleEvent:keyDownEvent(0x53)];
535  EXPECT_EQ([embedderCallbacks count], 1u);
536  EXPECT_EQ([channelCallbacks count], 1u);
537  embedderCallbacks[0](FALSE);
538  channelCallbacks[0](FALSE);
539  EXPECT_EQ([embedderCallbacks count], 1u);
540  EXPECT_EQ([channelCallbacks count], 1u);
541  OCMVerify([tester.nextResponder keyDown:checkKeyDownEvent(0x53)]);
542  [embedderCallbacks removeAllObjects];
543  [channelCallbacks removeAllObjects];
544 
545  return true;
546 }
547 
549  KeyboardTester* tester = [[KeyboardTester alloc] init];
550 
551  // Send a down event first so we can send an up event later.
552  [tester respondEmbedderCallsWith:false];
553  [tester respondChannelCallsWith:false];
554  [tester.manager handleEvent:keyDownEvent(0x50)];
555 
556  NSMutableArray<FlutterAsyncKeyCallback>* callbacks =
557  [NSMutableArray<FlutterAsyncKeyCallback> array];
558  [tester recordEmbedderCallsTo:callbacks];
559 
560  // Case: Primary responder responds TRUE. The event shouldn't be handled by
561  // the secondary responder.
562  [tester respondTextInputWith:FALSE];
563  [tester.manager handleEvent:keyUpEvent(0x50)];
564  EXPECT_EQ([callbacks count], 1u);
565  callbacks[0](TRUE);
566  // [owner.nextResponder keyUp:] should not be called, otherwise an error will be thrown.
567  [callbacks removeAllObjects];
568 
569  // Send a down event first so we can send an up event later.
570  [tester respondEmbedderCallsWith:false];
571  [tester.manager handleEvent:keyDownEvent(0x50)];
572 
573  // Case: Primary responder responds FALSE. The secondary responder returns
574  // TRUE.
575  [tester recordEmbedderCallsTo:callbacks];
576  [tester respondTextInputWith:TRUE];
577  [tester.manager handleEvent:keyUpEvent(0x50)];
578  EXPECT_EQ([callbacks count], 1u);
579  callbacks[0](FALSE);
580  // [owner.nextResponder keyUp:] should not be called, otherwise an error will be thrown.
581  [callbacks removeAllObjects];
582 
583  // Case: Primary responder responds FALSE. The secondary responder returns FALSE.
584  [tester respondTextInputWith:FALSE];
585  [tester.manager handleEvent:keyDownEvent(0x50)];
586  EXPECT_EQ([callbacks count], 1u);
587  callbacks[0](FALSE);
588  OCMVerify([tester.nextResponder keyDown:checkKeyDownEvent(0x50)]);
589  [callbacks removeAllObjects];
590 
591  return true;
592 }
593 
595  KeyboardTester* tester = [[KeyboardTester alloc] init];
596  tester.nextResponder = nil;
597 
598  [tester respondEmbedderCallsWith:false];
599  [tester respondChannelCallsWith:false];
600  [tester respondTextInputWith:false];
601  [tester.manager handleEvent:keyDownEvent(0x50)];
602 
603  // Passes if no error is thrown.
604  return true;
605 }
606 
608  KeyboardTester* tester = [[KeyboardTester alloc] init];
609 
610  [tester respondEmbedderCallsWith:false];
611  [tester respondChannelCallsWith:false];
612  [tester respondTextInputWith:false];
613  [tester.manager handleEvent:keyDownEvent(kVK_ANSI_A)];
614 
615  NSDictionary* pressingRecords = [tester.manager getPressedState];
616  EXPECT_EQ([pressingRecords count], 1u);
617  EXPECT_EQ(pressingRecords[@(kPhysicalKeyA)], @(kLogicalKeyA));
618 
619  return true;
620 }
621 
623  KeyboardTester* tester = [[KeyboardTester alloc] init];
624 
625  [tester respondEmbedderCallsWith:false];
626  [tester respondChannelCallsWith:false];
627  [tester respondTextInputWith:false];
628  [tester.manager handleEvent:keyDownEvent(kVK_ANSI_A)];
629 
630  FlutterMethodCall* getKeyboardStateMethodCall =
631  [FlutterMethodCall methodCallWithMethodName:@"getKeyboardState" arguments:nil];
632  NSData* getKeyboardStateMessage =
633  [[FlutterStandardMethodCodec sharedInstance] encodeMethodCall:getKeyboardStateMethodCall];
634  [tester sendKeyboardChannelMessage:getKeyboardStateMessage];
635 
636  id encodedResult = [tester lastKeyboardChannelResult];
637  id decoded = [[FlutterStandardMethodCodec sharedInstance] decodeEnvelope:encodedResult];
638 
639  EXPECT_EQ([decoded count], 1u);
640  EXPECT_EQ(decoded[@(kPhysicalKeyA)], @(kLogicalKeyA));
641 
642  return true;
643 }
644 
645 // Regression test for https://github.com/flutter/flutter/issues/82673.
647  KeyboardTester* tester = [[KeyboardTester alloc] init];
648 
649  // Use Vietnamese IME (GoTiengViet, Telex mode) to type "uco".
650 
651  // The events received by the framework. The engine might receive
652  // a channel message "setEditingState" from the framework.
653  NSMutableArray<FlutterAsyncKeyCallback>* keyCallbacks =
654  [NSMutableArray<FlutterAsyncKeyCallback> array];
655  [tester recordEmbedderCallsTo:keyCallbacks];
656 
657  NSMutableArray<NSNumber*>* allCalls = [NSMutableArray<NSNumber*> array];
658  [tester recordCallTypesTo:allCalls forTypes:(kEmbedderCall | kTextCall)];
659 
660  // Tap key U, which is converted by IME into a pure text message "ư".
661 
662  [tester.manager handleEvent:keyDownEvent(kKeyCodeEmpty, @"ư", @"ư")];
663  EXPECT_EQ([keyCallbacks count], 1u);
664  EXPECT_EQ([allCalls count], 1u);
665  EXPECT_EQ(allCalls[0], @(kEmbedderCall));
666  keyCallbacks[0](false);
667  EXPECT_EQ([keyCallbacks count], 1u);
668  EXPECT_EQ([allCalls count], 2u);
669  EXPECT_EQ(allCalls[1], @(kTextCall));
670  [keyCallbacks removeAllObjects];
671  [allCalls removeAllObjects];
672 
673  [tester.manager handleEvent:keyUpEvent(kKeyCodeEmpty)];
674  EXPECT_EQ([keyCallbacks count], 1u);
675  keyCallbacks[0](false);
676  EXPECT_EQ([keyCallbacks count], 1u);
677  EXPECT_EQ([allCalls count], 2u);
678  [keyCallbacks removeAllObjects];
679  [allCalls removeAllObjects];
680 
681  // Tap key O, which is converted to normal KeyO events, but the responses are
682  // slow.
683 
684  [tester.manager handleEvent:keyDownEvent(kVK_ANSI_O, @"o", @"o")];
685  [tester.manager handleEvent:keyUpEvent(kVK_ANSI_O)];
686  EXPECT_EQ([keyCallbacks count], 1u);
687  EXPECT_EQ([allCalls count], 1u);
688  EXPECT_EQ(allCalls[0], @(kEmbedderCall));
689 
690  // Tap key C, which results in two Backspace messages first - and here they
691  // arrive before the key O messages are responded.
692 
693  [tester.manager handleEvent:keyDownEvent(kVK_Delete)];
694  [tester.manager handleEvent:keyUpEvent(kVK_Delete)];
695  EXPECT_EQ([keyCallbacks count], 1u);
696  EXPECT_EQ([allCalls count], 1u);
697 
698  // The key O down is responded, which releases a text call (for KeyO down) and
699  // an embedder call (for KeyO up) immediately.
700  keyCallbacks[0](false);
701  EXPECT_EQ([keyCallbacks count], 2u);
702  EXPECT_EQ([allCalls count], 3u);
703  EXPECT_EQ(allCalls[1], @(kTextCall)); // The order is important!
704  EXPECT_EQ(allCalls[2], @(kEmbedderCall));
705 
706  // The key O up is responded, which releases a text call (for KeyO up) and
707  // an embedder call (for Backspace down) immediately.
708  keyCallbacks[1](false);
709  EXPECT_EQ([keyCallbacks count], 3u);
710  EXPECT_EQ([allCalls count], 5u);
711  EXPECT_EQ(allCalls[3], @(kTextCall)); // The order is important!
712  EXPECT_EQ(allCalls[4], @(kEmbedderCall));
713 
714  // Finish up callbacks.
715  keyCallbacks[2](false);
716  keyCallbacks[3](false);
717 
718  return true;
719 }
720 
722  KeyboardTester* tester = [[KeyboardTester alloc] init];
723  tester.nextResponder = nil;
724 
725  std::vector<FlutterKeyEvent> events;
726  [tester recordEmbedderEventsTo:&events returning:true];
727  [tester respondChannelCallsWith:false];
728  [tester respondTextInputWith:false];
729 
730  auto sendTap = [&](uint16_t keyCode, NSString* chars, NSString* charsUnmod) {
731  [tester.manager handleEvent:keyDownEvent(keyCode, chars, charsUnmod)];
732  [tester.manager handleEvent:keyUpEvent(keyCode)];
733  };
734 
735  /* US keyboard layout */
736 
737  sendTap(kVK_ANSI_A, @"a", @"a"); // KeyA
738  VERIFY_DOWN(kLogicalKeyA, "a");
739 
740  sendTap(kVK_ANSI_A, @"A", @"A"); // Shift-KeyA
741  VERIFY_DOWN(kLogicalKeyA, "A");
742 
743  sendTap(kVK_ANSI_A, @"Ã¥", @"a"); // Option-KeyA
744  VERIFY_DOWN(kLogicalKeyA, "Ã¥");
745 
746  sendTap(kVK_ANSI_T, @"t", @"t"); // KeyT
747  VERIFY_DOWN(kLogicalKeyT, "t");
748 
749  sendTap(kVK_ANSI_1, @"1", @"1"); // Digit1
750  VERIFY_DOWN(kLogicalDigit1, "1");
751 
752  sendTap(kVK_ANSI_1, @"!", @"!"); // Shift-Digit1
753  VERIFY_DOWN(kLogicalDigit1, "!");
754 
755  sendTap(kVK_ANSI_Minus, @"-", @"-"); // Minus
756  VERIFY_DOWN('-', "-");
757 
758  sendTap(kVK_ANSI_Minus, @"=", @"="); // Shift-Minus
759  VERIFY_DOWN('=', "=");
760 
761  /* French keyboard layout */
762  [tester setLayout:kFrenchLayout];
763 
764  sendTap(kVK_ANSI_A, @"q", @"q"); // KeyA
765  VERIFY_DOWN(kLogicalKeyQ, "q");
766 
767  sendTap(kVK_ANSI_A, @"Q", @"Q"); // Shift-KeyA
768  VERIFY_DOWN(kLogicalKeyQ, "Q");
769 
770  sendTap(kVK_ANSI_Semicolon, @"m", @"m"); // ; but prints M
771  VERIFY_DOWN(kLogicalKeyM, "m");
772 
773  sendTap(kVK_ANSI_M, @",", @","); // M but prints ,
774  VERIFY_DOWN(',', ",");
775 
776  sendTap(kVK_ANSI_1, @"&", @"&"); // Digit1
777  VERIFY_DOWN(kLogicalDigit1, "&");
778 
779  sendTap(kVK_ANSI_1, @"1", @"1"); // Shift-Digit1
780  VERIFY_DOWN(kLogicalDigit1, "1");
781 
782  sendTap(kVK_ANSI_Minus, @")", @")"); // Minus
783  VERIFY_DOWN(')', ")");
784 
785  sendTap(kVK_ANSI_Minus, @"°", @"°"); // Shift-Minus
786  VERIFY_DOWN(L'°', "°");
787 
788  /* Russian keyboard layout */
789  [tester setLayout:kRussianLayout];
790 
791  sendTap(kVK_ANSI_A, @"Ñ„", @"Ñ„"); // KeyA
792  VERIFY_DOWN(kLogicalKeyA, "Ñ„");
793 
794  sendTap(kVK_ANSI_1, @"1", @"1"); // Digit1
795  VERIFY_DOWN(kLogicalDigit1, "1");
796 
797  sendTap(kVK_ANSI_LeftBracket, @"Ñ…", @"Ñ…");
798  VERIFY_DOWN(kLogicalBracketLeft, "Ñ…");
799 
800  /* Khmer keyboard layout */
801  // Regression test for https://github.com/flutter/flutter/issues/108729
802  [tester setLayout:kKhmerLayout];
803 
804  sendTap(kVK_ANSI_2, @"២", @"២"); // Digit2
805  VERIFY_DOWN(kLogicalDigit2, "២");
806 
807  return TRUE;
808 }
809 
810 @end
flutter::LayoutClue
Definition: FlutterKeyboardViewDelegate.h:17
-[KeyboardTester recordEmbedderCallsTo:]
void recordEmbedderCallsTo:(nonnull NSMutableArray< FlutterAsyncKeyCallback > *storage)
Definition: FlutterKeyboardManagerTest.mm:302
-[KeyboardTester recordChannelCallsTo:]
void recordChannelCallsTo:(nonnull NSMutableArray< FlutterAsyncKeyCallback > *storage)
Definition: FlutterKeyboardManagerTest.mm:329
-[FlutterKeyboardManagerUnittestsObjC correctLogicalKeyForLayouts]
bool correctLogicalKeyForLayouts()
Definition: FlutterKeyboardManagerTest.mm:721
+[FlutterMethodCall methodCallWithMethodName:arguments:]
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
FlutterKeyboardViewDelegate-p
Definition: FlutterKeyboardViewDelegate.h:39
FlutterEngine_Internal.h
-[KeyboardTester respondTextInputWith:]
void respondTextInputWith:(BOOL response)
Definition: FlutterKeyboardManagerTest.mm:335
_keyboardLayoutNotifier
flutter::KeyboardLayoutNotifier _keyboardLayoutNotifier
Definition: FlutterKeyboardManagerTest.mm:243
-[KeyboardTester recordEmbedderEventsTo:returning:]
void recordEmbedderEventsTo:returning:(nonnull std::vector< FlutterKeyEvent > *storage,[returning] bool handled)
Definition: FlutterKeyboardManagerTest.mm:308
flutter::testing
Definition: AccessibilityBridgeMacTest.mm:11
flutter::LayoutClue::character
uint32_t character
Definition: FlutterKeyboardViewDelegate.h:22
-[FlutterKeyboardManagerUnittestsObjC getPressedState]
bool getPressedState()
Definition: FlutterKeyboardManagerTest.mm:607
-[KeyboardTester init]
nonnull instancetype init()
Definition: FlutterKeyboardManagerTest.mm:251
FlutterKeyPrimaryResponder.h
FlutterBinaryMessageHandler
void(^ FlutterBinaryMessageHandler)(NSData *_Nullable message, FlutterBinaryReply reply)
Definition: FlutterBinaryMessenger.h:30
-[KeyboardTester recordCallTypesTo:forTypes:]
void recordCallTypesTo:forTypes:(nonnull NSMutableArray< NSNumber * > *typeStorage,[forTypes] uint32_t typeMask)
Definition: FlutterKeyboardManagerTest.mm:341
-[FlutterKeyboardManagerUnittestsObjC racingConditionBetweenKeyAndText]
bool racingConditionBetweenKeyAndText()
Definition: FlutterKeyboardManagerTest.mm:646
-[KeyboardTester respondEmbedderCallsWith:]
void respondEmbedderCallsWith:(BOOL response)
Definition: FlutterKeyboardManagerTest.mm:296
_channelHandler
AsyncKeyCallbackHandler _channelHandler
Definition: FlutterKeyboardManagerTest.mm:235
KeyboardTester::nextResponder
NSResponder * nextResponder
Definition: FlutterKeyboardManagerTest.mm:220
FlutterMethodCall
Definition: FlutterCodecs.h:220
flutter::testing::TEST
TEST(AccessibilityBridgeMacTest, SendsAccessibilityCreateNotificationToWindowOfFlutterView)
Definition: AccessibilityBridgeMacTest.mm:61
-[FlutterKeyboardManagerUnittestsObjC emptyNextResponder]
bool emptyNextResponder()
Definition: FlutterKeyboardManagerTest.mm:594
flutter::KeyboardLayoutNotifier
void(^ KeyboardLayoutNotifier)()
Definition: FlutterKeyboardViewDelegate.h:13
FlutterAsyncKeyCallback
void(^ FlutterAsyncKeyCallback)(BOOL handled)
Definition: FlutterKeyPrimaryResponder.h:7
flutter
Definition: AccessibilityBridgeMac.h:16
-[FlutterKeyboardManagerUnittestsObjC keyboardChannelGetPressedState]
bool keyboardChannelGetPressedState()
Definition: FlutterKeyboardManagerTest.mm:622
_typeStorageMask
uint32_t _typeStorageMask
Definition: FlutterKeyboardManagerTest.mm:241
-[FlutterKeyboardManagerUnittestsObjC singlePrimaryResponder]
bool singlePrimaryResponder()
Definition: FlutterKeyboardManagerTest.mm:466
KeyboardTester::manager
FlutterKeyboardManager * manager
Definition: FlutterKeyboardManagerTest.mm:219
_messengerMock
NSObject< FlutterBinaryMessenger > * _messengerMock
Definition: FlutterKeyboardManagerTest.mm:247
-[KeyboardTester lastKeyboardChannelResult]
id lastKeyboardChannelResult()
Definition: FlutterKeyboardManagerTest.mm:292
_keyboardChannelResult
id _keyboardChannelResult
Definition: FlutterKeyboardManagerTest.mm:246
FlutterKeyboardManager.h
VERIFY_DOWN
#define VERIFY_DOWN(OUT_LOGICAL, OUT_CHAR)
Definition: FlutterKeyboardManagerTest.mm:172
_keyboardHandler
FlutterBinaryMessageHandler _keyboardHandler
Definition: FlutterKeyboardManagerTest.mm:248
-[KeyboardTester respondChannelCallsWith:]
void respondChannelCallsWith:(BOOL response)
Definition: FlutterKeyboardManagerTest.mm:323
FlutterKeyboardManagerUnittestsObjC
Definition: FlutterKeyboardManagerTest.mm:418
FlutterKeyboardManager
Definition: FlutterKeyboardManager.h:24
_textCallback
TextInputCallback _textCallback
Definition: FlutterKeyboardManagerTest.mm:238
FlutterBinaryMessenger-p
Definition: FlutterBinaryMessenger.h:48
-[FlutterKeyboardManagerUnittestsObjC textInputPlugin]
bool textInputPlugin()
Definition: FlutterKeyboardManagerTest.mm:548
FlutterStandardMethodCodec
Definition: FlutterCodecs.h:467
_typeStorage
NSMutableArray< NSNumber * > * _typeStorage
Definition: FlutterKeyboardManagerTest.mm:240
KeyboardTester
Definition: FlutterKeyboardManagerTest.mm:180
FlutterBinaryReply
NS_ASSUME_NONNULL_BEGIN typedef void(^ FlutterBinaryReply)(NSData *_Nullable reply)
-[KeyboardTester sendKeyboardChannelMessage:]
void sendKeyboardChannelMessage:(NSData *_Nullable message)
Definition: FlutterKeyboardManagerTest.mm:347
+[FlutterMessageCodec-p sharedInstance]
instancetype sharedInstance()
-[FlutterKeyboardManagerUnittestsObjC doublePrimaryResponder]
bool doublePrimaryResponder()
Definition: FlutterKeyboardManagerTest.mm:488
FlutterJSONMessageCodec
Definition: FlutterCodecs.h:81
_currentLayout
const MockLayoutData * _currentLayout
Definition: FlutterKeyboardManagerTest.mm:244