Skip to content

Commit ea49db5

Browse files
authored
[dynamic_layouts] Refactor flaky test (#4681)
This refactors a test for the example of a wrapped layout in dynamic_tests. This test had become brittle to small text changes because it would check the precise layout offset of the text. It was patched in these cases in #4513 and #4677 to address tiny text variations across platforms and material 2/3 defaults. This refactor changes the test to check the layout offset of the parent Container of the text, which should not have these subtle variations. Fixes flutter/flutter#132321
1 parent 0bf0878 commit ea49db5

File tree

2 files changed

+68
-45
lines changed

2 files changed

+68
-45
lines changed

packages/dynamic_layouts/example/lib/wrap_layout_example.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class WrapExample extends StatelessWidget {
1818
),
1919
body: DynamicGridView.builder(
2020
gridDelegate: const SliverGridDelegateWithWrapping(),
21+
itemCount: 20,
2122
itemBuilder: (BuildContext context, int index) {
2223
return Container(
2324
height: index.isEven ? index % 7 * 50 + 150 : index % 4 * 50 + 100,

packages/dynamic_layouts/example/test/wrap_example_test.dart

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,81 @@ import 'package:flutter/material.dart';
77
import 'package:flutter_test/flutter_test.dart';
88

99
void main() {
10-
void withinTolerance(Offset actual, Offset expected, double tolerance) {
10+
testWidgets('Check wrap layout', (WidgetTester tester) async {
11+
const MaterialApp app = MaterialApp(
12+
home: WrapExample(),
13+
);
14+
await tester.pumpWidget(app);
15+
await tester.pumpAndSettle();
16+
17+
// Validate which children are laid out.
18+
for (int i = 0; i <= 12; i++) {
19+
expect(find.text('Index $i'), findsOneWidget);
20+
}
21+
for (int i = 13; i < 19; i++) {
22+
expect(find.text('Index $i'), findsNothing);
23+
}
24+
25+
// Validate with the position of the box, not the text.
26+
Finder getContainer(String text) {
27+
return find.ancestor(
28+
of: find.text(text),
29+
matching: find.byType(Container),
30+
);
31+
}
32+
33+
// Validate layout position.
1134
expect(
12-
actual.dx,
13-
(double actual) => actual <= expected.dx + tolerance,
14-
reason: '${actual.dx} <= ${expected.dx + tolerance}',
35+
tester.getTopLeft(getContainer('Index 0')),
36+
const Offset(0.0, 56.0),
1537
);
1638
expect(
17-
actual.dx,
18-
(double actual) => actual >= expected.dx - tolerance,
19-
reason: '${actual.dx} >= ${expected.dx - tolerance}',
39+
tester.getTopLeft(getContainer('Index 1')),
40+
const Offset(40.0, 56.0),
2041
);
2142
expect(
22-
actual.dy,
23-
(double actual) => actual <= expected.dy + tolerance,
24-
reason: '${actual.dy} <= ${expected.dy + tolerance}',
43+
tester.getTopLeft(getContainer('Index 2')),
44+
const Offset(190.0, 56.0),
2545
);
2646
expect(
27-
actual.dy,
28-
(double actual) => actual >= expected.dy - tolerance,
29-
reason: '${actual.dy} >= ${expected.dy - tolerance}',
47+
tester.getTopLeft(getContainer('Index 3')),
48+
const Offset(270.0, 56.0),
3049
);
31-
}
32-
33-
testWidgets('Check that the children are layed out.',
34-
(WidgetTester tester) async {
35-
const MaterialApp app = MaterialApp(
36-
home: WrapExample(),
50+
expect(
51+
tester.getTopLeft(getContainer('Index 4')),
52+
const Offset(370.0, 56.0),
53+
);
54+
expect(
55+
tester.getTopLeft(getContainer('Index 5')),
56+
const Offset(490.0, 56.0),
57+
);
58+
expect(
59+
tester.getTopLeft(getContainer('Index 6')),
60+
const Offset(690.0, 56.0),
61+
);
62+
expect(
63+
tester.getTopLeft(getContainer('Index 7')),
64+
const Offset(0.0, 506.0),
65+
);
66+
expect(
67+
tester.getTopLeft(getContainer('Index 8')),
68+
const Offset(150.0, 506.0),
69+
);
70+
expect(
71+
tester.getTopLeft(getContainer('Index 9')),
72+
const Offset(250.0, 506.0),
73+
);
74+
expect(
75+
tester.getTopLeft(getContainer('Index 10')),
76+
const Offset(350.0, 506.0),
77+
);
78+
expect(
79+
tester.getTopLeft(getContainer('Index 11')),
80+
const Offset(390.0, 506.0),
81+
);
82+
expect(
83+
tester.getTopLeft(getContainer('Index 12')),
84+
const Offset(590.0, 506.0),
3785
);
38-
await tester.pumpWidget(app);
39-
await tester.pumpAndSettle();
40-
41-
// See if there are children layed out.
42-
expect(find.text('Index 0'), findsOneWidget);
43-
expect(find.text('Index 1'), findsOneWidget);
44-
expect(find.text('Index 2'), findsOneWidget);
45-
expect(find.text('Index 3'), findsOneWidget);
46-
expect(find.text('Index 4'), findsOneWidget);
47-
48-
// Material 3 changes the expected layout positioning.
49-
final bool usesMaterial3 = (app.theme ?? ThemeData.light()).useMaterial3;
50-
final Offset offset0 =
51-
usesMaterial3 ? const Offset(0.0, 91.0) : const Offset(0.0, 103.0);
52-
final Offset offset1 =
53-
usesMaterial3 ? const Offset(65.0, 121.0) : const Offset(66.0, 124.0);
54-
final Offset offset3 =
55-
usesMaterial3 ? const Offset(270.0, 171.0) : const Offset(271.0, 174.0);
56-
final Offset offset4 =
57-
usesMaterial3 ? const Offset(380.0, 221.0) : const Offset(381.0, 224.0);
58-
59-
// See if they are in expected position.
60-
withinTolerance(tester.getTopLeft(find.text('Index 0')), offset0, 0.2);
61-
withinTolerance(tester.getTopLeft(find.text('Index 1')), offset1, 0.2);
62-
withinTolerance(tester.getTopLeft(find.text('Index 3')), offset3, 0.2);
63-
withinTolerance(tester.getTopLeft(find.text('Index 4')), offset4, 0.2);
6486
});
6587
}

0 commit comments

Comments
 (0)