Skip to content

Commit

Permalink
test: golden tests (#5)
Browse files Browse the repository at this point in the history
* test: add goldens
* fix: add storageAdapter to fix golden tests

---------

Co-authored-by: Joshua Wellbrock <[email protected]>
  • Loading branch information
JoshuaWell and JoshuaWellbrock authored Nov 19, 2024
1 parent 219530d commit aa19b69
Show file tree
Hide file tree
Showing 45 changed files with 437 additions and 15 deletions.
11 changes: 0 additions & 11 deletions .flutter-plugins

This file was deleted.

2 changes: 0 additions & 2 deletions .flutter-plugins-dependencies

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ migrate_working_dir/
**/doc/api/
.dart_tool/
build/
.flutter-plugins
.flutter-plugins-dependencies
test/failures
6 changes: 6 additions & 0 deletions lib/src/ui/imp_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ class ImpWidget extends StatelessWidget {
required this.onIdentificationDone,
required this.onIdentificationFailed,
required this.chipIdFormat,
this.storageAdapter,
super.key,
});

final ChipIdFormat? chipIdFormat;

/// The StorageAdapter to use for persisting the last connected and paired
/// devices.
final StorageAdapter? storageAdapter;

/// The strategy to use for the connection.
final ConnectionStrategy connectionStrategy;

