Skip to content

Commit 36d0427

Browse files
authored
[material_ui] Fix self-comparison and assert bugs in semantics_tester (flutter#12722)
`packages/material_ui/test/semantics_tester.dart` carries four bugs that make parts of the helper silently ineffective: - `controlsNodes != controlsNodes` compares the field to itself, so the `setEquals` check is unreachable and the `controlsNodes` expectation never runs - the locale comparison reads `second[i]` on both sides instead of `first[i]`, so mismatched locales compare equal - the `minValue`/`maxValue` checks sit in `assert()`'s message slot rather than its condition, so they gate nothing - `attributedLabel`, `attributedValue`, `attributedHint` and `hint` were missing from that assert's condition, so passing any one of them alone tripped it Adds `packages/material_ui/test/semantics_tester_test.dart` covering each fix. There was no test file for this helper previously. ## Context Ported from flutter#192164, which cannot land there because `packages/flutter/test/material` is frozen (flutter#188444). The same four fixes landed for the `widgets` copy of this helper in flutter#191938. ## Testing `flutter test test/semantics_tester_test.dart` in `packages/material_ui` passes 4/4, and `dart analyze` is clean, against Flutter 3.47.2. Changelog uses `## NEXT` since this is test-only and does not affect the published package.
1 parent 8890030 commit 36d0427

2 files changed

Lines changed: 147 additions & 4 deletions

File tree

‎packages/material_ui/test/semantics_tester.dart‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ class TestSemantics {
509509
);
510510
}
511511

