9 #import <OCMock/OCMock.h>
10 #import <XCTest/XCTest.h>
24 @property(nonatomic, copy) NSString* autofillId;
25 @property(nonatomic, assign) BOOL preventCursorDismissWhenResignFirstResponder;
26 - (void)setEditableTransform:(NSArray*)matrix;
27 - (void)setTextInputClient:(
int)client;
28 - (void)setTextInputState:(NSDictionary*)state;
29 - (void)setMarkedRect:(CGRect)markedRect;
30 - (void)updateEditingState;
31 - (BOOL)isVisibleToAutofill;
33 - (void)configureWithDictionary:(NSDictionary*)configuration;
34 - (void)handleSearchWebAction;
35 - (void)handleLookUpAction;
36 - (void)handleShareAction;
37 - (BOOL)becomeFirstResponderFromFramework;
38 - (BOOL)resignFirstResponderFromFramework;
39 - (void)handleTranslateAction;
47 - (void)postAccessibilityNotification:(UIAccessibilityNotifications)notification target:(
id)target;
54 - (void)postAccessibilityNotification:(UIAccessibilityNotifications)notification target:(
id)target {
56 self.receivedNotificationTarget = target;
59 - (BOOL)accessibilityElementIsFocused {
60 return _isAccessibilityFocused;
66 @property(nonatomic, strong) UITextField*
textField;
71 @property(nonatomic, readonly) UIView* inputHider;
72 @property(nonatomic, readonly) UIView* keyboardViewContainer;
73 @property(nonatomic, readonly) UIView* keyboardView;
74 @property(nonatomic, assign) UIView* cachedFirstResponder;
75 @property(nonatomic, readonly) CGRect keyboardRect;
76 @property(nonatomic, readonly) BOOL pendingAutofillRemoval;
77 @property(nonatomic, readonly) BOOL pendingInputViewRemoval;
78 @property(nonatomic, readonly)
79 NSMutableDictionary<NSString*, FlutterTextInputView*>* autofillContext;
81 - (void)cleanUpViewHierarchy:(BOOL)includeActiveView
82 clearText:(BOOL)clearText
83 delayRemoval:(BOOL)delayRemoval;
84 - (NSArray<UIView*>*)textInputViews;
87 - (void)startLiveTextInput;
88 - (void)showKeyboardAndRemoveScreenshot;
94 class MockPlatformViewDelegate :
public PlatformView::Delegate {
96 void OnPlatformViewCreated(std::unique_ptr<Surface> surface)
override {}
97 void OnPlatformViewDestroyed()
override {}
98 void OnPlatformViewScheduleFrame()
override {}
99 void OnPlatformViewAddView(int64_t view_id,
100 const ViewportMetrics& viewport_metrics,
101 AddViewCallback callback)
override {}
102 void OnPlatformViewRemoveView(int64_t view_id, RemoveViewCallback callback)
override {}
103 void OnPlatformViewSendViewFocusEvent(
const ViewFocusEvent& event)
override {};
104 void OnPlatformViewSetNextFrameCallback(
const fml::closure& closure)
override {}
105 void OnPlatformViewSetViewportMetrics(int64_t view_id,
const ViewportMetrics& metrics)
override {}
106 const flutter::Settings& OnPlatformViewGetSettings()
const override {
return settings_; }
107 void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message)
override {}
108 void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet)
override {
110 HitTestResponse OnPlatformViewHitTest(int64_t view_id,
const flutter::PointData offset)
override {
111 return {.has_platform_view =
false};
113 void OnPlatformViewDispatchSemanticsAction(int64_t view_id,
115 SemanticsAction action,
116 fml::MallocMapping args)
override {}
117 void OnPlatformViewSetSemanticsEnabled(
bool enabled)
override {}
118 void OnPlatformViewSetAccessibilityFeatures(int32_t flags)
override {}
119 void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture)
override {}
120 void OnPlatformViewUnregisterTexture(int64_t
texture_id)
override {}
121 void OnPlatformViewMarkTextureFrameAvailable(int64_t
texture_id)
override {}
123 void LoadDartDeferredLibrary(intptr_t loading_unit_id,
124 std::unique_ptr<const fml::Mapping> snapshot_data,
125 std::unique_ptr<const fml::Mapping> snapshot_instructions)
override {
127 void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
128 const std::string error_message,
129 bool transient)
override {}
130 void UpdateAssetResolverByType(std::unique_ptr<flutter::AssetResolver> updated_asset_resolver,
131 flutter::AssetResolver::AssetResolverType type)
override {}
132 std::shared_ptr<fml::BasicTaskRunner> OnPlatformViewGetShutdownSafeIOTaskRunner()
const override {
146 NSDictionary* _template;
164 UIPasteboard.generalPasteboard.items = @[];
170 [textInputPlugin.autofillContext removeAllObjects];
171 [textInputPlugin cleanUpViewHierarchy:YES clearText:YES delayRemoval:NO];
172 [[[[textInputPlugin textInputView] superview] subviews]
173 makeObjectsPerformSelector:@selector(removeFromSuperview)];
178 - (void)setClientId:(
int)clientId configuration:(NSDictionary*)config {
181 arguments:@[ [NSNumber numberWithInt:clientId], config ]];
182 [textInputPlugin handleMethodCall:setClientCall
183 result:^(id _Nullable result){
187 - (void)setClientClear {
190 [textInputPlugin handleMethodCall:clearClientCall
191 result:^(id _Nullable result){
195 - (void)setTextInputShow {
198 [textInputPlugin handleMethodCall:setClientCall
199 result:^(id _Nullable result){
203 - (void)setTextInputHide {
206 [textInputPlugin handleMethodCall:setClientCall
207 result:^(id _Nullable result){
211 - (void)flushScheduledAsyncBlocks {
212 __block
bool done =
false;
213 XCTestExpectation* expectation =
214 [[XCTestExpectation alloc] initWithDescription:@"Testing on main queue"];
215 dispatch_async(dispatch_get_main_queue(), ^{
218 dispatch_async(dispatch_get_main_queue(), ^{
220 [expectation fulfill];
222 [
self waitForExpectations:@[ expectation ] timeout:10];
225 - (NSMutableDictionary*)mutableTemplateCopy {
228 @"inputType" : @{
@"name" :
@"TextInuptType.text"},
229 @"keyboardAppearance" :
@"Brightness.light",
230 @"obscureText" : @NO,
231 @"inputAction" :
@"TextInputAction.unspecified",
232 @"smartDashesType" :
@"0",
233 @"smartQuotesType" :
@"0",
234 @"autocorrect" : @YES,
235 @"enableInteractiveSelection" : @YES,
239 return [_template mutableCopy];
243 return (NSArray<FlutterTextInputView*>*)[textInputPlugin.textInputViews
244 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self isKindOfClass: %@",
248 - (
FlutterTextRange*)getLineRangeFromTokenizer:(
id<UITextInputTokenizer>)tokenizer
249 atIndex:(NSInteger)index {
252 withGranularity:UITextGranularityLine
253 inDirection:UITextLayoutDirectionRight];
258 - (void)updateConfig:(NSDictionary*)config {
261 [textInputPlugin handleMethodCall:updateConfigCall
262 result:^(id _Nullable result){
268 - (void)testWillNotCrashWhenViewControllerIsNil {
275 XCTestExpectation* expectation = [[XCTestExpectation alloc] initWithDescription:@"result called"];
278 result:^(id _Nullable result) {
279 XCTAssertNil(result);
280 [expectation fulfill];
282 XCTAssertNil(inputPlugin.activeView);
283 [
self waitForExpectations:@[ expectation ] timeout:1.0];
286 - (void)testInvokeStartLiveTextInput {
291 result:^(id _Nullable result){
293 OCMVerify([mockPlugin startLiveTextInput]);
296 - (void)testNoDanglingEnginePointer {
306 weakFlutterEngine = flutterEngine;
307 XCTAssertNotNil(weakFlutterEngine,
@"flutter engine must not be nil");
309 initWithDelegate:(id<FlutterTextInputDelegate>)flutterEngine];
310 weakFlutterTextInputPlugin = flutterTextInputPlugin;
314 NSDictionary* config =
self.mutableTemplateCopy;
317 arguments:@[ [NSNumber numberWithInt:123], config ]];
319 result:^(id _Nullable result){
321 currentView = flutterTextInputPlugin.activeView;
324 XCTAssertNil(weakFlutterEngine,
@"flutter engine must be nil");
325 XCTAssertNotNil(currentView,
@"current view must not be nil");
327 XCTAssertNil(weakFlutterTextInputPlugin);
330 XCTAssertNil(currentView.textInputDelegate);
333 - (void)testSecureInput {
334 NSDictionary* config =
self.mutableTemplateCopy;
335 [config setValue:@"YES" forKey:@"obscureText"];
336 [
self setClientId:123 configuration:config];
339 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
346 XCTAssertTrue(inputView.secureTextEntry);
349 XCTAssertEqual(inputView.keyboardType, UIKeyboardTypeDefault);
352 XCTAssertEqual(inputFields.count, 1ul);
360 XCTAssert(inputView.autofillId.length > 0);
363 - (void)testKeyboardType {
364 NSDictionary* config =
self.mutableTemplateCopy;
365 [config setValue:@{@"name" : @"TextInputType.url"} forKey:@"inputType"];
366 [
self setClientId:123 configuration:config];
369 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
374 XCTAssertEqual(inputView.keyboardType, UIKeyboardTypeURL);
377 - (void)testKeyboardTypeWebSearch {
378 NSDictionary* config =
self.mutableTemplateCopy;
379 [config setValue:@{@"name" : @"TextInputType.webSearch"} forKey:@"inputType"];
380 [
self setClientId:123 configuration:config];
383 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
388 XCTAssertEqual(inputView.keyboardType, UIKeyboardTypeWebSearch);
391 - (void)testKeyboardTypeTwitter {
392 NSDictionary* config =
self.mutableTemplateCopy;
393 [config setValue:@{@"name" : @"TextInputType.twitter"} forKey:@"inputType"];
394 [
self setClientId:123 configuration:config];
397 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
402 XCTAssertEqual(inputView.keyboardType, UIKeyboardTypeTwitter);
405 - (void)testVisiblePasswordUseAlphanumeric {
406 NSDictionary* config =
self.mutableTemplateCopy;
407 [config setValue:@{@"name" : @"TextInputType.visiblePassword"} forKey:@"inputType"];
408 [
self setClientId:123 configuration:config];
411 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
416 XCTAssertEqual(inputView.keyboardType, UIKeyboardTypeASCIICapable);
419 - (void)testSettingKeyboardTypeNoneDisablesSystemKeyboard {
420 NSDictionary* config =
self.mutableTemplateCopy;
421 [config setValue:@{@"name" : @"TextInputType.none"} forKey:@"inputType"];
422 [
self setClientId:123 configuration:config];
427 [config setValue:@{@"name" : @"TextInputType.url"} forKey:@"inputType"];
428 [
self setClientId:124 configuration:config];
433 - (void)testAutocorrectionPromptRectAppearsBeforeIOS17AndDoesNotAppearAfterIOS17 {
437 if (@available(iOS 17.0, *)) {
439 OCMVerify(never(), [
engine flutterTextInputView:inputView
440 showAutocorrectionPromptRectForStart:0
444 OCMVerify([
engine flutterTextInputView:inputView
445 showAutocorrectionPromptRectForStart:0
451 - (void)testIgnoresSelectionChangeIfSelectionIsDisabled {
453 __block
int updateCount = 0;
454 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withState:[OCMArg isNotNil]])
455 .andDo(^(NSInvocation* invocation) {
459 [inputView.text setString:@"Some initial text"];
460 XCTAssertEqual(updateCount, 0);
463 [inputView setSelectedTextRange:textRange];
464 XCTAssertEqual(updateCount, 1);
467 NSDictionary* config =
self.mutableTemplateCopy;
468 [config setValue:@(NO) forKey:@"enableInteractiveSelection"];
469 [config setValue:@(NO) forKey:@"obscureText"];
470 [config setValue:@(NO) forKey:@"enableDeltaModel"];
471 [inputView configureWithDictionary:config];
474 [inputView setSelectedTextRange:textRange];
476 XCTAssertEqual(updateCount, 1);
479 - (void)testAutocorrectionPromptRectDoesNotAppearDuringScribble {
481 if (@available(iOS 17.0, *)) {
485 if (@available(iOS 14.0, *)) {
488 __block
int callCount = 0;
489 OCMStub([
engine flutterTextInputView:inputView
490 showAutocorrectionPromptRectForStart:0
493 .andDo(^(NSInvocation* invocation) {
499 XCTAssertEqual(callCount, 1);
501 UIScribbleInteraction* scribbleInteraction =
502 [[UIScribbleInteraction alloc] initWithDelegate:inputView];
504 [inputView scribbleInteractionWillBeginWriting:scribbleInteraction];
508 XCTAssertEqual(callCount, 1);
510 [inputView scribbleInteractionDidFinishWriting:scribbleInteraction];
511 [inputView resetScribbleInteractionStatusIfEnding];
514 XCTAssertEqual(callCount, 2);
516 inputView.scribbleFocusStatus = FlutterScribbleFocusStatusFocusing;
520 XCTAssertEqual(callCount, 2);
522 inputView.scribbleFocusStatus = FlutterScribbleFocusStatusFocused;
526 XCTAssertEqual(callCount, 2);
528 inputView.scribbleFocusStatus = FlutterScribbleFocusStatusUnfocused;
531 XCTAssertEqual(callCount, 3);
535 - (void)testInputHiderOverlapWithTextWhenScribbleIsDisabledAfterIOS17AndDoesNotOverlapBeforeIOS17 {
541 arguments:@[ @(123),
self.mutableTemplateCopy ]];
543 result:^(id _Nullable result){
550 NSArray* yOffsetMatrix = @[ @1, @0, @0, @0, @0, @1, @0, @0, @0, @0, @1, @0, @0, @200, @0, @1 ];
554 arguments:@{@"transform" : yOffsetMatrix}];
556 result:^(id _Nullable result){
559 if (@available(iOS 17, *)) {
560 XCTAssert(CGRectEqualToRect(myInputPlugin.inputHider.frame, CGRectMake(0, 200, 0, 0)),
561 @"The input hider should overlap with the text on and after iOS 17");
564 XCTAssert(CGRectEqualToRect(myInputPlugin.inputHider.frame, CGRectZero),
565 @"The input hider should be on the origin of screen on and before iOS 16.");
569 - (void)testTextRangeFromPositionMatchesUITextViewBehavior {
575 toPosition:toPosition];
576 NSRange range = flutterRange.
range;
578 XCTAssertEqual(range.location, 0ul);
579 XCTAssertEqual(range.length, 2ul);
582 - (void)testTextInRange {
583 NSDictionary* config =
self.mutableTemplateCopy;
584 [config setValue:@{@"name" : @"TextInputType.url"} forKey:@"inputType"];
585 [
self setClientId:123 configuration:config];
586 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
589 [inputView insertText:@"test"];
592 NSString* substring = [inputView textInRange:range];
593 XCTAssertEqual(substring.length, 4ul);
596 substring = [inputView textInRange:range];
597 XCTAssertEqual(substring.length, 0ul);
600 - (void)testInsertTextReplacesEntireMarkedRangeForDeadKeySequence {
602 NSDictionary* config =
self.mutableTemplateCopy;
603 [
self setClientId:123 configuration:config];
604 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
607 [inputView setMarkedText:@"� �" selectedRange:NSMakeRange(1, 0)];
608 [inputView insertText:@"� �"];
610 UITextRange* range = [inputView textRangeFromPosition:inputView.beginningOfDocument
611 toPosition:inputView.endOfDocument];
612 NSString* substring = [inputView textInRange:range];
613 XCTAssertEqualObjects(substring,
@"Á");
616 - (void)testTextInRangeAcceptsNSNotFoundLocationGracefully {
617 NSDictionary* config =
self.mutableTemplateCopy;
618 [
self setClientId:123 configuration:config];
619 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
622 [inputView insertText:@"text"];
625 NSString* substring = [inputView textInRange:range];
626 XCTAssertNil(substring);
629 - (void)testStandardEditActions {
630 NSDictionary* config =
self.mutableTemplateCopy;
631 [
self setClientId:123 configuration:config];
632 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
635 [inputView insertText:@"aaaa"];
636 [inputView selectAll:nil];
638 [inputView insertText:@"bbbb"];
639 XCTAssertTrue([inputView canPerformAction:
@selector(paste:) withSender:nil]);
640 [inputView paste:nil];
641 [inputView selectAll:nil];
642 [inputView copy:nil];
643 [inputView paste:nil];
644 [inputView selectAll:nil];
645 [inputView delete:nil];
646 [inputView paste:nil];
647 [inputView paste:nil];
650 NSString* substring = [inputView textInRange:range];
651 XCTAssertEqualObjects(substring,
@"bbbbaaaabbbbaaaa");
654 - (void)testCanPerformActionForSelectActions {
655 NSDictionary* config =
self.mutableTemplateCopy;
656 [
self setClientId:123 configuration:config];
657 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
660 XCTAssertFalse([inputView canPerformAction:
@selector(selectAll:) withSender:nil]);
662 [inputView insertText:@"aaaa"];
664 XCTAssertTrue([inputView canPerformAction:
@selector(selectAll:) withSender:nil]);
667 - (void)testCanPerformActionCaptureTextFromCamera {
668 if (@available(iOS 15.0, *)) {
669 NSDictionary* config =
self.mutableTemplateCopy;
670 [
self setClientId:123 configuration:config];
671 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
674 [inputView becomeFirstResponder];
675 XCTAssertTrue([inputView canPerformAction:
@selector(captureTextFromCamera:) withSender:nil]);
677 [inputView insertText:@"test"];
678 [inputView selectAll:nil];
679 XCTAssertTrue([inputView canPerformAction:
@selector(captureTextFromCamera:) withSender:nil]);
683 - (void)testDeletingBackward {
684 NSDictionary* config =
self.mutableTemplateCopy;
685 [
self setClientId:123 configuration:config];
686 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
689 [inputView insertText:@"� ��� ��� text � ���� ���� ��� ���� ��� ���� ��� ���� ���� ���� ��� �� "];
690 [inputView deleteBackward];
691 [inputView deleteBackward];
694 XCTAssertEqualObjects(inputView.text,
@"ឹ😀 text 🥰👨👩👧👦🇺🇳ด");
695 [inputView deleteBackward];
696 XCTAssertEqualObjects(inputView.text,
@"ឹ😀 text 🥰👨👩👧👦🇺🇳");
697 [inputView deleteBackward];
698 XCTAssertEqualObjects(inputView.text,
@"ឹ😀 text 🥰👨👩👧👦");
699 [inputView deleteBackward];
700 XCTAssertEqualObjects(inputView.text,
@"ឹ😀 text 🥰");
701 [inputView deleteBackward];
703 XCTAssertEqualObjects(inputView.text,
@"ឹ😀 text ");
704 [inputView deleteBackward];
705 [inputView deleteBackward];
706 [inputView deleteBackward];
707 [inputView deleteBackward];
708 [inputView deleteBackward];
709 [inputView deleteBackward];
711 XCTAssertEqualObjects(inputView.text,
@"ឹ😀");
712 [inputView deleteBackward];
713 XCTAssertEqualObjects(inputView.text,
@"ឹ");
714 [inputView deleteBackward];
715 XCTAssertEqualObjects(inputView.text,
@"");
720 - (void)testSystemOnlyAddingPartialComposedCharacter {
721 NSDictionary* config =
self.mutableTemplateCopy;
722 [
self setClientId:123 configuration:config];
723 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
726 [inputView insertText:@"� ���� ��� ���� ��� ���� ��� ���"];
727 [inputView deleteBackward];
730 [inputView insertText:[@"� ���� ��� ���� ��� ���� ��� ���" substringWithRange:NSMakeRange(0, 1)]];
731 [inputView insertText:@"� ��"];
733 XCTAssertEqualObjects(inputView.text,
@"👨👩👧👦아");
736 [inputView deleteBackward];
739 [inputView insertText:@"� ���"];
740 [inputView deleteBackward];
742 [inputView insertText:[@"� ���" substringWithRange:NSMakeRange(0, 1)]];
743 [inputView insertText:@"� ��"];
744 XCTAssertEqualObjects(inputView.text,
@"👨👩👧👦😀아");
747 [inputView deleteBackward];
750 [inputView deleteBackward];
752 [inputView insertText:[@"� ���" substringWithRange:NSMakeRange(0, 1)]];
753 [inputView insertText:@"� ��"];
755 XCTAssertEqualObjects(inputView.text,
@"👨👩👧👦😀아");
758 - (void)testCachedComposedCharacterClearedAtKeyboardInteraction {
759 NSDictionary* config =
self.mutableTemplateCopy;
760 [
self setClientId:123 configuration:config];
761 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
764 [inputView insertText:@"� ���� ��� ���� ��� ���� ��� ���"];
765 [inputView deleteBackward];
766 [inputView shouldChangeTextInRange:OCMClassMock([UITextRange class]) replacementText:@""];
769 NSString* brokenEmoji = [@"� ���� ��� ���� ��� ���� ��� ���" substringWithRange:NSMakeRange(0, 1)];
770 [inputView insertText:brokenEmoji];
771 [inputView insertText:@"� ��"];
773 NSString* finalText = [NSString stringWithFormat:@"%@� ��", brokenEmoji];
774 XCTAssertEqualObjects(inputView.text, finalText);
777 - (void)testPastingNonTextDisallowed {
778 NSDictionary* config =
self.mutableTemplateCopy;
779 [
self setClientId:123 configuration:config];
780 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
783 UIPasteboard.generalPasteboard.color = UIColor.redColor;
784 XCTAssertNil(UIPasteboard.generalPasteboard.string);
785 XCTAssertFalse([inputView canPerformAction:
@selector(paste:) withSender:nil]);
786 [inputView paste:nil];
788 XCTAssertEqualObjects(inputView.text,
@"");
791 - (void)testNoZombies {
798 [passwordView.textField description];
800 XCTAssert([[passwordView.
textField description] containsString:
@"TextField"]);
803 - (void)testInputViewCrash {
808 initWithDelegate:(id<FlutterTextInputDelegate>)flutterEngine];
809 activeView = inputPlugin.activeView;
811 [activeView updateEditingState];
814 - (void)testDoNotReuseInputViews {
815 NSDictionary* config =
self.mutableTemplateCopy;
816 [
self setClientId:123 configuration:config];
818 [
self setClientId:456 configuration:config];
820 XCTAssertNotNil(currentView);
825 - (void)ensureOnlyActiveViewCanBecomeFirstResponder {
827 XCTAssertEqual(inputView.canBecomeFirstResponder, inputView ==
textInputPlugin.activeView);
831 - (void)testPropagatePressEventsToViewController {
833 OCMStub([mockViewController pressesBegan:[OCMArg isNotNil] withEvent:[OCMArg isNotNil]]);
834 OCMStub([mockViewController pressesEnded:[OCMArg isNotNil] withEvent:[OCMArg isNotNil]]);
838 NSDictionary* config =
self.mutableTemplateCopy;
839 [
self setClientId:123 configuration:config];
841 [
self setTextInputShow];
843 [currentView pressesBegan:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil]
844 withEvent:OCMClassMock([UIPressesEvent class])];
846 OCMVerify(times(1), [mockViewController pressesBegan:[OCMArg isNotNil]
847 withEvent:[OCMArg isNotNil]]);
848 OCMVerify(times(0), [mockViewController pressesEnded:[OCMArg isNotNil]
849 withEvent:[OCMArg isNotNil]]);
851 [currentView pressesEnded:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil]
852 withEvent:OCMClassMock([UIPressesEvent class])];
854 OCMVerify(times(1), [mockViewController pressesBegan:[OCMArg isNotNil]
855 withEvent:[OCMArg isNotNil]]);
856 OCMVerify(times(1), [mockViewController pressesEnded:[OCMArg isNotNil]
857 withEvent:[OCMArg isNotNil]]);
860 - (void)testPropagatePressEventsToViewController2 {
862 OCMStub([mockViewController pressesBegan:[OCMArg isNotNil] withEvent:[OCMArg isNotNil]]);
863 OCMStub([mockViewController pressesEnded:[OCMArg isNotNil] withEvent:[OCMArg isNotNil]]);
867 NSDictionary* config =
self.mutableTemplateCopy;
868 [
self setClientId:123 configuration:config];
869 [
self setTextInputShow];
872 [currentView pressesBegan:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil]
873 withEvent:OCMClassMock([UIPressesEvent class])];
875 OCMVerify(times(1), [mockViewController pressesBegan:[OCMArg isNotNil]
876 withEvent:[OCMArg isNotNil]]);
877 OCMVerify(times(0), [mockViewController pressesEnded:[OCMArg isNotNil]
878 withEvent:[OCMArg isNotNil]]);
881 [
self setClientId:321 configuration:config];
882 [
self setTextInputShow];
884 NSAssert(
textInputPlugin.activeView != currentView,
@"active view must change");
886 [currentView pressesEnded:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil]
887 withEvent:OCMClassMock([UIPressesEvent class])];
889 OCMVerify(times(1), [mockViewController pressesBegan:[OCMArg isNotNil]
890 withEvent:[OCMArg isNotNil]]);
891 OCMVerify(times(1), [mockViewController pressesEnded:[OCMArg isNotNil]
892 withEvent:[OCMArg isNotNil]]);
895 - (void)testHotRestart {
896 flutter::MockPlatformViewDelegate mock_platform_view_delegate;
897 auto thread = std::make_unique<fml::Thread>(
"TextInputHotRestart");
898 auto thread_task_runner = thread->GetTaskRunner();
899 flutter::TaskRunners runners(
self.name.UTF8String,
904 id mockFlutterView = OCMClassMock([
FlutterView class]);
907 OCMStub([mockFlutterViewController viewIfLoaded]).andReturn(mockFlutterView);
908 OCMStub([mockFlutterViewController
textInputPlugin]).andReturn(mockFlutterTextInputPlugin);
910 fml::AutoResetWaitableEvent latch;
911 thread_task_runner->PostTask([&] {
912 auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
913 mock_platform_view_delegate,
916 std::make_shared<fml::SyncSwitch>());
918 platform_view->SetOwnerViewController(mockFlutterViewController);
920 OCMExpect([mockFlutterTextInputPlugin reset]);
922 OCMVerifyAll(mockFlutterView);
929 - (void)testUpdateSecureTextEntry {
930 NSDictionary* config =
self.mutableTemplateCopy;
931 [config setValue:@"YES" forKey:@"obscureText"];
932 [
self setClientId:123 configuration:config];
934 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
937 __block
int callCount = 0;
938 OCMStub([inputView reloadInputViews]).andDo(^(NSInvocation* invocation) {
942 XCTAssertTrue(inputView.isSecureTextEntry);
944 config =
self.mutableTemplateCopy;
945 [config setValue:@"NO" forKey:@"obscureText"];
946 [
self updateConfig:config];
948 XCTAssertEqual(callCount, 1);
949 XCTAssertFalse(inputView.isSecureTextEntry);
952 - (void)testInputActionContinueAction {
968 arguments:@[ @(123), @"TextInputAction.continueAction" ]];
970 OCMVerify([mockBinaryMessenger sendOnChannel:
@"flutter/textinput" message:encodedMethodCall]);
973 - (void)testDisablingAutocorrectDisablesSpellChecking {
977 NSDictionary* config =
self.mutableTemplateCopy;
978 [inputView configureWithDictionary:config];
980 XCTAssertEqual(inputView.autocorrectionType, UITextAutocorrectionTypeDefault);
981 XCTAssertEqual(inputView.spellCheckingType, UITextSpellCheckingTypeDefault);
983 [config setValue:@(NO) forKey:@"autocorrect"];
984 [inputView configureWithDictionary:config];
986 XCTAssertEqual(inputView.autocorrectionType, UITextAutocorrectionTypeNo);
987 XCTAssertEqual(inputView.spellCheckingType, UITextSpellCheckingTypeNo);
990 - (void)testEnableInlinePredictionFromConfiguration
API_AVAILABLE(ios(17.0)) {
992 NSMutableDictionary* config =
self.mutableTemplateCopy;
995 [inputView configureWithDictionary:config];
996 XCTAssertEqual(inputView.inlinePredictionType, UITextInlinePredictionTypeNo);
998 [config setValue:@NO forKey:@"enableInlinePrediction"];
999 [inputView configureWithDictionary:config];
1000 XCTAssertEqual(inputView.inlinePredictionType, UITextInlinePredictionTypeNo);
1002 [config setValue:@YES forKey:@"enableInlinePrediction"];
1003 [inputView configureWithDictionary:config];
1004 XCTAssertEqual(inputView.inlinePredictionType, UITextInlinePredictionTypeYes);
1007 [config removeObjectForKey:@"enableInlinePrediction"];
1008 [inputView configureWithDictionary:config];
1009 XCTAssertEqual(inputView.inlinePredictionType, UITextInlinePredictionTypeNo);
1012 [config setValue:[NSNull null] forKey:@"enableInlinePrediction"];
1013 [inputView configureWithDictionary:config];
1014 XCTAssertEqual(inputView.inlinePredictionType, UITextInlinePredictionTypeNo);
1017 - (void)testReplaceTestLocalAdjustSelectionAndMarkedTextRange {
1019 [inputView setMarkedText:@"test text" selectedRange:NSMakeRange(0, 5)];
1033 XCTAssertEqual(inputView.markedTextRange, nil);
1036 - (void)testFlutterTextInputViewIsNotClearWhenKeyboardShowAndHide {
1039 [inputView setMarkedText:@"test text" selectedRange:NSMakeRange(0, 5)];
1040 XCTAssertEqualObjects(inputView.text,
@"test text");
1043 [
self setTextInputShow];
1044 XCTAssertEqualObjects(inputView.text,
@"test text");
1047 [
self setTextInputHide];
1048 XCTAssertEqualObjects(inputView.text,
@"test text");
1051 - (void)testFlutterTextInputViewOnlyRespondsToInsertionPointColorBelowIOS17 {
1055 SEL insertionPointColor = NSSelectorFromString(
@"insertionPointColor");
1056 BOOL respondsToInsertionPointColor = [inputView respondsToSelector:insertionPointColor];
1057 if (@available(iOS 17, *)) {
1058 XCTAssertFalse(respondsToInsertionPointColor);
1060 XCTAssertTrue(respondsToInsertionPointColor);
1064 - (void)testSetAttributedMarkedTextSelectedRange
API_AVAILABLE(ios(17.0)) {
1066 NSAttributedString* attributedText =
1067 [[NSAttributedString alloc] initWithString:@"inline prediction"
1069 NSForegroundColorAttributeName : [UIColor grayColor],
1071 [inputView setAttributedMarkedText:attributedText selectedRange:NSMakeRange(0, 7)];
1073 XCTAssertEqualObjects(inputView.text,
@"inline prediction");
1074 NSRange selectedRange = ((
FlutterTextRange*)inputView.selectedTextRange).range;
1075 XCTAssertEqual(selectedRange.location, 0ul);
1076 XCTAssertEqual(selectedRange.length, 7ul);
1078 XCTAssertNotNil(markedRange);
1079 XCTAssertEqual(markedRange.
range.location, 0ul);
1081 XCTAssertEqual(markedRange.
range.length, 17ul);
1084 [inputView setAttributedMarkedText:nil selectedRange:NSMakeRange(0, 0)];
1085 XCTAssertEqualObjects(inputView.text,
@"");
1086 XCTAssertNil(inputView.markedTextRange);
1089 #pragma mark - TextEditingDelta tests
1090 - (void)testTextEditingDeltasAreGeneratedOnTextInput {
1092 inputView.enableDeltaModel = YES;
1094 __block
int updateCount = 0;
1096 [inputView insertText:@"text to insert"];
1099 flutterTextInputView:inputView
1100 updateEditingClient:0
1101 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1102 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1103 isEqualToString:
@""]) &&
1104 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1105 isEqualToString:
@"text to insert"]) &&
1106 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"] intValue] == 0) &&
1107 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"] intValue] == 0);
1109 .andDo(^(NSInvocation* invocation) {
1112 XCTAssertEqual(updateCount, 0);
1114 [
self flushScheduledAsyncBlocks];
1117 XCTAssertEqual(updateCount, 1);
1119 [inputView deleteBackward];
1120 OCMExpect([
engine flutterTextInputView:inputView
1121 updateEditingClient:0
1122 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1123 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1124 isEqualToString:
@"text to insert"]) &&
1125 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1126 isEqualToString:
@""]) &&
1127 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"]
1129 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"]
1132 .andDo(^(NSInvocation* invocation) {
1135 [
self flushScheduledAsyncBlocks];
1136 XCTAssertEqual(updateCount, 2);
1139 OCMExpect([
engine flutterTextInputView:inputView
1140 updateEditingClient:0
1141 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1142 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1143 isEqualToString:
@"text to inser"]) &&
1144 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1145 isEqualToString:
@""]) &&
1146 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"]
1148 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"]
1151 .andDo(^(NSInvocation* invocation) {
1154 [
self flushScheduledAsyncBlocks];
1155 XCTAssertEqual(updateCount, 3);
1158 withText:@"replace text"];
1161 flutterTextInputView:inputView
1162 updateEditingClient:0
1163 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1164 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1165 isEqualToString:
@"text to inser"]) &&
1166 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1167 isEqualToString:
@"replace text"]) &&
1168 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"] intValue] == 0) &&
1169 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"] intValue] == 1);
1171 .andDo(^(NSInvocation* invocation) {
1174 [
self flushScheduledAsyncBlocks];
1175 XCTAssertEqual(updateCount, 4);
1177 [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
1178 OCMExpect([
engine flutterTextInputView:inputView
1179 updateEditingClient:0
1180 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1181 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1182 isEqualToString:
@"replace textext to inser"]) &&
1183 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1184 isEqualToString:
@"marked text"]) &&
1185 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"]
1187 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"]
1190 .andDo(^(NSInvocation* invocation) {
1193 [
self flushScheduledAsyncBlocks];
1194 XCTAssertEqual(updateCount, 5);
1196 [inputView unmarkText];
1198 flutterTextInputView:inputView
1199 updateEditingClient:0
1200 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1201 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1202 isEqualToString:
@"replace textmarked textext to inser"]) &&
1203 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1204 isEqualToString:
@""]) &&
1205 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"] intValue] ==
1207 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"] intValue] ==
1210 .andDo(^(NSInvocation* invocation) {
1213 [
self flushScheduledAsyncBlocks];
1215 XCTAssertEqual(updateCount, 6);
1219 - (void)testTextEditingDeltasAreBatchedAndForwardedToFramework {
1222 inputView.enableDeltaModel = YES;
1225 OCMExpect([
engine flutterTextInputView:inputView
1226 updateEditingClient:0
1227 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1228 NSArray* deltas = state[@"deltas"];
1229 NSDictionary* firstDelta = deltas[0];
1230 NSDictionary* secondDelta = deltas[1];
1231 NSDictionary* thirdDelta = deltas[2];
1232 return [firstDelta[@"oldText"] isEqualToString:@""] &&
1233 [firstDelta[@"deltaText"] isEqualToString:@"-"] &&
1234 [firstDelta[@"deltaStart"] intValue] == 0 &&
1235 [firstDelta[@"deltaEnd"] intValue] == 0 &&
1236 [secondDelta[@"oldText"] isEqualToString:@"-"] &&
1237 [secondDelta[@"deltaText"] isEqualToString:@""] &&
1238 [secondDelta[@"deltaStart"] intValue] == 0 &&
1239 [secondDelta[@"deltaEnd"] intValue] == 1 &&
1240 [thirdDelta[@"oldText"] isEqualToString:@""] &&
1241 [thirdDelta[@"deltaText"] isEqualToString:@"� ��"] &&
1242 [thirdDelta[@"deltaStart"] intValue] == 0 &&
1243 [thirdDelta[@"deltaEnd"] intValue] == 0;
1247 [inputView insertText:@"-"];
1248 [inputView deleteBackward];
1249 [inputView insertText:@"� ��"];
1251 [
self flushScheduledAsyncBlocks];
1255 - (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextReplacement {
1257 inputView.enableDeltaModel = YES;
1259 __block
int updateCount = 0;
1260 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withDelta:[OCMArg isNotNil]])
1261 .andDo(^(NSInvocation* invocation) {
1265 [inputView.text setString:@"Some initial text"];
1266 XCTAssertEqual(updateCount, 0);
1269 inputView.markedTextRange = range;
1270 inputView.selectedTextRange = nil;
1271 [
self flushScheduledAsyncBlocks];
1272 XCTAssertEqual(updateCount, 1);
1274 [inputView setMarkedText:@"new marked text." selectedRange:NSMakeRange(0, 1)];
1276 flutterTextInputView:inputView
1277 updateEditingClient:0
1278 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1279 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1280 isEqualToString:
@"Some initial text"]) &&
1281 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1282 isEqualToString:
@"new marked text."]) &&
1283 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"] intValue] == 13) &&
1284 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"] intValue] == 17);
1286 [
self flushScheduledAsyncBlocks];
1287 XCTAssertEqual(updateCount, 2);
1290 - (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextInsertion {
1292 inputView.enableDeltaModel = YES;
1294 __block
int updateCount = 0;
1295 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withDelta:[OCMArg isNotNil]])
1296 .andDo(^(NSInvocation* invocation) {
1300 [inputView.text setString:@"Some initial text"];
1301 [
self flushScheduledAsyncBlocks];
1302 XCTAssertEqual(updateCount, 0);
1305 inputView.markedTextRange = range;
1306 inputView.selectedTextRange = nil;
1307 [
self flushScheduledAsyncBlocks];
1308 XCTAssertEqual(updateCount, 1);
1310 [inputView setMarkedText:@"text." selectedRange:NSMakeRange(0, 1)];
1312 flutterTextInputView:inputView
1313 updateEditingClient:0
1314 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1315 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1316 isEqualToString:
@"Some initial text"]) &&
1317 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1318 isEqualToString:
@"text."]) &&
1319 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"] intValue] == 13) &&
1320 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"] intValue] == 17);
1322 [
self flushScheduledAsyncBlocks];
1323 XCTAssertEqual(updateCount, 2);
1326 - (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextDeletion {
1328 inputView.enableDeltaModel = YES;
1330 __block
int updateCount = 0;
1331 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withDelta:[OCMArg isNotNil]])
1332 .andDo(^(NSInvocation* invocation) {
1336 [inputView.text setString:@"Some initial text"];
1337 [
self flushScheduledAsyncBlocks];
1338 XCTAssertEqual(updateCount, 0);
1341 inputView.markedTextRange = range;
1342 inputView.selectedTextRange = nil;
1343 [
self flushScheduledAsyncBlocks];
1344 XCTAssertEqual(updateCount, 1);
1346 [inputView setMarkedText:@"tex" selectedRange:NSMakeRange(0, 1)];
1348 flutterTextInputView:inputView
1349 updateEditingClient:0
1350 withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1351 return ([[state[
@"deltas"] objectAtIndex:0][
@"oldText"]
1352 isEqualToString:
@"Some initial text"]) &&
1353 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaText"]
1354 isEqualToString:
@"tex"]) &&
1355 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaStart"] intValue] == 13) &&
1356 ([[state[
@"deltas"] objectAtIndex:0][
@"deltaEnd"] intValue] == 17);
1358 [
self flushScheduledAsyncBlocks];
1359 XCTAssertEqual(updateCount, 2);
1362 #pragma mark - EditingState tests
1364 - (void)testUITextInputCallsUpdateEditingStateOnce {
1367 __block
int updateCount = 0;
1368 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withState:[OCMArg isNotNil]])
1369 .andDo(^(NSInvocation* invocation) {
1373 [inputView insertText:@"text to insert"];
1375 XCTAssertEqual(updateCount, 1);
1377 [inputView deleteBackward];
1378 XCTAssertEqual(updateCount, 2);
1381 XCTAssertEqual(updateCount, 3);
1384 withText:@"replace text"];
1385 XCTAssertEqual(updateCount, 4);
1387 [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
1388 XCTAssertEqual(updateCount, 5);
1390 [inputView unmarkText];
1391 XCTAssertEqual(updateCount, 6);
1394 - (void)testUITextInputCallsUpdateEditingStateWithDeltaOnce {
1396 inputView.enableDeltaModel = YES;
1398 __block
int updateCount = 0;
1399 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withDelta:[OCMArg isNotNil]])
1400 .andDo(^(NSInvocation* invocation) {
1404 [inputView insertText:@"text to insert"];
1405 [
self flushScheduledAsyncBlocks];
1407 XCTAssertEqual(updateCount, 1);
1409 [inputView deleteBackward];
1410 [
self flushScheduledAsyncBlocks];
1411 XCTAssertEqual(updateCount, 2);
1414 [
self flushScheduledAsyncBlocks];
1415 XCTAssertEqual(updateCount, 3);
1418 withText:@"replace text"];
1419 [
self flushScheduledAsyncBlocks];
1420 XCTAssertEqual(updateCount, 4);
1422 [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
1423 [
self flushScheduledAsyncBlocks];
1424 XCTAssertEqual(updateCount, 5);
1426 [inputView unmarkText];
1427 [
self flushScheduledAsyncBlocks];
1428 XCTAssertEqual(updateCount, 6);
1431 - (void)testTextChangesDoNotTriggerUpdateEditingClient {
1434 __block
int updateCount = 0;
1435 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withState:[OCMArg isNotNil]])
1436 .andDo(^(NSInvocation* invocation) {
1440 [inputView.text setString:@"BEFORE"];
1441 XCTAssertEqual(updateCount, 0);
1443 inputView.markedTextRange = nil;
1444 inputView.selectedTextRange = nil;
1445 XCTAssertEqual(updateCount, 1);
1448 XCTAssertEqual(updateCount, 1);
1449 [inputView setTextInputState:@{@"text" : @"AFTER"}];
1450 XCTAssertEqual(updateCount, 1);
1451 [inputView setTextInputState:@{@"text" : @"AFTER"}];
1452 XCTAssertEqual(updateCount, 1);
1456 setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @0, @"selectionExtent" : @3}];
1457 XCTAssertEqual(updateCount, 1);
1459 setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @3}];
1460 XCTAssertEqual(updateCount, 1);
1464 setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @2}];
1465 XCTAssertEqual(updateCount, 1);
1467 setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}];
1468 XCTAssertEqual(updateCount, 1);
1471 - (void)testTextChangesDoNotTriggerUpdateEditingClientWithDelta {
1473 inputView.enableDeltaModel = YES;
1475 __block
int updateCount = 0;
1476 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withDelta:[OCMArg isNotNil]])
1477 .andDo(^(NSInvocation* invocation) {
1481 [inputView.text setString:@"BEFORE"];
1482 [
self flushScheduledAsyncBlocks];
1483 XCTAssertEqual(updateCount, 0);
1485 inputView.markedTextRange = nil;
1486 inputView.selectedTextRange = nil;
1487 [
self flushScheduledAsyncBlocks];
1488 XCTAssertEqual(updateCount, 1);
1491 XCTAssertEqual(updateCount, 1);
1492 [inputView setTextInputState:@{@"text" : @"AFTER"}];
1493 [
self flushScheduledAsyncBlocks];
1494 XCTAssertEqual(updateCount, 1);
1496 [inputView setTextInputState:@{@"text" : @"AFTER"}];
1497 [
self flushScheduledAsyncBlocks];
1498 XCTAssertEqual(updateCount, 1);
1502 setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @0, @"selectionExtent" : @3}];
1503 [
self flushScheduledAsyncBlocks];
1504 XCTAssertEqual(updateCount, 1);
1507 setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @3}];
1508 [
self flushScheduledAsyncBlocks];
1509 XCTAssertEqual(updateCount, 1);
1513 setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @2}];
1514 [
self flushScheduledAsyncBlocks];
1515 XCTAssertEqual(updateCount, 1);
1518 setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}];
1519 [
self flushScheduledAsyncBlocks];
1520 XCTAssertEqual(updateCount, 1);
1523 - (void)testUITextInputAvoidUnnecessaryUndateEditingClientCalls {
1526 __block
int updateCount = 0;
1527 OCMStub([
engine flutterTextInputView:inputView updateEditingClient:0 withState:[OCMArg isNotNil]])
1528 .andDo(^(NSInvocation* invocation) {
1532 [inputView unmarkText];
1534 XCTAssertEqual(updateCount, 0);
1536 [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
1538 XCTAssertEqual(updateCount, 1);
1540 [inputView unmarkText];
1542 XCTAssertEqual(updateCount, 2);
1545 - (void)testCanCopyPasteWithScribbleEnabled {
1546 if (@available(iOS 14.0, *)) {
1547 NSDictionary* config =
self.mutableTemplateCopy;
1548 [
self setClientId:123 configuration:config];
1549 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
1555 [mockInputView insertText:@"aaaa"];
1556 [mockInputView selectAll:nil];
1558 XCTAssertTrue([mockInputView canPerformAction:
@selector(copy:) withSender:NULL]);
1559 XCTAssertTrue([mockInputView canPerformAction:
@selector(copy:) withSender:
@"sender"]);
1560 XCTAssertFalse([mockInputView canPerformAction:
@selector(paste:) withSender:NULL]);
1561 XCTAssertFalse([mockInputView canPerformAction:
@selector(paste:) withSender:
@"sender"]);
1563 [mockInputView copy:NULL];
1564 XCTAssertTrue([mockInputView canPerformAction:
@selector(copy:) withSender:NULL]);
1565 XCTAssertTrue([mockInputView canPerformAction:
@selector(copy:) withSender:
@"sender"]);
1566 XCTAssertTrue([mockInputView canPerformAction:
@selector(paste:) withSender:NULL]);
1567 XCTAssertTrue([mockInputView canPerformAction:
@selector(paste:) withSender:
@"sender"]);
1571 - (void)testSetMarkedTextDuringScribbleDoesNotTriggerUpdateEditingClient {
1572 if (@available(iOS 14.0, *)) {
1575 __block
int updateCount = 0;
1576 OCMStub([
engine flutterTextInputView:inputView
1577 updateEditingClient:0
1578 withState:[OCMArg isNotNil]])
1579 .andDo(^(NSInvocation* invocation) {
1583 [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
1585 XCTAssertEqual(updateCount, 1);
1587 UIScribbleInteraction* scribbleInteraction =
1588 [[UIScribbleInteraction alloc] initWithDelegate:inputView];
1590 [inputView scribbleInteractionWillBeginWriting:scribbleInteraction];
1591 [inputView setMarkedText:@"during writing" selectedRange:NSMakeRange(1, 2)];
1593 XCTAssertEqual(updateCount, 1);
1595 [inputView scribbleInteractionDidFinishWriting:scribbleInteraction];
1596 [inputView resetScribbleInteractionStatusIfEnding];
1597 [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
1599 XCTAssertEqual(updateCount, 2);
1601 inputView.scribbleFocusStatus = FlutterScribbleFocusStatusFocusing;
1602 [inputView setMarkedText:@"during focus" selectedRange:NSMakeRange(1, 2)];
1605 XCTAssertEqual(updateCount, 2);
1607 inputView.scribbleFocusStatus = FlutterScribbleFocusStatusFocused;
1608 [inputView setMarkedText:@"after focus" selectedRange:NSMakeRange(2, 3)];
1611 XCTAssertEqual(updateCount, 2);
1613 inputView.scribbleFocusStatus = FlutterScribbleFocusStatusUnfocused;
1614 [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
1616 XCTAssertEqual(updateCount, 3);
1620 - (void)testUpdateEditingClientNegativeSelection {
1623 [inputView.text setString:@"SELECTION"];
1624 inputView.markedTextRange = nil;
1625 inputView.selectedTextRange = nil;
1627 [inputView setTextInputState:@{
1628 @"text" : @"SELECTION",
1629 @"selectionBase" : @-1,
1630 @"selectionExtent" : @-1
1632 [inputView updateEditingState];
1633 OCMVerify([
engine flutterTextInputView:inputView
1634 updateEditingClient:0
1635 withState:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1636 return ([state[
@"selectionBase"] intValue]) == 0 &&
1637 ([state[
@"selectionExtent"] intValue] == 0);
1642 setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @-1, @"selectionExtent" : @1}];
1643 [inputView updateEditingState];
1644 OCMVerify([
engine flutterTextInputView:inputView
1645 updateEditingClient:0
1646 withState:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1647 return ([state[
@"selectionBase"] intValue]) == 0 &&
1648 ([state[
@"selectionExtent"] intValue] == 0);
1652 setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @-1}];
1653 [inputView updateEditingState];
1654 OCMVerify([
engine flutterTextInputView:inputView
1655 updateEditingClient:0
1656 withState:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1657 return ([state[
@"selectionBase"] intValue]) == 0 &&
1658 ([state[
@"selectionExtent"] intValue] == 0);
1662 - (void)testUpdateEditingClientSelectionClamping {
1666 [inputView.text setString:@"SELECTION"];
1667 inputView.markedTextRange = nil;
1668 inputView.selectedTextRange = nil;
1671 setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @0, @"selectionExtent" : @0}];
1672 [inputView updateEditingState];
1673 OCMVerify([
engine flutterTextInputView:inputView
1674 updateEditingClient:0
1675 withState:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1676 return ([state[
@"selectionBase"] intValue]) == 0 &&
1677 ([state[
@"selectionExtent"] intValue] == 0);
1681 [inputView setTextInputState:@{
1682 @"text" : @"SELECTION",
1683 @"selectionBase" : @0,
1684 @"selectionExtent" : @9999
1686 [inputView updateEditingState];
1688 OCMVerify([
engine flutterTextInputView:inputView
1689 updateEditingClient:0
1690 withState:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1691 return ([state[
@"selectionBase"] intValue]) == 0 &&
1692 ([state[
@"selectionExtent"] intValue] == 9);
1697 setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @0}];
1698 [inputView updateEditingState];
1699 OCMVerify([
engine flutterTextInputView:inputView
1700 updateEditingClient:0
1701 withState:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1702 return ([state[
@"selectionBase"] intValue]) == 0 &&
1703 ([state[
@"selectionExtent"] intValue] == 1);
1707 [inputView setTextInputState:@{
1708 @"text" : @"SELECTION",
1709 @"selectionBase" : @9999,
1710 @"selectionExtent" : @9999
1712 [inputView updateEditingState];
1713 OCMVerify([
engine flutterTextInputView:inputView
1714 updateEditingClient:0
1715 withState:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
1716 return ([state[
@"selectionBase"] intValue]) == 9 &&
1717 ([state[
@"selectionExtent"] intValue] == 9);
1721 - (void)testRestoringPlatformFirstResponderNotifiesFrameworkOnce {
1723 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1724 [inputView setTextInputClient:123];
1726 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1727 OCMVerify(never(), [
engine flutterTextInputView:inputView
1728 didRestoreFirstResponderWithTextInputClient:123
1729 result:[OCMArg any]]);
1731 XCTAssertTrue([inputView resignFirstResponder]);
1732 OCMVerify(times(1), [
engine flutterTextInputView:inputView
1733 didResignFirstResponderWithTextInputClient:123]);
1734 OCMVerify(never(), [
engine flutterTextInputView:inputView
1735 didRestoreFirstResponderWithTextInputClient:123
1736 result:[OCMArg any]]);
1737 XCTAssertTrue([inputView becomeFirstResponder]);
1738 OCMVerify(times(1), [
engine flutterTextInputView:inputView
1739 didRestoreFirstResponderWithTextInputClient:123
1740 result:[OCMArg any]]);
1742 XCTAssertTrue([inputView becomeFirstResponder]);
1743 OCMVerify(times(1), [
engine flutterTextInputView:inputView
1744 didRestoreFirstResponderWithTextInputClient:123
1745 result:[OCMArg any]]);
1747 XCTAssertTrue([inputView resignFirstResponder]);
1748 OCMVerify(times(2), [
engine flutterTextInputView:inputView
1749 didResignFirstResponderWithTextInputClient:123]);
1750 OCMVerify(times(1), [
engine flutterTextInputView:inputView
1751 didRestoreFirstResponderWithTextInputClient:123
1752 result:[OCMArg any]]);
1753 XCTAssertTrue([inputView becomeFirstResponder]);
1754 OCMVerify(times(2), [
engine flutterTextInputView:inputView
1755 didRestoreFirstResponderWithTextInputClient:123
1756 result:[OCMArg any]]);
1758 [inputView resignFirstResponderFromFramework];
1759 [inputView removeFromSuperview];
1762 - (void)testFrameworkResponderChangesCancelPendingFocusRestore {
1764 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1765 [inputView setTextInputClient:123];
1767 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1768 XCTAssertTrue([inputView resignFirstResponderFromFramework]);
1769 XCTAssertTrue([inputView becomeFirstResponder]);
1771 XCTAssertTrue([inputView resignFirstResponder]);
1772 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1774 OCMVerify(never(), [
engine flutterTextInputView:inputView
1775 didRestoreFirstResponderWithTextInputClient:123
1776 result:[OCMArg any]]);
1778 [inputView resignFirstResponderFromFramework];
1779 [inputView removeFromSuperview];
1782 - (void)testInteractiveKeyboardResignDoesNotArmFocusRestore {
1784 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1785 [inputView setTextInputClient:123];
1787 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1788 inputView.preventCursorDismissWhenResignFirstResponder = YES;
1789 XCTAssertTrue([inputView resignFirstResponder]);
1790 inputView.preventCursorDismissWhenResignFirstResponder = NO;
1791 XCTAssertTrue([inputView becomeFirstResponder]);
1793 OCMVerify(never(), [
engine flutterTextInputView:inputView
1794 didResignFirstResponderWithTextInputClient:123]);
1795 OCMVerify(never(), [
engine flutterTextInputView:inputView
1796 didRestoreFirstResponderWithTextInputClient:123
1797 result:[OCMArg any]]);
1799 [inputView resignFirstResponderFromFramework];
1800 [inputView removeFromSuperview];
1803 - (void)testChangingClientCancelsPendingFocusRestore {
1805 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1806 [inputView setTextInputClient:123];
1808 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1809 XCTAssertTrue([inputView resignFirstResponder]);
1810 [inputView setTextInputClient:456];
1811 XCTAssertTrue([inputView becomeFirstResponder]);
1813 OCMVerify(never(), [
engine flutterTextInputView:inputView
1814 didRestoreFirstResponderWithTextInputClient:123
1815 result:[OCMArg any]]);
1816 OCMVerify(never(), [
engine flutterTextInputView:inputView
1817 didRestoreFirstResponderWithTextInputClient:456
1818 result:[OCMArg any]]);
1820 [inputView resignFirstResponderFromFramework];
1821 [inputView removeFromSuperview];
1824 - (void)testResettingSameClientCancelsPendingFocusRestore {
1826 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1827 [inputView setTextInputClient:123];
1829 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1830 XCTAssertTrue([inputView resignFirstResponder]);
1831 [inputView setTextInputClient:123];
1832 XCTAssertTrue([inputView becomeFirstResponder]);
1834 OCMVerify(never(), [
engine flutterTextInputView:inputView
1835 didRestoreFirstResponderWithTextInputClient:123
1836 result:[OCMArg any]]);
1838 [inputView resignFirstResponderFromFramework];
1839 [inputView removeFromSuperview];
1847 OCMStub([
engine flutterTextInputView:inputView
1848 didRestoreFirstResponderWithTextInputClient:123
1849 result:[OCMArg any]])
1850 .andDo(^(NSInvocation* invocation) {
1852 [invocation getArgument:&resultUnsafe atIndex:4];
1853 restoreResult = resultUnsafe;
1856 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1857 XCTAssertTrue([inputView resignFirstResponder]);
1858 XCTAssertTrue([inputView becomeFirstResponder]);
1859 XCTAssertNotNil(restoreResult);
1860 return restoreResult;
1863 - (void)testDeclinedFocusRestoreResignsFirstResponder {
1865 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1866 [inputView setTextInputClient:123];
1868 FlutterResult restoreResult = [
self restorePlatformFirstResponderOfInputView:inputView];
1871 XCTAssertFalse(inputView.isFirstResponder);
1872 OCMVerify(times(2), [
engine flutterTextInputView:inputView
1873 didResignFirstResponderWithTextInputClient:123]);
1876 XCTAssertTrue([inputView becomeFirstResponder]);
1877 OCMVerify(times(1), [
engine flutterTextInputView:inputView
1878 didRestoreFirstResponderWithTextInputClient:123
1879 result:[OCMArg any]]);
1881 [inputView resignFirstResponderFromFramework];
1882 [inputView removeFromSuperview];
1885 - (void)testAcceptedFocusRestoreKeepsFirstResponder {
1887 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1888 [inputView setTextInputClient:123];
1890 FlutterResult restoreResult = [
self restorePlatformFirstResponderOfInputView:inputView];
1891 restoreResult(@YES);
1893 XCTAssertTrue(inputView.isFirstResponder);
1894 OCMVerify(times(1), [
engine flutterTextInputView:inputView
1895 didResignFirstResponderWithTextInputClient:123]);
1897 [inputView resignFirstResponderFromFramework];
1898 [inputView removeFromSuperview];
1901 - (void)testFailedFocusRestoreReplyResignsFirstResponder {
1903 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1904 [inputView setTextInputClient:123];
1906 FlutterResult restoreResult = [
self restorePlatformFirstResponderOfInputView:inputView];
1907 restoreResult([
FlutterError errorWithCode:
@"error" message:nil details:nil]);
1909 XCTAssertFalse(inputView.isFirstResponder);
1911 [inputView removeFromSuperview];
1914 - (void)testChangingClientIgnoresPendingFocusRestoreReply {
1916 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1917 [inputView setTextInputClient:123];
1919 FlutterResult restoreResult = [
self restorePlatformFirstResponderOfInputView:inputView];
1920 [inputView setTextInputClient:456];
1923 XCTAssertTrue(inputView.isFirstResponder);
1925 [inputView resignFirstResponderFromFramework];
1926 [inputView removeFromSuperview];
1929 - (void)testPluginUsesFrameworkResponderChangesForExplicitTextInputMethods {
1930 [
self setClientId:123 configuration:self.mutableTemplateCopy];
1932 OCMStub([mockInputView becomeFirstResponderFromFramework]).andReturn(YES);
1933 OCMStub([mockInputView resignFirstResponderFromFramework]).andReturn(YES);
1935 [
self setTextInputShow];
1936 OCMVerify(times(1), [mockInputView becomeFirstResponderFromFramework]);
1938 [
self setTextInputHide];
1939 OCMVerify(times(1), [mockInputView resignFirstResponderFromFramework]);
1943 [textInputPlugin handleMethodCall:finishAutofillContextCall
1944 result:^(id _Nullable result){
1947 OCMVerify(times(2), [mockInputView resignFirstResponderFromFramework]);
1948 [mockInputView stopMocking];
1951 - (void)testInputViewsHasNonNilInputDelegate {
1953 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1955 [inputView setTextInputClient:123];
1956 [inputView reloadInputViews];
1957 [inputView becomeFirstResponder];
1958 NSAssert(inputView.isFirstResponder,
@"inputView is not first responder");
1959 inputView.inputDelegate = nil;
1963 setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}];
1964 OCMVerify([mockInputView setInputDelegate:[OCMArg isNotNil]]);
1965 [inputView removeFromSuperview];
1968 - (void)testInputViewsDoNotHaveUITextInteractions {
1970 BOOL hasTextInteraction = NO;
1971 for (
id interaction in inputView.interactions) {
1972 hasTextInteraction = [interaction isKindOfClass:[UITextInteraction class]];
1973 if (hasTextInteraction) {
1977 XCTAssertFalse(hasTextInteraction);
1980 #pragma mark - UITextInput methods - Tests
1982 - (void)testUpdateFirstRectForRange {
1983 [
self setClientId:123 configuration:self.mutableTemplateCopy];
1989 setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}];
1994 NSArray* yOffsetMatrix = @[ @1, @0, @0, @0, @0, @1, @0, @0, @0, @0, @1, @0, @0, @200, @0, @1 ];
1995 NSArray* zeroMatrix = @[ @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0 ];
1999 NSArray* affineMatrix = @[
2000 @(0.0), @(3.0), @(0.0), @(0.0), @(-3.0), @(0.0), @(0.0), @(0.0), @(0.0), @(0.0), @(3.0), @(0.0),
2001 @(-6.0), @(3.0), @(9.0), @(1.0)
2005 XCTAssertTrue(CGRectEqualToRect(
kInvalidFirstRect, [inputView firstRectForRange:range]));
2007 [inputView setEditableTransform:yOffsetMatrix];
2009 XCTAssertTrue(CGRectEqualToRect(
kInvalidFirstRect, [inputView firstRectForRange:range]));
2012 CGRect testRect = CGRectMake(0, 0, 100, 100);
2013 [inputView setMarkedRect:testRect];
2015 CGRect finalRect = CGRectOffset(testRect, 0, 200);
2016 XCTAssertTrue(CGRectEqualToRect(finalRect, [inputView firstRectForRange:range]));
2018 XCTAssertTrue(CGRectEqualToRect(finalRect, [inputView firstRectForRange:range]));
2021 [inputView setEditableTransform:zeroMatrix];
2023 XCTAssertTrue(CGRectEqualToRect(
kInvalidFirstRect, [inputView firstRectForRange:range]));
2024 XCTAssertTrue(CGRectEqualToRect(
kInvalidFirstRect, [inputView firstRectForRange:range]));
2027 [inputView setEditableTransform:yOffsetMatrix];
2028 [inputView setMarkedRect:testRect];
2029 XCTAssertTrue(CGRectEqualToRect(finalRect, [inputView firstRectForRange:range]));
2032 [inputView setMarkedRect:kInvalidFirstRect];
2034 XCTAssertTrue(CGRectEqualToRect(
kInvalidFirstRect, [inputView firstRectForRange:range]));
2035 XCTAssertTrue(CGRectEqualToRect(
kInvalidFirstRect, [inputView firstRectForRange:range]));
2038 [inputView setEditableTransform:affineMatrix];
2039 [inputView setMarkedRect:testRect];
2041 CGRectEqualToRect(CGRectMake(-306, 3, 300, 300), [inputView firstRectForRange:range]));
2043 NSAssert(inputView.superview,
@"inputView is not in the view hierarchy!");
2044 const CGPoint offset = CGPointMake(113, 119);
2045 CGRect currentFrame = inputView.frame;
2046 currentFrame.origin = offset;
2047 inputView.frame = currentFrame;
2050 XCTAssertTrue(CGRectEqualToRect(CGRectMake(-306 - 113, 3 - 119, 300, 300),
2051 [inputView firstRectForRange:range]));
2054 - (void)testFirstRectForRangeReturnsNoneZeroRectWhenScribbleIsEnabled {
2056 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2061 [inputView setSelectionRects:@[
2070 if (@available(iOS 17, *)) {
2071 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, 0, 300, 100),
2072 [inputView firstRectForRange:multiRectRange]));
2074 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, 0, 100, 100),
2075 [inputView firstRectForRange:multiRectRange]));
2079 - (void)testFirstRectForRangeReturnsCorrectRectOnASingleLineLeftToRight {
2081 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2083 [inputView setSelectionRects:@[
2090 if (@available(iOS 17, *)) {
2091 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, 0, 100, 100),
2092 [inputView firstRectForRange:singleRectRange]));
2094 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:singleRectRange]));
2099 if (@available(iOS 17, *)) {
2100 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, 0, 300, 100),
2101 [inputView firstRectForRange:multiRectRange]));
2103 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2106 [inputView setTextInputState:@{@"text" : @"COM"}];
2108 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:rangeOutsideBounds]));
2111 - (void)testFirstRectForRangeReturnsCorrectRectOnASingleLineRightToLeft {
2113 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2115 [inputView setSelectionRects:@[
2122 if (@available(iOS 17, *)) {
2123 XCTAssertTrue(CGRectEqualToRect(CGRectMake(200, 0, 100, 100),
2124 [inputView firstRectForRange:singleRectRange]));
2126 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:singleRectRange]));
2130 if (@available(iOS 17, *)) {
2131 XCTAssertTrue(CGRectEqualToRect(CGRectMake(0, 0, 300, 100),
2132 [inputView firstRectForRange:multiRectRange]));
2134 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2137 [inputView setTextInputState:@{@"text" : @"COM"}];
2139 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:rangeOutsideBounds]));
2142 - (void)testFirstRectForRangeReturnsCorrectRectOnMultipleLinesLeftToRight {
2144 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2146 [inputView setSelectionRects:@[
2157 if (@available(iOS 17, *)) {
2158 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, 0, 100, 100),
2159 [inputView firstRectForRange:singleRectRange]));
2161 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:singleRectRange]));
2166 if (@available(iOS 17, *)) {
2167 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, 0, 300, 100),
2168 [inputView firstRectForRange:multiRectRange]));
2170 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2174 - (void)testFirstRectForRangeReturnsCorrectRectOnMultipleLinesRightToLeft {
2176 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2178 [inputView setSelectionRects:@[
2189 if (@available(iOS 17, *)) {
2190 XCTAssertTrue(CGRectEqualToRect(CGRectMake(200, 0, 100, 100),
2191 [inputView firstRectForRange:singleRectRange]));
2193 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:singleRectRange]));
2197 if (@available(iOS 17, *)) {
2198 XCTAssertTrue(CGRectEqualToRect(CGRectMake(0, 0, 300, 100),
2199 [inputView firstRectForRange:multiRectRange]));
2201 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2205 - (void)testFirstRectForRangeReturnsCorrectRectOnSingleLineWithVaryingMinYAndMaxYLeftToRight {
2207 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2209 [inputView setSelectionRects:@[
2220 if (@available(iOS 17, *)) {
2221 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, -10, 300, 120),
2222 [inputView firstRectForRange:multiRectRange]));
2224 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2228 - (void)testFirstRectForRangeReturnsCorrectRectOnSingleLineWithVaryingMinYAndMaxYRightToLeft {
2230 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2232 [inputView setSelectionRects:@[
2243 if (@available(iOS 17, *)) {
2244 XCTAssertTrue(CGRectEqualToRect(CGRectMake(0, -10, 300, 120),
2245 [inputView firstRectForRange:multiRectRange]));
2247 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2251 - (void)testFirstRectForRangeReturnsCorrectRectWithOverlappingRectsExceedingThresholdLeftToRight {
2253 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2255 [inputView setSelectionRects:@[
2266 if (@available(iOS 17, *)) {
2267 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, 0, 300, 100),
2268 [inputView firstRectForRange:multiRectRange]));
2270 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2274 - (void)testFirstRectForRangeReturnsCorrectRectWithOverlappingRectsExceedingThresholdRightToLeft {
2276 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2278 [inputView setSelectionRects:@[
2289 if (@available(iOS 17, *)) {
2290 XCTAssertTrue(CGRectEqualToRect(CGRectMake(0, 0, 300, 100),
2291 [inputView firstRectForRange:multiRectRange]));
2293 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2297 - (void)testFirstRectForRangeReturnsCorrectRectWithOverlappingRectsWithinThresholdLeftToRight {
2299 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2301 [inputView setSelectionRects:@[
2312 if (@available(iOS 17, *)) {
2313 XCTAssertTrue(CGRectEqualToRect(CGRectMake(100, 0, 400, 140),
2314 [inputView firstRectForRange:multiRectRange]));
2316 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2320 - (void)testFirstRectForRangeReturnsCorrectRectWithOverlappingRectsWithinThresholdRightToLeft {
2322 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2324 [inputView setSelectionRects:@[
2335 if (@available(iOS 17, *)) {
2336 XCTAssertTrue(CGRectEqualToRect(CGRectMake(0, 0, 400, 140),
2337 [inputView firstRectForRange:multiRectRange]));
2339 XCTAssertTrue(CGRectEqualToRect(CGRectZero, [inputView firstRectForRange:multiRectRange]));
2343 - (void)testClosestPositionToPoint {
2345 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2348 [inputView setSelectionRects:@[
2353 CGPoint point = CGPointMake(150, 150);
2354 XCTAssertEqual(2U, ((
FlutterTextPosition*)[inputView closestPositionToPoint:point]).index);
2355 XCTAssertEqual(UITextStorageDirectionBackward,
2360 [inputView setSelectionRects:@[
2367 point = CGPointMake(125, 150);
2368 XCTAssertEqual(2U, ((
FlutterTextPosition*)[inputView closestPositionToPoint:point]).index);
2369 XCTAssertEqual(UITextStorageDirectionForward,
2374 [inputView setSelectionRects:@[
2381 point = CGPointMake(125, 201);
2382 XCTAssertEqual(4U, ((
FlutterTextPosition*)[inputView closestPositionToPoint:point]).index);
2383 XCTAssertEqual(UITextStorageDirectionBackward,
2387 [inputView setSelectionRects:@[
2393 point = CGPointMake(125, 250);
2394 XCTAssertEqual(4U, ((
FlutterTextPosition*)[inputView closestPositionToPoint:point]).index);
2395 XCTAssertEqual(UITextStorageDirectionBackward,
2399 [inputView setSelectionRects:@[
2404 point = CGPointMake(110, 50);
2405 XCTAssertEqual(2U, ((
FlutterTextPosition*)[inputView closestPositionToPoint:point]).index);
2406 XCTAssertEqual(UITextStorageDirectionForward,
2411 [inputView beginFloatingCursorAtPoint:CGPointZero];
2412 XCTAssertEqual(1U, ((
FlutterTextPosition*)[inputView closestPositionToPoint:point]).index);
2413 XCTAssertEqual(UITextStorageDirectionForward,
2415 [inputView endFloatingCursor];
2418 - (void)testClosestPositionToPointRTL {
2420 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2422 [inputView setSelectionRects:@[
2438 XCTAssertEqual(0U, position.
index);
2439 XCTAssertEqual(UITextStorageDirectionForward, position.
affinity);
2441 XCTAssertEqual(1U, position.
index);
2442 XCTAssertEqual(UITextStorageDirectionBackward, position.
affinity);
2444 XCTAssertEqual(1U, position.
index);
2445 XCTAssertEqual(UITextStorageDirectionForward, position.
affinity);
2447 XCTAssertEqual(2U, position.
index);
2448 XCTAssertEqual(UITextStorageDirectionBackward, position.
affinity);
2450 XCTAssertEqual(2U, position.
index);
2451 XCTAssertEqual(UITextStorageDirectionForward, position.
affinity);
2453 XCTAssertEqual(3U, position.
index);
2454 XCTAssertEqual(UITextStorageDirectionBackward, position.
affinity);
2456 XCTAssertEqual(3U, position.
index);
2457 XCTAssertEqual(UITextStorageDirectionBackward, position.
affinity);
2460 - (void)testSelectionRectsForRange {
2462 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2464 CGRect testRect0 = CGRectMake(100, 100, 100, 100);
2465 CGRect testRect1 = CGRectMake(200, 200, 100, 100);
2466 [inputView setSelectionRects:@[
2475 XCTAssertTrue(CGRectEqualToRect(testRect0, [inputView selectionRectsForRange:range][0].rect));
2476 XCTAssertTrue(CGRectEqualToRect(testRect1, [inputView selectionRectsForRange:range][1].rect));
2477 XCTAssertEqual(2U, [[inputView selectionRectsForRange:range] count]);
2481 XCTAssertEqual(1U, [[inputView selectionRectsForRange:range] count]);
2482 XCTAssertTrue(CGRectEqualToRect(
2483 CGRectMake(testRect0.origin.x, testRect0.origin.y, 0, testRect0.size.height),
2484 [inputView selectionRectsForRange:range][0].rect));
2487 - (void)testClosestPositionToPointWithinRange {
2489 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2492 [inputView setSelectionRects:@[
2499 CGPoint point = CGPointMake(125, 150);
2502 3U, ((
FlutterTextPosition*)[inputView closestPositionToPoint:point withinRange:range]).index);
2504 UITextStorageDirectionForward,
2505 ((
FlutterTextPosition*)[inputView closestPositionToPoint:point withinRange:range]).affinity);
2508 [inputView setSelectionRects:@[
2515 point = CGPointMake(125, 150);
2518 1U, ((
FlutterTextPosition*)[inputView closestPositionToPoint:point withinRange:range]).index);
2520 UITextStorageDirectionForward,
2521 ((
FlutterTextPosition*)[inputView closestPositionToPoint:point withinRange:range]).affinity);
2524 - (void)testClosestPositionToPointWithPartialSelectionRects {
2526 [inputView setTextInputState:@{@"text" : @"COMPOSING"}];
2533 XCTAssertTrue(CGRectEqualToRect(
2536 affinity:UITextStorageDirectionForward]],
2537 CGRectMake(100, 0, 0, 100)));
2540 XCTAssertTrue(CGRectEqualToRect(
2543 affinity:UITextStorageDirectionForward]],
2547 #pragma mark - Floating Cursor - Tests
2549 - (void)testFloatingCursorDoesNotThrow {
2552 [inputView beginFloatingCursorAtPoint:CGPointMake(123, 321)];
2553 [inputView beginFloatingCursorAtPoint:CGPointMake(123, 321)];
2554 [inputView endFloatingCursor];
2555 [inputView beginFloatingCursorAtPoint:CGPointMake(123, 321)];
2556 [inputView endFloatingCursor];
2559 - (void)testFloatingCursor {
2561 [inputView setTextInputState:@{
2563 @"selectionBase" : @1,
2564 @"selectionExtent" : @1,
2575 [inputView setSelectionRects:@[ first, second, third, fourth ]];
2578 XCTAssertTrue(CGRectEqualToRect(
2581 affinity:UITextStorageDirectionForward]],
2582 CGRectMake(0, 0, 0, 100)));
2585 XCTAssertTrue(CGRectEqualToRect(
2588 affinity:UITextStorageDirectionForward]],
2589 CGRectMake(100, 100, 0, 100)));
2590 XCTAssertTrue(CGRectEqualToRect(
2593 affinity:UITextStorageDirectionForward]],
2594 CGRectMake(200, 200, 0, 100)));
2595 XCTAssertTrue(CGRectEqualToRect(
2598 affinity:UITextStorageDirectionForward]],
2599 CGRectMake(300, 300, 0, 100)));
2602 XCTAssertTrue(CGRectEqualToRect(
2605 affinity:UITextStorageDirectionForward]],
2606 CGRectMake(400, 300, 0, 100)));
2608 XCTAssertTrue(CGRectEqualToRect(
2611 affinity:UITextStorageDirectionForward]],
2615 [inputView setTextInputState:@{
2617 @"selectionBase" : @2,
2618 @"selectionExtent" : @2,
2621 XCTAssertTrue(CGRectEqualToRect(
2624 affinity:UITextStorageDirectionBackward]],
2625 CGRectMake(0, 0, 0, 100)));
2628 XCTAssertTrue(CGRectEqualToRect(
2631 affinity:UITextStorageDirectionBackward]],
2632 CGRectMake(100, 0, 0, 100)));
2633 XCTAssertTrue(CGRectEqualToRect(
2636 affinity:UITextStorageDirectionBackward]],
2637 CGRectMake(200, 100, 0, 100)));
2638 XCTAssertTrue(CGRectEqualToRect(
2641 affinity:UITextStorageDirectionBackward]],
2642 CGRectMake(300, 200, 0, 100)));
2643 XCTAssertTrue(CGRectEqualToRect(
2646 affinity:UITextStorageDirectionBackward]],
2647 CGRectMake(400, 300, 0, 100)));
2649 XCTAssertTrue(CGRectEqualToRect(
2652 affinity:UITextStorageDirectionBackward]],
2657 CGRect initialBounds = inputView.bounds;
2658 [inputView beginFloatingCursorAtPoint:CGPointMake(123, 321)];
2659 XCTAssertTrue(CGRectEqualToRect(initialBounds, inputView.bounds));
2660 OCMVerify([
engine flutterTextInputView:inputView
2661 updateFloatingCursor:FlutterFloatingCursorDragStateStart
2663 withPosition:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
2664 return ([state[
@"X"] isEqualToNumber:@(0)]) &&
2665 ([state[
@"Y"] isEqualToNumber:@(0)]);
2668 [inputView updateFloatingCursorAtPoint:CGPointMake(456, 654)];
2669 XCTAssertTrue(CGRectEqualToRect(initialBounds, inputView.bounds));
2670 OCMVerify([
engine flutterTextInputView:inputView
2671 updateFloatingCursor:FlutterFloatingCursorDragStateUpdate
2673 withPosition:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
2674 return ([state[
@"X"] isEqualToNumber:@(333)]) &&
2675 ([state[
@"Y"] isEqualToNumber:@(333)]);
2678 [inputView endFloatingCursor];
2679 XCTAssertTrue(CGRectEqualToRect(initialBounds, inputView.bounds));
2680 OCMVerify([
engine flutterTextInputView:inputView
2681 updateFloatingCursor:FlutterFloatingCursorDragStateEnd
2683 withPosition:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
2684 return ([state[
@"X"] isEqualToNumber:@(0)]) &&
2685 ([state[
@"Y"] isEqualToNumber:@(0)]);
2689 #pragma mark - UIKeyInput Overrides - Tests
2691 - (void)testInsertTextAddsPlaceholderSelectionRects {
2694 setTextInputState:@{@"text" : @"test", @"selectionBase" : @1, @"selectionExtent" : @1}];
2704 [inputView setSelectionRects:@[ first, second, third, fourth ]];
2707 [inputView insertText:@"in"];
2735 #pragma mark - Autofill - Utilities
2737 - (NSMutableDictionary*)mutablePasswordTemplateCopy {
2740 @"inputType" : @{
@"name" :
@"TextInuptType.text"},
2741 @"keyboardAppearance" :
@"Brightness.light",
2742 @"obscureText" : @YES,
2743 @"inputAction" :
@"TextInputAction.unspecified",
2744 @"smartDashesType" :
@"0",
2745 @"smartQuotesType" :
@"0",
2746 @"autocorrect" : @YES
2750 return [_passwordTemplate mutableCopy];
2754 return [
self.installedInputViews
2755 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isVisibleToAutofill == YES"]];
2758 - (void)commitAutofillContextAndVerify {
2762 [textInputPlugin handleMethodCall:methodCall
2763 result:^(id _Nullable result){
2766 XCTAssertEqual(
self.viewsVisibleToAutofill.count,
2771 XCTAssertEqual(
self.installedInputViews.count, 1ul);
2775 #pragma mark - Autofill - Tests
2777 - (void)testDisablingAutofillOnInputClient {
2778 NSDictionary* config =
self.mutableTemplateCopy;
2779 [config setValue:@"YES" forKey:@"obscureText"];
2781 [
self setClientId:123 configuration:config];
2784 XCTAssertEqualObjects(inputView.textContentType,
@"");
2787 - (void)testAutofillEnabledByDefault {
2788 NSDictionary* config =
self.mutableTemplateCopy;
2789 [config setValue:@"NO" forKey:@"obscureText"];
2790 [config setValue:@{@"uniqueIdentifier" : @"field1", @"editingValue" : @{@"text" : @""}}
2791 forKey:@"autofill"];
2793 [
self setClientId:123 configuration:config];
2796 XCTAssertNil(inputView.textContentType);
2799 - (void)testAutofillContext {
2800 NSMutableDictionary* field1 =
self.mutableTemplateCopy;
2803 @"uniqueIdentifier" : @"field1",
2804 @"hints" : @[ @"hint1" ],
2805 @"editingValue" : @{@"text" : @""}
2807 forKey:@"autofill"];
2809 NSMutableDictionary* field2 =
self.mutablePasswordTemplateCopy;
2811 @"uniqueIdentifier" : @"field2",
2812 @"hints" : @[ @"hint2" ],
2813 @"editingValue" : @{@"text" : @""}
2815 forKey:@"autofill"];
2817 NSMutableDictionary* config = [field1 mutableCopy];
2818 [config setValue:@[ field1, field2 ] forKey:@"fields"];
2820 [
self setClientId:123 configuration:config];
2821 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 2ul);
2825 [textInputPlugin cleanUpViewHierarchy:NO clearText:YES delayRemoval:NO];
2826 XCTAssertEqual(
self.installedInputViews.count, 2ul);
2828 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2831 NSMutableDictionary* field3 =
self.mutablePasswordTemplateCopy;
2833 @"uniqueIdentifier" : @"field3",
2834 @"hints" : @[ @"hint3" ],
2835 @"editingValue" : @{@"text" : @""}
2837 forKey:@"autofill"];
2841 [config setValue:@[ field1, field3 ] forKey:@"fields"];
2843 [
self setClientId:123 configuration:config];
2845 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 2ul);
2848 [textInputPlugin cleanUpViewHierarchy:NO clearText:YES delayRemoval:NO];
2849 XCTAssertEqual(
self.installedInputViews.count, 3ul);
2851 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2854 for (NSString* key in oldContext.allKeys) {
2855 XCTAssertEqual(oldContext[key],
textInputPlugin.autofillContext[key]);
2859 config =
self.mutablePasswordTemplateCopy;
2862 [
self setClientId:124 configuration:config];
2863 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2865 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 1ul);
2868 [textInputPlugin cleanUpViewHierarchy:NO clearText:YES delayRemoval:NO];
2869 XCTAssertEqual(
self.installedInputViews.count, 4ul);
2872 for (NSString* key in oldContext.allKeys) {
2873 XCTAssertEqual(oldContext[key],
textInputPlugin.autofillContext[key]);
2877 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2881 [
self setClientId:200 configuration:config];
2884 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 1ul);
2887 [textInputPlugin cleanUpViewHierarchy:NO clearText:YES delayRemoval:NO];
2888 XCTAssertEqual(
self.installedInputViews.count, 4ul);
2891 for (NSString* key in oldContext.allKeys) {
2892 XCTAssertEqual(oldContext[key],
textInputPlugin.autofillContext[key]);
2895 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2898 - (void)testCommitAutofillContext {
2899 NSMutableDictionary* field1 =
self.mutableTemplateCopy;
2901 @"uniqueIdentifier" : @"field1",
2902 @"hints" : @[ @"hint1" ],
2903 @"editingValue" : @{@"text" : @""}
2905 forKey:@"autofill"];
2907 NSMutableDictionary* field2 =
self.mutablePasswordTemplateCopy;
2909 @"uniqueIdentifier" : @"field2",
2910 @"hints" : @[ @"hint2" ],
2911 @"editingValue" : @{@"text" : @""}
2913 forKey:@"autofill"];
2915 NSMutableDictionary* field3 =
self.mutableTemplateCopy;
2917 @"uniqueIdentifier" : @"field3",
2918 @"hints" : @[ @"hint3" ],
2919 @"editingValue" : @{@"text" : @""}
2921 forKey:@"autofill"];
2923 NSMutableDictionary* config = [field1 mutableCopy];
2924 [config setValue:@[ field1, field2 ] forKey:@"fields"];
2926 [
self setClientId:123 configuration:config];
2927 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 2ul);
2929 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2931 [
self commitAutofillContextAndVerify];
2932 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2935 [
self setClientId:123 configuration:config];
2937 [
self setClientId:124 configuration:field3];
2938 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 1ul);
2940 [textInputPlugin cleanUpViewHierarchy:NO clearText:YES delayRemoval:NO];
2941 XCTAssertEqual(
self.installedInputViews.count, 3ul);
2944 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2946 [
self commitAutofillContextAndVerify];
2947 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2950 [
self setClientId:125 configuration:self.mutableTemplateCopy];
2952 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 0ul);
2956 [textInputPlugin cleanUpViewHierarchy:NO clearText:YES delayRemoval:NO];
2957 XCTAssertEqual(
self.installedInputViews.count, 1ul);
2959 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2961 [
self commitAutofillContextAndVerify];
2962 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2965 - (void)testAutofillInputViews {
2966 NSMutableDictionary* field1 =
self.mutableTemplateCopy;
2968 @"uniqueIdentifier" : @"field1",
2969 @"hints" : @[ @"hint1" ],
2970 @"editingValue" : @{@"text" : @""}
2972 forKey:@"autofill"];
2974 NSMutableDictionary* field2 =
self.mutablePasswordTemplateCopy;
2976 @"uniqueIdentifier" : @"field2",
2977 @"hints" : @[ @"hint2" ],
2978 @"editingValue" : @{@"text" : @""}
2980 forKey:@"autofill"];
2982 NSMutableDictionary* config = [field1 mutableCopy];
2983 [config setValue:@[ field1, field2 ] forKey:@"fields"];
2985 [
self setClientId:123 configuration:config];
2986 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
2989 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
2992 XCTAssertEqual(inputFields.count, 2ul);
2993 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 2ul);
2998 withText:@"Autofilled!"];
2999 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
3002 OCMVerify([
engine flutterTextInputView:inactiveView
3003 updateEditingClient:0
3004 withState:[OCMArg isNotNil]
3005 withTag:
@"field2"]);
3008 - (void)testAutofillContextPersistsAfterClearClient {
3009 NSMutableDictionary* field1 =
self.mutableTemplateCopy;
3011 @"uniqueIdentifier" : @"field1",
3012 @"hints" : @[ @"username" ],
3013 @"editingValue" : @{@"text" : @""}
3015 forKey:@"autofill"];
3017 NSMutableDictionary* field2 =
self.mutablePasswordTemplateCopy;
3019 @"uniqueIdentifier" : @"field2",
3020 @"hints" : @[ @"password" ],
3021 @"editingValue" : @{@"text" : @""}
3023 forKey:@"autofill"];
3025 NSMutableDictionary* config = [field1 mutableCopy];
3026 [config setValue:@[ field1, field2 ] forKey:@"fields"];
3029 [
self setClientId:123 configuration:config];
3034 [
self setClientClear];
3039 [
self commitAutofillContextAndVerify];
3044 - (void)testSetClientResetsPendingAutofillRemoval {
3048 NSMutableDictionary* field =
self.mutablePasswordTemplateCopy;
3050 @"uniqueIdentifier" : @"field1",
3051 @"hints" : @[ @"password" ],
3052 @"editingValue" : @{@"text" : @""}
3054 forKey:@"autofill"];
3055 [field setValue:@[ field ] forKey:@"fields"];
3058 [
self setClientId:123 configuration:field];
3062 [
self setClientClear];
3066 [
self setClientId:456 configuration:self.mutableTemplateCopy];
3071 - (void)testPendingInputViewRemovalAfterClearClient {
3075 NSDictionary* config =
self.mutableTemplateCopy;
3078 [
self setClientId:123 configuration:config];
3085 OCMStub([mockActiveView isFirstResponder]).andReturn(YES);
3088 [
self setClientClear];
3093 [
self setTextInputHide];
3098 - (void)testHideBeforeClearClientRemovesViewImmediately {
3102 NSDictionary* config =
self.mutableTemplateCopy;
3104 [
self setClientId:123 configuration:config];
3105 [
self setTextInputShow];
3109 [
self setTextInputHide];
3114 [
self setClientClear];
3119 - (void)testSetClientResetsPendingInputViewRemoval {
3123 NSDictionary* config =
self.mutableTemplateCopy;
3126 [
self setClientId:123 configuration:config];
3127 [
self setTextInputShow];
3132 OCMStub([mockActiveView isFirstResponder]).andReturn(YES);
3134 [
self setClientClear];
3138 [
self setClientId:456 configuration:config];
3142 - (void)testPasswordAutofillHack {
3143 NSDictionary* config =
self.mutableTemplateCopy;
3144 [config setValue:@"YES" forKey:@"obscureText"];
3145 [
self setClientId:123 configuration:config];
3148 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
3152 XCTAssert([inputView isKindOfClass:[UITextField
class]]);
3155 XCTAssertNotEqual([inputView performSelector:
@selector(font)], nil);
3158 - (void)testClearAutofillContextClearsSelection {
3159 NSMutableDictionary* regularField =
self.mutableTemplateCopy;
3160 NSDictionary* editingValue = @{
3161 @"text" :
@"REGULAR_TEXT_FIELD",
3162 @"composingBase" : @0,
3163 @"composingExtent" : @3,
3164 @"selectionBase" : @1,
3165 @"selectionExtent" : @4
3167 [regularField setValue:@{
3168 @"uniqueIdentifier" : @"field2",
3169 @"hints" : @[ @"hint2" ],
3170 @"editingValue" : editingValue,
3172 forKey:@"autofill"];
3173 [regularField addEntriesFromDictionary:editingValue];
3174 [
self setClientId:123 configuration:regularField];
3175 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
3176 XCTAssertEqual(
self.installedInputViews.count, 1ul);
3179 XCTAssert([oldInputView.text isEqualToString:
@"REGULAR_TEXT_FIELD"]);
3181 XCTAssert(NSEqualRanges(selectionRange.
range, NSMakeRange(1, 3)));
3185 [
self setClientId:124 configuration:self.mutablePasswordTemplateCopy];
3186 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
3188 XCTAssertEqual(
self.installedInputViews.count, 2ul);
3190 [textInputPlugin cleanUpViewHierarchy:NO clearText:YES delayRemoval:NO];
3191 XCTAssertEqual(
self.installedInputViews.count, 1ul);
3194 XCTAssert([oldInputView.text isEqualToString:
@""]);
3196 XCTAssert(NSEqualRanges(selectionRange.
range, NSMakeRange(0, 0)));
3199 - (void)testGarbageInputViewsAreNotRemovedImmediately {
3201 [
self setClientId:123 configuration:self.mutablePasswordTemplateCopy];
3202 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
3204 XCTAssertEqual(
self.installedInputViews.count, 1ul);
3207 [
self setClientId:124 configuration:self.mutableTemplateCopy];
3208 [
self ensureOnlyActiveViewCanBecomeFirstResponder];
3210 XCTAssertEqual(
self.installedInputViews.count, 2ul);
3212 [
self commitAutofillContextAndVerify];
3215 - (void)testScribbleSetSelectionRects {
3216 NSMutableDictionary* regularField =
self.mutableTemplateCopy;
3217 NSDictionary* editingValue = @{
3218 @"text" :
@"REGULAR_TEXT_FIELD",
3219 @"composingBase" : @0,
3220 @"composingExtent" : @3,
3221 @"selectionBase" : @1,
3222 @"selectionExtent" : @4
3224 [regularField setValue:@{
3225 @"uniqueIdentifier" : @"field1",
3226 @"hints" : @[ @"hint2" ],
3227 @"editingValue" : editingValue,
3229 forKey:@"autofill"];
3230 [regularField addEntriesFromDictionary:editingValue];
3231 [
self setClientId:123 configuration:regularField];
3232 XCTAssertEqual(
self.installedInputViews.count, 1ul);
3233 XCTAssertEqual([
textInputPlugin.activeView.selectionRects count], 0u);
3235 NSArray<NSNumber*>* selectionRect = [NSArray arrayWithObjects:@0, @0, @100, @100, @0, @1, nil];
3236 NSArray*
selectionRects = [NSArray arrayWithObjects:selectionRect, nil];
3240 [textInputPlugin handleMethodCall:methodCall
3241 result:^(id _Nullable result){
3244 XCTAssertEqual([
textInputPlugin.activeView.selectionRects count], 1u);
3247 - (void)testDecommissionedViewAreNotReusedByAutofill {
3249 NSMutableDictionary* configuration =
self.mutableTemplateCopy;
3250 [configuration setValue:@{
3251 @"uniqueIdentifier" : @"field1",
3252 @"hints" : @[ UITextContentTypePassword ],
3253 @"editingValue" : @{@"text" : @""}
3255 forKey:@"autofill"];
3256 [configuration setValue:@[ [configuration copy] ] forKey:@"fields"];
3258 [
self setClientId:123 configuration:configuration];
3260 [
self setTextInputHide];
3263 [
self setClientId:124 configuration:configuration];
3267 XCTAssertNotNil(previousActiveView);
3271 - (void)testInitialActiveViewCantAccessTextInputDelegate {
3278 - (void)testAutoFillDoesNotTriggerOnShowAndHideKeyboard {
3280 NSMutableDictionary* configuration =
self.mutableTemplateCopy;
3281 [configuration setValue:@{
3282 @"uniqueIdentifier" : @"field1",
3283 @"hints" : @[ UITextContentTypePassword ],
3284 @"editingValue" : @{@"text" : @""}
3286 forKey:@"autofill"];
3287 [configuration setValue:@[ [configuration copy] ] forKey:@"fields"];
3288 [
self setClientId:123 configuration:configuration];
3290 [
self setTextInputShow];
3291 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 1ul);
3294 [
self setTextInputHide];
3295 XCTAssertEqual(
self.viewsVisibleToAutofill.count, 1ul);
3297 [
self commitAutofillContextAndVerify];
3300 #pragma mark - Accessibility - Tests
3302 - (void)testUITextInputAccessibilityNotHiddenWhenKeyboardIsShownAndHidden {
3303 [
self setClientId:123 configuration:self.mutableTemplateCopy];
3306 NSArray<FlutterTextInputView*>* inputFields =
self.installedInputViews;
3309 XCTAssertEqual([inputFields count], 1u);
3312 [
self setTextInputShow];
3314 inputFields =
self.installedInputViews;
3316 XCTAssertEqual([inputFields count], 1u);
3319 [
self setTextInputHide];
3321 inputFields =
self.installedInputViews;
3323 XCTAssertEqual([inputFields count], 1u);
3326 [
self setClientClear];
3328 inputFields =
self.installedInputViews;
3331 XCTAssertEqual([inputFields count], 0u);
3334 - (void)testFlutterTextInputViewDirectFocusToBackingTextInput {
3337 UIView* container = [[UIView alloc] init];
3338 UIAccessibilityElement* backing =
3339 [[UIAccessibilityElement alloc] initWithAccessibilityContainer:container];
3340 inputView.backingTextInputAccessibilityObject = backing;
3343 [inputView accessibilityElementDidBecomeFocused];
3349 - (void)testFlutterTokenizerCanParseLines {
3351 id<UITextInputTokenizer> tokenizer = [inputView tokenizer];
3354 FlutterTextRange* range = [
self getLineRangeFromTokenizer:tokenizer atIndex:0];
3355 XCTAssertEqual(range.
range.location, 0u);
3356 XCTAssertEqual(range.
range.length, 0u);
3358 [inputView insertText:@"how are you\nI am fine, Thank you"];
3360 range = [
self getLineRangeFromTokenizer:tokenizer atIndex:0];
3361 XCTAssertEqual(range.
range.location, 0u);
3362 XCTAssertEqual(range.
range.length, 11u);
3364 range = [
self getLineRangeFromTokenizer:tokenizer atIndex:2];
3365 XCTAssertEqual(range.
range.location, 0u);
3366 XCTAssertEqual(range.
range.length, 11u);
3368 range = [
self getLineRangeFromTokenizer:tokenizer atIndex:11];
3369 XCTAssertEqual(range.
range.location, 0u);
3370 XCTAssertEqual(range.
range.length, 11u);
3372 range = [
self getLineRangeFromTokenizer:tokenizer atIndex:12];
3373 XCTAssertEqual(range.
range.location, 12u);
3374 XCTAssertEqual(range.
range.length, 20u);
3376 range = [
self getLineRangeFromTokenizer:tokenizer atIndex:15];
3377 XCTAssertEqual(range.
range.location, 12u);
3378 XCTAssertEqual(range.
range.length, 20u);
3380 range = [
self getLineRangeFromTokenizer:tokenizer atIndex:32];
3381 XCTAssertEqual(range.
range.location, 12u);
3382 XCTAssertEqual(range.
range.length, 20u);
3385 - (void)testFlutterTokenizerLineEnclosingEndOfDocumentInBackwardDirectionShouldNotReturnNil {
3387 [inputView insertText:@"0123456789\n012345"];
3388 id<UITextInputTokenizer> tokenizer = [inputView tokenizer];
3391 (
FlutterTextRange*)[tokenizer rangeEnclosingPosition:[inputView endOfDocument]
3392 withGranularity:UITextGranularityLine
3393 inDirection:UITextStorageDirectionBackward];
3394 XCTAssertEqual(range.
range.location, 11u);
3395 XCTAssertEqual(range.
range.length, 6u);
3398 - (void)testFlutterTokenizerLineEnclosingEndOfDocumentInForwardDirectionShouldReturnNilOnIOS17 {
3400 [inputView insertText:@"0123456789\n012345"];
3401 id<UITextInputTokenizer> tokenizer = [inputView tokenizer];
3404 (
FlutterTextRange*)[tokenizer rangeEnclosingPosition:[inputView endOfDocument]
3405 withGranularity:UITextGranularityLine
3406 inDirection:UITextStorageDirectionForward];
3407 if (@available(iOS 17.0, *)) {
3408 XCTAssertNil(range);
3410 XCTAssertEqual(range.
range.location, 11u);
3411 XCTAssertEqual(range.
range.length, 6u);
3415 - (void)testFlutterTokenizerLineEnclosingOutOfRangePositionShouldReturnNilOnIOS17 {
3417 [inputView insertText:@"0123456789\n012345"];
3418 id<UITextInputTokenizer> tokenizer = [inputView tokenizer];
3423 withGranularity:UITextGranularityLine
3424 inDirection:UITextStorageDirectionForward];
3425 if (@available(iOS 17.0, *)) {
3426 XCTAssertNil(range);
3428 XCTAssertEqual(range.
range.location, 0u);
3429 XCTAssertEqual(range.
range.length, 0u);
3433 - (void)testFlutterTextInputPluginRetainsFlutterTextInputView {
3438 __weak UIView* activeView;
3443 [NSNumber numberWithInt:123], self.mutablePasswordTemplateCopy
3446 result:^(id _Nullable result){
3452 result:^(id _Nullable result){
3454 XCTAssertNotNil(activeView);
3457 XCTAssertNotNil(activeView);
3460 - (void)testFlutterTextInputPluginHostViewNilCrash {
3463 XCTAssertThrows([myInputPlugin hostView],
@"Throws exception if host view is nil");
3466 - (void)testFlutterTextInputPluginHostViewNotNil {
3472 XCTAssertNotNil([flutterEngine.textInputPlugin hostView]);
3475 - (void)testSetPlatformViewClient {
3482 arguments:@[ [NSNumber numberWithInt:123], self.mutablePasswordTemplateCopy ]];
3484 result:^(id _Nullable result){
3487 XCTAssertNotNil(activeView.superview,
@"activeView must be added to the view hierarchy.");
3490 arguments:@{@"platformViewId" : [NSNumber numberWithLong:456]}];
3492 result:^(id _Nullable result){
3494 XCTAssertNil(activeView.superview,
@"activeView must be removed from view hierarchy.");
3497 - (void)testEditMenu_shouldSetupEditMenuDelegateCorrectly {
3498 if (@available(iOS 16.0, *)) {
3500 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
3501 XCTAssertEqual(inputView.editMenuInteraction.delegate, inputView,
3502 @"editMenuInteraction setup delegate correctly");
3506 - (void)testEditMenu_shouldNotPresentEditMenuIfNotFirstResponder {
3507 if (@available(iOS 16.0, *)) {
3511 XCTAssertFalse(shownEditMenu,
@"Should not show edit menu if not first responder.");
3515 - (void)testEditMenu_shouldPresentEditMenuWithCorrectConfiguration {
3516 if (@available(iOS 16.0, *)) {
3521 [myViewController loadView];
3524 arguments:@[ @(123),
self.mutableTemplateCopy ]];
3526 result:^(id _Nullable result){
3532 OCMStub([mockInputView isFirstResponder]).andReturn(YES);
3534 XCTestExpectation* expectation = [[XCTestExpectation alloc]
3535 initWithDescription:@"presentEditMenuWithConfiguration must be called."];
3537 id mockInteraction = OCMClassMock([UIEditMenuInteraction
class]);
3538 OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
3539 OCMStub([mockInteraction presentEditMenuWithConfiguration:[OCMArg any]])
3540 .andDo(^(NSInvocation* invocation) {
3542 [invocation retainArguments];
3543 UIEditMenuConfiguration* config;
3544 [invocation getArgument:&config atIndex:2];
3545 XCTAssertEqual(config.preferredArrowDirection, UIEditMenuArrowDirectionAutomatic,
3546 @"UIEditMenuConfiguration must use automatic arrow direction.");
3547 XCTAssert(CGPointEqualToPoint(config.sourcePoint, CGPointZero),
3548 @"UIEditMenuConfiguration must have the correct point.");
3549 [expectation fulfill];
3552 NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
3553 @{
@"x" : @(0),
@"y" : @(0),
@"width" : @(0),
@"height" : @(0)};
3555 BOOL shownEditMenu = [myInputPlugin
showEditMenu:@{@"targetRect" : encodedTargetRect}];
3556 XCTAssertTrue(shownEditMenu,
@"Should show edit menu with correct configuration.");
3557 [
self waitForExpectations:@[ expectation ] timeout:1.0];
3561 - (void)testEditMenu_shouldPresentEditMenuWithCorectTargetRect {
3562 if (@available(iOS 16.0, *)) {
3567 [myViewController loadView];
3571 arguments:@[ @(123),
self.mutableTemplateCopy ]];
3573 result:^(id _Nullable result){
3579 OCMStub([mockInputView isFirstResponder]).andReturn(YES);
3581 XCTestExpectation* expectation = [[XCTestExpectation alloc]
3582 initWithDescription:@"presentEditMenuWithConfiguration must be called."];
3584 id mockInteraction = OCMClassMock([UIEditMenuInteraction
class]);
3585 OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
3586 OCMStub([mockInteraction presentEditMenuWithConfiguration:[OCMArg any]])
3587 .andDo(^(NSInvocation* invocation) {
3588 [expectation fulfill];
3591 myInputView.frame = CGRectMake(10, 20, 30, 40);
3592 NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
3593 @{
@"x" : @(100),
@"y" : @(200),
@"width" : @(300),
@"height" : @(400)};
3595 BOOL shownEditMenu = [myInputPlugin
showEditMenu:@{@"targetRect" : encodedTargetRect}];
3596 XCTAssertTrue(shownEditMenu,
@"Should show edit menu with correct configuration.");
3597 [
self waitForExpectations:@[ expectation ] timeout:1.0];
3600 [myInputView editMenuInteraction:mockInteraction
3601 targetRectForConfiguration:OCMClassMock([UIEditMenuConfiguration class])];
3603 XCTAssert(CGRectEqualToRect(targetRect, CGRectMake(90, 180, 300, 400)),
3604 @"targetRectForConfiguration must return the correct target rect.");
3608 - (void)testEditMenu_shouldPresentEditMenuWithSuggestedItemsByDefaultIfNoFrameworkData {
3609 if (@available(iOS 16.0, *)) {
3614 [myViewController loadView];
3618 arguments:@[ @(123),
self.mutableTemplateCopy ]];
3620 result:^(id _Nullable result){
3626 OCMStub([mockInputView isFirstResponder]).andReturn(YES);
3628 XCTestExpectation* expectation = [[XCTestExpectation alloc]
3629 initWithDescription:@"presentEditMenuWithConfiguration must be called."];
3631 id mockInteraction = OCMClassMock([UIEditMenuInteraction
class]);
3632 OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
3633 OCMStub([mockInteraction presentEditMenuWithConfiguration:[OCMArg any]])
3634 .andDo(^(NSInvocation* invocation) {
3635 [expectation fulfill];
3638 myInputView.frame = CGRectMake(10, 20, 30, 40);
3639 NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
3640 @{
@"x" : @(100),
@"y" : @(200),
@"width" : @(300),
@"height" : @(400)};
3642 BOOL shownEditMenu = [myInputPlugin
showEditMenu:@{@"targetRect" : encodedTargetRect}];
3643 XCTAssertTrue(shownEditMenu,
@"Should show edit menu with correct configuration.");
3644 [
self waitForExpectations:@[ expectation ] timeout:1.0];
3646 UICommand* copyItem = [UICommand commandWithTitle:@"Copy"
3648 action:@selector(copy:)
3650 UICommand* pasteItem = [UICommand commandWithTitle:@"Paste"
3652 action:@selector(paste:)
3654 NSArray<UICommand*>* suggestedActions = @[ copyItem, pasteItem ];
3656 UIMenu* menu = [myInputView editMenuInteraction:mockInteraction
3657 menuForConfiguration:OCMClassMock([UIEditMenuConfiguration class])
3658 suggestedActions:suggestedActions];
3659 XCTAssertEqualObjects(menu.children, suggestedActions,
3660 @"Must show suggested items by default.");
3664 - (void)testEditMenu_shouldPresentEditMenuWithCorectItemsAndCorrectOrderingForBasicEditingActions {
3665 if (@available(iOS 16.0, *)) {
3670 [myViewController loadView];
3674 arguments:@[ @(123),
self.mutableTemplateCopy ]];
3676 result:^(id _Nullable result){
3682 OCMStub([mockInputView isFirstResponder]).andReturn(YES);
3684 XCTestExpectation* expectation = [[XCTestExpectation alloc]
3685 initWithDescription:@"presentEditMenuWithConfiguration must be called."];
3687 id mockInteraction = OCMClassMock([UIEditMenuInteraction
class]);
3688 OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
3689 OCMStub([mockInteraction presentEditMenuWithConfiguration:[OCMArg any]])
3690 .andDo(^(NSInvocation* invocation) {
3691 [expectation fulfill];
3694 myInputView.frame = CGRectMake(10, 20, 30, 40);
3695 NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
3696 @{
@"x" : @(100),
@"y" : @(200),
@"width" : @(300),
@"height" : @(400)};
3698 NSArray<NSDictionary<NSString*, id>*>* encodedItems =
3699 @[ @{@"type" : @"paste"}, @{@"type" : @"copy"} ];
3701 BOOL shownEditMenu =
3702 [myInputPlugin
showEditMenu:@{@"targetRect" : encodedTargetRect, @"items" : encodedItems}];
3703 XCTAssertTrue(shownEditMenu,
@"Should show edit menu with correct configuration.");
3704 [
self waitForExpectations:@[ expectation ] timeout:1.0];
3706 UICommand* copyItem = [UICommand commandWithTitle:@"Copy"
3708 action:@selector(copy:)
3710 UICommand* pasteItem = [UICommand commandWithTitle:@"Paste"
3712 action:@selector(paste:)
3714 NSArray<UICommand*>* suggestedActions = @[ copyItem, pasteItem ];
3716 UIMenu* menu = [myInputView editMenuInteraction:mockInteraction
3717 menuForConfiguration:OCMClassMock([UIEditMenuConfiguration class])
3718 suggestedActions:suggestedActions];
3720 NSArray<UICommand*>* expectedChildren = @[ pasteItem, copyItem ];
3721 XCTAssertEqualObjects(menu.children, expectedChildren);
3725 - (void)testEditMenu_shouldPresentEditMenuWithCorectItemsUnderNestedSubtreeForBasicEditingActions {
3726 if (@available(iOS 16.0, *)) {
3731 [myViewController loadView];
3735 arguments:@[ @(123),
self.mutableTemplateCopy ]];
3737 result:^(id _Nullable result){
3743 OCMStub([mockInputView isFirstResponder]).andReturn(YES);
3745 XCTestExpectation* expectation = [[XCTestExpectation alloc]
3746 initWithDescription:@"presentEditMenuWithConfiguration must be called."];
3748 id mockInteraction = OCMClassMock([UIEditMenuInteraction
class]);
3749 OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
3750 OCMStub([mockInteraction presentEditMenuWithConfiguration:[OCMArg any]])
3751 .andDo(^(NSInvocation* invocation) {
3752 [expectation fulfill];
3755 myInputView.frame = CGRectMake(10, 20, 30, 40);
3756 NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
3757 @{
@"x" : @(100),
@"y" : @(200),
@"width" : @(300),
@"height" : @(400)};
3759 NSArray<NSDictionary<NSString*, id>*>* encodedItems =
3760 @[ @{@"type" : @"cut"}, @{@"type" : @"paste"}, @{@"type" : @"copy"} ];
3762 BOOL shownEditMenu =
3763 [myInputPlugin
showEditMenu:@{@"targetRect" : encodedTargetRect, @"items" : encodedItems}];
3764 XCTAssertTrue(shownEditMenu,
@"Should show edit menu with correct configuration.");
3765 [
self waitForExpectations:@[ expectation ] timeout:1.0];
3767 UICommand* copyItem = [UICommand commandWithTitle:@"Copy"
3769 action:@selector(copy:)
3771 UICommand* cutItem = [UICommand commandWithTitle:@"Cut"
3773 action:@selector(cut:)
3775 UICommand* pasteItem = [UICommand commandWithTitle:@"Paste"
3777 action:@selector(paste:)
3790 NSArray<UIMenuElement*>* suggestedActions = @[
3791 copyItem, [UIMenu menuWithChildren:@[ pasteItem ]],
3792 [UIMenu menuWithChildren:@[ [UIMenu menuWithChildren:@[ cutItem ]] ]]
3795 UIMenu* menu = [myInputView editMenuInteraction:mockInteraction
3796 menuForConfiguration:OCMClassMock([UIEditMenuConfiguration class])
3797 suggestedActions:suggestedActions];
3799 NSArray<UICommand*>* expectedActions = @[ cutItem, pasteItem, copyItem ];
3800 XCTAssertEqualObjects(menu.children, expectedActions);
3804 - (void)testEditMenu_shouldPresentEditMenuWithCorectItemsForMoreAdditionalItems {
3805 if (@available(iOS 16.0, *)) {
3810 [myViewController loadView];
3814 arguments:@[ @(123),
self.mutableTemplateCopy ]];
3816 result:^(id _Nullable result){
3822 OCMStub([mockInputView isFirstResponder]).andReturn(YES);
3824 XCTestExpectation* expectation = [[XCTestExpectation alloc]
3825 initWithDescription:@"presentEditMenuWithConfiguration must be called."];
3827 id mockInteraction = OCMClassMock([UIEditMenuInteraction
class]);
3828 OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
3829 OCMStub([mockInteraction presentEditMenuWithConfiguration:[OCMArg any]])
3830 .andDo(^(NSInvocation* invocation) {
3831 [expectation fulfill];
3834 myInputView.frame = CGRectMake(10, 20, 30, 40);
3835 NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
3836 @{
@"x" : @(100),
@"y" : @(200),
@"width" : @(300),
@"height" : @(400)};
3838 NSMutableArray<NSDictionary<NSString*, id>*>* encodedItems = [@[
3839 @{@"type" : @"searchWeb", @"title" : @"Search Web"},
3840 @{@"type" : @"lookUp", @"title" : @"Look Up"}, @{@"type" : @"share", @"title" : @"Share"}
3842 if (@available(iOS 17.4, *)) {
3843 [encodedItems addObject:@{@"type" : @"translate", @"title" : @"Translate"}];
3846 BOOL shownEditMenu =
3847 [myInputPlugin
showEditMenu:@{@"targetRect" : encodedTargetRect, @"items" : encodedItems}];
3848 XCTAssertTrue(shownEditMenu,
@"Should show edit menu with correct configuration.");
3849 [
self waitForExpectations:@[ expectation ] timeout:1.0];
3851 NSArray<UICommand*>* suggestedActions = @[
3852 [UICommand commandWithTitle:@"copy" image:nil action:@selector(copy:) propertyList:nil],
3855 UIMenu* menu = [myInputView editMenuInteraction:mockInteraction
3856 menuForConfiguration:OCMClassMock([UIEditMenuConfiguration class])
3857 suggestedActions:suggestedActions];
3858 if (@available(iOS 17.4, *)) {
3859 XCTAssert(menu.children.count == 4,
@"There must be 4 menu items");
3861 XCTAssert(menu.children.count == 3,
@"There must be 3 menu items");
3864 XCTAssert(((UICommand*)menu.children[0]).action ==
@selector(handleSearchWebAction),
3865 @"Must create search web item in the tree.");
3866 XCTAssert(((UICommand*)menu.children[1]).action ==
@selector(handleLookUpAction),
3867 @"Must create look up item in the tree.");
3868 XCTAssert(((UICommand*)menu.children[2]).action ==
@selector(handleShareAction),
3869 @"Must create share item in the tree.");
3870 if (@available(iOS 17.4, *)) {
3871 XCTAssert(((UICommand*)menu.children[3]).action ==
@selector(handleTranslateAction),
3872 @"Must create translate item in the tree.");
3877 - (void)testInteractiveKeyboardAfterUserScrollWillResignFirstResponder {
3878 if (@available(iOS 17.0, *)) {
3879 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
3880 "https://github.com/flutter/flutter/issues/183473");
3883 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
3885 [inputView setTextInputClient:123];
3886 [inputView reloadInputViews];
3887 [inputView becomeFirstResponder];
3888 XCTAssert(inputView.isFirstResponder);
3890 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
3891 [NSNotificationCenter.defaultCenter
3892 postNotificationName:UIKeyboardWillShowNotification
3894 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
3898 [textInputPlugin handleMethodCall:onPointerMoveCall
3899 result:^(id _Nullable result){
3901 XCTAssertFalse(inputView.isFirstResponder);
3905 - (void)testInteractiveKeyboardAfterUserScrollToTopOfKeyboardWillTakeScreenshot {
3906 if (@available(iOS 17.0, *)) {
3907 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
3908 "https://github.com/flutter/flutter/issues/183473");
3910 NSSet<UIScene*>* scenes = UIApplication.sharedApplication.connectedScenes;
3911 XCTAssertEqual(scenes.count, 1UL,
@"There must only be 1 scene for test");
3912 UIScene* scene = scenes.anyObject;
3913 XCTAssert([scene isKindOfClass:[UIWindowScene
class]],
@"Must be a window scene for test");
3914 UIWindowScene* windowScene = (UIWindowScene*)scene;
3915 XCTAssert(windowScene.windows.count > 0,
@"There must be at least 1 window for test");
3916 UIWindow* window = windowScene.windows[0];
3917 [window addSubview:viewController.view];
3919 [viewController loadView];
3922 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
3924 [inputView setTextInputClient:123];
3925 [inputView reloadInputViews];
3926 [inputView becomeFirstResponder];
3929 for (UIView* subView in
textInputPlugin.keyboardViewContainer.subviews) {
3930 [subView removeFromSuperview];
3934 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
3935 [NSNotificationCenter.defaultCenter
3936 postNotificationName:UIKeyboardWillShowNotification
3938 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
3942 [textInputPlugin handleMethodCall:onPointerMoveCall
3943 result:^(id _Nullable result){
3946 for (UIView* subView in
textInputPlugin.keyboardViewContainer.subviews) {
3947 [subView removeFromSuperview];
3952 - (void)testInteractiveKeyboardScreenshotWillBeMovedDownAfterUserScroll {
3953 if (@available(iOS 17.0, *)) {
3954 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
3955 "https://github.com/flutter/flutter/issues/183473");
3957 NSSet<UIScene*>* scenes = UIApplication.sharedApplication.connectedScenes;
3958 XCTAssertEqual(scenes.count, 1UL,
@"There must only be 1 scene for test");
3959 UIScene* scene = scenes.anyObject;
3960 XCTAssert([scene isKindOfClass:[UIWindowScene
class]],
@"Must be a window scene for test");
3961 UIWindowScene* windowScene = (UIWindowScene*)scene;
3962 XCTAssert(windowScene.windows.count > 0,
@"There must be at least 1 window for test");
3963 UIWindow* window = windowScene.windows[0];
3964 [window addSubview:viewController.view];
3966 [viewController loadView];
3969 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
3971 [inputView setTextInputClient:123];
3972 [inputView reloadInputViews];
3973 [inputView becomeFirstResponder];
3975 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
3976 [NSNotificationCenter.defaultCenter
3977 postNotificationName:UIKeyboardWillShowNotification
3979 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
3983 [textInputPlugin handleMethodCall:onPointerMoveCall
3984 result:^(id _Nullable result){
3988 XCTAssertEqual(
textInputPlugin.keyboardViewContainer.frame.origin.y, keyboardFrame.origin.y);
3993 [textInputPlugin handleMethodCall:onPointerMoveCallMove
3994 result:^(id _Nullable result){
3998 XCTAssertEqual(
textInputPlugin.keyboardViewContainer.frame.origin.y, 600.0);
4000 for (UIView* subView in
textInputPlugin.keyboardViewContainer.subviews) {
4001 [subView removeFromSuperview];
4006 - (void)testInteractiveKeyboardScreenshotWillBeMovedToOrginalPositionAfterUserScroll {
4007 if (@available(iOS 17.0, *)) {
4008 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
4009 "https://github.com/flutter/flutter/issues/183473");
4011 NSSet<UIScene*>* scenes = UIApplication.sharedApplication.connectedScenes;
4012 XCTAssertEqual(scenes.count, 1UL,
@"There must only be 1 scene for test");
4013 UIScene* scene = scenes.anyObject;
4014 XCTAssert([scene isKindOfClass:[UIWindowScene
class]],
@"Must be a window scene for test");
4015 UIWindowScene* windowScene = (UIWindowScene*)scene;
4016 XCTAssert(windowScene.windows.count > 0,
@"There must be at least 1 window for test");
4017 UIWindow* window = windowScene.windows[0];
4018 [window addSubview:viewController.view];
4020 [viewController loadView];
4023 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
4025 [inputView setTextInputClient:123];
4026 [inputView reloadInputViews];
4027 [inputView becomeFirstResponder];
4029 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
4030 [NSNotificationCenter.defaultCenter
4031 postNotificationName:UIKeyboardWillShowNotification
4033 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
4037 [textInputPlugin handleMethodCall:onPointerMoveCall
4038 result:^(id _Nullable result){
4041 XCTAssertEqual(
textInputPlugin.keyboardViewContainer.frame.origin.y, keyboardFrame.origin.y);
4046 [textInputPlugin handleMethodCall:onPointerMoveCallMove
4047 result:^(id _Nullable result){
4050 XCTAssertEqual(
textInputPlugin.keyboardViewContainer.frame.origin.y, 600.0);
4055 [textInputPlugin handleMethodCall:onPointerMoveCallBackUp
4056 result:^(id _Nullable result){
4059 XCTAssertEqual(
textInputPlugin.keyboardViewContainer.frame.origin.y, keyboardFrame.origin.y);
4060 for (UIView* subView in
textInputPlugin.keyboardViewContainer.subviews) {
4061 [subView removeFromSuperview];
4066 - (void)testInteractiveKeyboardFindFirstResponderRecursive {
4068 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
4069 [inputView setTextInputClient:123];
4070 [inputView reloadInputViews];
4071 [inputView becomeFirstResponder];
4073 UIView* firstResponder = UIApplication.sharedApplication.keyWindow.flutterFirstResponder;
4074 XCTAssertEqualObjects(inputView, firstResponder);
4078 - (void)testInteractiveKeyboardFindFirstResponderRecursiveInMultipleSubviews {
4085 [subInputView addSubview:subFirstResponderInputView];
4086 [inputView addSubview:subInputView];
4087 [inputView addSubview:otherSubInputView];
4088 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
4089 [inputView setTextInputClient:123];
4090 [inputView reloadInputViews];
4091 [subInputView setTextInputClient:123];
4092 [subInputView reloadInputViews];
4093 [otherSubInputView setTextInputClient:123];
4094 [otherSubInputView reloadInputViews];
4095 [subFirstResponderInputView setTextInputClient:123];
4096 [subFirstResponderInputView reloadInputViews];
4097 [subFirstResponderInputView becomeFirstResponder];
4099 UIView* firstResponder = UIApplication.sharedApplication.keyWindow.flutterFirstResponder;
4100 XCTAssertEqualObjects(subFirstResponderInputView, firstResponder);
4104 - (void)testInteractiveKeyboardFindFirstResponderIsNilRecursive {
4106 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
4107 [inputView setTextInputClient:123];
4108 [inputView reloadInputViews];
4110 UIView* firstResponder = UIApplication.sharedApplication.keyWindow.flutterFirstResponder;
4111 XCTAssertNil(firstResponder);
4115 - (void)testInteractiveKeyboardDidResignFirstResponderDelegateisCalledAfterDismissedKeyboard {
4116 if (@available(iOS 17.0, *)) {
4117 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
4118 "https://github.com/flutter/flutter/issues/183473");
4120 NSSet<UIScene*>* scenes = UIApplication.sharedApplication.connectedScenes;
4121 XCTAssertEqual(scenes.count, 1UL,
@"There must only be 1 scene for test");
4122 UIScene* scene = scenes.anyObject;
4123 XCTAssert([scene isKindOfClass:[UIWindowScene
class]],
@"Must be a window scene for test");
4124 UIWindowScene* windowScene = (UIWindowScene*)scene;
4125 XCTAssert(windowScene.windows.count > 0,
@"There must be at least 1 window for test");
4126 UIWindow* window = windowScene.windows[0];
4127 [window addSubview:viewController.view];
4129 [viewController loadView];
4131 XCTestExpectation* expectation = [[XCTestExpectation alloc]
4132 initWithDescription:
4133 @"didResignFirstResponder is called after screenshot keyboard dismissed."];
4134 OCMStub([
engine flutterTextInputView:[OCMArg any] didResignFirstResponderWithTextInputClient:0])
4135 .andDo(^(NSInvocation* invocation) {
4136 [expectation fulfill];
4138 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
4139 [NSNotificationCenter.defaultCenter
4140 postNotificationName:UIKeyboardWillShowNotification
4142 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
4146 [textInputPlugin handleMethodCall:initialMoveCall
4147 result:^(id _Nullable result){
4152 [textInputPlugin handleMethodCall:subsequentMoveCall
4153 result:^(id _Nullable result){
4159 [textInputPlugin handleMethodCall:pointerUpCall
4160 result:^(id _Nullable result){
4163 [
self waitForExpectations:@[ expectation ] timeout:2.0];
4167 - (void)testInteractiveKeyboardScreenshotDismissedAfterPointerLiftedAboveMiddleYOfKeyboard {
4168 if (@available(iOS 17.0, *)) {
4169 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
4170 "https://github.com/flutter/flutter/issues/183473");
4172 NSSet<UIScene*>* scenes = UIApplication.sharedApplication.connectedScenes;
4173 XCTAssertEqual(scenes.count, 1UL,
@"There must only be 1 scene for test");
4174 UIScene* scene = scenes.anyObject;
4175 XCTAssert([scene isKindOfClass:[UIWindowScene
class]],
@"Must be a window scene for test");
4176 UIWindowScene* windowScene = (UIWindowScene*)scene;
4177 XCTAssert(windowScene.windows.count > 0,
@"There must be at least 1 window for test");
4178 UIWindow* window = windowScene.windows[0];
4179 [window addSubview:viewController.view];
4181 [viewController loadView];
4183 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
4184 [NSNotificationCenter.defaultCenter
4185 postNotificationName:UIKeyboardWillShowNotification
4187 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
4191 [textInputPlugin handleMethodCall:initialMoveCall
4192 result:^(id _Nullable result){
4197 [textInputPlugin handleMethodCall:subsequentMoveCall
4198 result:^(id _Nullable result){
4204 [textInputPlugin handleMethodCall:subsequentMoveBackUpCall
4205 result:^(id _Nullable result){
4211 [textInputPlugin handleMethodCall:pointerUpCall
4212 result:^(id _Nullable result){
4214 NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(id item, NSDictionary* bindings) {
4215 return textInputPlugin.keyboardViewContainer.subviews.count == 0;
4217 XCTNSPredicateExpectation* expectation =
4218 [[XCTNSPredicateExpectation alloc] initWithPredicate:predicate object:nil];
4219 [
self waitForExpectations:@[ expectation ] timeout:10.0];
4223 - (void)testInteractiveKeyboardKeyboardReappearsAfterPointerLiftedAboveMiddleYOfKeyboard {
4224 if (@available(iOS 17.0, *)) {
4225 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
4226 "https://github.com/flutter/flutter/issues/183473");
4228 NSSet<UIScene*>* scenes = UIApplication.sharedApplication.connectedScenes;
4229 XCTAssertEqual(scenes.count, 1UL,
@"There must only be 1 scene for test");
4230 UIScene* scene = scenes.anyObject;
4231 XCTAssert([scene isKindOfClass:[UIWindowScene
class]],
@"Must be a window scene for test");
4232 UIWindowScene* windowScene = (UIWindowScene*)scene;
4233 XCTAssert(windowScene.windows.count > 0,
@"There must be at least 1 window for test");
4234 UIWindow* window = windowScene.windows[0];
4235 [window addSubview:viewController.view];
4237 [viewController loadView];
4240 [UIApplication.sharedApplication.keyWindow addSubview:inputView];
4242 [inputView setTextInputClient:123];
4243 [inputView reloadInputViews];
4244 [inputView becomeFirstResponder];
4246 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
4247 [NSNotificationCenter.defaultCenter
4248 postNotificationName:UIKeyboardWillShowNotification
4250 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
4254 [textInputPlugin handleMethodCall:initialMoveCall
4255 result:^(id _Nullable result){
4260 [textInputPlugin handleMethodCall:subsequentMoveCall
4261 result:^(id _Nullable result){
4267 [textInputPlugin handleMethodCall:subsequentMoveBackUpCall
4268 result:^(id _Nullable result){
4274 [textInputPlugin handleMethodCall:pointerUpCall
4275 result:^(id _Nullable result){
4277 NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(id item, NSDictionary* bindings) {
4278 return textInputPlugin.cachedFirstResponder.isFirstResponder;
4280 XCTNSPredicateExpectation* expectation =
4281 [[XCTNSPredicateExpectation alloc] initWithPredicate:predicate object:nil];
4282 [
self waitForExpectations:@[ expectation ] timeout:10.0];
4286 - (void)testInteractiveKeyboardKeyboardAnimatesToOriginalPositionalOnPointerUp {
4287 if (@available(iOS 17.0, *)) {
4288 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
4289 "https://github.com/flutter/flutter/issues/183473");
4291 NSSet<UIScene*>* scenes = UIApplication.sharedApplication.connectedScenes;
4292 XCTAssertEqual(scenes.count, 1UL,
@"There must only be 1 scene for test");
4293 UIScene* scene = scenes.anyObject;
4294 XCTAssert([scene isKindOfClass:[UIWindowScene
class]],
@"Must be a window scene for test");
4295 UIWindowScene* windowScene = (UIWindowScene*)scene;
4296 XCTAssert(windowScene.windows.count > 0,
@"There must be at least 1 window for test");
4297 UIWindow* window = windowScene.windows[0];
4298 [window addSubview:viewController.view];
4300 [viewController loadView];
4302 XCTestExpectation* expectation =
4303 [[XCTestExpectation alloc] initWithDescription:@"Keyboard animates to proper position."];
4304 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
4305 [NSNotificationCenter.defaultCenter
4306 postNotificationName:UIKeyboardWillShowNotification
4308 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
4312 [textInputPlugin handleMethodCall:initialMoveCall
4313 result:^(id _Nullable result){
4318 [textInputPlugin handleMethodCall:subsequentMoveCall
4319 result:^(id _Nullable result){
4324 [textInputPlugin handleMethodCall:upwardVelocityMoveCall
4325 result:^(id _Nullable result){
4332 handleMethodCall:pointerUpCall
4333 result:^(id _Nullable result) {
4334 XCTAssertEqual(textInputPlugin.keyboardViewContainer.frame.origin.y,
4335 viewController.flutterScreenIfViewLoaded.bounds.size.height -
4336 keyboardFrame.origin.y);
4337 [expectation fulfill];
4342 - (void)testInteractiveKeyboardKeyboardAnimatesToDismissalPositionalOnPointerUp {
4343 if (@available(iOS 17.0, *)) {
4344 XCTSkip(
@"Interactive keyboard tests broken on iOS 17+ due to SDK bugs. See: "
4345 "https://github.com/flutter/flutter/issues/183473");
4347 NSSet<UIScene*>* scenes = UIApplication.sharedApplication.connectedScenes;
4348 XCTAssertEqual(scenes.count, 1UL,
@"There must only be 1 scene for test");
4349 UIScene* scene = scenes.anyObject;
4350 XCTAssert([scene isKindOfClass:[UIWindowScene
class]],
@"Must be a window scene for test");
4351 UIWindowScene* windowScene = (UIWindowScene*)scene;
4352 XCTAssert(windowScene.windows.count > 0,
@"There must be at least 1 window for test");
4353 UIWindow* window = windowScene.windows[0];
4354 [window addSubview:viewController.view];
4356 [viewController loadView];
4358 XCTestExpectation* expectation =
4359 [[XCTestExpectation alloc] initWithDescription:@"Keyboard animates to proper position."];
4360 CGRect keyboardFrame = CGRectMake(0, 500, 500, 500);
4361 [NSNotificationCenter.defaultCenter
4362 postNotificationName:UIKeyboardWillShowNotification
4364 userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}];
4368 [textInputPlugin handleMethodCall:initialMoveCall
4369 result:^(id _Nullable result){
4374 [textInputPlugin handleMethodCall:subsequentMoveCall
4375 result:^(id _Nullable result){
4382 handleMethodCall:pointerUpCall
4383 result:^(id _Nullable result) {
4384 XCTAssertEqual(textInputPlugin.keyboardViewContainer.frame.origin.y,
4385 viewController.flutterScreenIfViewLoaded.bounds.size.height);
4386 [expectation fulfill];
4390 - (void)testInteractiveKeyboardShowKeyboardAndRemoveScreenshotAnimationIsNotImmediatelyEnable {
4391 [UIView setAnimationsEnabled:YES];
4392 [textInputPlugin showKeyboardAndRemoveScreenshot];
4394 UIView.areAnimationsEnabled,
4395 @"The animation should still be disabled following showKeyboardAndRemoveScreenshot");
4398 - (void)testInteractiveKeyboardShowKeyboardAndRemoveScreenshotAnimationIsReenabledAfterDelay {
4399 [UIView setAnimationsEnabled:YES];
4400 [textInputPlugin showKeyboardAndRemoveScreenshot];
4402 NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(id item, NSDictionary* bindings) {
4404 return UIView.areAnimationsEnabled;
4406 XCTNSPredicateExpectation* expectation =
4407 [[XCTNSPredicateExpectation alloc] initWithPredicate:predicate object:nil];
4408 [
self waitForExpectations:@[ expectation ] timeout:10.0];
4411 - (void)testEditMenu_shouldCreateCustomMenuItemWithCorrectProperties {
4412 if (@available(iOS 16.0, *)) {
4417 [myViewController loadView];
4421 arguments:@[ @(123),
self.mutableTemplateCopy ]];
4423 result:^(id _Nullable result){
4428 OCMStub([mockInputView isFirstResponder]).andReturn(YES);
4430 id mockInteraction = OCMClassMock([UIEditMenuInteraction
class]);
4431 OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
4433 NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
4434 @{
@"x" : @(0),
@"y" : @(0),
@"width" : @(100),
@"height" : @(50)};
4436 NSArray<NSDictionary*>* encodedItems = @[
4437 @{@"type" : @"custom", @"id" : @"custom-action-1", @"title" : @"Custom Action 1"},
4438 @{@"type" : @"custom", @"id" : @"custom-action-2", @"title" : @"Custom Action 2"},
4441 BOOL shownEditMenu =
4442 [myInputPlugin
showEditMenu:@{@"targetRect" : encodedTargetRect, @"items" : encodedItems}];
4443 XCTAssertTrue(shownEditMenu,
@"Should show edit menu");
4445 UIMenu* menu = [myInputView editMenuInteraction:mockInteraction
4446 menuForConfiguration:OCMClassMock([UIEditMenuConfiguration class])
4447 suggestedActions:@[]];
4449 XCTAssertEqual(menu.children.count, 2UL,
@"Should create 2 custom menu items");
4450 UIAction* firstAction = (UIAction*)menu.children[0];
4451 UIAction* secondAction = (UIAction*)menu.children[1];
4452 XCTAssertEqualObjects(firstAction.title,
@"Custom Action 1",
4453 @"First action title should match");
4454 XCTAssertEqualObjects(secondAction.title,
@"Custom Action 2",
4455 @"Second action title should match");
4459 - (void)testEditMenu_customActionShouldTriggerDelegateCallback {
4460 if (@available(iOS 16.0, *)) {
4463 OCMStub([mockEngine platformChannel]).andReturn(mockPlatformChannel);
4465 OCMStub([mockEngine flutterTextInputView:[OCMArg any]
4466 performContextMenuCustomActionWithActionID:
@"test-callback-id"
4467 textInputClient:123])
4468 .andDo((^(NSInvocation* invocation) {
4469 [mockPlatformChannel invokeMethod:@"ContextMenu.onPerformCustomAction"
4470 arguments:@[ @(123), @"test-callback-id" ]];
4477 [myViewController loadView];
4481 arguments:@[ @(123),
self.mutableTemplateCopy ]];
4483 result:^(id _Nullable result){
4488 OCMStub([mockInputView isFirstResponder]).andReturn(YES);
4489 XCTestExpectation* expectation = [[XCTestExpectation alloc]
4490 initWithDescription:@"Custom action delegate callback should be called"];
4491 OCMStub(([mockPlatformChannel invokeMethod:
@"ContextMenu.onPerformCustomAction"
4492 arguments:@[ @(123),
@"test-callback-id" ]]))
4493 .andDo(^(NSInvocation* invocation) {
4494 [expectation fulfill];
4496 id mockInteraction = OCMClassMock([UIEditMenuInteraction
class]);
4497 OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
4499 NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
4500 @{
@"x" : @(0),
@"y" : @(0),
@"width" : @(100),
@"height" : @(50)};
4502 NSArray<NSDictionary*>* encodedItems = @[
4503 @{@"type" : @"custom", @"id" : @"test-callback-id", @"title" : @"Test Action"},
4506 BOOL shownEditMenu =
4507 [myInputPlugin
showEditMenu:@{@"targetRect" : encodedTargetRect, @"items" : encodedItems}];
4508 XCTAssertTrue(shownEditMenu,
@"Should show edit menu");
4510 UIMenu* menu = [myInputView editMenuInteraction:mockInteraction
4511 menuForConfiguration:OCMClassMock([UIEditMenuConfiguration class])
4512 suggestedActions:@[]];
4514 XCTAssertEqual(menu.children.count, 1UL,
@"Should have 1 custom menu item");
4515 UIAction* customAction = (UIAction*)menu.children[0];
4516 XCTAssertEqualObjects(customAction.title,
@"Test Action",
@"Action title should match");
4518 [myInputView.textInputDelegate flutterTextInputView:myInputView
4519 performContextMenuCustomActionWithActionID:@"test-callback-id"
4520 textInputClient:123];
4522 [
self waitForExpectations:@[ expectation ] timeout:1.0];
4523 OCMVerifyAll(mockPlatformChannel);
void(^ FlutterResult)(id _Nullable result)
NSArray< FlutterTextSelectionRect * > * selectionRects
UITextRange * markedTextRange
API_AVAILABLE(ios(13.0)) @interface FlutterTextPlaceholder UITextRange * selectedTextRange
UITextSmartQuotesType smartQuotesType API_AVAILABLE(ios(11.0))
CGRect caretRectForPosition
const CGRect kInvalidFirstRect
NSDictionary * _passwordTemplate
FlutterViewController * viewController
FlutterTextInputPlugin * textInputPlugin
BOOL runWithEntrypoint:initialRoute:(nullable NSString *entrypoint,[initialRoute] nullable NSString *NS_SWIFT_UI_ACTOR)
void setBinaryMessenger:(FlutterBinaryMessengerRelay *binaryMessenger)
FlutterViewController * viewController
void flutterTextInputView:performAction:withClient:(FlutterTextInputView *textInputView,[performAction] FlutterTextInputAction action,[withClient] int client)
BOOL runWithEntrypoint:(nullable NSString *NS_SWIFT_UI_ACTOR)
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
UIView< UITextInput > * textInputView()
UIIndirectScribbleInteractionDelegate UIViewController * viewController
BOOL showEditMenu:(ios(16.0) API_AVAILABLE)
void handleMethodCall:result:(FlutterMethodCall *call,[result] FlutterResult result)
UIAccessibilityNotifications receivedNotification
id receivedNotificationTarget
BOOL isAccessibilityFocused
instancetype positionWithIndex:(NSUInteger index)
UITextStorageDirection affinity
instancetype rangeWithNSRange:(NSRange range)
instancetype selectionRectWithRect:position:(CGRect rect,[position] NSUInteger position)
instancetype selectionRectWithRect:position:writingDirection:(CGRect rect,[position] NSUInteger position,[writingDirection] NSWritingDirection writingDirection)