Skip to content

Commit 7694682

Browse files
authored
Do not return partial semantics from tester.getSemantics (flutter#60367)
1 parent aa0382e commit 7694682

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/flutter_test/lib/src/widget_tester.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,11 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
997997
/// The ancestor's semantic data will include the child's as well as
998998
/// other nodes that have been merged together.
999999
///
1000+
/// If the [SemanticsNode] of the object identified by the finder is
1001+
/// force-merged into an ancestor (e.g. via the [MergeSemantics] widget)
1002+
/// the node into which it is merged is returned. That node will include
1003+
/// all the semantics information of the nodes merged into it.
1004+
///
10001005
/// Will throw a [StateError] if the finder returns more than one element or
10011006
/// if no semantics are found or are not enabled.
10021007
SemanticsNode getSemantics(Finder finder) {
@@ -1012,7 +1017,7 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
10121017
final Element element = candidates.single;
10131018
RenderObject renderObject = element.findRenderObject();
10141019
SemanticsNode result = renderObject.debugSemantics;
1015-
while (renderObject != null && result == null) {
1020+
while (renderObject != null && (result == null || result.isMergedIntoParent)) {
10161021
renderObject = renderObject?.parent as RenderObject;
10171022
result = renderObject?.debugSemantics;
10181023
}

packages/flutter_test/test/widget_tester_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,34 @@ void main() {
127127
expect(semantics.label, 'A\nB\nC');
128128
semanticsHandle.dispose();
129129
});
130+
131+
testWidgets('Does not return partial semantics', (WidgetTester tester) async {
132+
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
133+
final Key key = UniqueKey();
134+
await tester.pumpWidget(
135+
MaterialApp(
136+
home: Scaffold(
137+
body: MergeSemantics(
138+
child: Semantics(
139+
container: true,
140+
label: 'A',
141+
child: Semantics(
142+
container: true,
143+
key: key,
144+
label: 'B',
145+
child: Container(),
146+
),
147+
),
148+
),
149+
),
150+
),
151+
);
152+
153+
final SemanticsNode node = tester.getSemantics(find.byKey(key));
154+
final SemanticsData semantics = node.getSemanticsData();
155+
expect(semantics.label, 'A\nB');
156+
semanticsHandle.dispose();
157+
});
130158
});
131159

132160
group('ensureVisible', () {

0 commit comments

Comments
 (0)