Skip to content

Commit b3d8dfd

Browse files
committed
fix: [iOS] Skip unnamed route screen-change announcements
1 parent e38a002 commit b3d8dfd

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
155155
SemanticsObject* root = objects_[@(kRootNodeId)];
156156

157157
bool routeChanged = false;
158+
NSString* routeName = nil;
158159
SemanticsObject* lastAdded = nil;
159160

160161
if (root) {
@@ -187,6 +188,7 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
187188
([lastAdded uid] != previous_route_id_ || [newRoutes count] != previous_routes_.size())) {
188189
previous_route_id_ = [lastAdded uid];
189190
routeChanged = true;
191+
routeName = [lastAdded routeName];
190192
}
191193
previous_routes_.clear();
192194
for (SemanticsObject* route in newRoutes) {
@@ -209,8 +211,9 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
209211
if (!ios_delegate_->IsFlutterViewControllerPresentingModalViewController(view_controller_)) {
210212
layoutChanged = layoutChanged || [doomed_uids count] > 0;
211213

212-
if (routeChanged) {
213-
NSString* routeName = [lastAdded routeName];
214+
const bool shouldAnnounceRoute = routeChanged && routeName.length > 0;
215+
216+
if (shouldAnnounceRoute) {
214217
ios_delegate_->PostAccessibilityNotification(UIAccessibilityScreenChangedNotification,
215218
routeName);
216219
}
@@ -223,7 +226,7 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
223226
// change, we always refocus.
224227
ios_delegate_->PostAccessibilityNotification(
225228
UIAccessibilityLayoutChangedNotification,
226-
(routeChanged || next != lastFocused) ? next.nativeAccessibility : NULL);
229+
(shouldAnnounceRoute || next != lastFocused) ? next.nativeAccessibility : NULL);
227230
} else if (scrollOccured) {
228231
// TODO(chunhtai): figure out what string to use for notification. At this
229232
// point, it is guarantee the previous focused object is still in the tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,96 @@ - (void)testAnnouncesRouteChanges {
709709
UIAccessibilityScreenChangedNotification);
710710
}
711711

712+
- (void)testDoesNotAnnounceUnnamedRouteChanges {
713+
flutter::MockDelegate mock_delegate;
714+
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
715+
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
716+
/*platform=*/thread_task_runner,
717+
/*raster=*/thread_task_runner,
718+
/*ui=*/thread_task_runner,
719+
/*io=*/thread_task_runner);
720+
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
721+
/*delegate=*/mock_delegate,
722+
/*rendering_api=*/mock_delegate.settings_.enable_impeller
723+
? flutter::IOSRenderingAPI::kMetal
724+
: flutter::IOSRenderingAPI::kSoftware,
725+
/*platform_views_controller=*/nil,
726+
/*task_runners=*/runners,
727+
/*worker_task_runner=*/nil,
728+
/*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
729+
id mockFlutterView = OCMClassMock([FlutterView class]);
730+
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
731+
OCMStub([mockFlutterViewController view]).andReturn(mockFlutterView);
732+
733+
NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
734+
[[NSMutableArray alloc] init];
735+
auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
736+
ios_delegate->on_PostAccessibilityNotification_ =
737+
[accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
738+
[accessibility_notifications addObject:@{
739+
@"notification" : @(notification),
740+
@"argument" : argument ? argument : [NSNull null],
741+
}];
742+
};
743+
__block auto bridge =
744+
std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
745+
/*platform_view=*/platform_view.get(),
746+
/*platform_views_controller=*/nil,
747+
/*ios_delegate=*/std::move(ios_delegate));
748+
749+
flutter::CustomAccessibilityActionUpdates actions;
750+
751+
// Build the first tree: unnamed route + one focusable item.
752+
flutter::SemanticsNodeUpdates first_nodes;
753+
754+
flutter::SemanticsNode first_route;
755+
first_route.id = 1;
756+
first_route.flags.scopesRoute = true;
757+
first_nodes[first_route.id] = first_route;
758+
759+
flutter::SemanticsNode focused_node;
760+
focused_node.id = 10;
761+
focused_node.label = "Item";
762+
focused_node.actions = static_cast<int32_t>(flutter::SemanticsAction::kTap);
763+
first_nodes[focused_node.id] = focused_node;
764+
765+
flutter::SemanticsNode first_root;
766+
first_root.id = kRootNodeId;
767+
first_root.flags.scopesRoute = true;
768+
first_root.childrenInTraversalOrder = {1, 10};
769+
first_root.childrenInHitTestOrder = {1, 10};
770+
first_nodes[first_root.id] = first_root;
771+
772+
bridge->UpdateSemantics(/*nodes=*/first_nodes, /*actions=*/actions);
773+
774+
bridge->AccessibilityObjectDidBecomeFocused(10);
775+
[accessibility_notifications removeAllObjects];
776+
777+
// Replace only the route (still unnamed), keep focus on the same item.
778+
flutter::SemanticsNodeUpdates second_nodes;
779+
780+
flutter::SemanticsNode second_route;
781+
second_route.id = 2;
782+
second_route.flags.scopesRoute = true;
783+
second_nodes[second_route.id] = second_route;
784+
785+
second_nodes[focused_node.id] = focused_node;
786+
787+
flutter::SemanticsNode second_root;
788+
second_root.id = kRootNodeId;
789+
second_root.flags.scopesRoute = true;
790+
second_root.childrenInTraversalOrder = {2, 10};
791+
second_root.childrenInHitTestOrder = {2, 10};
792+
second_nodes[second_root.id] = second_root;
793+
794+
bridge->UpdateSemantics(/*nodes=*/second_nodes, /*actions=*/actions);
795+
796+
XCTAssertEqual([accessibility_notifications count], 1ul);
797+
XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], [NSNull null]);
798+
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
799+
UIAccessibilityLayoutChangedNotification);
800+
}
801+
712802
- (void)testRadioButtonIsNotSwitchButton {
713803
flutter::MockDelegate mock_delegate;
714804
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");

0 commit comments

Comments
 (0)