From 146e133c05fc488f9f47935014f9f50961263f89 Mon Sep 17 00:00:00 2001 From: hellohuanlin <41930132+hellohuanlin@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:53:43 -0700 Subject: [PATCH] [ios] Fix text input edit rotor accessibility (flutter/engine#54351) Fixes text input edit rotor accessibility missing edit actions. It also fixes a few edit items that is displayed where it's not supposed to. (e.g. displaying select item while nothing to select). *List which issues are fixed by this PR. You must list at least one issue.* Fixes https://github.com/flutter/flutter/issues/151029 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../Source/FlutterTextInputPlugin.mm | 2 + .../Source/FlutterTextInputPluginTest.mm | 13 +++ .../framework/Source/SemanticsObjectTest.mm | 83 +++++++++++++++++++ .../Source/TextInputSemanticsObject.mm | 28 +++++++ 4 files changed, 126 insertions(+) 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 87be4ee9bf..61419ed9cd 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 @@ -1187,6 +1187,8 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point, } else if (action == @selector(copy:) || action == @selector(cut:) || action == @selector(delete:)) { return [self textInRange:_selectedTextRange].length > 0; + } else if (action == @selector(selectAll:)) { + return self.hasText; } return [super canPerformAction:action withSender:sender]; } 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 46bffc9228..cd75d64dc9 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 @@ -527,6 +527,19 @@ FLUTTER_ASSERT_ARC XCTAssertEqualObjects(substring, @"bbbbaaaabbbbaaaa"); } +- (void)testCanPerformActionForSelectActions { + NSDictionary* config = self.mutableTemplateCopy; + [self setClientId:123 configuration:config]; + NSArray* inputFields = self.installedInputViews; + FlutterTextInputView* inputView = inputFields[0]; + + XCTAssertFalse([inputView canPerformAction:@selector(selectAll:) withSender:nil]); + + [inputView insertText:@"aaaa"]; + + XCTAssertTrue([inputView canPerformAction:@selector(selectAll:) withSender:nil]); +} + - (void)testDeletingBackward { NSDictionary* config = self.mutableTemplateCopy; [self setClientId:123 configuration:config]; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm index 328d32b691..e95078a419 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm @@ -16,6 +16,10 @@ FLUTTER_ASSERT_ARC const float kFloatCompareEpsilon = 0.001; +@interface TextInputSemanticsObject (Test) +- (UIView*)textInputSurrogate; +@end + @interface SemanticsObjectTest : XCTestCase @end @@ -1069,4 +1073,83 @@ const float kFloatCompareEpsilon = 0.001; XCTAssertEqual([object accessibilityTraits], UIAccessibilityTraitNone); } +- (void)testTextInputSemanticsObject_canPerformAction { + fml::WeakPtrFactory factory( + new flutter::testing::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + + flutter::SemanticsNode node; + node.label = "foo"; + node.flags = static_cast(flutter::SemanticsFlags::kIsTextField) | + static_cast(flutter::SemanticsFlags::kIsReadOnly); + TextInputSemanticsObject* object = [[TextInputSemanticsObject alloc] initWithBridge:bridge uid:0]; + [object setSemanticsNode:&node]; + [object accessibilityBridgeDidFinishUpdate]; + + id textInputSurrogate = OCMClassMock([UIResponder class]); + id partialSemanticsObject = OCMPartialMock(object); + OCMStub([partialSemanticsObject textInputSurrogate]).andReturn(textInputSurrogate); + + OCMExpect([textInputSurrogate canPerformAction:[OCMArg anySelector] withSender:OCMOCK_ANY]) + .andReturn(YES); + XCTAssertTrue([partialSemanticsObject canPerformAction:@selector(copy:) withSender:nil]); + + OCMExpect([textInputSurrogate canPerformAction:[OCMArg anySelector] withSender:OCMOCK_ANY]) + .andReturn(NO); + XCTAssertFalse([partialSemanticsObject canPerformAction:@selector(copy:) withSender:nil]); +} + +- (void)testTextInputSemanticsObject_editActions { + fml::WeakPtrFactory factory( + new flutter::testing::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + + flutter::SemanticsNode node; + node.label = "foo"; + node.flags = static_cast(flutter::SemanticsFlags::kIsTextField) | + static_cast(flutter::SemanticsFlags::kIsReadOnly); + TextInputSemanticsObject* object = [[TextInputSemanticsObject alloc] initWithBridge:bridge uid:0]; + [object setSemanticsNode:&node]; + [object accessibilityBridgeDidFinishUpdate]; + + id textInputSurrogate = OCMClassMock([UIResponder class]); + id partialSemanticsObject = OCMPartialMock(object); + OCMStub([partialSemanticsObject textInputSurrogate]).andReturn(textInputSurrogate); + + XCTestExpectation* copyExpectation = + [self expectationWithDescription:@"Surrogate's copy method is called."]; + XCTestExpectation* cutExpectation = + [self expectationWithDescription:@"Surrogate's cut method is called."]; + XCTestExpectation* pasteExpectation = + [self expectationWithDescription:@"Surrogate's paste method is called."]; + XCTestExpectation* selectAllExpectation = + [self expectationWithDescription:@"Surrogate's selectAll method is called."]; + XCTestExpectation* deleteExpectation = + [self expectationWithDescription:@"Surrogate's delete method is called."]; + + OCMStub([textInputSurrogate copy:OCMOCK_ANY]).andDo(^(NSInvocation* invocation) { + [copyExpectation fulfill]; + }); + OCMStub([textInputSurrogate cut:OCMOCK_ANY]).andDo(^(NSInvocation* invocation) { + [cutExpectation fulfill]; + }); + OCMStub([textInputSurrogate paste:OCMOCK_ANY]).andDo(^(NSInvocation* invocation) { + [pasteExpectation fulfill]; + }); + OCMStub([textInputSurrogate selectAll:OCMOCK_ANY]).andDo(^(NSInvocation* invocation) { + [selectAllExpectation fulfill]; + }); + OCMStub([textInputSurrogate delete:OCMOCK_ANY]).andDo(^(NSInvocation* invocation) { + [deleteExpectation fulfill]; + }); + + [partialSemanticsObject copy:nil]; + [partialSemanticsObject cut:nil]; + [partialSemanticsObject paste:nil]; + [partialSemanticsObject selectAll:nil]; + [partialSemanticsObject delete:nil]; + + [self waitForExpectationsWithTimeout:1 handler:nil]; +} + @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/TextInputSemanticsObject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/TextInputSemanticsObject.mm index 6827991556..3695947b3c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/TextInputSemanticsObject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/TextInputSemanticsObject.mm @@ -462,4 +462,32 @@ static const UIAccessibilityTraits kUIAccessibilityTraitUndocumentedEmptyLine = return [[self textInputSurrogate] hasText]; } +#pragma mark - UIResponder overrides + +- (void)cut:(id)sender { + [[self textInputSurrogate] cut:sender]; +} + +- (void)copy:(id)sender { + [[self textInputSurrogate] copy:sender]; +} + +- (void)paste:(id)sender { + [[self textInputSurrogate] paste:sender]; +} + +// TODO(hellohuanlin): should also support `select:`, which is not implemented by the surrogate yet. +// See: https://github.com/flutter/flutter/issues/107578. +- (void)selectAll:(id)sender { + [[self textInputSurrogate] selectAll:sender]; +} + +- (void)delete:(id)sender { + [[self textInputSurrogate] delete:sender]; +} + +- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { + return [[self textInputSurrogate] canPerformAction:action withSender:sender]; +} + @end