512-
if (controlsNodes != controlsNodes && !setEquals(controlsNodes, node.controlsNodes)) {
512+
if (controlsNodes != null && !setEquals(controlsNodes, node.controlsNodes)) {
513513
return fail(
514514
'expected node id $id to controls nodes $controlsNodes but found controlling nodes ${node.controlsNodes}',
515515
);
@@ -716,7 +716,7 @@ class SemanticsTester {
716716
if (first[i] is LocaleStringAttribute &&
717717
(second[i] is! LocaleStringAttribute ||
718718
second[i].range != first[i].range ||
719-
(second[i] as LocaleStringAttribute).locale !=
719+
(first[i] as LocaleStringAttribute).locale !=
720720
(second[i] as LocaleStringAttribute).locale)) {
721721
return false;
722722
}
@@ -1169,20 +1169,32 @@ class _IncludesNodeWith extends Matcher {
11691169
this.maxValue,
11701170
}) : assert(
11711171
label != null ||
1172+
attributedLabel != null ||
11721173
value != null ||
1174+
attributedValue != null ||
1175+
hint != null ||
1176+
attributedHint != null ||
11731177
actions != null ||
11741178
flags != null ||
11751179
flagsCollection != null ||
11761180
tags != null ||
11771181
increasedValue != null ||
11781182
decreasedValue != null ||
1183+
textDirection != null ||
11791184
scrollPosition != null ||
11801185
scrollExtentMax != null ||
11811186
scrollExtentMin != null ||
11821187
maxValueLength != null ||
11831188
currentValueLength != null ||
1184-
inputType != null,
1185-
minValue != null || maxValue != null,
1189+
inputType != null ||
1190+
minValue != null ||
1191+
maxValue != null,
1192+
'At least one matcher field must be non-null so the matcher checks for at least one '
1193+
'property of a semantics node. Pass any of `label`, `attributedLabel`, `value`, '
1194+
'`attributedValue`, `hint`, `attributedHint`, `actions`, `flags`, `flagsCollection`, '
1195+
'`tags`, `increasedValue`, `decreasedValue`, `textDirection`, `scrollPosition`, '
1196+
'`scrollExtentMax`, `scrollExtentMin`, `maxValueLength`, `currentValueLength`, '
1197+
'`inputType`, `minValue`, or `maxValue`.',
11861198
);
11871199
final AttributedString? attributedLabel;
11881200
final AttributedString? attributedValue;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2013 The Flutter Authors
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/rendering.dart';
6+
import 'package:flutter/widgets.dart';
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
import 'semantics_tester.dart';
10+
11+
void main() {
12+
testWidgets('Semantics tester compares controlsNodes', (WidgetTester tester) async {
13+
final semantics = SemanticsTester(tester);
14+
15+
await tester.pumpWidget(
16+
Semantics(container: true, controlsNodes: const <String>{'actual'}, child: Container()),
17+
);
18+
19+
final expectedSemantics = TestSemantics.root(
20+
children: <TestSemantics>[
21+
TestSemantics.rootChild(
22+
id: 1,
23+
rect: TestSemantics.fullScreen,
24+
controlsNodes: const <String>{'expected'},
25+
),
26+
],
27+
);
28+
29+
expect(semantics, isNot(hasSemantics(expectedSemantics)));
30+
semantics.dispose();
31+
});
32+
33+
testWidgets('Semantics tester compares attributed string locales', (WidgetTester tester) async {
34+
final semantics = SemanticsTester(tester);
35+
36+
await tester.pumpWidget(
37+
Semantics(
38+
container: true,
39+
attributedLabel: AttributedString(
40+
'label',
41+
attributes: <StringAttribute>[
42+
LocaleStringAttribute(
43+
locale: const Locale('en'),
44+
range: const TextRange(start: 0, end: 5),
45+
),
46+
],
47+
),
48+
textDirection: TextDirection.ltr,
49+
child: Container(),
50+
),
51+
);
52+
53+
expect(
54+
semantics,
55+
isNot(
56+
includesNodeWith(
57+
label: 'label',
58+
attributedLabel: AttributedString(
59+
'label',
60+
attributes: <StringAttribute>[
61+
LocaleStringAttribute(
62+
locale: const Locale('es'),
63+
range: const TextRange(start: 0, end: 5),
64+
),
65+
],
66+
),
67+
),
68+
),
69+
);
70+
semantics.dispose();
71+
});
72+
73+
testWidgets('includesNodeWith accepts minValue and maxValue', (WidgetTester tester) async {
74+
final semantics = SemanticsTester(tester);
75+
76+
await tester.pumpWidget(
77+
Semantics(
78+
container: true,
79+
value: '5',
80+
minValue: '0',
81+
maxValue: '10',
82+
textDirection: TextDirection.ltr,
83+
child: Container(),
84+
),
85+
);
86+
87+
expect(semantics, includesNodeWith(minValue: '0'));
88+
expect(semantics, includesNodeWith(maxValue: '10'));
89+
semantics.dispose();
90+
});
91+
92+
testWidgets('includesNodeWith accepts attributed fields and hint alone', (
93+
WidgetTester tester,
94+
) async {
95+
final semantics = SemanticsTester(tester);
96+
97+
await tester.pumpWidget(
98+
Semantics(
99+
container: true,
100+
attributedLabel: AttributedString('label'),
101+
attributedValue: AttributedString('value'),
102+
attributedHint: AttributedString('hint'),
103+
textDirection: TextDirection.ltr,
104+
child: Container(),
105+
),
106+
);
107+
108+
expect(semantics, includesNodeWith(attributedLabel: AttributedString('label')));
109+
expect(semantics, includesNodeWith(attributedValue: AttributedString('value')));
110+
expect(semantics, includesNodeWith(attributedHint: AttributedString('hint')));
111+
expect(semantics, includesNodeWith(hint: 'hint'));
112+
semantics.dispose();
113+
});
114+
115+
testWidgets('includesNodeWith accepts textDirection alone', (WidgetTester tester) async {
116+
final semantics = SemanticsTester(tester);
117+
118+
await tester.pumpWidget(
119+
Semantics(
120+
container: true,
121+
label: 'label',
122+
textDirection: TextDirection.ltr,
123+
child: Container(),
124+
),
125+
);
126+
127+
expect(semantics, includesNodeWith(textDirection: TextDirection.ltr));
128+
expect(semantics, isNot(includesNodeWith(textDirection: TextDirection.rtl)));
129+
semantics.dispose();
130+
});
131+
}

0 commit comments

Comments
 (0)