Skip to content

Commit 34c5993

Browse files
committed
Preserve reparented iOS semantics children
Only detach a semantics child when the object being updated still owns its parent link. This prevents a removed reorderable-list container from clearing a child that has already moved into its replacement and losing the ancestor transform used for its accessibility frame. Issue: flutter#100946
1 parent dc41b6c commit 34c5993

3 files changed

Lines changed: 131 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ - (void)dealloc {
302302
// See https://github.com/flutter/engine/pull/4602 and
303303
// https://github.com/flutter/engine/pull/27786.
304304
for (SemanticsObject* child in _children) {
305-
child.parent = nil;
305+
if (child.parent == self) {
306+
child.parent = nil;
307+
}
306308
}
307309
[_children removeAllObjects];
308310

@@ -318,7 +320,9 @@ - (void)dealloc {
318320

319321
- (void)setChildren:(NSArray<SemanticsObject*>*)children {
320322
for (SemanticsObject* child in _children) {
321-
child.parent = nil;
323+
if (child.parent == self) {
324+
child.parent = nil;
325+
}
322326
}
323327
_children = [children mutableCopy];
324328
for (SemanticsObject* child in _children) {
@@ -328,7 +332,9 @@ - (void)setChildren:(NSArray<SemanticsObject*>*)children {
328332

329333
- (void)setChildrenInHitTestOrder:(NSArray<SemanticsObject*>*)childrenInHitTestOrder {
330334
for (SemanticsObject* child in _childrenInHitTestOrder) {
331-
child.parent = nil;
335+
if (child.parent == self) {
336+
child.parent = nil;
337+
}
332338
}
333339
_childrenInHitTestOrder = [childrenInHitTestOrder copy];
334340
for (SemanticsObject* child in _childrenInHitTestOrder) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ - (void)testSetChildren {
6969
XCTAssertNil(child.parent);
7070
}
7171

72+
- (void)testOldParentDoesNotDetachReparentedChild {
73+
fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(
74+
new flutter::testing::MockAccessibilityBridge());
75+
fml::WeakPtr<flutter::AccessibilityBridgeIos> bridge = factory.GetWeakPtr();
76+
SemanticsObject* oldParent = [[SemanticsObject alloc] initWithBridge:bridge uid:0];
77+
SemanticsObject* newParent = [[SemanticsObject alloc] initWithBridge:bridge uid:1];
78+
SemanticsObject* child = [[SemanticsObject alloc] initWithBridge:bridge uid:2];
79+
80+
oldParent.children = @[ child ];
81+
oldParent.childrenInHitTestOrder = @[ child ];
82+
newParent.children = @[ child ];
83+
newParent.childrenInHitTestOrder = @[ child ];
84+
85+
oldParent.children = @[];
86+
oldParent.childrenInHitTestOrder = @[];
87+
88+
XCTAssertEqual(child.parent, newParent);
89+
}
90+
7291
- (void)testAccessibilityHitTestFocusAtLeaf {
7392
fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(
7493
new flutter::testing::MockAccessibilityBridge());

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,109 @@ - (void)testReplacedSemanticsDoesNotCleanupChildren {
500500
XCTAssertNotNil([child accessibilityContainer]);
501501
}
502502

503+
- (void)testRemovedSemanticsDoesNotDetachReparentedChild {
504+
// Regression test for https://github.com/flutter/flutter/issues/100946.
505+
flutter::MockDelegate mock_delegate;
506+
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
507+
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
508+
/*platform=*/thread_task_runner,
509+
/*raster=*/thread_task_runner,
510+
/*ui=*/thread_task_runner,
511+
/*io=*/thread_task_runner);
512+
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
513+
/*delegate=*/mock_delegate,
514+
/*rendering_api=*/mock_delegate.settings_.enable_impeller
515+
? flutter::IOSRenderingAPI::kMetal
516+
: flutter::IOSRenderingAPI::kSoftware,
517+
/*platform_views_controller=*/nil,
518+
/*task_runners=*/runners,
519+
/*worker_task_runner=*/nil,
520+
/*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
521+
id engine = OCMClassMock([FlutterEngine class]);
522+
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
523+
FlutterView* flutterView = [[FlutterView alloc] initWithDelegate:engine
524+
opaque:YES
525+
enableWideGamut:NO];
526+
OCMStub([mockFlutterViewController view]).andReturn(flutterView);
527+
528+
NSMutableArray<NSDictionary<NSString*, id>*>* accessibility_notifications =
529+
[[NSMutableArray alloc] init];
530+
auto ios_delegate = std::make_unique<flutter::MockIosDelegate>();
531+
ios_delegate->on_PostAccessibilityNotification_ =
532+
[accessibility_notifications](UIAccessibilityNotifications notification, id argument) {
533+
[accessibility_notifications addObject:@{
534+
@"notification" : @(notification),
535+
@"argument" : argument ? argument : [NSNull null],
536+
}];
537+
};
538+
auto bridge =
539+
std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
540+
/*platform_view=*/platform_view.get(),
541+
/*platform_views_controller=*/nil,
542+
/*ios_delegate=*/std::move(ios_delegate));
543+
544+
flutter::CustomAccessibilityActionUpdates actions;
545+
flutter::SemanticsNodeUpdates first_update;
546+
547+
flutter::SemanticsNode root;
548+
root.id = kRootNodeId;
549+
root.childrenInTraversalOrder = {1};
550+
root.childrenInHitTestOrder = {1};
551+
first_update[root.id] = root;
552+
553+
flutter::SemanticsNode old_container;
554+
old_container.id = 1;
555+
old_container.actions = static_cast<int32_t>(flutter::SemanticsAction::kTap);
556+
old_container.childrenInTraversalOrder = {2};
557+
old_container.childrenInHitTestOrder = {2};
558+
first_update[old_container.id] = old_container;
559+
560+
flutter::SemanticsNode child;
561+
child.id = 2;
562+
child.label = "Item 2";
563+
child.rect = SkRect::MakeXYWH(0, 0, 300, 60);
564+
first_update[child.id] = child;
565+
566+
bridge->UpdateSemantics(/*nodes=*/first_update, /*actions=*/actions);
567+
[accessibility_notifications removeAllObjects];
568+
bridge->AccessibilityObjectDidBecomeFocused(child.id);
569+
570+
@autoreleasepool {
571+
flutter::SemanticsNodeUpdates second_update;
572+
573+
flutter::SemanticsNode new_root;
574+
new_root.id = kRootNodeId;
575+
new_root.childrenInTraversalOrder = {3};
576+
new_root.childrenInHitTestOrder = {3};
577+
second_update[new_root.id] = new_root;
578+
579+
flutter::SemanticsNode new_container;
580+
new_container.id = 3;
581+
new_container.actions = static_cast<int32_t>(flutter::SemanticsAction::kTap);
582+
new_container.transform = SkM44::Translate(0, 300);
583+
new_container.childrenInTraversalOrder = {2};
584+
new_container.childrenInHitTestOrder = {2};
585+
second_update[new_container.id] = new_container;
586+
587+
bridge->UpdateSemantics(/*nodes=*/second_update, /*actions=*/actions);
588+
}
589+
590+
XCTAssertEqual([accessibility_notifications count], 1ul);
591+
XCTAssertEqualObjects(accessibility_notifications[0][@"argument"], [NSNull null]);
592+
XCTAssertEqual([accessibility_notifications[0][@"notification"] unsignedIntValue],
593+
UIAccessibilityLayoutChangedNotification);
594+
595+
SemanticsObjectContainer* rootContainer = flutterView.accessibilityElements[0];
596+
SemanticsObjectContainer* newContainer = [rootContainer accessibilityElementAtIndex:1];
597+
XCTAssertEqual(newContainer.semanticsObject.uid, 3);
598+
SemanticsObject* reparentedChild = [newContainer accessibilityElementAtIndex:1];
599+
XCTAssertEqual(reparentedChild.uid, child.id);
600+
XCTAssertEqual(reparentedChild.accessibilityContainer, newContainer);
601+
const CGFloat screenScale = UIScreen.mainScreen.scale;
602+
CGRect expectedFrame = CGRectMake(0, 300 / screenScale, 300 / screenScale, 60 / screenScale);
603+
XCTAssertTrue(CGRectEqualToRect(reparentedChild.accessibilityFrame, expectedFrame));
604+
}
605+
503606
- (void)testScrollableSemanticsDeallocated {
504607
flutter::MockDelegate mock_delegate;
505608
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");

0 commit comments

Comments
 (0)