Skip to content

Commit

Permalink
Add integration test for taking screenshots of the example
Browse files Browse the repository at this point in the history
Ref: #315
  • Loading branch information
jpnurmi committed Mar 14, 2023
1 parent e928538 commit a873f40
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 0 deletions.
133 changes: 133 additions & 0 deletions example/integration_test/screenshot_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:yaru/yaru.dart';
import 'package:yaru_example/main.dart' as app;
import 'package:yaru_example/view/home_page.dart';

// NOTE: run `flutter test --update-goldens integration_test` to update screenshots

Future<void> main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

testWidgets('fonts', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.takeScreenshots('fonts');
});

testWidgets('buttons', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(find.text('Controls'));
await tester.pump();
await tester.takeScreenshots('buttons');
});

testWidgets('checks', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(find.text('Controls'));
await tester.pump();
await tester.tap(find.text('Checks'));
await tester.pump();
await tester.takeScreenshots('checks');
});

testWidgets('switches', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(find.text('Controls'));
await tester.pump();
await tester.tap(find.text('Switches'));
await tester.pump();
await tester.takeScreenshots('switches');
});

testWidgets('text fields', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(find.text('Text Fields'));
await tester.pump();
await tester.takeScreenshots('switches');
});

testWidgets('containers', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(find.text('Containers'));
await tester.pump();
await tester.takeScreenshots('containers');
});
}

extension on WidgetTester {
Brightness get brightness {
final context = element(find.byType(HomePage));
return Theme.of(context).brightness;
}

YaruVariant get variant {
final context = element(find.byType(HomePage));
return YaruTheme.of(context).variant!;
}

bool get highContrast {
final context = element(find.byType(HomePage));
return YaruTheme.of(context).highContrast!;
}

Future<void> toggleBrightness() async {
for (final icon in [Icons.light_mode, Icons.dark_mode]) {
final button = find.widgetWithIcon(TextButton, icon);
if (button.evaluate().isNotEmpty) {
await tap(button);
await pumpAndSettle();
break;
}
}
}

Future<void> selectTheme(Color color) async {
await tap(find.byType(PopupMenuButton<Color>));
await pumpAndSettle();

final menuItem = find.byWidgetPredicate((widget) {
return widget is PopupMenuItem<Color> && widget.value == color;
});
await ensureVisible(menuItem);
await tap(menuItem);
await pumpAndSettle();
}

Future<void> takeScreenshots(String screen) async {
for (var i = 0; i < 2; ++i) {
await toggleBrightness();

await selectTheme(YaruVariant.orange.color);
await takeScreenshot(screen);

switch (brightness) {
case Brightness.light:
await selectTheme(Colors.black);
break;
case Brightness.dark:
await selectTheme(Colors.white);
break;
}
await takeScreenshot(screen);
}
}

Future<void> takeScreenshot(String screen) async {
final suffix = [
if (highContrast) 'high-contrast' else variant.name,
brightness.name,
].join('-');

await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('screenshots/$screen-$suffix.png'),
);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions example/lib/view/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class HomePageState extends State<HomePage> {
itemBuilder: (context) {
return [
PopupMenuItem(
value: theme.themeMode == ThemeMode.light
? Colors.black
: Colors.white,
onTap: () => AppTheme.apply(context, highContrast: true),
child: Row(
children: [
Expand All @@ -84,6 +87,7 @@ class HomePageState extends State<HomePage> {
),
for (final variant in YaruVariant.values) // skip flavors
PopupMenuItem(
value: variant.color,
onTap: () => AppTheme.apply(
context,
variant: variant,
Expand Down
2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dev_dependencies:
flutter_lints: ^2.0.1
flutter_test:
sdk: flutter
integration_test:
sdk: flutter

flutter:
uses-material-design: true

0 comments on commit a873f40

Please sign in to comment.