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