Challenge - 5 Problems
Golden UI Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What does this golden test verify?
Consider this Flutter golden test code snippet:
What is this test checking?
testWidgets('Button golden test', (tester) async {
await tester.pumpWidget(MaterialApp(home: Scaffold(body: ElevatedButton(onPressed: () {}, child: Text('Tap me')))));
await expectLater(find.byType(ElevatedButton), matchesGoldenFile('button_golden.png'));
});What is this test checking?
Flutter
testWidgets('Button golden test', (tester) async { await tester.pumpWidget(MaterialApp(home: Scaffold(body: ElevatedButton(onPressed: () {}, child: Text('Tap me'))))); await expectLater(find.byType(ElevatedButton), matchesGoldenFile('button_golden.png')); });
Attempts:
2 left
💡 Hint
Golden tests compare the widget's visual output to a saved image.
✗ Incorrect
Golden tests capture the widget's rendered appearance and compare it to a reference image to detect unintended UI changes.
❓ lifecycle
intermediate1:30remaining
When should you update golden files?
You run a golden test and it fails because the UI changed intentionally. What is the correct next step?
Attempts:
2 left
💡 Hint
Golden files represent the expected UI snapshot.
✗ Incorrect
When UI changes are intentional, update the golden files to reflect the new correct appearance to keep tests valid.
🔧 Debug
advanced2:30remaining
Why does this golden test fail with a pixel mismatch?
This golden test fails with a pixel mismatch error:
Possible cause?
testWidgets('Text widget golden test', (tester) async {
await tester.pumpWidget(MaterialApp(home: Scaffold(body: Text('Hello'))));
await expectLater(find.byType(Text), matchesGoldenFile('text_golden.png'));
});Possible cause?
Attempts:
2 left
💡 Hint
Golden tests are sensitive to device pixel ratio and screen size.
✗ Incorrect
Golden tests can fail if run on different devices or environments with varying pixel densities or screen sizes, causing slight visual differences.
🧠 Conceptual
advanced1:30remaining
What is a key benefit of golden tests in UI development?
Why do developers use golden tests for UI components?
Attempts:
2 left
💡 Hint
Golden tests focus on visual correctness.
✗ Incorrect
Golden tests help catch visual regressions by comparing the current UI rendering to a saved image, alerting developers to unexpected changes.
📝 Syntax
expert3:00remaining
Which option correctly sets up a golden test with a custom device pixel ratio?
You want to run a golden test with device pixel ratio 2.0 to avoid pixel mismatch errors. Which code snippet is correct?
Flutter
testWidgets('Custom DPR golden test', (tester) async { await tester.binding.setSurfaceSize(Size(200, 200)); tester.binding.window.devicePixelRatioTestValue = 2.0; await tester.pumpWidget(MaterialApp(home: Scaffold(body: Container(color: Colors.red)))); await expectLater(find.byType(Container), matchesGoldenFile('red_container.png')); tester.binding.window.clearDevicePixelRatioTestValue(); await tester.binding.setSurfaceSize(null); });
Attempts:
2 left
💡 Hint
Flutter test binding allows overriding device pixel ratio for tests.
✗ Incorrect
The code uses the correct Flutter test APIs to set device pixel ratio and surface size to control rendering for golden tests.