Skip to content

[dynamic_layouts] Refactor flaky test #4681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class WrapExample extends StatelessWidget {
),
body: DynamicGridView.builder(
gridDelegate: const SliverGridDelegateWithWrapping(),
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return Container(
height: index.isEven ? index % 7 * 50 + 150 : index % 4 * 50 + 100,
Expand Down
112 changes: 67 additions & 45 deletions packages/dynamic_layouts/example/test/wrap_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,81 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
void withinTolerance(Offset actual, Offset expected, double tolerance) {
testWidgets('Check wrap layout', (WidgetTester tester) async {
const MaterialApp app = MaterialApp(
home: WrapExample(),
);
await tester.pumpWidget(app);
await tester.pumpAndSettle();

// Validate which children are laid out.
for (int i = 0; i <= 12; i++) {
expect(find.text('Index $i'), findsOneWidget);
}
for (int i = 13; i < 19; i++) {
expect(find.text('Index $i'), findsNothing);
}

// Validate with the position of the box, not the text.
Finder getContainer(String text) {
return find.ancestor(
of: find.text(text),
matching: find.byType(Container),
);
}

// Validate layout position.
expect(
actual.dx,
(double actual) => actual <= expected.dx + tolerance,
reason: '${actual.dx} <= ${expected.dx + tolerance}',
tester.getTopLeft(getContainer('Index 0')),
const Offset(0.0, 56.0),
);
expect(
actual.dx,
(double actual) => actual >= expected.dx - tolerance,
reason: '${actual.dx} >= ${expected.dx - tolerance}',
tester.getTopLeft(getContainer('Index 1')),
const Offset(40.0, 56.0),
);
expect(
actual.dy,
(double actual) => actual <= expected.dy + tolerance,
reason: '${actual.dy} <= ${expected.dy + tolerance}',
tester.getTopLeft(getContainer('Index 2')),
const Offset(190.0, 56.0),
);
expect(
actual.dy,
(double actual) => actual >= expected.dy - tolerance,
reason: '${actual.dy} >= ${expected.dy - tolerance}',
tester.getTopLeft(getContainer('Index 3')),
const Offset(270.0, 56.0),
);
}

testWidgets('Check that the children are layed out.',
(WidgetTester tester) async {
const MaterialApp app = MaterialApp(
home: WrapExample(),
expect(
tester.getTopLeft(getContainer('Index 4')),
const Offset(370.0, 56.0),
);
expect(
tester.getTopLeft(getContainer('Index 5')),
const Offset(490.0, 56.0),
);
expect(
tester.getTopLeft(getContainer('Index 6')),
const Offset(690.0, 56.0),
);
expect(
tester.getTopLeft(getContainer('Index 7')),
const Offset(0.0, 506.0),
);
expect(
tester.getTopLeft(getContainer('Index 8')),
const Offset(150.0, 506.0),
);
expect(
tester.getTopLeft(getContainer('Index 9')),
const Offset(250.0, 506.0),
);
expect(
tester.getTopLeft(getContainer('Index 10')),
const Offset(350.0, 506.0),
);
expect(
tester.getTopLeft(getContainer('Index 11')),
const Offset(390.0, 506.0),
);
expect(
tester.getTopLeft(getContainer('Index 12')),
const Offset(590.0, 506.0),
);
await tester.pumpWidget(app);
await tester.pumpAndSettle();

// See if there are children layed out.
expect(find.text('Index 0'), findsOneWidget);
expect(find.text('Index 1'), findsOneWidget);
expect(find.text('Index 2'), findsOneWidget);
expect(find.text('Index 3'), findsOneWidget);
expect(find.text('Index 4'), findsOneWidget);

// Material 3 changes the expected layout positioning.
final bool usesMaterial3 = (app.theme ?? ThemeData.light()).useMaterial3;
final Offset offset0 =
usesMaterial3 ? const Offset(0.0, 91.0) : const Offset(0.0, 103.0);
final Offset offset1 =
usesMaterial3 ? const Offset(65.0, 121.0) : const Offset(66.0, 124.0);
final Offset offset3 =
usesMaterial3 ? const Offset(270.0, 171.0) : const Offset(271.0, 174.0);
final Offset offset4 =
usesMaterial3 ? const Offset(380.0, 221.0) : const Offset(381.0, 224.0);

// See if they are in expected position.
withinTolerance(tester.getTopLeft(find.text('Index 0')), offset0, 0.2);
withinTolerance(tester.getTopLeft(find.text('Index 1')), offset1, 0.2);
withinTolerance(tester.getTopLeft(find.text('Index 3')), offset3, 0.2);
withinTolerance(tester.getTopLeft(find.text('Index 4')), offset4, 0.2);
});
}