Skip to content

Commit c958555

Browse files
committed
fix: avoid announing current focused element during push transition
1 parent bfd1a0e commit c958555

2 files changed

Lines changed: 96 additions & 21 deletions

File tree

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

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

157157
bool routeChanged = false;
158-
NSString* routeName = nil;
159158
SemanticsObject* lastAdded = nil;
160159

161160
if (root) {
@@ -188,7 +187,6 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
188187
([lastAdded uid] != previous_route_id_ || [newRoutes count] != previous_routes_.size())) {
189188
previous_route_id_ = [lastAdded uid];
190189
routeChanged = true;
191-
routeName = [lastAdded routeName];
192190
}
193191
previous_routes_.clear();
194192
for (SemanticsObject* route in newRoutes) {
@@ -211,22 +209,25 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification,
211209
if (!ios_delegate_->IsFlutterViewControllerPresentingModalViewController(view_controller_)) {
212210
layoutChanged = layoutChanged || [doomed_uids count] > 0;
213211

214-
const bool shouldAnnounceRoute = routeChanged && routeName.length > 0;
212+
NSString* routeName = routeChanged ? [lastAdded routeName] : nil;
213+
SemanticsObject* next = FindNextFocusableIfNecessary();
214+
SemanticsObject* lastFocused = [objects_ objectForKey:@(last_focused_semantics_object_id_)];
215215

216-
if (shouldAnnounceRoute) {
216+
const bool focusPreserved = lastFocused != nil && lastFocused == next;
217+
const bool announceRoute = routeChanged && (routeName.length > 0 || !focusPreserved);
218+
219+
if (announceRoute) {
217220
ios_delegate_->PostAccessibilityNotification(UIAccessibilityScreenChangedNotification,
218221
routeName);
219222
}
220223

221224
if (layoutChanged) {
222-
SemanticsObject* next = FindNextFocusableIfNecessary();
223-
SemanticsObject* lastFocused = [objects_ objectForKey:@(last_focused_semantics_object_id_)];
224225
// Only specify the focus item if the new focus is different, avoiding double focuses on the
225-
// same item. See: https://github.com/flutter/flutter/issues/104176. If there is a named
226-
// route announcement, we always refocus.
226+
// same item. See: https://github.com/flutter/flutter/issues/104176. If there is a route
227+
// announcement, we always refocus.
227228
ios_delegate_->PostAccessibilityNotification(
228229
UIAccessibilityLayoutChangedNotification,
229-
(shouldAnnounceRoute || next != lastFocused) ? next.nativeAccessibility : NULL);
230+
(announceRoute || next != lastFocused) ? next.nativeAccessibility : NULL);
230231
} else if (scrollOccured) {
231232
// TODO(chunhtai): figure out what string to use for notification. At this
232233
// 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: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ - (void)testAnnouncesRouteChanges {
709709
UIAccessibilityScreenChangedNotification);
710710
}
711711

712-
- (void)testDoesNotAnnounceUnnamedRouteChanges {
712+
- (void)testTabSwitchDoesNotPostScreenChange {
713713
flutter::MockDelegate mock_delegate;
714714
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
715715
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
@@ -748,54 +748,128 @@ - (void)testDoesNotAnnounceUnnamedRouteChanges {
748748

749749
flutter::CustomAccessibilityActionUpdates actions;
750750

751-
// Build the first tree: unnamed route + one focusable item.
752751
flutter::SemanticsNodeUpdates first_nodes;
753-
754752
flutter::SemanticsNode first_route;
755753
first_route.id = 1;
756754
first_route.flags.scopesRoute = true;
757755
first_nodes[first_route.id] = first_route;
758-
759756
flutter::SemanticsNode focused_node;
760757
focused_node.id = 10;
761758
focused_node.label = "Item";
762759
focused_node.actions = static_cast<int32_t>(flutter::SemanticsAction::kTap);
763760
first_nodes[focused_node.id] = focused_node;
764-
765761
flutter::SemanticsNode first_root;
766762
first_root.id = kRootNodeId;
767763
first_root.flags.scopesRoute = true;
768764
first_root.childrenInTraversalOrder = {1, 10};
769765
first_root.childrenInHitTestOrder = {1, 10};
770766
first_nodes[first_root.id] = first_root;
771-
772767
bridge->UpdateSemantics(/*nodes=*/first_nodes, /*actions=*/actions);
773-
774768
bridge->AccessibilityObjectDidBecomeFocused(10);
775769
[accessibility_notifications removeAllObjects];
776770

777-
// Replace only the route (still unnamed), keep focus on the same item.
778771
flutter::SemanticsNodeUpdates second_nodes;
779-
780772
flutter::SemanticsNode second_route;
781773
second_route.id = 2;
782774
second_route.flags.scopesRoute = true;
783775
second_nodes[second_route.id] = second_route;
784-
785776
second_nodes[focused_node.id] = focused_node;
786-
787777
flutter::SemanticsNode second_root;
788778
second_root.id = kRootNodeId;
789779
second_root.flags.scopesRoute = true;
790780
second_root.childrenInTraversalOrder = {2, 10};
791781
second_root.childrenInHitTestOrder = {2, 10};
792782
second_nodes[second_root.id] = second_root;
793-
794783
bridge->UpdateSemantics(/*nodes=*/second_nodes, /*actions=*/actions);
795784

796785
XCTAssertEqual([accessibility_notifications count], 1ul);
786+
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
787+
UIAccessibilityLayoutChangedNotification);
797788
XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], [NSNull null]);
789+
}
790+
791+
- (void)testUnnamedPushTransitionPostsScreenChangeWithNilRouteName {
792+
flutter::MockDelegate mock_delegate;
793+
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
794+
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
795+
/*platform=*/thread_task_runner,
796+
/*raster=*/thread_task_runner,
797+
/*ui=*/thread_task_runner,
798+
/*io=*/thread_task_runner);
799+
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
800+
/*delegate=*/mock_delegate,
801+
/*rendering_api=*/mock_delegate.settings_.enable_impeller
802+
? flutter::IOSRenderingAPI::kMetal
803+
: flutter::IOSRenderingAPI::kSoftware,
804+
/*platform_views_controller=*/nil,
805+
/*task_runners=*/runners,
806+
/*worker_task_runner=*/nil,
807+
/*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
808+
id mockFlutterView = OCMClassMock([FlutterView class]);
809+
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
810+
OCMStub([mockFlutterViewController view]).andReturn(mockFlutterView);
811+
812+
NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
813+
[[NSMutableArray alloc] init];
814+
auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
815+
ios_delegate->on_PostAccessibilityNotification_ =
816+
[accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
817+
[accessibility_notifications addObject:@{
818+
@"notification" : @(notification),
819+
@"argument" : argument ? argument : [NSNull null],
820+
}];
821+
};
822+
__block auto bridge =
823+
std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
824+
/*platform_view=*/platform_view.get(),
825+
/*platform_views_controller=*/nil,
826+
/*ios_delegate=*/std::move(ios_delegate));
827+
828+
flutter::CustomAccessibilityActionUpdates actions;
829+
830+
flutter::SemanticsNodeUpdates first_nodes;
831+
flutter::SemanticsNode first_route;
832+
first_route.id = 1;
833+
first_route.flags.scopesRoute = true;
834+
first_nodes[first_route.id] = first_route;
835+
flutter::SemanticsNode first_item;
836+
first_item.id = 10;
837+
first_item.label = "First";
838+
first_item.actions = static_cast<int32_t>(flutter::SemanticsAction::kTap);
839+
first_nodes[first_item.id] = first_item;
840+
flutter::SemanticsNode first_root;
841+
first_root.id = kRootNodeId;
842+
first_root.flags.scopesRoute = true;
843+
first_root.childrenInTraversalOrder = {1, 10};
844+
first_root.childrenInHitTestOrder = {1, 10};
845+
first_nodes[first_root.id] = first_root;
846+
bridge->UpdateSemantics(/*nodes=*/first_nodes, /*actions=*/actions);
847+
bridge->AccessibilityObjectDidBecomeFocused(10);
848+
[accessibility_notifications removeAllObjects];
849+
850+
flutter::SemanticsNodeUpdates second_nodes;
851+
flutter::SemanticsNode second_route;
852+
second_route.id = 2;
853+
second_route.flags.scopesRoute = true;
854+
second_nodes[second_route.id] = second_route;
855+
flutter::SemanticsNode second_item;
856+
second_item.id = 20;
857+
second_item.label = "Second";
858+
second_item.actions = static_cast<int32_t>(flutter::SemanticsAction::kTap);
859+
second_nodes[second_item.id] = second_item;
860+
flutter::SemanticsNode second_root;
861+
second_root.id = kRootNodeId;
862+
second_root.flags.scopesRoute = true;
863+
second_root.childrenInTraversalOrder = {2, 20};
864+
second_root.childrenInHitTestOrder = {2, 20};
865+
second_nodes[second_root.id] = second_root;
866+
bridge->UpdateSemantics(/*nodes=*/second_nodes, /*actions=*/actions);
867+
868+
XCTAssertEqual([accessibility_notifications count], 2ul);
798869
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
870+
UIAccessibilityScreenChangedNotification);
871+
XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], [NSNull null]);
872+
XCTAssertEqual([accessibility_notifications[1][@"notification"] unsignedIntValue],
799873
UIAccessibilityLayoutChangedNotification);
800874
}
801875

0 commit comments

Comments
 (0)