Fixes iOS accessibility can focus wrong content due to rapid animations (flutter/engine#25115)

This commit is contained in:
chunhtai
2021-03-29 13:49:04 -07:00
committed by GitHub
parent 2cd1c0280c
commit 4f3e691b5a
2 changed files with 100 additions and 2 deletions

View File

@@ -206,14 +206,16 @@ void AccessibilityBridge::UpdateSemantics(flutter::SemanticsNodeUpdates nodes,
[objects_ removeObjectsForKeys:doomed_uids];
layoutChanged = layoutChanged || [doomed_uids count] > 0;
// We should send out only one notification per semantics update.
if (routeChanged) {
if (!ios_delegate_->IsFlutterViewControllerPresentingModalViewController(view_controller_)) {
NSString* routeName = [lastAdded routeName];
ios_delegate_->PostAccessibilityNotification(UIAccessibilityScreenChangedNotification,
routeName);
}
} else if (layoutChanged) {
}
if (layoutChanged) {
SemanticsObject* nextToFocus = nil;
// This property will be -1 if the focus is outside of the flutter
// application. In this case, we should not refocus anything.

View File

@@ -344,6 +344,102 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(std::string name) {
UIAccessibilityScreenChangedNotification);
}
- (void)testAnnouncesRouteChangesAndLayoutChangeInOneUpdate {
flutter::MockDelegate mock_delegate;
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
/*platform=*/thread_task_runner,
/*raster=*/thread_task_runner,
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
/*platform_views_controller=*/nil,
/*task_runners=*/runners);
id mockFlutterView = OCMClassMock([FlutterView class]);
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
OCMStub([mockFlutterViewController view]).andReturn(mockFlutterView);
NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
[[[NSMutableArray alloc] init] autorelease];
auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
ios_delegate->on_PostAccessibilityNotification_ =
[accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
[accessibility_notifications addObject:@{
@"notification" : @(notification),
@"argument" : argument ? argument : [NSNull null],
}];
};
__block auto bridge =
std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
/*platform_view=*/platform_view.get(),
/*platform_views_controller=*/nil,
/*ios_delegate=*/std::move(ios_delegate));
flutter::CustomAccessibilityActionUpdates actions;
flutter::SemanticsNodeUpdates nodes;
flutter::SemanticsNode node1;
node1.id = 1;
node1.label = "node1";
node1.flags = static_cast<int32_t>(flutter::SemanticsFlags::kScopesRoute) |
static_cast<int32_t>(flutter::SemanticsFlags::kNamesRoute);
nodes[node1.id] = node1;
flutter::SemanticsNode node3;
node3.id = 3;
node3.label = "node3";
nodes[node3.id] = node3;
flutter::SemanticsNode root_node;
root_node.id = kRootNodeId;
root_node.flags = static_cast<int32_t>(flutter::SemanticsFlags::kScopesRoute);
root_node.childrenInTraversalOrder = {1, 3};
root_node.childrenInHitTestOrder = {1, 3};
nodes[root_node.id] = root_node;
bridge->UpdateSemantics(/*nodes=*/nodes, /*actions=*/actions);
XCTAssertEqual([accessibility_notifications count], 1ul);
XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], @"node1");
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
UIAccessibilityScreenChangedNotification);
// Simulates the focusing on the node 0.
bridge->AccessibilityObjectDidBecomeFocused(0);
flutter::SemanticsNodeUpdates new_nodes;
flutter::SemanticsNode new_node1;
new_node1.id = 1;
new_node1.label = "new_node1";
new_node1.flags = static_cast<int32_t>(flutter::SemanticsFlags::kScopesRoute) |
static_cast<int32_t>(flutter::SemanticsFlags::kNamesRoute);
new_node1.childrenInTraversalOrder = {2};
new_node1.childrenInHitTestOrder = {2};
new_nodes[new_node1.id] = new_node1;
flutter::SemanticsNode new_node2;
new_node2.id = 2;
new_node2.label = "new_node2";
new_node2.flags = static_cast<int32_t>(flutter::SemanticsFlags::kScopesRoute) |
static_cast<int32_t>(flutter::SemanticsFlags::kNamesRoute);
new_nodes[new_node2.id] = new_node2;
flutter::SemanticsNode new_root_node;
new_root_node.id = kRootNodeId;
new_root_node.flags = static_cast<int32_t>(flutter::SemanticsFlags::kScopesRoute);
new_root_node.childrenInTraversalOrder = {1};
new_root_node.childrenInHitTestOrder = {1};
new_nodes[new_root_node.id] = new_root_node;
bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions);
XCTAssertEqual([accessibility_notifications count], 3ul);
XCTAssertEqualObjects(accessibility_notifications[1][@"argument"], @"new_node2");
XCTAssertEqual([accessibility_notifications[1][@"notification"] unsignedIntValue],
UIAccessibilityScreenChangedNotification);
SemanticsObject* focusObject = accessibility_notifications[2][@"argument"];
// It should still focus the root.
XCTAssertEqual([focusObject uid], 0);
XCTAssertEqual([accessibility_notifications[2][@"notification"] unsignedIntValue],
UIAccessibilityLayoutChangedNotification);
}
- (void)testAnnouncesRouteChangesWhenAddAdditionalRoute {
flutter::MockDelegate mock_delegate;
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");