Expand All @@ -36,6 +41,7 @@ class ImpWidget extends StatelessWidget {
Widget build(BuildContext context) {
return DeviceConnector(
connectionStrategy: connectionStrategy,
storageAdapter: storageAdapter,
connectedBuilder: (BuildContext context) {
return LdSubmit<bool>(
config: LdSubmitConfig<bool>(
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
golden_toolkit: ^0.15.0
mockito: ^5.4.4
mtrust_urp_virtual_strategy: ^8.0.1
flutter:
uses-material-design: true
generate: true
assets:
- assets/
109 changes: 109 additions & 0 deletions test/golden_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart';
import 'package:liquid_flutter/liquid_flutter.dart';
import 'package:mtrust_imp_kit/mtrust_imp_kit.dart';

typedef WidgetBuilder = Future<Future<void> Function()?> Function(
WidgetTester tester,
Future<void> Function(Widget widget) placeWidget,
);
Widget liquidFrame({
required Key key,
required Widget child,
required bool isDark,
required LdThemeSize size,
}) {
final theme = LdTheme()..setThemeSize(size);
return Localizations(
delegates: const [
GlobalWidgetsLocalizations.delegate,
LiquidLocalizations.delegate,
UrpUiLocalizations.delegate,
ImpLocalizations.delegate,
],
locale: const Locale('en'),
child: Directionality(
textDirection: TextDirection.ltr,
child: LdThemeProvider(
theme: theme,
autoSize: false,
brightnessMode:
isDark ? LdThemeBrightnessMode.dark : LdThemeBrightnessMode.light,
child: Builder(
builder: (context) {
return Container(
padding: const EdgeInsets.all(16),
margin: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: theme.background,
borderRadius: ldRadiusM,
),
child: Directionality(
textDirection: TextDirection.ltr,
child: Builder(
builder: (context) {
return SingleChildScrollView(
child: Center(key: key, child: child),
);
},
),
),
);
},
),
),
),
);
}

Future<void> multiGolden(
WidgetTester tester,
String name,
Map<String, WidgetBuilder> widgets, {
int width = 900,
}) async {
ldDisableAnimations = true;
urpUiDisableAnimations = true;
await loadAppFonts();
for (final entry in widgets.entries) {
for (final themeSize in LdThemeSize.values) {
for (final brightness in Brightness.values) {
final slug = '${entry.key}/'
"${themeSize.toString().split(".").last}"
"-${brightness.toString().split(".").last}";
await tester.binding.setSurfaceSize(
Size(width.toDouble(), 1000),
);
final cleanup = await entry.value(tester, (widget) async {
await tester.pumpWidget(
liquidFrame(
key: ValueKey(slug),
child: widget,
isDark: brightness == Brightness.dark,
size: themeSize,
),
);
});
final size =
find.byKey(ValueKey(slug)).evaluate().single.size ?? Size.zero;
await tester.binding.setSurfaceSize(
Size(width.toDouble(), size.height + 64),
);
await tester.pump();
await screenMatchesGolden(
tester,
'$name/$slug',
customPump: (tester) async {
await tester.pumpAndSettle();
},
);
if (cleanup != null) {
await cleanup();
}
await tester.pumpAndSettle();
}
}
}
}
Binary file added test/goldens/ImpWidget/Idle/l-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Idle/l-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Idle/m-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Idle/m-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Idle/s-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Idle/s-light.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.
Binary file added test/goldens/ImpWidget/Measure Fail/l-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measure Fail/l-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measure Fail/m-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measure Fail/m-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measure Fail/s-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measure Fail/s-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measuring/l-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measuring/l-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measuring/m-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measuring/m-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measuring/s-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Measuring/s-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Priming/l-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/ImpWidget/Priming/l-light.png
Binary file added test/goldens/ImpWidget/Priming/m-dark.png
Binary file added test/goldens/ImpWidget/Priming/m-light.png
Binary file added test/goldens/ImpWidget/Priming/s-dark.png
Binary file added test/goldens/ImpWidget/Priming/s-light.png
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:flutter_test/flutter_test.dart';

void main() {
group('pChipKit', () {
group('ImpKit', () {
test(' test suite example', () {
expect(1, 1);
});
});
}
}
210 changes: 210 additions & 0 deletions test/imp_widget_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart';
import 'package:mtrust_imp_kit/mtrust_imp_kit.dart';
import 'package:mtrust_imp_kit/src/format_utils.dart';

import 'golden_utils.dart';
import 'test_utils.dart';

void main() {
testGoldens('ImpWidget', (WidgetTester test) async {
urpUiDisableAnimations = true;

await multiGolden(
test,
'ImpWidget',
{
'Idle': (tester, place) async {
final strategy = CompleterStrategy(withReaders: true);

final storageAdapter = MockStorageAdapter();

await place(
AspectRatio(
aspectRatio: 1,
child: ImpWidget(
connectionStrategy: strategy.strategy,
storageAdapter: storageAdapter,
chipIdFormat: ChipIdFormat.hex,
onIdentificationDone: (_) async {},
onIdentificationFailed: () async {},
),
),
);

await tester.pumpAndSettle();
return null;
},
'Priming': (tester, place) async {
final strategy = CompleterStrategy(withReaders: true);

final storageAdapter = MockStorageAdapter();

await place(
AspectRatio(
aspectRatio: 1,
child: ImpWidget(
connectionStrategy: strategy.strategy,
storageAdapter: storageAdapter,
chipIdFormat: ChipIdFormat.hex,
onIdentificationDone: (_) async {},
onIdentificationFailed: () async {},
),
),
);

await tester.pumpAndSettle();

await tester.tap(find.byKey(const Key('connect_button')));

return () async {
strategy.primeCompleter.complete();
};
},
'Waiting for measurement': (tester, place) async {
final strategy = CompleterStrategy(withReaders: true);

final storageAdapter = MockStorageAdapter();

await place(
AspectRatio(
aspectRatio: 1,
child: ImpWidget(
connectionStrategy: strategy.strategy,
storageAdapter: storageAdapter,
chipIdFormat: ChipIdFormat.hex,
onIdentificationDone: (_) async {},
onIdentificationFailed: () async {},
),
),
);

await tester.pumpAndSettle();

await tester.tap(find.byKey(const Key('connect_button')));

await tester.pumpAndSettle();

strategy.primeCompleter.complete();

await tester.pumpAndSettle();

return () async {
strategy.startMeasurementCompleter.complete();
};
},
'Measuring': (tester, place) async {
final strategy = CompleterStrategy(withReaders: true);

final storageAdapter = MockStorageAdapter();

await place(
AspectRatio(
aspectRatio: 1,
child: ImpWidget(
connectionStrategy: strategy.strategy,
storageAdapter: storageAdapter,
chipIdFormat: ChipIdFormat.hex,
onIdentificationDone: (_) async {},
onIdentificationFailed: () async {},
),
),
);

await tester.pumpAndSettle();

await tester.tap(find.byKey(const Key('connect_button')));

await tester.pumpAndSettle();

strategy.primeCompleter.complete();

await tester.pumpAndSettle();

await tester.tap(find.text('Start reading'));

await tester.pumpAndSettle();

return () async {
strategy.startMeasurementCompleter.complete();
};
},
'Measure Fail': (tester, place) async {
final strategy = CompleterStrategy(withReaders: true);

final storageAdapter = MockStorageAdapter();

await place(
AspectRatio(
aspectRatio: 1,
child: ImpWidget(
connectionStrategy: strategy.strategy,
storageAdapter: storageAdapter,
chipIdFormat: ChipIdFormat.hex,
onIdentificationDone: (_) async {},
onIdentificationFailed: () async {},
),
),
);

await tester.pumpAndSettle();

await tester.tap(find.byKey(const Key('connect_button')));

await tester.pumpAndSettle();

strategy.primeCompleter.complete();

await tester.pumpAndSettle();

await tester.tap(find.text('Start reading'));

strategy.startMeasurementCompleter.completeError('');

await tester.pumpAndSettle();

return () async {};
},
'Measure Complete': (tester, place) async {
final strategy = CompleterStrategy(withReaders: true);

final storageAdapter = MockStorageAdapter();

await place(
AspectRatio(
aspectRatio: 1,
child: ImpWidget(
connectionStrategy: strategy.strategy,
storageAdapter: storageAdapter,
chipIdFormat: ChipIdFormat.hex,
onIdentificationDone: (_) async {},
onIdentificationFailed: () async {},
),
),
);

await tester.pumpAndSettle();

await tester.tap(find.byKey(const Key('connect_button')));

await tester.pumpAndSettle();

strategy.primeCompleter.complete();

await tester.pumpAndSettle();

await tester.tap(find.text('Start reading'));

strategy.startMeasurementCompleter.complete();

await tester.pumpAndSettle();

return () async {};
},
},
width: 500,
);
});
}
Loading

0 comments on commit aa19b69

Please sign in to comment.