From 5e37c966c04077fff16a48c045c71471be16739c Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Tue, 11 Feb 2025 07:10:15 +0900 Subject: [PATCH] [iOS][Engine] Fix view removal process for AutofillContextAction.cancel (#160653) Original PR https://github.com/flutter/engine/pull/57209 --- The `removeFromSuperview` in the `hideTextInput` method was triggering the save password, so I changed it to `cleanUpViewHierarchy`. fix https://github.com/flutter/flutter/issues/145681 sample app code https://github.com/koji-1009/autofill_cancel_issue before https://github.com/user-attachments/assets/c380538b-6783-4706-9a09-4db1c3b761df after https://github.com/user-attachments/assets/b6654b80-f383-408d-9820-6fe53279ce14 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../Source/FlutterTextInputPlugin.mm | 7 +++++-- .../Source/FlutterTextInputPluginTest.mm | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index a287a54f8b..08ea6910bb 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -2685,8 +2685,11 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point, [self removeEnableFlutterTextInputViewAccessibilityTimer]; _activeView.accessibilityEnabled = NO; [_activeView resignFirstResponder]; - [_activeView removeFromSuperview]; - [_inputHider removeFromSuperview]; + // Removes the focus from the `_activeView` (UIView) + // when the user stops typing (keyboard is hidden). + // For more details, refer to the discussion at: + // https://github.com/flutter/engine/pull/57209#discussion_r1905942577 + [self cleanUpViewHierarchy:YES clearText:YES delayRemoval:NO]; } - (void)triggerAutofillSave:(BOOL)saveEntries { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index f12b2aa296..c51db3219c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -2718,6 +2718,27 @@ FLUTTER_ASSERT_ARC XCTAssertNil(textInputPlugin.activeView.textInputDelegate); } +- (void)testAutoFillDoesNotTriggerOnHideButTriggersOnCommit { + // Regression test for https://github.com/flutter/flutter/issues/145681. + NSMutableDictionary* configuration = self.mutableTemplateCopy; + [configuration setValue:@{ + @"uniqueIdentifier" : @"field1", + @"hints" : @[ UITextContentTypePassword ], + @"editingValue" : @{@"text" : @""} + } + forKey:@"autofill"]; + [configuration setValue:@[ [configuration copy] ] forKey:@"fields"]; + + [self setClientId:123 configuration:configuration]; + XCTAssertEqual(self.viewsVisibleToAutofill.count, 1ul); + + [self setTextInputHide]; + // Before the fix in https://github.com/flutter/flutter/pull/160653, it was 0ul. + XCTAssertEqual(self.viewsVisibleToAutofill.count, 1ul); + + [self commitAutofillContextAndVerify]; +} + #pragma mark - Accessibility - Tests - (void)testUITextInputAccessibilityNotHiddenWhenShowed {