Skip to content

Commit 03ebdcd

Browse files
[iOS] Preserve semantics parents after reparenting (flutter#189686)
Fixes flutter#100946. ## Problem Reordering a `ReorderableListView` on iOS could move VoiceOver focus to the upper-left corner of the screen. The affected items could no longer be focused at their visual positions. During a reorder, semantics children move through temporary containers used by the drag overlay and drop animation. A child may already have been attached to its replacement `SemanticsObject` when its previous parent is updated or deallocated. The previous parent unconditionally cleared the child's `parent` property. This overwrote the new parent relationship and disconnected the child from the native semantics hierarchy. `SemanticsObject` calculates its global accessibility frame by walking this parent chain and composing each ancestor's transform. Once the chain was disconnected, the element retained only its local physical-pixel rectangle. On a 3x simulator, the expected frame: ``` {{0, 286}, {402, 56}} ``` became: ``` {{0, 0}, {134, 18.67}} ``` This accounts for both the missing translation and the frame being reduced by the device pixel ratio. ## Fix Only clear a child's parent when the `SemanticsObject` performing cleanup still owns that parent relationship: ```objc child.parent == self ``` This guard is applied when: - A `SemanticsObject` is deallocated. - Its traversal-order children are replaced. - Its hit-test-order children are replaced. A previous parent can now release its stale child references without detaching a child that has already moved under a replacement container. The complete ancestor transform remains available when calculating the native accessibility frame. ## Testing Added An iOS XCUITest that performs the default mobile long-press reorder gesture, verifies the resulting native semantic traversal order, and asserts that the reordered item retains the expected accessibility frame. The integration app also includes a deterministic direct-drag fixture for exercising XCUITest drag synthesis. Validation performed: - `testReorderableListAccessibilityHierarchyUpdates` passes with the locally built engine. - `packages/flutter/test/widgets/reorderable_list_test.dart` passes. - `packages/flutter/test/material/reorderable_list_test.dart` passes. - `ios_test_flutter` builds successfully. - Engine formatting and Dart analysis pass. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. --------- Co-authored-by: LongCatIsLooong <[email protected]>
1 parent 9c71f8b commit 03ebdcd

6 files changed

Lines changed: 296 additions & 3 deletions

File tree

‎dev/integration_tests/ios_platform_view_tests/ios/PlatformViewUITests/PlatformViewUITests.m‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
static const CGFloat kStandardTimeOut = 60.0;
88

9+
static NSArray<NSString *> *ElementLabels(XCUIElementQuery *query) {
10+
NSMutableArray<NSString *> *labels = [[NSMutableArray alloc] init];
11+
for (XCUIElement *element in query.allElementsBoundByIndex) {
12+
[labels addObject:element.label];
13+
}
14+
return labels;
15+
}
16+
917
@interface XCUIElement(Test)
1018
@property (nonatomic, readonly) BOOL flt_hasKeyboardFocus;
1119
- (void)flt_forceTap;
@@ -68,6 +76,101 @@ - (void)testPlatformViewFocus {
6876
XCTAssertFalse(flutterTextField.flt_hasKeyboardFocus);
6977
}
7078

79+
- (void)testReorderableListAccessibilityHierarchyUpdates {
80+
// Regression test for https://github.com/flutter/flutter/issues/100946.
81+
XCUIElement *entranceButton =
82+
self.app.buttons[@"reorderable list semantics test"];
83+
XCTAssertTrue([entranceButton waitForExistenceWithTimeout:kStandardTimeOut],
84+
@"The element tree is %@", self.app.debugDescription);
85+
[entranceButton tap];
86+
87+
XCUIElement *item2 = self.app.staticTexts[@"Item 2"];
88+
XCUIElement *item4 = self.app.staticTexts[@"Item 4"];
89+
XCTAssertTrue([item2 waitForExistenceWithTimeout:kStandardTimeOut],
90+
@"The element tree is %@", self.app.debugDescription);
91+
XCTAssertTrue([item4 waitForExistenceWithTimeout:kStandardTimeOut],
92+
@"The element tree is %@", self.app.debugDescription);
93+
94+
XCUIElementQuery *items = [self.app.staticTexts
95+
matchingPredicate:[NSPredicate
96+
predicateWithFormat:@"label MATCHES 'Item [1-4]'"]];
97+
NSArray<NSString *> *initialOrder =
98+
@[ @"Item 1", @"Item 2", @"Item 3", @"Item 4" ];
99+
XCTAssertEqualObjects(ElementLabels(items), initialOrder,
100+
@"The element tree is %@", self.app.debugDescription);
101+
102+
CGRect item2FrameBeforeReorder = item2.frame;
103+
CGRect expectedItem2FrameAfterReorder = item4.frame;
104+
XCUICoordinate *dragStart =
105+
[item2 coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
106+
XCUICoordinate *dragEnd =
107+
[item4 coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
108+
[dragStart pressForDuration:1.0 thenDragToCoordinate:dragEnd];
109+
110+
NSArray<NSString *> *reorderedItems =
111+
@[ @"Item 1", @"Item 3", @"Item 4", @"Item 2" ];
112+
NSPredicate *hierarchyUpdated =
113+
[NSPredicate predicateWithBlock:^BOOL(XCUIElementQuery *query,
114+
NSDictionary *bindings) {
115+
return [ElementLabels(query) isEqualToArray:reorderedItems];
116+
}];
117+
XCTNSPredicateExpectation *hierarchyExpectation =
118+
[[XCTNSPredicateExpectation alloc] initWithPredicate:hierarchyUpdated
119+
object:items];
120+
XCTWaiterResult result =
121+
[XCTWaiter waitForExpectations:@[ hierarchyExpectation ]
122+
timeout:kStandardTimeOut];
123+
CGRect item2FrameAfterReorder = item2.frame;
124+
XCTAssertEqual(result, XCTWaiterResultCompleted,
125+
@"Item 2 frame before: %@; after: %@. The element tree is %@",
126+
NSStringFromCGRect(item2FrameBeforeReorder),
127+
NSStringFromCGRect(item2FrameAfterReorder),
128+
self.app.debugDescription);
129+
XCTAssertEqualObjects(ElementLabels(items), reorderedItems,
130+
@"The element tree is %@", self.app.debugDescription);
131+
XCTAssertTrue(
132+
CGRectEqualToRect(item2FrameAfterReorder, expectedItem2FrameAfterReorder),
133+
@"Expected Item 2 frame %@ after reorder, but found %@. The element tree "
134+
@"is %@",
135+
NSStringFromCGRect(expectedItem2FrameAfterReorder),
136+
NSStringFromCGRect(item2FrameAfterReorder), self.app.debugDescription);
137+
}
138+
139+
- (void)testDragScreen {
140+
XCUIElement *entranceButton = self.app.buttons[@"drag test"];
141+
XCTAssertTrue([entranceButton waitForExistenceWithTimeout:kStandardTimeOut],
142+
@"The element tree is %@", self.app.debugDescription);
143+
[entranceButton tap];
144+
145+
XCUIElement *item3 = self.app.staticTexts[@"Item 3"];
146+
XCUIElement *item1 = self.app.staticTexts[@"Item 1"];
147+
XCTAssertTrue([item3 waitForExistenceWithTimeout:kStandardTimeOut],
148+
@"The element tree is %@", self.app.debugDescription);
149+
XCTAssertTrue([item1 waitForExistenceWithTimeout:kStandardTimeOut],
150+
@"The element tree is %@", self.app.debugDescription);
151+
152+
XCUICoordinate *dragStart =
153+
[item3 coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
154+
XCUICoordinate *dragEnd =
155+
[item1 coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
156+
[dragStart pressForDuration:1.0 thenDragToCoordinate:dragEnd];
157+
158+
XCUIElement *item2 = self.app.staticTexts[@"Item 2"];
159+
XCUIElement *item5 = self.app.staticTexts[@"Item 5"];
160+
XCTAssertTrue([item2 waitForExistenceWithTimeout:kStandardTimeOut],
161+
@"The element tree is %@", self.app.debugDescription);
162+
XCTAssertTrue([item5 waitForExistenceWithTimeout:kStandardTimeOut],
163+
@"The element tree is %@", self.app.debugDescription);
164+
165+
dragStart = [item2 coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
166+
dragEnd = [item5 coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.9)];
167+
[dragStart pressForDuration:1.0 thenDragToCoordinate:dragEnd];
168+
169+
XCUIElement *success = self.app.staticTexts[@"Drag Success"];
170+
XCTAssertTrue([success waitForExistenceWithTimeout:kStandardTimeOut],
171+
@"The element tree is %@", self.app.debugDescription);
172+
}
173+
71174
- (void)testPlatformViewZOrder {
72175
XCUIElement *entranceButton = self.app.buttons[@"platform view z order test"];
73176
XCTAssertTrue([entranceButton waitForExistenceWithTimeout:kStandardTimeOut], @"The element tree is %@", self.app.debugDescription);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/material.dart';
7+
8+
class DragScreen extends StatefulWidget {
9+
const DragScreen({super.key});
10+
11+
@override
12+
State<DragScreen> createState() => _DragScreenState();
13+
}
14+
15+
class _DragScreenState extends State<DragScreen> {
16+
List<int> items = <int>[1, 2, 3, 4, 5];
17+
18+
static const List<int> targetOrder = <int>[3, 1, 4, 5, 2];
19+
20+
bool get isCorrectOrder => listEquals(items, targetOrder);
21+
22+
void _onReorderItem(int oldIndex, int newIndex) {
23+
setState(() {
24+
final int item = items.removeAt(oldIndex);
25+
items.insert(newIndex, item);
26+
});
27+
}
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
return Scaffold(
32+
appBar: AppBar(
33+
title: const Text('Drag Test'),
34+
leading: IconButton(
35+
icon: const Icon(Icons.arrow_back),
36+
onPressed: () => Navigator.of(context).pop(),
37+
),
38+
),
39+
body: Column(
40+
children: <Widget>[
41+
Padding(
42+
padding: const EdgeInsets.all(16.0),
43+
child: Text(
44+
'Reorder items to: ${targetOrder.join(", ")}',
45+
style: Theme.of(context).textTheme.titleMedium,
46+
),
47+
),
48+
Padding(
49+
padding: const EdgeInsets.symmetric(horizontal: 16.0),
50+
child: Text(
51+
'Current order: ${items.join(", ")}',
52+
style: Theme.of(context).textTheme.bodyMedium,
53+
),
54+
),
55+
const SizedBox(height: 16),
56+
Expanded(
57+
child: ReorderableListView.builder(
58+
itemCount: items.length,
59+
onReorderItem: _onReorderItem,
60+
buildDefaultDragHandles: false,
61+
proxyDecorator: (Widget child, int index, Animation<double> animation) {
62+
return Material(elevation: 4, color: Colors.transparent, child: child);
63+
},
64+
itemBuilder: (BuildContext context, int index) {
65+
final int item = items[index];
66+
return ReorderableDragStartListener(
67+
key: ValueKey<int>(item),
68+
index: index,
69+
child: Container(
70+
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
71+
child: ListTile(
72+
leading: const Icon(Icons.drag_handle),
73+
title: Text(
74+
'Item $item',
75+
style: const TextStyle(fontSize: 18),
76+
semanticsLabel: 'Item $item',
77+
),
78+
),
79+
),
80+
);
81+
},
82+
),
83+
),
84+
if (isCorrectOrder)
85+
Container(
86+
padding: const EdgeInsets.all(24),
87+
color: Colors.green,
88+
width: double.infinity,
89+
child: const Text(
90+
'Drag Success',
91+
textAlign: TextAlign.center,
92+
style: TextStyle(color: Colors.white, fontSize: 24, fontWeight: FontWeight.bold),
93+
),
94+
),
95+
],
96+
),
97+
);
98+
}
99+
}

‎dev/integration_tests/ios_platform_view_tests/lib/main.dart‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import 'package:flutter/material.dart';
66
import 'package:flutter/services.dart';
77

8+
import 'drag.dart';
9+
import 'reorderable_list_semantics.dart';
10+
811
void main() {
912
runApp(const MyApp());
1013
}
@@ -102,6 +105,28 @@ class _MyHomePageState extends State<MyHomePage> {
102105
);
103106
},
104107
),
108+
TextButton(
109+
child: const Text('reorderable list semantics test'),
110+
onPressed: () {
111+
Navigator.push(
112+
context,
113+
MaterialPageRoute<ReorderableListSemanticsScreen>(
114+
builder: (BuildContext context) => const ReorderableListSemanticsScreen(),
115+
),
116+
);
117+
},
118+
),
119+
TextButton(
120+
child: const Text('drag test'),
121+
onPressed: () {
122+
Navigator.push(
123+
context,
124+
MaterialPageRoute<DragScreen>(
125+
builder: (BuildContext context) => const DragScreen(),
126+
),
127+
);
128+
},
129+
),
105130
],
106131
),
107132
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
7+
/// A mobile [ReorderableListView] fixture that starts a drag after a long press.
8+
class ReorderableListSemanticsScreen extends StatefulWidget {
9+
const ReorderableListSemanticsScreen({super.key});
10+
11+
@override
12+
State<ReorderableListSemanticsScreen> createState() => _ReorderableListSemanticsScreenState();
13+
}
14+
15+
class _ReorderableListSemanticsScreenState extends State<ReorderableListSemanticsScreen> {
16+
final List<String> _items = <String>['Item 1', 'Item 2', 'Item 3', 'Item 4'];
17+
18+
@override
19+
Widget build(BuildContext context) {
20+
return Scaffold(
21+
appBar: AppBar(title: const Text('Reorderable list semantics test')),
22+
body: ReorderableListView.builder(
23+
itemCount: _items.length,
24+
itemBuilder: (BuildContext context, int index) {
25+
final String item = _items[index];
26+
return Semantics(
27+
container: true,
28+
explicitChildNodes: true,
29+
key: ValueKey<String>(item),
30+
child: ListTile(title: Text(item)),
31+
);
32+
},
33+
onReorderItem: (int oldIndex, int newIndex) {
34+
setState(() {
35+
_items.insert(newIndex, _items.removeAt(oldIndex));
36+
});
37+
},
38+
),
39+
);
40+
}
41+
}

‎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());

0 commit comments

Comments
 (0)