|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:integration_test/integration_test.dart'; |
| 4 | +import 'package:yaru/yaru.dart'; |
| 5 | +import 'package:yaru_example/main.dart' as app; |
| 6 | +import 'package:yaru_example/main.dart'; |
| 7 | +import 'package:yaru_example/view/home_page.dart'; |
| 8 | + |
| 9 | +// NOTE: run `flutter test --update-goldens integration_test` to update screenshots |
| 10 | + |
| 11 | +Future<void> main() async { |
| 12 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 13 | + |
| 14 | + testWidgets('fonts', (tester) async { |
| 15 | + tester.runApp(); |
| 16 | + await tester.pumpAndSettle(); |
| 17 | + await tester.takeScreenshots('fonts'); |
| 18 | + }); |
| 19 | + |
| 20 | + testWidgets('buttons', (tester) async { |
| 21 | + tester.runApp(); |
| 22 | + await tester.pumpAndSettle(); |
| 23 | + await tester.tap(find.text('Controls')); |
| 24 | + await tester.pump(); |
| 25 | + await tester.takeScreenshots('buttons'); |
| 26 | + }); |
| 27 | + |
| 28 | + testWidgets('checks', (tester) async { |
| 29 | + tester.runApp(); |
| 30 | + await tester.pumpAndSettle(); |
| 31 | + await tester.tap(find.text('Controls')); |
| 32 | + await tester.pump(); |
| 33 | + await tester.tap(find.text('Checks')); |
| 34 | + await tester.pump(); |
| 35 | + await tester.takeScreenshots('checks'); |
| 36 | + }); |
| 37 | + |
| 38 | + testWidgets('switches', (tester) async { |
| 39 | + tester.runApp(); |
| 40 | + await tester.pumpAndSettle(); |
| 41 | + await tester.tap(find.text('Controls')); |
| 42 | + await tester.pump(); |
| 43 | + await tester.tap(find.text('Switches')); |
| 44 | + await tester.pump(); |
| 45 | + await tester.takeScreenshots('switches'); |
| 46 | + }); |
| 47 | + |
| 48 | + testWidgets('text fields', (tester) async { |
| 49 | + tester.runApp(); |
| 50 | + await tester.pumpAndSettle(); |
| 51 | + await tester.tap(find.text('Text Fields')); |
| 52 | + await tester.pump(); |
| 53 | + primaryFocus?.unfocus(); |
| 54 | + await tester.pump(); |
| 55 | + await tester.takeScreenshots('text-fields'); |
| 56 | + }); |
| 57 | + |
| 58 | + testWidgets('containers', (tester) async { |
| 59 | + tester.runApp(); |
| 60 | + await tester.pumpAndSettle(); |
| 61 | + await tester.tap(find.text('Containers')); |
| 62 | + await tester.pump(); |
| 63 | + await tester.takeScreenshots('containers'); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +extension on WidgetTester { |
| 68 | + void runApp() { |
| 69 | + view.devicePixelRatio = 1; |
| 70 | + view.physicalSize = const Size(600, 700); |
| 71 | + app.main(); |
| 72 | + } |
| 73 | + |
| 74 | + void selectTheme({ |
| 75 | + YaruVariant? variant, |
| 76 | + bool? highContrast, |
| 77 | + ThemeMode? themeMode, |
| 78 | + }) async { |
| 79 | + final context = element(find.byType(HomePage)); |
| 80 | + AppTheme.apply( |
| 81 | + context, |
| 82 | + variant: variant, |
| 83 | + highContrast: highContrast, |
| 84 | + themeMode: themeMode, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + Future<void> takeScreenshots(String screen) async { |
| 89 | + for (final themeMode in [ThemeMode.light, ThemeMode.dark]) { |
| 90 | + selectTheme( |
| 91 | + variant: YaruVariant.orange, |
| 92 | + themeMode: themeMode, |
| 93 | + highContrast: false, |
| 94 | + ); |
| 95 | + await pumpAndSettle(); |
| 96 | + await takeScreenshot(screen); |
| 97 | + |
| 98 | + selectTheme(themeMode: themeMode, highContrast: true); |
| 99 | + await pumpAndSettle(); |
| 100 | + await takeScreenshot(screen); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + Future<void> takeScreenshot(String screen) async { |
| 105 | + final context = element(find.byType(HomePage)); |
| 106 | + final theme = AppTheme.of(context); |
| 107 | + final suffix = [ |
| 108 | + if (theme.highContrast == true) 'high-contrast' else theme.variant?.name, |
| 109 | + theme.themeMode?.name, |
| 110 | + ].join('-'); |
| 111 | + |
| 112 | + await expectLater( |
| 113 | + find.byType(MaterialApp), |
| 114 | + matchesGoldenFile('goldens/$screen-$suffix.png'), |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments