[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
This commit is contained in:
hellohuanlin
2024-08-26 12:53:43 -07:00
committed by GitHub
parent 8c3652a74c
commit 146e133c05
4 changed files with 126 additions and 0 deletions

View File

@@ -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];
}

View File

@@ -527,6 +527,19 @@ FLUTTER_ASSERT_ARC
XCTAssertEqualObjects(substring, @"bbbbaaaabbbbaaaa");
}
- (void)testCanPerformActionForSelectActions {
NSDictionary* config = self.mutableTemplateCopy;
[self setClientId:123 configuration:config];
NSArray<FlutterTextInputView*>* 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];

View File

@@ -16,6 +16,10 @@ FLUTTER_ASSERT_ARC
const float kFloatCompareEpsilon = 0.001;
@interface TextInputSemanticsObject (Test)
- (UIView<UITextInput>*)textInputSurrogate;
@end
@interface SemanticsObjectTest : XCTestCase
@end
@@ -1069,4 +1073,83 @@ const float kFloatCompareEpsilon = 0.001;
XCTAssertEqual([object accessibilityTraits], UIAccessibilityTraitNone);
}
- (void)testTextInputSemanticsObject_canPerformAction {
fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(
new flutter::testing::MockAccessibilityBridge());
fml::WeakPtr<flutter::AccessibilityBridgeIos> bridge = factory.GetWeakPtr();
flutter::SemanticsNode node;
node.label = "foo";
node.flags = static_cast<int32_t>(flutter::SemanticsFlags::kIsTextField) |
static_cast<int32_t>(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<flutter::AccessibilityBridgeIos> factory(
new flutter::testing::MockAccessibilityBridge());
fml::WeakPtr<flutter::AccessibilityBridgeIos> bridge = factory.GetWeakPtr();
flutter::SemanticsNode node;
node.label = "foo";
node.flags = static_cast<int32_t>(flutter::SemanticsFlags::kIsTextField) |
static_cast<int32_t>(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

View File

@@ -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