Flutter macOS Embedder
FlutterTextInputPluginTest.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 
13 
14 #import <OCMock/OCMock.h>
15 #import "flutter/testing/testing.h"
16 
17 #include <cstdint>
18 #include "flutter/common/constants.h"
19 
21 - (void)setPlatformNode:(flutter::FlutterTextPlatformNode*)node;
22 @end
23 
25 
26 @property(nonatomic, nullable, copy) NSString* lastUpdatedString;
27 @property(nonatomic) NSRange lastUpdatedSelection;
28 
29 @end
30 
31 @implementation FlutterTextFieldMock
32 
33 - (void)updateString:(NSString*)string withSelection:(NSRange)selection {
34  _lastUpdatedString = string;
35  _lastUpdatedSelection = selection;
36 }
37 
38 @end
39 
41 // This is a private method.
42 - (BOOL)isActive;
43 @end
44 
46 @end
47 
48 @implementation TextInputTestViewController {
49  FlutterView* _flutterViewMock;
50 }
51 
52 - (void)loadView {
53  self.view = [[NSView alloc] initWithFrame:NSZeroRect];
54  _flutterViewMock = OCMClassMock([FlutterView class]);
55 }
56 
57 - (FlutterView*)flutterView {
58  return _flutterViewMock;
59 }
60 @end
61 
62 // Matches an encoded JSON message whose content equals that of |expected|.
63 //
64 // The encoded bytes are not comparable directly: clang emits constant
65 // dictionaries for literals, which order their keys differently to the
66 // dictionaries the plugins build at runtime.
67 static id JSONMessageWithContentOf(NSData* expected) {
68  id expectedContent = [[FlutterJSONMessageCodec sharedInstance] decode:expected];
69  return [OCMArg checkWithBlock:^BOOL(NSData* message) {
70  return [[[FlutterJSONMessageCodec sharedInstance] decode:message] isEqual:expectedContent];
71  }];
72 }
73 
74 @interface FlutterInputPluginTestObjc : NSObject
77 @end
78 
80  id<FlutterBinaryMessenger> _binaryMessenger;
83 }
84 
85 @end
86 
88 
89 static const FlutterViewIdentifier kViewId = 1;
90 
91 @synthesize binaryMessenger = _binaryMessenger;
92 
93 - (instancetype)initWithBinaryMessenger:(id<FlutterBinaryMessenger>)messenger
94  viewController:(FlutterViewController*)viewController {
95  self = [super init];
96  if (self) {
97  _binaryMessenger = messenger;
98  _viewController = viewController;
99  }
100  return self;
101 }
102 
103 - (instancetype)initWithBinaryMessenger:(id<FlutterBinaryMessenger>)messenger
104  implicitViewController:(FlutterViewController*)viewController {
105  self = [super init];
106  if (self) {
107  _binaryMessenger = messenger;
108  _implicitViewController = viewController;
109  }
110  return self;
111 }
112 
113 - (nullable FlutterViewController*)viewControllerForIdentifier:
114  (FlutterViewIdentifier)viewIdentifier {
115  if (viewIdentifier == kViewId) {
116  return _viewController;
117  } else if (viewIdentifier == flutter::kFlutterImplicitViewId) {
119  } else {
120  return nil;
121  }
122 }
123 
124 @end
125 
126 @implementation FlutterInputPluginTestObjc
127 
129  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
130  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
131  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
132  [engineMock binaryMessenger])
133  .andReturn(binaryMessengerMock);
134 
135  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
136  nibName:@""
137  bundle:nil];
138 
140  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
141  viewController:viewController];
142 
143  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
144 
145  NSDictionary* setClientConfig = @{
146  @"viewId" : @(kViewId),
147  @"inputAction" : @"action",
148  @"inputType" : @{@"name" : @"inputName"},
149  };
150  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
151  arguments:@[ @(1), setClientConfig ]]
152  result:^(id){
153  }];
154 
155  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
156  arguments:@{
157  @"text" : @"Text",
158  @"selectionBase" : @(0),
159  @"selectionExtent" : @(0),
160  @"composingBase" : @(-1),
161  @"composingExtent" : @(-1),
162  }];
163 
164  [plugin handleMethodCall:call
165  result:^(id){
166  }];
167 
168  // Verify editing state was set.
169  NSDictionary* editingState = [plugin editingState];
170  EXPECT_STREQ([editingState[@"text"] UTF8String], "Text");
171  EXPECT_STREQ([editingState[@"selectionAffinity"] UTF8String], "TextAffinity.upstream");
172  EXPECT_FALSE([editingState[@"selectionIsDirectional"] boolValue]);
173  EXPECT_EQ([editingState[@"selectionBase"] intValue], 0);
174  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 0);
175  EXPECT_EQ([editingState[@"composingBase"] intValue], -1);
176  EXPECT_EQ([editingState[@"composingExtent"] intValue], -1);
177  return true;
178 }
179 
180 - (bool)testSetMarkedTextWithSelectionChange {
181  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
182  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
183  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
184  [engineMock binaryMessenger])
185  .andReturn(binaryMessengerMock);
186 
187  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
188  nibName:@""
189  bundle:nil];
190 
192  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
193  viewController:viewController];
194 
195  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
196 
197  NSDictionary* setClientConfig = @{
198  @"viewId" : @(kViewId),
199  @"inputAction" : @"action",
200  @"inputType" : @{@"name" : @"inputName"},
201  };
202  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
203  arguments:@[ @(1), setClientConfig ]]
204  result:^(id){
205  }];
206 
207  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
208  arguments:@{
209  @"text" : @"Text",
210  @"selectionBase" : @(4),
211  @"selectionExtent" : @(4),
212  @"composingBase" : @(-1),
213  @"composingExtent" : @(-1),
214  }];
215  [plugin handleMethodCall:call
216  result:^(id){
217  }];
218 
219  [plugin setMarkedText:@"marked"
220  selectedRange:NSMakeRange(1, 0)
221  replacementRange:NSMakeRange(NSNotFound, 0)];
222 
223  NSDictionary* expectedState = @{
224  @"selectionBase" : @(5),
225  @"selectionExtent" : @(5),
226  @"selectionAffinity" : @"TextAffinity.upstream",
227  @"selectionIsDirectional" : @(NO),
228  @"composingBase" : @(4),
229  @"composingExtent" : @(10),
230  @"text" : @"Textmarked",
231  };
232 
233  NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
234  encodeMethodCall:[FlutterMethodCall
235  methodCallWithMethodName:@"TextInputClient.updateEditingState"
236  arguments:@[ @(1), expectedState ]]];
237 
238  OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
239  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
240  message:JSONMessageWithContentOf(updateCall)]);
241 
242  @try {
243  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
244  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
245  message:JSONMessageWithContentOf(updateCall)]);
246  } @catch (...) {
247  return false;
248  }
249  return true;
250 }
251 
252 - (bool)testSetMarkedTextWithReplacementRange {
253  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
254  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
255  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
256  [engineMock binaryMessenger])
257  .andReturn(binaryMessengerMock);
258 
259  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
260  nibName:@""
261  bundle:nil];
262 
264  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
265  viewController:viewController];
266 
267  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
268 
269  NSDictionary* setClientConfig = @{
270  @"viewId" : @(kViewId),
271  @"inputAction" : @"action",
272  @"inputType" : @{@"name" : @"inputName"},
273  };
274  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
275  arguments:@[ @(1), setClientConfig ]]
276  result:^(id){
277  }];
278 
279  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
280  arguments:@{
281  @"text" : @"1234",
282  @"selectionBase" : @(3),
283  @"selectionExtent" : @(3),
284  @"composingBase" : @(-1),
285  @"composingExtent" : @(-1),
286  }];
287  [plugin handleMethodCall:call
288  result:^(id){
289  }];
290 
291  [plugin setMarkedText:@"marked"
292  selectedRange:NSMakeRange(1, 0)
293  replacementRange:NSMakeRange(1, 2)];
294 
295  NSDictionary* expectedState = @{
296  @"selectionBase" : @(2),
297  @"selectionExtent" : @(2),
298  @"selectionAffinity" : @"TextAffinity.upstream",
299  @"selectionIsDirectional" : @(NO),
300  @"composingBase" : @(1),
301  @"composingExtent" : @(7),
302  @"text" : @"1marked4",
303  };
304 
305  NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
306  encodeMethodCall:[FlutterMethodCall
307  methodCallWithMethodName:@"TextInputClient.updateEditingState"
308  arguments:@[ @(1), expectedState ]]];
309 
310  OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
311  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
312  message:JSONMessageWithContentOf(updateCall)]);
313 
314  @try {
315  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
316  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
317  message:JSONMessageWithContentOf(updateCall)]);
318  } @catch (...) {
319  return false;
320  }
321  return true;
322 }
323 
324 - (bool)testSetMarkedTextWithSelectedRangeNotFound {
325  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
326  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
327  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
328  [engineMock binaryMessenger])
329  .andReturn(binaryMessengerMock);
330 
331  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
332  nibName:@""
333  bundle:nil];
334 
336  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
337  viewController:viewController];
338 
339  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
340 
341  NSDictionary* setClientConfig = @{
342  @"viewId" : @(kViewId),
343  @"inputAction" : @"action",
344  @"inputType" : @{@"name" : @"inputName"},
345  };
346  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
347  arguments:@[ @(1), setClientConfig ]]
348  result:^(id){
349  }];
350 
351  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
352  arguments:@{
353  @"text" : @"Text",
354  @"selectionBase" : @(4),
355  @"selectionExtent" : @(4),
356  @"composingBase" : @(-1),
357  @"composingExtent" : @(-1),
358  }];
359  [plugin handleMethodCall:call
360  result:^(id){
361  }];
362 
363  [plugin setMarkedText:@"marked"
364  selectedRange:NSMakeRange(1, 0)
365  replacementRange:NSMakeRange(NSNotFound, 0)];
366 
367  // IMEs report no selection within the marked text by passing NSNotFound,
368  // which is not a valid offset. Clearing the marked text and marking again is
369  // the sequence an input method produces when the input source is switched
370  // mid-composing.
371  [plugin setMarkedText:@""
372  selectedRange:NSMakeRange(NSNotFound, 0)
373  replacementRange:NSMakeRange(NSNotFound, 0)];
374  [plugin setMarkedText:@"n"
375  selectedRange:NSMakeRange(NSNotFound, 0)
376  replacementRange:NSMakeRange(NSNotFound, 0)];
377 
378  // A missing selection places the caret at the end of the marked text.
379  NSDictionary* editingState = [plugin editingState];
380  EXPECT_STREQ([editingState[@"text"] UTF8String], "Textn");
381  EXPECT_EQ([editingState[@"selectionBase"] intValue], 5);
382  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 5);
383  EXPECT_EQ([editingState[@"composingBase"] intValue], 4);
384  EXPECT_EQ([editingState[@"composingExtent"] intValue], 5);
385  return true;
386 }
387 
388 - (bool)testSetMarkedTextWithOutOfBoundsRanges {
389  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
390  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
391  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
392  [engineMock binaryMessenger])
393  .andReturn(binaryMessengerMock);
394 
395  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
396  nibName:@""
397  bundle:nil];
398 
400  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
401  viewController:viewController];
402 
403  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
404 
405  NSDictionary* setClientConfig = @{
406  @"viewId" : @(kViewId),
407  @"inputAction" : @"action",
408  @"inputType" : @{@"name" : @"inputName"},
409  };
410  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
411  arguments:@[ @(1), setClientConfig ]]
412  result:^(id){
413  }];
414 
415  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
416  arguments:@{
417  @"text" : @"1234",
418  @"selectionBase" : @(3),
419  @"selectionExtent" : @(3),
420  @"composingBase" : @(-1),
421  @"composingExtent" : @(-1),
422  }];
423  [plugin handleMethodCall:call
424  result:^(id){
425  }];
426 
427  // Both ranges are computed by the input method against its own version of the
428  // text, which can lag behind the engine's. Each is clamped to what actually
429  // exists rather than dropped.
430  [plugin setMarkedText:@"marked"
431  selectedRange:NSMakeRange(100, 20)
432  replacementRange:NSMakeRange(10, 2)];
433 
434  NSDictionary* editingState = [plugin editingState];
435  EXPECT_STREQ([editingState[@"text"] UTF8String], "1234marked");
436  EXPECT_EQ([editingState[@"selectionBase"] intValue], 10);
437  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 10);
438  EXPECT_EQ([editingState[@"composingBase"] intValue], 4);
439  EXPECT_EQ([editingState[@"composingExtent"] intValue], 10);
440  return true;
441 }
442 
443 - (bool)testSetEditingStateWithOutOfBoundsSelection {
444  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
445  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
446  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
447  [engineMock binaryMessenger])
448  .andReturn(binaryMessengerMock);
449 
450  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
451  nibName:@""
452  bundle:nil];
453 
455  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
456  viewController:viewController];
457 
458  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
459 
460  NSDictionary* setClientConfig = @{
461  @"viewId" : @(kViewId),
462  @"inputAction" : @"action",
463  @"inputType" : @{@"name" : @"inputName"},
464  };
465  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
466  arguments:@[ @(1), setClientConfig ]]
467  result:^(id){
468  }];
469 
470  // The framework computes the selection against its own copy of the text and
471  // may send one that does not fit the text accompanying it. The text must
472  // still be applied, with the selection clamped to it, because a selection
473  // that outlives the text it was measured against would be carried into the
474  // next edit as an index into the text buffer.
475  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
476  arguments:@{
477  @"text" : @"Te",
478  @"selectionBase" : @(4),
479  @"selectionExtent" : @(4),
480  @"composingBase" : @(-1),
481  @"composingExtent" : @(-1),
482  }];
483  [plugin handleMethodCall:call
484  result:^(id){
485  }];
486 
487  NSDictionary* editingState = [plugin editingState];
488  EXPECT_STREQ([editingState[@"text"] UTF8String], "Te");
489  EXPECT_EQ([editingState[@"selectionBase"] intValue], 2);
490  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 2);
491 
492  [plugin setMarkedText:@"m" selectedRange:NSMakeRange(1, 0)];
493 
494  editingState = [plugin editingState];
495  EXPECT_STREQ([editingState[@"text"] UTF8String], "Tem");
496  EXPECT_EQ([editingState[@"selectionBase"] intValue], 3);
497  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 3);
498  EXPECT_EQ([editingState[@"composingBase"] intValue], 2);
499  EXPECT_EQ([editingState[@"composingExtent"] intValue], 3);
500  return true;
501 }
502 
503 - (bool)testComposingRegionRemovedByFramework {
504  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
505  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
506  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
507  [engineMock binaryMessenger])
508  .andReturn(binaryMessengerMock);
509 
510  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
511  nibName:@""
512  bundle:nil];
513 
515  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
516  viewController:viewController];
517 
518  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
519 
520  NSDictionary* setClientConfig = @{
521  @"viewId" : @(kViewId),
522  @"inputAction" : @"action",
523  @"inputType" : @{@"name" : @"inputName"},
524  };
525  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
526  arguments:@[ @(1), setClientConfig ]]
527  result:^(id){
528  }];
529 
530  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
531  arguments:@{
532  @"text" : @"Text",
533  @"selectionBase" : @(4),
534  @"selectionExtent" : @(4),
535  @"composingBase" : @(2),
536  @"composingExtent" : @(4),
537  }];
538  [plugin handleMethodCall:call
539  result:^(id){
540  }];
541 
542  // Update with the composing region removed.
543  call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
544  arguments:@{
545  @"text" : @"Te",
546  @"selectionBase" : @(2),
547  @"selectionExtent" : @(2),
548  @"composingBase" : @(-1),
549  @"composingExtent" : @(-1),
550  }];
551  [plugin handleMethodCall:call
552  result:^(id){
553  }];
554 
555  // Verify editing state was set.
556  NSDictionary* editingState = [plugin editingState];
557  EXPECT_STREQ([editingState[@"text"] UTF8String], "Te");
558  EXPECT_STREQ([editingState[@"selectionAffinity"] UTF8String], "TextAffinity.upstream");
559  EXPECT_FALSE([editingState[@"selectionIsDirectional"] boolValue]);
560  EXPECT_EQ([editingState[@"selectionBase"] intValue], 2);
561  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 2);
562  EXPECT_EQ([editingState[@"composingBase"] intValue], -1);
563  EXPECT_EQ([editingState[@"composingExtent"] intValue], -1);
564  return true;
565 }
566 
568  // Set up FlutterTextInputPlugin.
569  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
570  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
571  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
572  [engineMock binaryMessenger])
573  .andReturn(binaryMessengerMock);
574  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
575  nibName:@""
576  bundle:nil];
578  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
579  viewController:viewController];
580 
581  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
582 
583  // Set input client 1.
584  NSDictionary* setClientConfig = @{
585  @"viewId" : @(kViewId),
586  @"inputAction" : @"action",
587  @"inputType" : @{@"name" : @"inputName"},
588  };
589  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
590  arguments:@[ @(1), setClientConfig ]]
591  result:^(id){
592  }];
593 
594  // Set editing state with an active composing range.
595  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
596  arguments:@{
597  @"text" : @"Text",
598  @"selectionBase" : @(0),
599  @"selectionExtent" : @(0),
600  @"composingBase" : @(0),
601  @"composingExtent" : @(1),
602  }]
603  result:^(id){
604  }];
605 
606  // Verify composing range is (0, 1).
607  NSDictionary* editingState = [plugin editingState];
608  EXPECT_EQ([editingState[@"composingBase"] intValue], 0);
609  EXPECT_EQ([editingState[@"composingExtent"] intValue], 1);
610 
611  // Clear input client.
612  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.clearClient"
613  arguments:@[]]
614  result:^(id){
615  }];
616 
617  // Verify composing range is collapsed.
618  editingState = [plugin editingState];
619  EXPECT_EQ([editingState[@"composingBase"] intValue], [editingState[@"composingExtent"] intValue]);
620  return true;
621 }
622 
623 - (bool)testAutocompleteDisabledWhenAutofillNotSet {
624  // Set up FlutterTextInputPlugin.
625  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
626  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
627  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
628  [engineMock binaryMessenger])
629  .andReturn(binaryMessengerMock);
630  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
631  nibName:@""
632  bundle:nil];
634  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
635  viewController:viewController];
636 
637  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
638 
639  // Set input client 1.
640  NSDictionary* setClientConfig = @{
641  @"viewId" : @(kViewId),
642  @"inputAction" : @"action",
643  @"inputType" : @{@"name" : @"inputName"},
644  };
645  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
646  arguments:@[ @(1), setClientConfig ]]
647  result:^(id){
648  }];
649 
650  // Verify autocomplete is disabled.
651  EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
652  return true;
653 }
654 
655 - (bool)testAutocompleteEnabledWhenAutofillSet {
656  // Set up FlutterTextInputPlugin.
657  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
658  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
659  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
660  [engineMock binaryMessenger])
661  .andReturn(binaryMessengerMock);
662  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
663  nibName:@""
664  bundle:nil];
666  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
667  viewController:viewController];
668 
669  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
670 
671  // Set input client 1.
672  NSDictionary* setClientConfig = @{
673  @"viewId" : @(kViewId),
674  @"inputAction" : @"action",
675  @"inputType" : @{@"name" : @"inputName"},
676  @"autofill" : @{
677  @"uniqueIdentifier" : @"field1",
678  @"hints" : @[ @"name" ],
679  @"editingValue" : @{@"text" : @""},
680  }
681  };
682  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
683  arguments:@[ @(1), setClientConfig ]]
684  result:^(id){
685  }];
686 
687  // Verify autocomplete is enabled.
688  EXPECT_TRUE([plugin isAutomaticTextCompletionEnabled]);
689 
690  // Verify content type is nil for unsupported content types.
691  if (@available(macOS 11.0, *)) {
692  EXPECT_EQ([plugin contentType], nil);
693  }
694  return true;
695 }
696 
697 - (bool)testAutocompleteEnabledWhenAutofillSetNoHint {
698  // Set up FlutterTextInputPlugin.
699  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
700  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
701  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
702  [engineMock binaryMessenger])
703  .andReturn(binaryMessengerMock);
704  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
705  nibName:@""
706  bundle:nil];
708  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
709  viewController:viewController];
710 
711  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
712 
713  // Set input client 1.
714  NSDictionary* setClientConfig = @{
715  @"viewId" : @(kViewId),
716  @"inputAction" : @"action",
717  @"inputType" : @{@"name" : @"inputName"},
718  @"autofill" : @{
719  @"uniqueIdentifier" : @"field1",
720  @"hints" : @[],
721  @"editingValue" : @{@"text" : @""},
722  }
723  };
724  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
725  arguments:@[ @(1), setClientConfig ]]
726  result:^(id){
727  }];
728 
729  // Verify autocomplete is enabled.
730  EXPECT_TRUE([plugin isAutomaticTextCompletionEnabled]);
731  return true;
732 }
733 
734 - (bool)testAutocompleteDisabledWhenObscureTextSet {
735  // Set up FlutterTextInputPlugin.
736  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
737  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
738  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
739  [engineMock binaryMessenger])
740  .andReturn(binaryMessengerMock);
741  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
742  nibName:@""
743  bundle:nil];
745  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
746  viewController:viewController];
747 
748  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
749 
750  // Set input client 1.
751  NSDictionary* setClientConfig = @{
752  @"viewId" : @(kViewId),
753  @"inputAction" : @"action",
754  @"inputType" : @{@"name" : @"inputName"},
755  @"obscureText" : @YES,
756  @"autofill" : @{
757  @"uniqueIdentifier" : @"field1",
758  @"editingValue" : @{@"text" : @""},
759  }
760  };
761  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
762  arguments:@[ @(1), setClientConfig ]]
763  result:^(id){
764  }];
765 
766  // Verify autocomplete is disabled.
767  EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
768  return true;
769 }
770 
771 - (bool)testAutocompleteDisabledWhenPasswordAutofillSet {
772  // Set up FlutterTextInputPlugin.
773  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
774  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
775  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
776  [engineMock binaryMessenger])
777  .andReturn(binaryMessengerMock);
778  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
779  nibName:@""
780  bundle:nil];
782  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
783  viewController:viewController];
784 
785  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
786 
787  // Set input client 1.
788  NSDictionary* setClientConfig = @{
789  @"viewId" : @(kViewId),
790  @"inputAction" : @"action",
791  @"inputType" : @{@"name" : @"inputName"},
792  @"autofill" : @{
793  @"uniqueIdentifier" : @"field1",
794  @"hints" : @[ @"password" ],
795  @"editingValue" : @{@"text" : @""},
796  }
797  };
798  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
799  arguments:@[ @(1), setClientConfig ]]
800  result:^(id){
801  }];
802 
803  // Verify autocomplete is disabled.
804  EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
805 
806  // Verify content type is password.
807  if (@available(macOS 11.0, *)) {
808  EXPECT_EQ([plugin contentType], NSTextContentTypePassword);
809  }
810  return true;
811 }
812 
813 - (bool)testAutocompleteDisabledWhenAutofillGroupIncludesPassword {
814  // Set up FlutterTextInputPlugin.
815  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
816  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
817  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
818  [engineMock binaryMessenger])
819  .andReturn(binaryMessengerMock);
820  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
821  nibName:@""
822  bundle:nil];
824  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
825  viewController:viewController];
826 
827  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
828 
829  // Set input client 1.
830  NSDictionary* setClientConfig = @{
831  @"viewId" : @(kViewId),
832  @"inputAction" : @"action",
833  @"inputType" : @{@"name" : @"inputName"},
834  @"fields" : @[
835  @{
836  @"inputAction" : @"action",
837  @"inputType" : @{@"name" : @"inputName"},
838  @"autofill" : @{
839  @"uniqueIdentifier" : @"field1",
840  @"hints" : @[ @"password" ],
841  @"editingValue" : @{@"text" : @""},
842  }
843  },
844  @{
845  @"inputAction" : @"action",
846  @"inputType" : @{@"name" : @"inputName"},
847  @"autofill" : @{
848  @"uniqueIdentifier" : @"field2",
849  @"hints" : @[ @"name" ],
850  @"editingValue" : @{@"text" : @""},
851  }
852  }
853  ]
854  };
855  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
856  arguments:@[ @(1), setClientConfig ]]
857  result:^(id){
858  }];
859 
860  // Verify autocomplete is disabled.
861  EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
862  return true;
863 }
864 
865 - (bool)testContentTypeWhenAutofillTypeIsUsername {
866  // Set up FlutterTextInputPlugin.
867  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
868  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
869  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
870  [engineMock binaryMessenger])
871  .andReturn(binaryMessengerMock);
872  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
873  nibName:@""
874  bundle:nil];
876  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
877  viewController:viewController];
878 
879  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
880 
881  // Set input client 1.
882  NSDictionary* setClientConfig = @{
883  @"viewId" : @(kViewId),
884  @"inputAction" : @"action",
885  @"inputType" : @{@"name" : @"inputName"},
886  @"autofill" : @{
887  @"uniqueIdentifier" : @"field1",
888  @"hints" : @[ @"name" ],
889  @"editingValue" : @{@"text" : @""},
890  }
891  };
892  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
893  arguments:@[ @(1), setClientConfig ]]
894  result:^(id){
895  }];
896 
897  // Verify autocomplete is disabled.
898  EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
899 
900  // Verify content type is username.
901  if (@available(macOS 11.0, *)) {
902  EXPECT_EQ([plugin contentType], NSTextContentTypeUsername);
903  }
904  return true;
905 }
906 
907 - (bool)testContentTypeWhenAutofillTypeIsOneTimeCode {
908  // Set up FlutterTextInputPlugin.
909  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
910  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
911  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
912  [engineMock binaryMessenger])
913  .andReturn(binaryMessengerMock);
914  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
915  nibName:@""
916  bundle:nil];
918  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
919  viewController:viewController];
920 
921  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
922 
923  // Set input client 1.
924  NSDictionary* setClientConfig = @{
925  @"viewId" : @(kViewId),
926  @"inputAction" : @"action",
927  @"inputType" : @{@"name" : @"inputName"},
928  @"autofill" : @{
929  @"uniqueIdentifier" : @"field1",
930  @"hints" : @[ @"oneTimeCode" ],
931  @"editingValue" : @{@"text" : @""},
932  }
933  };
934  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
935  arguments:@[ @(1), setClientConfig ]]
936  result:^(id){
937  }];
938 
939  // Verify autocomplete is disabled.
940  EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
941 
942  // Verify content type is username.
943  if (@available(macOS 11.0, *)) {
944  EXPECT_EQ([plugin contentType], NSTextContentTypeOneTimeCode);
945  }
946  return true;
947 }
948 
949 - (bool)testFirstRectForCharacterRange {
950  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
951  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
952  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
953  [engineMock binaryMessenger])
954  .andReturn(binaryMessengerMock);
955  FlutterViewController* controllerMock =
956  [[TextInputTestViewController alloc] initWithEngine:engineMock nibName:nil bundle:nil];
957  [controllerMock loadView];
958  id viewMock = controllerMock.flutterView;
959  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
960  [viewMock bounds])
961  .andReturn(NSMakeRect(0, 0, 200, 200));
962 
963  id windowMock = OCMClassMock([NSWindow class]);
964  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
965  [viewMock window])
966  .andReturn(windowMock);
967 
968  OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
969  [viewMock convertRect:NSMakeRect(28, 10, 2, 19) toView:nil])
970  .andReturn(NSMakeRect(28, 10, 2, 19));
971 
972  OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
973  [windowMock convertRectToScreen:NSMakeRect(28, 10, 2, 19)])
974  .andReturn(NSMakeRect(38, 20, 2, 19));
975 
977  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
978  viewController:controllerMock];
979 
980  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
981 
982  NSDictionary* setClientConfig = @{
983  @"viewId" : @(kViewId),
984  };
985  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
986  arguments:@[ @(1), setClientConfig ]]
987  result:^(id){
988  }];
989 
991  methodCallWithMethodName:@"TextInput.setEditableSizeAndTransform"
992  arguments:@{
993  @"height" : @(20.0),
994  @"transform" : @[
995  @(1.0), @(0.0), @(0.0), @(0.0), @(0.0), @(1.0), @(0.0), @(0.0), @(0.0),
996  @(0.0), @(1.0), @(0.0), @(20.0), @(10.0), @(0.0), @(1.0)
997  ],
998  @"width" : @(400.0),
999  }];
1000 
1001  [plugin handleMethodCall:call
1002  result:^(id){
1003  }];
1004 
1005  call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setCaretRect"
1006  arguments:@{
1007  @"height" : @(19.0),
1008  @"width" : @(2.0),
1009  @"x" : @(8.0),
1010  @"y" : @(0.0),
1011  }];
1012 
1013  [plugin handleMethodCall:call
1014  result:^(id){
1015  }];
1016 
1017  NSRect rect = [plugin firstRectForCharacterRange:NSMakeRange(0, 0) actualRange:nullptr];
1018  @try {
1019  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1020  [windowMock convertRectToScreen:NSMakeRect(28, 10, 2, 19)]);
1021  } @catch (...) {
1022  return false;
1023  }
1024 
1025  return NSEqualRects(rect, NSMakeRect(38, 20, 2, 19));
1026 }
1027 
1028 - (bool)testFirstRectForCharacterRangeAtInfinity {
1029  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1030  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1031  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1032  [engineMock binaryMessenger])
1033  .andReturn(binaryMessengerMock);
1034  FlutterViewController* controllerMock =
1035  [[TextInputTestViewController alloc] initWithEngine:engineMock nibName:nil bundle:nil];
1036  [controllerMock loadView];
1037  id viewMock = controllerMock.flutterView;
1038  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1039  [viewMock bounds])
1040  .andReturn(NSMakeRect(0, 0, 200, 200));
1041 
1042  id windowMock = OCMClassMock([NSWindow class]);
1043  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1044  [viewMock window])
1045  .andReturn(windowMock);
1046 
1048  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1049  viewController:controllerMock];
1050 
1051  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1052 
1053  NSDictionary* setClientConfig = @{
1054  @"viewId" : @(kViewId),
1055  };
1056  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1057  arguments:@[ @(1), setClientConfig ]]
1058  result:^(id){
1059  }];
1060 
1062  methodCallWithMethodName:@"TextInput.setEditableSizeAndTransform"
1063  arguments:@{
1064  @"height" : @(20.0),
1065  // Projects all points to infinity.
1066  @"transform" : @[
1067  @(1.0), @(0.0), @(0.0), @(0.0), @(0.0), @(1.0), @(0.0), @(0.0), @(0.0),
1068  @(0.0), @(1.0), @(0.0), @(20.0), @(10.0), @(0.0), @(0.0)
1069  ],
1070  @"width" : @(400.0),
1071  }];
1072 
1073  [plugin handleMethodCall:call
1074  result:^(id){
1075  }];
1076 
1077  call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setCaretRect"
1078  arguments:@{
1079  @"height" : @(19.0),
1080  @"width" : @(2.0),
1081  @"x" : @(8.0),
1082  @"y" : @(0.0),
1083  }];
1084 
1085  [plugin handleMethodCall:call
1086  result:^(id){
1087  }];
1088 
1089  NSRect rect = [plugin firstRectForCharacterRange:NSMakeRange(0, 0) actualRange:nullptr];
1090  return NSEqualRects(rect, CGRectZero);
1091 }
1092 
1093 - (bool)testFirstRectForCharacterRangeWithEsotericAffineTransform {
1094  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1095  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1096  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1097  [engineMock binaryMessenger])
1098  .andReturn(binaryMessengerMock);
1099  FlutterViewController* controllerMock =
1100  [[TextInputTestViewController alloc] initWithEngine:engineMock nibName:nil bundle:nil];
1101  [controllerMock loadView];
1102  id viewMock = controllerMock.flutterView;
1103  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1104  [viewMock bounds])
1105  .andReturn(NSMakeRect(0, 0, 200, 200));
1106 
1107  id windowMock = OCMClassMock([NSWindow class]);
1108  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1109  [viewMock window])
1110  .andReturn(windowMock);
1111 
1112  OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
1113  [viewMock convertRect:NSMakeRect(-18, 6, 3, 3) toView:nil])
1114  .andReturn(NSMakeRect(-18, 6, 3, 3));
1115 
1116  OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
1117  [windowMock convertRectToScreen:NSMakeRect(-18, 6, 3, 3)])
1118  .andReturn(NSMakeRect(-18, 6, 3, 3));
1119 
1121  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1122  viewController:controllerMock];
1123 
1124  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1125 
1126  NSDictionary* setClientConfig = @{
1127  @"viewId" : @(kViewId),
1128  };
1129  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1130  arguments:@[ @(1), setClientConfig ]]
1131  result:^(id){
1132  }];
1133 
1135  methodCallWithMethodName:@"TextInput.setEditableSizeAndTransform"
1136  arguments:@{
1137  @"height" : @(20.0),
1138  // This matrix can be generated by running this dart code snippet:
1139  // Matrix4.identity()..scale(3.0)..rotateZ(math.pi/2)..translate(1.0, 2.0,
1140  // 3.0);
1141  @"transform" : @[
1142  @(0.0), @(3.0), @(0.0), @(0.0), @(-3.0), @(0.0), @(0.0), @(0.0), @(0.0),
1143  @(0.0), @(3.0), @(0.0), @(-6.0), @(3.0), @(9.0), @(1.0)
1144  ],
1145  @"width" : @(400.0),
1146  }];
1147 
1148  [plugin handleMethodCall:call
1149  result:^(id){
1150  }];
1151 
1152  call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setCaretRect"
1153  arguments:@{
1154  @"height" : @(1.0),
1155  @"width" : @(1.0),
1156  @"x" : @(1.0),
1157  @"y" : @(3.0),
1158  }];
1159 
1160  [plugin handleMethodCall:call
1161  result:^(id){
1162  }];
1163 
1164  NSRect rect = [plugin firstRectForCharacterRange:NSMakeRange(0, 0) actualRange:nullptr];
1165 
1166  @try {
1167  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1168  [windowMock convertRectToScreen:NSMakeRect(-18, 6, 3, 3)]);
1169  } @catch (...) {
1170  return false;
1171  }
1172 
1173  return NSEqualRects(rect, NSMakeRect(-18, 6, 3, 3));
1174 }
1175 
1176 - (bool)testSetEditingStateWithTextEditingDelta {
1177  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1178  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1179  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1180  [engineMock binaryMessenger])
1181  .andReturn(binaryMessengerMock);
1182 
1183  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
1184  nibName:@""
1185  bundle:nil];
1186 
1188  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1189  viewController:viewController];
1190 
1191  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1192 
1193  NSDictionary* setClientConfig = @{
1194  @"viewId" : @(kViewId),
1195  @"inputAction" : @"action",
1196  @"enableDeltaModel" : @"true",
1197  @"inputType" : @{@"name" : @"inputName"},
1198  };
1199  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1200  arguments:@[ @(1), setClientConfig ]]
1201  result:^(id){
1202  }];
1203 
1204  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
1205  arguments:@{
1206  @"text" : @"Text",
1207  @"selectionBase" : @(0),
1208  @"selectionExtent" : @(0),
1209  @"composingBase" : @(-1),
1210  @"composingExtent" : @(-1),
1211  }];
1212 
1213  [plugin handleMethodCall:call
1214  result:^(id){
1215  }];
1216 
1217  // Verify editing state was set.
1218  NSDictionary* editingState = [plugin editingState];
1219  EXPECT_STREQ([editingState[@"text"] UTF8String], "Text");
1220  EXPECT_STREQ([editingState[@"selectionAffinity"] UTF8String], "TextAffinity.upstream");
1221  EXPECT_FALSE([editingState[@"selectionIsDirectional"] boolValue]);
1222  EXPECT_EQ([editingState[@"selectionBase"] intValue], 0);
1223  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 0);
1224  EXPECT_EQ([editingState[@"composingBase"] intValue], -1);
1225  EXPECT_EQ([editingState[@"composingExtent"] intValue], -1);
1226  return true;
1227 }
1228 
1229 - (bool)testOperationsThatTriggerDelta {
1230  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1231  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1232  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1233  [engineMock binaryMessenger])
1234  .andReturn(binaryMessengerMock);
1235 
1236  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
1237  nibName:@""
1238  bundle:nil];
1239 
1241  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1242  viewController:viewController];
1243 
1244  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1245 
1246  NSDictionary* setClientConfig = @{
1247  @"viewId" : @(kViewId),
1248  @"inputAction" : @"action",
1249  @"enableDeltaModel" : @"true",
1250  @"inputType" : @{@"name" : @"inputName"},
1251  };
1252  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1253  arguments:@[ @(1), setClientConfig ]]
1254  result:^(id){
1255  }];
1256  [plugin insertText:@"text to insert"];
1257 
1258  NSDictionary* deltaToFramework = @{
1259  @"oldText" : @"",
1260  @"deltaText" : @"text to insert",
1261  @"deltaStart" : @(0),
1262  @"deltaEnd" : @(0),
1263  @"selectionBase" : @(14),
1264  @"selectionExtent" : @(14),
1265  @"selectionAffinity" : @"TextAffinity.upstream",
1266  @"selectionIsDirectional" : @(false),
1267  @"composingBase" : @(-1),
1268  @"composingExtent" : @(-1),
1269  };
1270  NSDictionary* expectedState = @{
1271  @"deltas" : @[ deltaToFramework ],
1272  };
1273 
1274  NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
1275  encodeMethodCall:[FlutterMethodCall
1276  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1277  arguments:@[ @(1), expectedState ]]];
1278 
1279  @try {
1280  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1281  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1282  message:JSONMessageWithContentOf(updateCall)]);
1283  } @catch (...) {
1284  return false;
1285  }
1286 
1287  [plugin setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
1288 
1289  deltaToFramework = @{
1290  @"oldText" : @"text to insert",
1291  @"deltaText" : @"marked text",
1292  @"deltaStart" : @(14),
1293  @"deltaEnd" : @(14),
1294  @"selectionBase" : @(14),
1295  @"selectionExtent" : @(15),
1296  @"selectionAffinity" : @"TextAffinity.upstream",
1297  @"selectionIsDirectional" : @(false),
1298  @"composingBase" : @(14),
1299  @"composingExtent" : @(25),
1300  };
1301  expectedState = @{
1302  @"deltas" : @[ deltaToFramework ],
1303  };
1304 
1305  updateCall = [[FlutterJSONMethodCodec sharedInstance]
1306  encodeMethodCall:[FlutterMethodCall
1307  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1308  arguments:@[ @(1), expectedState ]]];
1309 
1310  @try {
1311  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1312  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1313  message:JSONMessageWithContentOf(updateCall)]);
1314  } @catch (...) {
1315  return false;
1316  }
1317 
1318  [plugin unmarkText];
1319 
1320  deltaToFramework = @{
1321  @"oldText" : @"text to insertmarked text",
1322  @"deltaText" : @"",
1323  @"deltaStart" : @(-1),
1324  @"deltaEnd" : @(-1),
1325  @"selectionBase" : @(25),
1326  @"selectionExtent" : @(25),
1327  @"selectionAffinity" : @"TextAffinity.upstream",
1328  @"selectionIsDirectional" : @(false),
1329  @"composingBase" : @(-1),
1330  @"composingExtent" : @(-1),
1331  };
1332  expectedState = @{
1333  @"deltas" : @[ deltaToFramework ],
1334  };
1335 
1336  updateCall = [[FlutterJSONMethodCodec sharedInstance]
1337  encodeMethodCall:[FlutterMethodCall
1338  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1339  arguments:@[ @(1), expectedState ]]];
1340 
1341  @try {
1342  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1343  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1344  message:JSONMessageWithContentOf(updateCall)]);
1345  } @catch (...) {
1346  return false;
1347  }
1348  return true;
1349 }
1350 
1351 - (bool)testComposingWithDelta {
1352  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1353  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1354  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1355  [engineMock binaryMessenger])
1356  .andReturn(binaryMessengerMock);
1357 
1358  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
1359  nibName:@""
1360  bundle:nil];
1361 
1363  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1364  viewController:viewController];
1365 
1366  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1367 
1368  NSDictionary* setClientConfig = @{
1369  @"viewId" : @(kViewId),
1370  @"inputAction" : @"action",
1371  @"enableDeltaModel" : @"true",
1372  @"inputType" : @{@"name" : @"inputName"},
1373  };
1374  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1375  arguments:@[ @(1), setClientConfig ]]
1376  result:^(id){
1377  }];
1378  [plugin setMarkedText:@"m" selectedRange:NSMakeRange(0, 1)];
1379 
1380  NSDictionary* deltaToFramework = @{
1381  @"oldText" : @"",
1382  @"deltaText" : @"m",
1383  @"deltaStart" : @(0),
1384  @"deltaEnd" : @(0),
1385  @"selectionBase" : @(0),
1386  @"selectionExtent" : @(1),
1387  @"selectionAffinity" : @"TextAffinity.upstream",
1388  @"selectionIsDirectional" : @(false),
1389  @"composingBase" : @(0),
1390  @"composingExtent" : @(1),
1391  };
1392  NSDictionary* expectedState = @{
1393  @"deltas" : @[ deltaToFramework ],
1394  };
1395 
1396  NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
1397  encodeMethodCall:[FlutterMethodCall
1398  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1399  arguments:@[ @(1), expectedState ]]];
1400 
1401  @try {
1402  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1403  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1404  message:JSONMessageWithContentOf(updateCall)]);
1405  } @catch (...) {
1406  return false;
1407  }
1408 
1409  [plugin setMarkedText:@"ma" selectedRange:NSMakeRange(0, 1)];
1410 
1411  deltaToFramework = @{
1412  @"oldText" : @"m",
1413  @"deltaText" : @"ma",
1414  @"deltaStart" : @(0),
1415  @"deltaEnd" : @(1),
1416  @"selectionBase" : @(0),
1417  @"selectionExtent" : @(1),
1418  @"selectionAffinity" : @"TextAffinity.upstream",
1419  @"selectionIsDirectional" : @(false),
1420  @"composingBase" : @(0),
1421  @"composingExtent" : @(2),
1422  };
1423  expectedState = @{
1424  @"deltas" : @[ deltaToFramework ],
1425  };
1426 
1427  updateCall = [[FlutterJSONMethodCodec sharedInstance]
1428  encodeMethodCall:[FlutterMethodCall
1429  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1430  arguments:@[ @(1), expectedState ]]];
1431 
1432  @try {
1433  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1434  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1435  message:JSONMessageWithContentOf(updateCall)]);
1436  } @catch (...) {
1437  return false;
1438  }
1439 
1440  [plugin setMarkedText:@"mar" selectedRange:NSMakeRange(0, 1)];
1441 
1442  deltaToFramework = @{
1443  @"oldText" : @"ma",
1444  @"deltaText" : @"mar",
1445  @"deltaStart" : @(0),
1446  @"deltaEnd" : @(2),
1447  @"selectionBase" : @(0),
1448  @"selectionExtent" : @(1),
1449  @"selectionAffinity" : @"TextAffinity.upstream",
1450  @"selectionIsDirectional" : @(false),
1451  @"composingBase" : @(0),
1452  @"composingExtent" : @(3),
1453  };
1454  expectedState = @{
1455  @"deltas" : @[ deltaToFramework ],
1456  };
1457 
1458  updateCall = [[FlutterJSONMethodCodec sharedInstance]
1459  encodeMethodCall:[FlutterMethodCall
1460  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1461  arguments:@[ @(1), expectedState ]]];
1462 
1463  @try {
1464  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1465  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1466  message:JSONMessageWithContentOf(updateCall)]);
1467  } @catch (...) {
1468  return false;
1469  }
1470 
1471  [plugin setMarkedText:@"mark" selectedRange:NSMakeRange(0, 1)];
1472 
1473  deltaToFramework = @{
1474  @"oldText" : @"mar",
1475  @"deltaText" : @"mark",
1476  @"deltaStart" : @(0),
1477  @"deltaEnd" : @(3),
1478  @"selectionBase" : @(0),
1479  @"selectionExtent" : @(1),
1480  @"selectionAffinity" : @"TextAffinity.upstream",
1481  @"selectionIsDirectional" : @(false),
1482  @"composingBase" : @(0),
1483  @"composingExtent" : @(4),
1484  };
1485  expectedState = @{
1486  @"deltas" : @[ deltaToFramework ],
1487  };
1488 
1489  updateCall = [[FlutterJSONMethodCodec sharedInstance]
1490  encodeMethodCall:[FlutterMethodCall
1491  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1492  arguments:@[ @(1), expectedState ]]];
1493 
1494  @try {
1495  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1496  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1497  message:JSONMessageWithContentOf(updateCall)]);
1498  } @catch (...) {
1499  return false;
1500  }
1501 
1502  [plugin setMarkedText:@"marke" selectedRange:NSMakeRange(0, 1)];
1503 
1504  deltaToFramework = @{
1505  @"oldText" : @"mark",
1506  @"deltaText" : @"marke",
1507  @"deltaStart" : @(0),
1508  @"deltaEnd" : @(4),
1509  @"selectionBase" : @(0),
1510  @"selectionExtent" : @(1),
1511  @"selectionAffinity" : @"TextAffinity.upstream",
1512  @"selectionIsDirectional" : @(false),
1513  @"composingBase" : @(0),
1514  @"composingExtent" : @(5),
1515  };
1516  expectedState = @{
1517  @"deltas" : @[ deltaToFramework ],
1518  };
1519 
1520  updateCall = [[FlutterJSONMethodCodec sharedInstance]
1521  encodeMethodCall:[FlutterMethodCall
1522  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1523  arguments:@[ @(1), expectedState ]]];
1524 
1525  @try {
1526  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1527  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1528  message:JSONMessageWithContentOf(updateCall)]);
1529  } @catch (...) {
1530  return false;
1531  }
1532 
1533  [plugin setMarkedText:@"marked" selectedRange:NSMakeRange(0, 1)];
1534 
1535  deltaToFramework = @{
1536  @"oldText" : @"marke",
1537  @"deltaText" : @"marked",
1538  @"deltaStart" : @(0),
1539  @"deltaEnd" : @(5),
1540  @"selectionBase" : @(0),
1541  @"selectionExtent" : @(1),
1542  @"selectionAffinity" : @"TextAffinity.upstream",
1543  @"selectionIsDirectional" : @(false),
1544  @"composingBase" : @(0),
1545  @"composingExtent" : @(6),
1546  };
1547  expectedState = @{
1548  @"deltas" : @[ deltaToFramework ],
1549  };
1550 
1551  updateCall = [[FlutterJSONMethodCodec sharedInstance]
1552  encodeMethodCall:[FlutterMethodCall
1553  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1554  arguments:@[ @(1), expectedState ]]];
1555 
1556  @try {
1557  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1558  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1559  message:JSONMessageWithContentOf(updateCall)]);
1560  } @catch (...) {
1561  return false;
1562  }
1563 
1564  [plugin unmarkText];
1565 
1566  deltaToFramework = @{
1567  @"oldText" : @"marked",
1568  @"deltaText" : @"",
1569  @"deltaStart" : @(-1),
1570  @"deltaEnd" : @(-1),
1571  @"selectionBase" : @(6),
1572  @"selectionExtent" : @(6),
1573  @"selectionAffinity" : @"TextAffinity.upstream",
1574  @"selectionIsDirectional" : @(false),
1575  @"composingBase" : @(-1),
1576  @"composingExtent" : @(-1),
1577  };
1578  expectedState = @{
1579  @"deltas" : @[ deltaToFramework ],
1580  };
1581 
1582  updateCall = [[FlutterJSONMethodCodec sharedInstance]
1583  encodeMethodCall:[FlutterMethodCall
1584  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1585  arguments:@[ @(1), expectedState ]]];
1586 
1587  @try {
1588  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1589  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1590  message:JSONMessageWithContentOf(updateCall)]);
1591  } @catch (...) {
1592  return false;
1593  }
1594  return true;
1595 }
1596 
1597 - (bool)testComposingWithDeltasWhenSelectionIsActive {
1598  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1599  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1600  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1601  [engineMock binaryMessenger])
1602  .andReturn(binaryMessengerMock);
1603 
1604  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
1605  nibName:@""
1606  bundle:nil];
1607 
1609  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1610  viewController:viewController];
1611 
1612  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1613 
1614  NSDictionary* setClientConfig = @{
1615  @"viewId" : @(kViewId),
1616  @"inputAction" : @"action",
1617  @"enableDeltaModel" : @"true",
1618  @"inputType" : @{@"name" : @"inputName"},
1619  };
1620  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1621  arguments:@[ @(1), setClientConfig ]]
1622  result:^(id){
1623  }];
1624 
1625  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
1626  arguments:@{
1627  @"text" : @"Text",
1628  @"selectionBase" : @(0),
1629  @"selectionExtent" : @(4),
1630  @"composingBase" : @(-1),
1631  @"composingExtent" : @(-1),
1632  }];
1633  [plugin handleMethodCall:call
1634  result:^(id){
1635  }];
1636 
1637  [plugin setMarkedText:@"~"
1638  selectedRange:NSMakeRange(1, 0)
1639  replacementRange:NSMakeRange(NSNotFound, 0)];
1640 
1641  NSDictionary* deltaToFramework = @{
1642  @"oldText" : @"Text",
1643  @"deltaText" : @"~",
1644  @"deltaStart" : @(0),
1645  @"deltaEnd" : @(4),
1646  @"selectionBase" : @(1),
1647  @"selectionExtent" : @(1),
1648  @"selectionAffinity" : @"TextAffinity.upstream",
1649  @"selectionIsDirectional" : @(false),
1650  @"composingBase" : @(0),
1651  @"composingExtent" : @(1),
1652  };
1653  NSDictionary* expectedState = @{
1654  @"deltas" : @[ deltaToFramework ],
1655  };
1656 
1657  NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
1658  encodeMethodCall:[FlutterMethodCall
1659  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
1660  arguments:@[ @(1), expectedState ]]];
1661 
1662  @try {
1663  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1664  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
1665  message:JSONMessageWithContentOf(updateCall)]);
1666  } @catch (...) {
1667  return false;
1668  }
1669  return true;
1670 }
1671 
1672 - (bool)testPerformKeyEquivalent {
1673  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1674  __block NSEvent* eventBeingDispatchedByKeyboardManager = nil;
1675  FlutterViewController* viewControllerMock = OCMClassMock([FlutterViewController class]);
1676  OCMStub([viewControllerMock isDispatchingKeyEvent:[OCMArg any]])
1677  .andDo(^(NSInvocation* invocation) {
1678  NSEvent* event;
1679  [invocation getArgument:(void*)&event atIndex:2];
1680  BOOL result = event == eventBeingDispatchedByKeyboardManager;
1681  [invocation setReturnValue:&result];
1682  });
1683 
1684  NSEvent* event = [NSEvent keyEventWithType:NSEventTypeKeyDown
1685  location:NSZeroPoint
1686  modifierFlags:0x100
1687  timestamp:0
1688  windowNumber:0
1689  context:nil
1690  characters:@""
1691  charactersIgnoringModifiers:@""
1692  isARepeat:NO
1693  keyCode:0x50];
1694 
1696  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1697  viewController:viewControllerMock];
1698 
1699  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1700 
1701  NSDictionary* setClientConfig = @{
1702  @"viewId" : @(kViewId),
1703  };
1704  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1705  arguments:@[ @(1), setClientConfig ]]
1706  result:^(id){
1707  }];
1708 
1709  OCMExpect([viewControllerMock keyDown:event]);
1710 
1711  // Require that event is handled (returns YES)
1712  if (![plugin performKeyEquivalent:event]) {
1713  return false;
1714  };
1715 
1716  @try {
1717  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
1718  [viewControllerMock keyDown:event]);
1719  } @catch (...) {
1720  return false;
1721  }
1722 
1723  // performKeyEquivalent must not forward event if it is being
1724  // dispatched by keyboard manager
1725  eventBeingDispatchedByKeyboardManager = event;
1726 
1727  OCMReject([viewControllerMock keyDown:event]);
1728  @try {
1729  // Require that event is not handled (returns NO) and not
1730  // forwarded to controller
1731  if ([plugin performKeyEquivalent:event]) {
1732  return false;
1733  };
1734  } @catch (...) {
1735  return false;
1736  }
1737 
1738  return true;
1739 }
1740 
1741 - (bool)handleArrowKeyWhenImePopoverIsActive {
1742  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1743  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1744  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1745  [engineMock binaryMessenger])
1746  .andReturn(binaryMessengerMock);
1747  OCMStub([[engineMock ignoringNonObjectArgs] sendKeyEvent:FlutterKeyEvent {}
1748  callback:nil
1749  userData:nil]);
1750 
1751  NSTextInputContext* textInputContext = OCMClassMock([NSTextInputContext class]);
1752  OCMStub([textInputContext handleEvent:[OCMArg any]]).andReturn(YES);
1753 
1754  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
1755  nibName:@""
1756  bundle:nil];
1757 
1759  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1760  viewController:viewController];
1761 
1762  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1763 
1764  plugin.textInputContext = textInputContext;
1765 
1766  NSDictionary* setClientConfig = @{
1767  @"viewId" : @(kViewId),
1768  @"inputAction" : @"action",
1769  @"enableDeltaModel" : @"true",
1770  @"inputType" : @{@"name" : @"inputName"},
1771  };
1772  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1773  arguments:@[ @(1), setClientConfig ]]
1774  result:^(id){
1775  }];
1776 
1778  arguments:@[]]
1779  result:^(id){
1780  }];
1781 
1782  // Set marked text, simulate active IME popover.
1783  [plugin setMarkedText:@"m"
1784  selectedRange:NSMakeRange(0, 1)
1785  replacementRange:NSMakeRange(NSNotFound, 0)];
1786 
1787  // Right arrow key. This, unlike the key below should be handled by the plugin.
1788  NSEvent* event = [NSEvent keyEventWithType:NSEventTypeKeyDown
1789  location:NSZeroPoint
1790  modifierFlags:0xa00100
1791  timestamp:0
1792  windowNumber:0
1793  context:nil
1794  characters:@"\uF702"
1795  charactersIgnoringModifiers:@"\uF702"
1796  isARepeat:NO
1797  keyCode:0x4];
1798 
1799  // Plugin should mark the event as key equivalent.
1800  [plugin performKeyEquivalent:event];
1801 
1802  if ([plugin handleKeyEvent:event] != true) {
1803  return false;
1804  }
1805 
1806  // CTRL+H (delete backwards)
1807  event = [NSEvent keyEventWithType:NSEventTypeKeyDown
1808  location:NSZeroPoint
1809  modifierFlags:0x40101
1810  timestamp:0
1811  windowNumber:0
1812  context:nil
1813  characters:@"\uF702"
1814  charactersIgnoringModifiers:@"\uF702"
1815  isARepeat:NO
1816  keyCode:0x4];
1817 
1818  // Plugin should mark the event as key equivalent.
1819  [plugin performKeyEquivalent:event];
1820 
1821  if ([plugin handleKeyEvent:event] != false) {
1822  return false;
1823  }
1824 
1825  return true;
1826 }
1827 
1828 - (bool)unhandledKeyEquivalent {
1829  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1830  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1831  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1832  [engineMock binaryMessenger])
1833  .andReturn(binaryMessengerMock);
1834 
1835  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
1836  nibName:@""
1837  bundle:nil];
1838 
1840  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1841  viewController:viewController];
1842 
1843  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1844 
1845  NSDictionary* setClientConfig = @{
1846  @"viewId" : @(kViewId),
1847  @"inputAction" : @"action",
1848  @"enableDeltaModel" : @"true",
1849  @"inputType" : @{@"name" : @"inputName"},
1850  };
1851  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1852  arguments:@[ @(1), setClientConfig ]]
1853  result:^(id){
1854  }];
1855 
1857  arguments:@[]]
1858  result:^(id){
1859  }];
1860 
1861  // CTRL+H (delete backwards)
1862  NSEvent* event = [NSEvent keyEventWithType:NSEventTypeKeyDown
1863  location:NSZeroPoint
1864  modifierFlags:0x40101
1865  timestamp:0
1866  windowNumber:0
1867  context:nil
1868  characters:@""
1869  charactersIgnoringModifiers:@"h"
1870  isARepeat:NO
1871  keyCode:0x4];
1872 
1873  // Plugin should mark the event as key equivalent.
1874  [plugin performKeyEquivalent:event];
1875 
1876  // Simulate KeyboardManager sending unhandled event to plugin. This must return
1877  // true because it is a known editing command.
1878  if ([plugin handleKeyEvent:event] != true) {
1879  return false;
1880  }
1881 
1882  // CMD+W
1883  event = [NSEvent keyEventWithType:NSEventTypeKeyDown
1884  location:NSZeroPoint
1885  modifierFlags:0x100108
1886  timestamp:0
1887  windowNumber:0
1888  context:nil
1889  characters:@"w"
1890  charactersIgnoringModifiers:@"w"
1891  isARepeat:NO
1892  keyCode:0x13];
1893 
1894  // Plugin should mark the event as key equivalent.
1895  [plugin performKeyEquivalent:event];
1896 
1897  // This is not a valid editing command, plugin must return false so that
1898  // KeyboardManager sends the event to next responder.
1899  if ([plugin handleKeyEvent:event] != false) {
1900  return false;
1901  }
1902 
1903  return true;
1904 }
1905 
1906 - (bool)testInsertNewLine {
1907  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1908  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1909  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1910  [engineMock binaryMessenger])
1911  .andReturn(binaryMessengerMock);
1912  OCMStub([[engineMock ignoringNonObjectArgs] sendKeyEvent:FlutterKeyEvent {}
1913  callback:nil
1914  userData:nil]);
1915 
1916  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
1917  nibName:@""
1918  bundle:nil];
1919 
1921  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1922  viewController:viewController];
1923 
1924  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1925 
1926  NSDictionary* setClientConfig = @{
1927  @"viewId" : @(kViewId),
1928  @"inputType" : @{@"name" : @"TextInputType.multiline"},
1929  @"inputAction" : @"TextInputAction.newline",
1930  };
1931  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
1932  arguments:@[ @(1), setClientConfig ]]
1933  result:^(id){
1934  }];
1935 
1936  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
1937  arguments:@{
1938  @"text" : @"Text",
1939  @"selectionBase" : @(4),
1940  @"selectionExtent" : @(4),
1941  @"composingBase" : @(-1),
1942  @"composingExtent" : @(-1),
1943  }];
1944 
1945  [plugin handleMethodCall:call
1946  result:^(id){
1947  }];
1948 
1949  // Verify editing state was set.
1950  NSDictionary* editingState = [plugin editingState];
1951  EXPECT_STREQ([editingState[@"text"] UTF8String], "Text");
1952  EXPECT_STREQ([editingState[@"selectionAffinity"] UTF8String], "TextAffinity.upstream");
1953  EXPECT_FALSE([editingState[@"selectionIsDirectional"] boolValue]);
1954  EXPECT_EQ([editingState[@"selectionBase"] intValue], 4);
1955  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 4);
1956  EXPECT_EQ([editingState[@"composingBase"] intValue], -1);
1957  EXPECT_EQ([editingState[@"composingExtent"] intValue], -1);
1958 
1959  [plugin doCommandBySelector:@selector(insertNewline:)];
1960 
1961  // Verify editing state was set.
1962  editingState = [plugin editingState];
1963  EXPECT_STREQ([editingState[@"text"] UTF8String], "Text\n");
1964  EXPECT_STREQ([editingState[@"selectionAffinity"] UTF8String], "TextAffinity.upstream");
1965  EXPECT_FALSE([editingState[@"selectionIsDirectional"] boolValue]);
1966  EXPECT_EQ([editingState[@"selectionBase"] intValue], 5);
1967  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 5);
1968  EXPECT_EQ([editingState[@"composingBase"] intValue], -1);
1969  EXPECT_EQ([editingState[@"composingExtent"] intValue], -1);
1970 
1971  return true;
1972 }
1973 
1974 - (bool)testSendActionDoNotInsertNewLine {
1975  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
1976  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
1977  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
1978  [engineMock binaryMessenger])
1979  .andReturn(binaryMessengerMock);
1980  OCMStub([[engineMock ignoringNonObjectArgs] sendKeyEvent:FlutterKeyEvent {}
1981  callback:nil
1982  userData:nil]);
1983 
1984  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
1985  nibName:@""
1986  bundle:nil];
1987 
1989  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
1990  viewController:viewController];
1991 
1992  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
1993 
1994  NSDictionary* setClientConfig = @{
1995  @"viewId" : @(kViewId),
1996  @"inputType" : @{@"name" : @"TextInputType.multiline"},
1997  @"inputAction" : @"TextInputAction.send",
1998  };
1999  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2000  arguments:@[ @(1), setClientConfig ]]
2001  result:^(id){
2002  }];
2003 
2004  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
2005  arguments:@{
2006  @"text" : @"Text",
2007  @"selectionBase" : @(4),
2008  @"selectionExtent" : @(4),
2009  @"composingBase" : @(-1),
2010  @"composingExtent" : @(-1),
2011  }];
2012 
2013  NSDictionary* expectedState = @{
2014  @"selectionBase" : @(4),
2015  @"selectionExtent" : @(4),
2016  @"selectionAffinity" : @"TextAffinity.upstream",
2017  @"selectionIsDirectional" : @(NO),
2018  @"composingBase" : @(-1),
2019  @"composingExtent" : @(-1),
2020  @"text" : @"Text",
2021  };
2022 
2023  NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
2024  encodeMethodCall:[FlutterMethodCall
2025  methodCallWithMethodName:@"TextInputClient.updateEditingState"
2026  arguments:@[ @(1), expectedState ]]];
2027 
2028  OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
2029  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
2030  message:JSONMessageWithContentOf(updateCall)]);
2031 
2032  [plugin handleMethodCall:call
2033  result:^(id){
2034  }];
2035 
2036  [plugin doCommandBySelector:@selector(insertNewline:)];
2037 
2038  NSData* performActionCall = [[FlutterJSONMethodCodec sharedInstance]
2039  encodeMethodCall:[FlutterMethodCall
2040  methodCallWithMethodName:@"TextInputClient.performAction"
2041  arguments:@[ @(1), @"TextInputAction.send" ]]];
2042 
2043  // Input action should be notified.
2044  @try {
2045  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
2046  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
2047  message:JSONMessageWithContentOf(performActionCall)]);
2048  } @catch (...) {
2049  return false;
2050  }
2051 
2052  NSDictionary* updatedState = @{
2053  @"selectionBase" : @(5),
2054  @"selectionExtent" : @(5),
2055  @"selectionAffinity" : @"TextAffinity.upstream",
2056  @"selectionIsDirectional" : @(NO),
2057  @"composingBase" : @(-1),
2058  @"composingExtent" : @(-1),
2059  @"text" : @"Text\n",
2060  };
2061 
2062  updateCall = [[FlutterJSONMethodCodec sharedInstance]
2063  encodeMethodCall:[FlutterMethodCall
2064  methodCallWithMethodName:@"TextInputClient.updateEditingState"
2065  arguments:@[ @(1), updatedState ]]];
2066 
2067  // Verify that editing state was not be updated.
2068  @try {
2069  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
2070  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
2071  message:JSONMessageWithContentOf(updateCall)]);
2072  return false;
2073  } @catch (...) {
2074  // Expected.
2075  }
2076 
2077  return true;
2078 }
2079 
2080 - (bool)testLocalTextAndSelectionUpdateAfterDelta {
2081  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2082  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2083  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2084  [engineMock binaryMessenger])
2085  .andReturn(binaryMessengerMock);
2086 
2087  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2088  nibName:@""
2089  bundle:nil];
2090 
2092  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2093  viewController:viewController];
2094 
2095  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2096 
2097  NSDictionary* setClientConfig = @{
2098  @"viewId" : @(kViewId),
2099  @"inputAction" : @"action",
2100  @"enableDeltaModel" : @"true",
2101  @"inputType" : @{@"name" : @"inputName"},
2102  };
2103  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2104  arguments:@[ @(1), setClientConfig ]]
2105  result:^(id){
2106  }];
2107  [plugin insertText:@"text to insert"];
2108 
2109  NSDictionary* deltaToFramework = @{
2110  @"oldText" : @"",
2111  @"deltaText" : @"text to insert",
2112  @"deltaStart" : @(0),
2113  @"deltaEnd" : @(0),
2114  @"selectionBase" : @(14),
2115  @"selectionExtent" : @(14),
2116  @"selectionAffinity" : @"TextAffinity.upstream",
2117  @"selectionIsDirectional" : @(false),
2118  @"composingBase" : @(-1),
2119  @"composingExtent" : @(-1),
2120  };
2121  NSDictionary* expectedState = @{
2122  @"deltas" : @[ deltaToFramework ],
2123  };
2124 
2125  NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
2126  encodeMethodCall:[FlutterMethodCall
2127  methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
2128  arguments:@[ @(1), expectedState ]]];
2129 
2130  @try {
2131  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
2132  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
2133  message:JSONMessageWithContentOf(updateCall)]);
2134  } @catch (...) {
2135  return false;
2136  }
2137 
2138  bool localTextAndSelectionUpdated = [plugin.string isEqualToString:@"text to insert"] &&
2139  NSEqualRanges(plugin.selectedRange, NSMakeRange(14, 0));
2140 
2141  return localTextAndSelectionUpdated;
2142 }
2143 
2144 - (bool)testSelectorsAreForwardedToFramework {
2145  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2146  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2147  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2148  [engineMock binaryMessenger])
2149  .andReturn(binaryMessengerMock);
2150 
2151  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2152  nibName:@""
2153  bundle:nil];
2154 
2156  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2157  viewController:viewController];
2158 
2159  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2160 
2161  NSDictionary* setClientConfig = @{
2162  @"viewId" : @(kViewId),
2163  @"inputAction" : @"action",
2164  @"enableDeltaModel" : @"true",
2165  @"inputType" : @{@"name" : @"inputName"},
2166  };
2167  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2168  arguments:@[ @(1), setClientConfig ]]
2169  result:^(id){
2170  }];
2171 
2172  // Can't run CFRunLoop in default mode because it causes crashes from scheduled
2173  // sources from other tests.
2174  NSString* runLoopMode = @"FlutterTestRunLoopMode";
2175  plugin.customRunLoopMode = runLoopMode;
2176 
2177  // Ensure both selectors are grouped in one platform channel call.
2178  [plugin doCommandBySelector:@selector(moveUp:)];
2179  [plugin doCommandBySelector:@selector(moveRightAndModifySelection:)];
2180 
2181  __block bool done = false;
2182  CFRunLoopPerformBlock(CFRunLoopGetMain(), (__bridge CFStringRef)runLoopMode, ^{
2183  done = true;
2184  });
2185 
2186  while (!done) {
2187  // Each invocation will handle one source.
2188  CFRunLoopRunInMode((__bridge CFStringRef)runLoopMode, 0, true);
2189  }
2190 
2191  NSData* performSelectorCall = [[FlutterJSONMethodCodec sharedInstance]
2192  encodeMethodCall:[FlutterMethodCall
2193  methodCallWithMethodName:@"TextInputClient.performSelectors"
2194  arguments:@[
2195  @(1), @[ @"moveUp:", @"moveRightAndModifySelection:" ]
2196  ]]];
2197 
2198  @try {
2199  OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
2200  [binaryMessengerMock sendOnChannel:@"flutter/textinput"
2201  message:JSONMessageWithContentOf(performSelectorCall)]);
2202  } @catch (...) {
2203  return false;
2204  }
2205 
2206  return true;
2207 }
2208 
2209 - (bool)testSelectorsNotForwardedToFrameworkIfNoClient {
2210  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2211  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2212  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2213  [engineMock binaryMessenger])
2214  .andReturn(binaryMessengerMock);
2215  // Make sure the selectors are not forwarded to the framework.
2216  OCMReject([binaryMessengerMock sendOnChannel:@"flutter/textinput" message:[OCMArg any]]);
2217  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2218  nibName:@""
2219  bundle:nil];
2220 
2222  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2223  viewController:viewController];
2224 
2225  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2226 
2227  // Can't run CFRunLoop in default mode because it causes crashes from scheduled
2228  // sources from other tests.
2229  NSString* runLoopMode = @"FlutterTestRunLoopMode";
2230  plugin.customRunLoopMode = runLoopMode;
2231 
2232  // Call selectors without setting a client.
2233  [plugin doCommandBySelector:@selector(moveUp:)];
2234  [plugin doCommandBySelector:@selector(moveRightAndModifySelection:)];
2235 
2236  __block bool done = false;
2237  CFRunLoopPerformBlock(CFRunLoopGetMain(), (__bridge CFStringRef)runLoopMode, ^{
2238  done = true;
2239  });
2240 
2241  while (!done) {
2242  CFRunLoopRunInMode((__bridge CFStringRef)runLoopMode, 0, true);
2243  }
2244  // At this point the selectors should be dropped; otherwise, OCMReject will throw.
2245  return true;
2246 }
2247 
2248 - (bool)testInsertTextWithCollapsedSelectionInsideComposing {
2249  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2250  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2251  OCMStub([engineMock binaryMessenger]).andReturn(binaryMessengerMock);
2252 
2253  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2254  nibName:@""
2255  bundle:nil];
2257  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2258  viewController:viewController];
2259  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2260 
2261  NSDictionary* setClientConfig = @{
2262  @"viewId" : @(kViewId),
2263  @"inputAction" : @"action",
2264  @"inputType" : @{@"name" : @"text"},
2265  };
2266  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2267  arguments:@[ @(1), setClientConfig ]]
2268  result:^(id result){
2269  }];
2270 
2271  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
2272  arguments:@{
2273  @"text" : @"今日ã�¯å®¶ã�«å¸°ã‚Šã�¾ã�™",
2274  @"selectionBase" : @(0),
2275  @"selectionExtent" : @(3),
2276  @"composingBase" : @(0),
2277  @"composingExtent" : @(9),
2278  }];
2279  [plugin handleMethodCall:call
2280  result:^(id result){
2281  }];
2282 
2283  [plugin insertText:@"今日ã�¯å®¶ã�«å¸°ã‚Šã�¾ã�™" replacementRange:NSMakeRange(NSNotFound, 0)];
2284 
2285  NSDictionary* editingState = [plugin editingState];
2286  EXPECT_STREQ([editingState[@"text"] UTF8String], "今日ã�¯å®¶ã�«å¸°ã‚Šã�¾ã�™");
2287  EXPECT_EQ([editingState[@"selectionBase"] intValue], 9);
2288  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 9);
2289 
2290  return true;
2291 }
2292 
2293 @end
2294 
2295 namespace flutter::testing {
2296 
2297 namespace {
2298 // Allocates and returns an engine configured for the text fixture resource configuration.
2299 FlutterEngine* CreateTestEngine() {
2300  NSString* fixtures = @(testing::GetFixturesPath());
2301  FlutterDartProject* project = [[FlutterDartProject alloc]
2302  initWithAssetsPath:fixtures
2303  ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
2304  return [[FlutterEngine alloc] initWithName:@"test" project:project allowHeadlessExecution:true];
2305 }
2306 } // namespace
2307 
2308 TEST(FlutterTextInputPluginTest, TestEmptyCompositionRange) {
2309  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testEmptyCompositionRange]);
2310 }
2311 
2312 TEST(FlutterTextInputPluginTest, TestSetMarkedTextWithSelectionChange) {
2313  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSetMarkedTextWithSelectionChange]);
2314 }
2315 
2316 TEST(FlutterTextInputPluginTest, TestSetMarkedTextWithReplacementRange) {
2317  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSetMarkedTextWithReplacementRange]);
2318 }
2319 
2320 TEST(FlutterTextInputPluginTest, TestSetMarkedTextWithSelectedRangeNotFound) {
2321  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSetMarkedTextWithSelectedRangeNotFound]);
2322 }
2323 
2324 TEST(FlutterTextInputPluginTest, TestSetMarkedTextWithOutOfBoundsRanges) {
2325  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSetMarkedTextWithOutOfBoundsRanges]);
2326 }
2327 
2328 TEST(FlutterTextInputPluginTest, TestSetEditingStateWithOutOfBoundsSelection) {
2329  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSetEditingStateWithOutOfBoundsSelection]);
2330 }
2331 
2332 TEST(FlutterTextInputPluginTest, TestComposingRegionRemovedByFramework) {
2333  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingRegionRemovedByFramework]);
2334 }
2335 
2336 TEST(FlutterTextInputPluginTest, TestClearClientDuringComposing) {
2337  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testClearClientDuringComposing]);
2338 }
2339 
2340 TEST(FlutterTextInputPluginTest, TestAutocompleteDisabledWhenAutofillNotSet) {
2341  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testAutocompleteDisabledWhenAutofillNotSet]);
2342 }
2343 
2344 TEST(FlutterTextInputPluginTest, TestAutocompleteEnabledWhenAutofillSet) {
2345  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testAutocompleteEnabledWhenAutofillSet]);
2346 }
2347 
2348 TEST(FlutterTextInputPluginTest, TestAutocompleteEnabledWhenAutofillSetNoHint) {
2349  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testAutocompleteEnabledWhenAutofillSetNoHint]);
2350 }
2351 
2352 TEST(FlutterTextInputPluginTest, TestAutocompleteDisabledWhenObscureTextSet) {
2353  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testAutocompleteDisabledWhenObscureTextSet]);
2354 }
2355 
2356 TEST(FlutterTextInputPluginTest, TestAutocompleteDisabledWhenPasswordAutofillSet) {
2357  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testAutocompleteDisabledWhenPasswordAutofillSet]);
2358 }
2359 
2360 TEST(FlutterTextInputPluginTest, TestAutocompleteDisabledWhenAutofillGroupIncludesPassword) {
2361  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc]
2362  testAutocompleteDisabledWhenAutofillGroupIncludesPassword]);
2363 }
2364 
2365 TEST(FlutterTextInputPluginTest, TestFirstRectForCharacterRange) {
2366  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testFirstRectForCharacterRange]);
2367 }
2368 
2369 TEST(FlutterTextInputPluginTest, TestFirstRectForCharacterRangeAtInfinity) {
2370  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testFirstRectForCharacterRangeAtInfinity]);
2371 }
2372 
2373 TEST(FlutterTextInputPluginTest, TestFirstRectForCharacterRangeWithEsotericAffineTransform) {
2374  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc]
2375  testFirstRectForCharacterRangeWithEsotericAffineTransform]);
2376 }
2377 
2378 TEST(FlutterTextInputPluginTest, TestSetEditingStateWithTextEditingDelta) {
2379  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSetEditingStateWithTextEditingDelta]);
2380 }
2381 
2382 TEST(FlutterTextInputPluginTest, TestOperationsThatTriggerDelta) {
2383  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testOperationsThatTriggerDelta]);
2384 }
2385 
2386 TEST(FlutterTextInputPluginTest, TestComposingWithDelta) {
2387  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingWithDelta]);
2388 }
2389 
2390 TEST(FlutterTextInputPluginTest, TestComposingWithDeltasWhenSelectionIsActive) {
2391  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingWithDeltasWhenSelectionIsActive]);
2392 }
2393 
2394 TEST(FlutterTextInputPluginTest, TestLocalTextAndSelectionUpdateAfterDelta) {
2395  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testLocalTextAndSelectionUpdateAfterDelta]);
2396 }
2397 
2398 TEST(FlutterTextInputPluginTest, TestPerformKeyEquivalent) {
2399  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testPerformKeyEquivalent]);
2400 }
2401 
2402 TEST(FlutterTextInputPluginTest, HandleArrowKeyWhenImePopoverIsActive) {
2403  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] handleArrowKeyWhenImePopoverIsActive]);
2404 }
2405 
2406 TEST(FlutterTextInputPluginTest, UnhandledKeyEquivalent) {
2407  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] unhandledKeyEquivalent]);
2408 }
2409 
2410 TEST(FlutterTextInputPluginTest, TestSelectorsAreForwardedToFramework) {
2411  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSelectorsAreForwardedToFramework]);
2412 }
2413 
2414 TEST(FlutterTextInputPluginTest, TestSelectorsNotForwardedToFrameworkIfNoClient) {
2415  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSelectorsNotForwardedToFrameworkIfNoClient]);
2416 }
2417 
2418 TEST(FlutterTextInputPluginTest, TestInsertNewLine) {
2419  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testInsertNewLine]);
2420 }
2421 
2422 TEST(FlutterTextInputPluginTest, TestSendActionDoNotInsertNewLine) {
2423  ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSendActionDoNotInsertNewLine]);
2424 }
2425 
2426 TEST(FlutterTextInputPluginTest, TestAttributedSubstringOutOfRange) {
2427  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2428  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2429  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2430  [engineMock binaryMessenger])
2431  .andReturn(binaryMessengerMock);
2432 
2433  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2434  nibName:@""
2435  bundle:nil];
2436 
2438  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2439  viewController:viewController];
2440 
2441  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2442 
2443  NSDictionary* setClientConfig = @{
2444  @"viewId" : @(kViewId),
2445  @"inputAction" : @"action",
2446  @"enableDeltaModel" : @"true",
2447  @"inputType" : @{@"name" : @"inputName"},
2448  };
2449  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2450  arguments:@[ @(1), setClientConfig ]]
2451  result:^(id){
2452  }];
2453 
2454  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
2455  arguments:@{
2456  @"text" : @"Text",
2457  @"selectionBase" : @(0),
2458  @"selectionExtent" : @(0),
2459  @"composingBase" : @(-1),
2460  @"composingExtent" : @(-1),
2461  }];
2462 
2463  [plugin handleMethodCall:call
2464  result:^(id){
2465  }];
2466 
2467  NSRange out;
2468  NSAttributedString* text = [plugin attributedSubstringForProposedRange:NSMakeRange(1, 10)
2469  actualRange:&out];
2470  EXPECT_TRUE([text.string isEqualToString:@"ext"]);
2471  EXPECT_EQ(out.location, 1u);
2472  EXPECT_EQ(out.length, 3u);
2473 
2474  text = [plugin attributedSubstringForProposedRange:NSMakeRange(4, 10) actualRange:&out];
2475  EXPECT_EQ(text, nil);
2476 }
2477 
2478 TEST(FlutterTextInputPluginTest, CanWorkWithFlutterTextField) {
2479  FlutterEngine* engine = CreateTestEngine();
2480  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
2481  nibName:nil
2482  bundle:nil];
2483  [viewController loadView];
2484  // Create a NSWindow so that the native text field can become first responder.
2485  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
2486  styleMask:NSBorderlessWindowMask
2487  backing:NSBackingStoreBuffered
2488  defer:NO];
2489  window.contentView = viewController.view;
2490 
2491  engine.semanticsEnabled = YES;
2492 
2493  auto bridge = viewController.accessibilityBridge.lock();
2494  FlutterPlatformNodeDelegateMac delegate(bridge, viewController);
2495  ui::AXTree tree;
2496  ui::AXNode ax_node(&tree, nullptr, 0, 0);
2497  ui::AXNodeData node_data;
2498  node_data.SetValue("initial text");
2499  ax_node.SetData(node_data);
2500  delegate.Init(viewController.accessibilityBridge, &ax_node);
2501  {
2502  FlutterTextPlatformNode text_platform_node(&delegate, viewController);
2503 
2504  FlutterTextFieldMock* mockTextField =
2505  [[FlutterTextFieldMock alloc] initWithPlatformNode:&text_platform_node
2506  fieldEditor:engine.textInputPlugin];
2507  [viewController.view addSubview:mockTextField];
2508  [mockTextField startEditing];
2509 
2510  NSDictionary* setClientConfig = @{
2511  @"viewId" : @(flutter::kFlutterImplicitViewId),
2512  @"inputAction" : @"action",
2513  @"inputType" : @{@"name" : @"inputName"},
2514  };
2515  FlutterMethodCall* methodCall =
2516  [FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2517  arguments:@[ @(1), setClientConfig ]];
2518  FlutterResult result = ^(id result) {
2519  };
2520  [engine.textInputPlugin handleMethodCall:methodCall result:result];
2521 
2522  NSDictionary* arguments = @{
2523  @"text" : @"new text",
2524  @"selectionBase" : @(1),
2525  @"selectionExtent" : @(2),
2526  @"composingBase" : @(-1),
2527  @"composingExtent" : @(-1),
2528  };
2529  methodCall = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
2530  arguments:arguments];
2531  [engine.textInputPlugin handleMethodCall:methodCall result:result];
2532  EXPECT_EQ([mockTextField.lastUpdatedString isEqualToString:@"new text"], YES);
2533  EXPECT_EQ(NSEqualRanges(mockTextField.lastUpdatedSelection, NSMakeRange(1, 1)), YES);
2534 
2535  // This blocks the FlutterTextFieldMock, which is held onto by the main event
2536  // loop, from crashing.
2537  [mockTextField setPlatformNode:nil];
2538  }
2539 
2540  // This verifies that clearing the platform node works.
2541  [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
2542 }
2543 
2544 TEST(FlutterTextInputPluginTest, CanNotBecomeResponderIfNoViewController) {
2545  FlutterEngine* engine = CreateTestEngine();
2546  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
2547  nibName:nil
2548  bundle:nil];
2549  [viewController loadView];
2550  // Creates a NSWindow so that the native text field can become first responder.
2551  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
2552  styleMask:NSBorderlessWindowMask
2553  backing:NSBackingStoreBuffered
2554  defer:NO];
2555  window.contentView = viewController.view;
2556 
2557  engine.semanticsEnabled = YES;
2558 
2559  auto bridge = viewController.accessibilityBridge.lock();
2560  FlutterPlatformNodeDelegateMac delegate(bridge, viewController);
2561  ui::AXTree tree;
2562  ui::AXNode ax_node(&tree, nullptr, 0, 0);
2563  ui::AXNodeData node_data;
2564  node_data.SetValue("initial text");
2565  ax_node.SetData(node_data);
2566  delegate.Init(viewController.accessibilityBridge, &ax_node);
2567  FlutterTextPlatformNode text_platform_node(&delegate, viewController);
2568 
2569  FlutterTextField* textField = text_platform_node.GetNativeViewAccessible();
2570  EXPECT_EQ([textField becomeFirstResponder], YES);
2571  // Removes view controller.
2572  [engine setViewController:nil];
2573  FlutterTextPlatformNode text_platform_node_no_controller(&delegate, nil);
2574  textField = text_platform_node_no_controller.GetNativeViewAccessible();
2575  EXPECT_EQ([textField becomeFirstResponder], NO);
2576 }
2577 
2578 TEST(FlutterTextInputPluginTest, IsAddedAndRemovedFromViewHierarchy) {
2579  FlutterEngine* engine = CreateTestEngine();
2580  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
2581  nibName:nil
2582  bundle:nil];
2583  [viewController loadView];
2584 
2585  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
2586  styleMask:NSBorderlessWindowMask
2587  backing:NSBackingStoreBuffered
2588  defer:NO];
2589  window.contentView = viewController.view;
2590 
2591  ASSERT_EQ(engine.textInputPlugin.superview, nil);
2592  ASSERT_FALSE(window.firstResponder == engine.textInputPlugin);
2593 
2594  NSDictionary* setClientConfig = @{
2595  @"viewId" : @(flutter::kFlutterImplicitViewId),
2596  };
2597  [engine.textInputPlugin
2598  handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2599  arguments:@[ @(1), setClientConfig ]]
2600  result:^(id){
2601  }];
2602 
2603  [engine.textInputPlugin
2604  handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.show" arguments:@[]]
2605  result:^(id){
2606  }];
2607 
2608  ASSERT_EQ(engine.textInputPlugin.superview, viewController.view);
2609  ASSERT_TRUE(window.firstResponder == engine.textInputPlugin);
2610 
2611  [engine.textInputPlugin
2612  handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.hide" arguments:@[]]
2613  result:^(id){
2614  }];
2615 
2616  ASSERT_EQ(engine.textInputPlugin.superview, nil);
2617  ASSERT_FALSE(window.firstResponder == engine.textInputPlugin);
2618 }
2619 
2620 TEST(FlutterTextInputPluginTest, FirstResponderIsCorrect) {
2621  FlutterEngine* engine = CreateTestEngine();
2622  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
2623  nibName:nil
2624  bundle:nil];
2625  [viewController loadView];
2626 
2627  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
2628  styleMask:NSBorderlessWindowMask
2629  backing:NSBackingStoreBuffered
2630  defer:NO];
2631  window.contentView = viewController.view;
2632 
2633  ASSERT_TRUE(viewController.flutterView.acceptsFirstResponder);
2634 
2635  [window makeFirstResponder:viewController.flutterView];
2636 
2637  NSDictionary* setClientConfig = @{
2638  @"viewId" : @(flutter::kFlutterImplicitViewId),
2639  };
2640  [engine.textInputPlugin
2641  handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2642  arguments:@[ @(1), setClientConfig ]]
2643  result:^(id){
2644  }];
2645 
2646  [engine.textInputPlugin
2647  handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.show" arguments:@[]]
2648  result:^(id){
2649  }];
2650 
2651  ASSERT_TRUE(window.firstResponder == engine.textInputPlugin);
2652 
2653  ASSERT_FALSE(viewController.flutterView.acceptsFirstResponder);
2654 
2655  [engine.textInputPlugin
2656  handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.hide" arguments:@[]]
2657  result:^(id){
2658  }];
2659 
2660  ASSERT_TRUE(viewController.flutterView.acceptsFirstResponder);
2661  ASSERT_TRUE(window.firstResponder == viewController.flutterView);
2662 }
2663 
2664 TEST(FlutterTextInputPluginTest, HasZeroSizeAndClipsToBounds) {
2665  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2666  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2667  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2668  [engineMock binaryMessenger])
2669  .andReturn(binaryMessengerMock);
2670 
2671  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2672  nibName:@""
2673  bundle:nil];
2674 
2676  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2677  viewController:viewController];
2678 
2679  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2680 
2681  ASSERT_TRUE(NSIsEmptyRect(plugin.frame));
2682  ASSERT_TRUE(plugin.clipsToBounds);
2683 }
2684 
2685 TEST(FlutterTextInputPluginTest, WorksWithoutViewId) {
2686  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2687  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2688  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2689  [engineMock binaryMessenger])
2690  .andReturn(binaryMessengerMock);
2691 
2692  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2693  nibName:@""
2694  bundle:nil];
2695 
2697  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2698  implicitViewController:viewController];
2699 
2700  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2701 
2702  NSDictionary* setClientConfig = @{
2703  // omit viewId
2704  };
2705  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2706  arguments:@[ @(1), setClientConfig ]]
2707  result:^(id){
2708  }];
2709 
2710  ASSERT_TRUE(plugin.currentViewController == viewController);
2711 }
2712 
2713 TEST(FlutterTextInputPluginTest, InsertTextWithCollapsedSelectionInsideComposing) {
2714  ASSERT_TRUE(
2715  [[FlutterInputPluginTestObjc alloc] testInsertTextWithCollapsedSelectionInsideComposing]);
2716 }
2717 
2718 TEST(FlutterTextInputPluginTest, InsertTextHandlesNSAttributedString) {
2719  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2720  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2721  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2722  [engineMock binaryMessenger])
2723  .andReturn(binaryMessengerMock);
2724 
2725  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2726  nibName:@""
2727  bundle:nil];
2728 
2730  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2731  viewController:viewController];
2732 
2733  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2734 
2735  NSDictionary* setClientConfig = @{
2736  @"viewId" : @(kViewId),
2737  @"inputAction" : @"action",
2738  @"inputType" : @{@"name" : @"inputName"},
2739  };
2740  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2741  arguments:@[ @(1), setClientConfig ]]
2742  result:^(id){
2743  }];
2744 
2745  // Test with NSAttributedString
2746  NSAttributedString* attributedString =
2747  [[NSAttributedString alloc] initWithString:@"attributed text"];
2748  [plugin insertText:attributedString replacementRange:NSMakeRange(NSNotFound, 0)];
2749 
2750  NSDictionary* editingState = [plugin editingState];
2751  EXPECT_STREQ([editingState[@"text"] UTF8String], "attributed text");
2752  EXPECT_EQ([editingState[@"selectionBase"] intValue], 15);
2753  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 15);
2754 }
2755 
2756 TEST(FlutterTextInputPluginTest, InsertTextWithReversedReplacementRange) {
2757  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2758  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2759  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2760  [engineMock binaryMessenger])
2761  .andReturn(binaryMessengerMock);
2762 
2763  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2764  nibName:@""
2765  bundle:nil];
2766 
2768  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2769  viewController:viewController];
2770 
2771  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2772 
2773  NSDictionary* setClientConfig = @{
2774  @"viewId" : @(kViewId),
2775  @"inputAction" : @"action",
2776  @"inputType" : @{@"name" : @"inputName"},
2777  };
2778  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2779  arguments:@[ @(1), setClientConfig ]]
2780  result:^(id){
2781  }];
2782 
2783  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
2784  arguments:@{
2785  @"text" : @"12345",
2786  @"selectionBase" : @(5),
2787  @"selectionExtent" : @(5),
2788  @"composingBase" : @(-1),
2789  @"composingExtent" : @(-1),
2790  }]
2791  result:^(id){
2792  }];
2793 
2794  // A selection made backwards arrives as a range whose length is negative when
2795  // read as a signed value. The two characters before the location are the ones
2796  // replaced; read unsigned, the range would instead cover the text after it.
2797  [plugin insertText:@"X" replacementRange:NSMakeRange(4, static_cast<NSUInteger>(-2))];
2798 
2799  NSDictionary* editingState = [plugin editingState];
2800  EXPECT_STREQ([editingState[@"text"] UTF8String], "12X5");
2801  EXPECT_EQ([editingState[@"selectionBase"] intValue], 3);
2802  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 3);
2803 }
2804 
2805 TEST(FlutterTextInputPluginTest, InsertTextHandlesEmptyAttributedString) {
2806  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2807  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2808  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2809  [engineMock binaryMessenger])
2810  .andReturn(binaryMessengerMock);
2811 
2812  FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
2813  nibName:@""
2814  bundle:nil];
2815 
2817  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2818  viewController:viewController];
2819 
2820  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2821 
2822  NSDictionary* setClientConfig = @{
2823  @"viewId" : @(kViewId),
2824  @"inputAction" : @"action",
2825  @"inputType" : @{@"name" : @"inputName"},
2826  };
2827  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2828  arguments:@[ @(1), setClientConfig ]]
2829  result:^(id){
2830  }];
2831 
2832  // Test with empty NSAttributedString
2833  NSAttributedString* emptyAttributedString = [[NSAttributedString alloc] initWithString:@""];
2834  [plugin insertText:emptyAttributedString replacementRange:NSMakeRange(NSNotFound, 0)];
2835 
2836  NSDictionary* editingState = [plugin editingState];
2837  EXPECT_STREQ([editingState[@"text"] UTF8String], "");
2838  EXPECT_EQ([editingState[@"selectionBase"] intValue], 0);
2839  EXPECT_EQ([editingState[@"selectionExtent"] intValue], 0);
2840 }
2841 
2842 TEST(FlutterTextInputPluginTest, WillUseParentControllerIfCanNotBecomeKey) {
2843  id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
2844  id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
2845  OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
2846  [engineMock binaryMessenger])
2847  .andReturn(binaryMessengerMock);
2848 
2849  [engineMock enableMultiView];
2850  [engineMock runWithEntrypoint:nil];
2851 
2852  // Create popupContoller1 first so that its viewId (1) matches kViewId
2853  FlutterViewController* popupController1 = [[FlutterViewController alloc] initWithEngine:engineMock
2854  nibName:nil
2855  bundle:nil];
2856  FlutterViewController* popupController2 = [[FlutterViewController alloc] initWithEngine:engineMock
2857  nibName:nil
2858  bundle:nil];
2859  FlutterViewController* rootViewController =
2860  [[FlutterViewController alloc] initWithEngine:engineMock nibName:nil bundle:nil];
2861 
2862  // Configure window hierarchy
2863  // RootWindow (canBecomeKeyWindow=YES)
2864  // PopupWindow1 (canBecomeKeyWindow=NO)
2865  // PopupWindow2 (canBecomeKeyWindow=NO)
2866 
2867  NSWindow* rootWindow = [[NSWindow alloc] initWithContentRect:NSZeroRect
2868  styleMask:NSWindowStyleMaskTitled
2869  backing:NSBackingStoreBuffered
2870  defer:NO];
2871  rootWindow.contentViewController = rootViewController;
2872 
2873  NSWindow* popupWindow1 = [[NSWindow alloc] initWithContentRect:NSZeroRect
2874  styleMask:NSWindowStyleMaskBorderless
2875  backing:NSBackingStoreBuffered
2876  defer:NO];
2877  popupWindow1.contentViewController = popupController1;
2878  [rootWindow addChildWindow:popupWindow1 ordered:NSWindowAbove];
2879 
2880  NSWindow* popupWindow2 = [[NSWindow alloc] initWithContentRect:NSZeroRect
2881  styleMask:NSWindowStyleMaskBorderless
2882  backing:NSBackingStoreBuffered
2883  defer:NO];
2884  popupWindow2.contentViewController = popupController2;
2885  [popupWindow1 addChildWindow:popupWindow2 ordered:NSWindowAbove];
2886 
2887  // Initialize delegate with PopupWindow2 (can not become key window)
2889  [[FlutterTextInputPluginTestDelegate alloc] initWithBinaryMessenger:binaryMessengerMock
2890  viewController:popupController2];
2891 
2892  FlutterTextInputPlugin* plugin = [[FlutterTextInputPlugin alloc] initWithDelegate:delegate];
2893 
2894  NSDictionary* setClientConfig = @{
2895  @"viewId" : @(kViewId),
2896  @"inputAction" : @"action",
2897  @"inputType" : @{@"name" : @"inputName"},
2898  };
2899  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
2900  arguments:@[ @(1), setClientConfig ]]
2901  result:^(id){
2902  }];
2903 
2904  // Make sure that TextInputPlugin uses the ancestor controller that has
2905  // canBecomeKey set to YES.
2906  EXPECT_EQ(plugin.currentViewController, rootViewController);
2907 }
2908 
2909 } // namespace flutter::testing
void(^ FlutterResult)(id _Nullable result)
FlutterBinaryMessengerRelay * _binaryMessenger
static const FlutterViewIdentifier kViewId
static id JSONMessageWithContentOf(NSData *expected)
int64_t FlutterViewIdentifier
void Init(std::weak_ptr< OwnerBridge > bridge, ui::AXNode *node) override
Called only once, immediately after construction. The constructor doesn't take any arguments because ...
The ax platform node for a text field.
gfx::NativeViewAccessible GetNativeViewAccessible() override
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
NSTextInputContext * textInputContext
FlutterViewController * currentViewController
NSRect firstRectForCharacterRange:actualRange:(NSRange range,[actualRange] NSRangePointer actualRange)
void handleMethodCall:result:(FlutterMethodCall *call,[result] FlutterResult result)
NSDictionary * editingState()
id< FlutterBinaryMessenger > _binaryMessenger
id CreateMockFlutterEngine(NSString *pasteboardString)
TEST(FlutterTextInputPluginTest, WillUseParentControllerIfCanNotBecomeKey)
instancetype sharedInstance()
id< FlutterBinaryMessenger > binaryMessenger