diff --git a/.flutter-plugins b/.flutter-plugins deleted file mode 100644 index ba80525..0000000 --- a/.flutter-plugins +++ /dev/null @@ -1,11 +0,0 @@ -# This is a generated file; do not edit or check into version control. -haptic_feedback=/Users/runner/.pub-cache/hosted/pub.dev/haptic_feedback-0.4.2/ -path_provider_linux=/Users/runner/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/ -path_provider_windows=/Users/runner/.pub-cache/hosted/pub.dev/path_provider_windows-2.3.0/ -sensors_plus=/Users/runner/.pub-cache/hosted/pub.dev/sensors_plus-5.0.1/ -shared_preferences=/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences-2.3.3/ -shared_preferences_android=/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_android-2.3.3/ -shared_preferences_foundation=/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_foundation-2.5.3/ -shared_preferences_linux=/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_linux-2.4.1/ -shared_preferences_web=/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_web-2.4.2/ -shared_preferences_windows=/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_windows-2.4.1/ diff --git a/.flutter-plugins-dependencies b/.flutter-plugins-dependencies deleted file mode 100644 index 22101f6..0000000 --- a/.flutter-plugins-dependencies +++ /dev/null @@ -1,2 +0,0 @@ - -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"haptic_feedback","path":"/Users/runner/.pub-cache/hosted/pub.dev/haptic_feedback-0.4.2/","native_build":true,"dependencies":[]},{"name":"sensors_plus","path":"/Users/runner/.pub-cache/hosted/pub.dev/sensors_plus-5.0.1/","native_build":true,"dependencies":[]},{"name":"shared_preferences_foundation","path":"/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_foundation-2.5.3/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"haptic_feedback","path":"/Users/runner/.pub-cache/hosted/pub.dev/haptic_feedback-0.4.2/","native_build":true,"dependencies":[]},{"name":"sensors_plus","path":"/Users/runner/.pub-cache/hosted/pub.dev/sensors_plus-5.0.1/","native_build":true,"dependencies":[]},{"name":"shared_preferences_android","path":"/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_android-2.3.3/","native_build":true,"dependencies":[]}],"macos":[{"name":"shared_preferences_foundation","path":"/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_foundation-2.5.3/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/runner/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/","native_build":false,"dependencies":[]},{"name":"shared_preferences_linux","path":"/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_linux-2.4.1/","native_build":false,"dependencies":["path_provider_linux"]}],"windows":[{"name":"path_provider_windows","path":"/Users/runner/.pub-cache/hosted/pub.dev/path_provider_windows-2.3.0/","native_build":false,"dependencies":[]},{"name":"shared_preferences_windows","path":"/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_windows-2.4.1/","native_build":false,"dependencies":["path_provider_windows"]}],"web":[{"name":"sensors_plus","path":"/Users/runner/.pub-cache/hosted/pub.dev/sensors_plus-5.0.1/","dependencies":[]},{"name":"shared_preferences_web","path":"/Users/runner/.pub-cache/hosted/pub.dev/shared_preferences_web-2.4.2/","dependencies":[]}]},"dependencyGraph":[{"name":"haptic_feedback","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"sensors_plus","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_android","shared_preferences_foundation","shared_preferences_linux","shared_preferences_web","shared_preferences_windows"]},{"name":"shared_preferences_android","dependencies":[]},{"name":"shared_preferences_foundation","dependencies":[]},{"name":"shared_preferences_linux","dependencies":["path_provider_linux"]},{"name":"shared_preferences_web","dependencies":[]},{"name":"shared_preferences_windows","dependencies":["path_provider_windows"]}],"date_created":"2024-11-18 13:47:56.096363","version":"3.24.3","swift_package_manager_enabled":false} diff --git a/.gitignore b/.gitignore index ac5aa98..8bcfb46 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,6 @@ migrate_working_dir/ **/doc/api/ .dart_tool/ build/ +.flutter-plugins +.flutter-plugins-dependencies +test/failures \ No newline at end of file diff --git a/lib/src/ui/imp_widget.dart b/lib/src/ui/imp_widget.dart index 1f443fa..2db7a5f 100644 --- a/lib/src/ui/imp_widget.dart +++ b/lib/src/ui/imp_widget.dart @@ -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; @@ -36,6 +41,7 @@ class ImpWidget extends StatelessWidget { Widget build(BuildContext context) { return DeviceConnector( connectionStrategy: connectionStrategy, + storageAdapter: storageAdapter, connectedBuilder: (BuildContext context) { return LdSubmit( config: LdSubmitConfig( diff --git a/pubspec.yaml b/pubspec.yaml index 2b63eaa..49aa501 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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/ diff --git a/test/golden_utils.dart b/test/golden_utils.dart new file mode 100644 index 0000000..2a91e25 --- /dev/null +++ b/test/golden_utils.dart @@ -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 Function()?> Function( + WidgetTester tester, + Future 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 multiGolden( + WidgetTester tester, + String name, + Map 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(); + } + } + } +} \ No newline at end of file diff --git a/test/goldens/ImpWidget/Idle/l-dark.png b/test/goldens/ImpWidget/Idle/l-dark.png new file mode 100644 index 0000000..b7d46e8 Binary files /dev/null and b/test/goldens/ImpWidget/Idle/l-dark.png differ diff --git a/test/goldens/ImpWidget/Idle/l-light.png b/test/goldens/ImpWidget/Idle/l-light.png new file mode 100644 index 0000000..46a9bca Binary files /dev/null and b/test/goldens/ImpWidget/Idle/l-light.png differ diff --git a/test/goldens/ImpWidget/Idle/m-dark.png b/test/goldens/ImpWidget/Idle/m-dark.png new file mode 100644 index 0000000..69eb18f Binary files /dev/null and b/test/goldens/ImpWidget/Idle/m-dark.png differ diff --git a/test/goldens/ImpWidget/Idle/m-light.png b/test/goldens/ImpWidget/Idle/m-light.png new file mode 100644 index 0000000..0c75054 Binary files /dev/null and b/test/goldens/ImpWidget/Idle/m-light.png differ diff --git a/test/goldens/ImpWidget/Idle/s-dark.png b/test/goldens/ImpWidget/Idle/s-dark.png new file mode 100644 index 0000000..5c441eb Binary files /dev/null and b/test/goldens/ImpWidget/Idle/s-dark.png differ diff --git a/test/goldens/ImpWidget/Idle/s-light.png b/test/goldens/ImpWidget/Idle/s-light.png new file mode 100644 index 0000000..aa916c9 Binary files /dev/null and b/test/goldens/ImpWidget/Idle/s-light.png differ diff --git a/test/goldens/ImpWidget/Measure Complete/l-dark.png b/test/goldens/ImpWidget/Measure Complete/l-dark.png new file mode 100644 index 0000000..ef72f7c Binary files /dev/null and b/test/goldens/ImpWidget/Measure Complete/l-dark.png differ diff --git a/test/goldens/ImpWidget/Measure Complete/l-light.png b/test/goldens/ImpWidget/Measure Complete/l-light.png new file mode 100644 index 0000000..6570deb Binary files /dev/null and b/test/goldens/ImpWidget/Measure Complete/l-light.png differ diff --git a/test/goldens/ImpWidget/Measure Complete/m-dark.png b/test/goldens/ImpWidget/Measure Complete/m-dark.png new file mode 100644 index 0000000..36506be Binary files /dev/null and b/test/goldens/ImpWidget/Measure Complete/m-dark.png differ diff --git a/test/goldens/ImpWidget/Measure Complete/m-light.png b/test/goldens/ImpWidget/Measure Complete/m-light.png new file mode 100644 index 0000000..105aa5a Binary files /dev/null and b/test/goldens/ImpWidget/Measure Complete/m-light.png differ diff --git a/test/goldens/ImpWidget/Measure Complete/s-dark.png b/test/goldens/ImpWidget/Measure Complete/s-dark.png new file mode 100644 index 0000000..8f6ea39 Binary files /dev/null and b/test/goldens/ImpWidget/Measure Complete/s-dark.png differ diff --git a/test/goldens/ImpWidget/Measure Complete/s-light.png b/test/goldens/ImpWidget/Measure Complete/s-light.png new file mode 100644 index 0000000..17ca46b Binary files /dev/null and b/test/goldens/ImpWidget/Measure Complete/s-light.png differ diff --git a/test/goldens/ImpWidget/Measure Fail/l-dark.png b/test/goldens/ImpWidget/Measure Fail/l-dark.png new file mode 100644 index 0000000..4956591 Binary files /dev/null and b/test/goldens/ImpWidget/Measure Fail/l-dark.png differ diff --git a/test/goldens/ImpWidget/Measure Fail/l-light.png b/test/goldens/ImpWidget/Measure Fail/l-light.png new file mode 100644 index 0000000..f146e35 Binary files /dev/null and b/test/goldens/ImpWidget/Measure Fail/l-light.png differ diff --git a/test/goldens/ImpWidget/Measure Fail/m-dark.png b/test/goldens/ImpWidget/Measure Fail/m-dark.png new file mode 100644 index 0000000..39bfcdd Binary files /dev/null and b/test/goldens/ImpWidget/Measure Fail/m-dark.png differ diff --git a/test/goldens/ImpWidget/Measure Fail/m-light.png b/test/goldens/ImpWidget/Measure Fail/m-light.png new file mode 100644 index 0000000..82cad5d Binary files /dev/null and b/test/goldens/ImpWidget/Measure Fail/m-light.png differ diff --git a/test/goldens/ImpWidget/Measure Fail/s-dark.png b/test/goldens/ImpWidget/Measure Fail/s-dark.png new file mode 100644 index 0000000..9c0fe6d Binary files /dev/null and b/test/goldens/ImpWidget/Measure Fail/s-dark.png differ diff --git a/test/goldens/ImpWidget/Measure Fail/s-light.png b/test/goldens/ImpWidget/Measure Fail/s-light.png new file mode 100644 index 0000000..d9c23af Binary files /dev/null and b/test/goldens/ImpWidget/Measure Fail/s-light.png differ diff --git a/test/goldens/ImpWidget/Measuring/l-dark.png b/test/goldens/ImpWidget/Measuring/l-dark.png new file mode 100644 index 0000000..4956591 Binary files /dev/null and b/test/goldens/ImpWidget/Measuring/l-dark.png differ diff --git a/test/goldens/ImpWidget/Measuring/l-light.png b/test/goldens/ImpWidget/Measuring/l-light.png new file mode 100644 index 0000000..f146e35 Binary files /dev/null and b/test/goldens/ImpWidget/Measuring/l-light.png differ diff --git a/test/goldens/ImpWidget/Measuring/m-dark.png b/test/goldens/ImpWidget/Measuring/m-dark.png new file mode 100644 index 0000000..39bfcdd Binary files /dev/null and b/test/goldens/ImpWidget/Measuring/m-dark.png differ diff --git a/test/goldens/ImpWidget/Measuring/m-light.png b/test/goldens/ImpWidget/Measuring/m-light.png new file mode 100644 index 0000000..82cad5d Binary files /dev/null and b/test/goldens/ImpWidget/Measuring/m-light.png differ diff --git a/test/goldens/ImpWidget/Measuring/s-dark.png b/test/goldens/ImpWidget/Measuring/s-dark.png new file mode 100644 index 0000000..9c0fe6d Binary files /dev/null and b/test/goldens/ImpWidget/Measuring/s-dark.png differ diff --git a/test/goldens/ImpWidget/Measuring/s-light.png b/test/goldens/ImpWidget/Measuring/s-light.png new file mode 100644 index 0000000..d9c23af Binary files /dev/null and b/test/goldens/ImpWidget/Measuring/s-light.png differ diff --git a/test/goldens/ImpWidget/Priming/l-dark.png b/test/goldens/ImpWidget/Priming/l-dark.png new file mode 100644 index 0000000..f6f598f Binary files /dev/null and b/test/goldens/ImpWidget/Priming/l-dark.png differ diff --git a/test/goldens/ImpWidget/Priming/l-light.png b/test/goldens/ImpWidget/Priming/l-light.png new file mode 100644 index 0000000..3065c46 Binary files /dev/null and b/test/goldens/ImpWidget/Priming/l-light.png differ diff --git a/test/goldens/ImpWidget/Priming/m-dark.png b/test/goldens/ImpWidget/Priming/m-dark.png new file mode 100644 index 0000000..4df7373 Binary files /dev/null and b/test/goldens/ImpWidget/Priming/m-dark.png differ diff --git a/test/goldens/ImpWidget/Priming/m-light.png b/test/goldens/ImpWidget/Priming/m-light.png new file mode 100644 index 0000000..6831fef Binary files /dev/null and b/test/goldens/ImpWidget/Priming/m-light.png differ diff --git a/test/goldens/ImpWidget/Priming/s-dark.png b/test/goldens/ImpWidget/Priming/s-dark.png new file mode 100644 index 0000000..5660582 Binary files /dev/null and b/test/goldens/ImpWidget/Priming/s-dark.png differ diff --git a/test/goldens/ImpWidget/Priming/s-light.png b/test/goldens/ImpWidget/Priming/s-light.png new file mode 100644 index 0000000..42df2d3 Binary files /dev/null and b/test/goldens/ImpWidget/Priming/s-light.png differ diff --git a/test/goldens/ImpWidget/Waiting for measurement/l-dark.png b/test/goldens/ImpWidget/Waiting for measurement/l-dark.png new file mode 100644 index 0000000..5cbd26d Binary files /dev/null and b/test/goldens/ImpWidget/Waiting for measurement/l-dark.png differ diff --git a/test/goldens/ImpWidget/Waiting for measurement/l-light.png b/test/goldens/ImpWidget/Waiting for measurement/l-light.png new file mode 100644 index 0000000..ff6a18f Binary files /dev/null and b/test/goldens/ImpWidget/Waiting for measurement/l-light.png differ diff --git a/test/goldens/ImpWidget/Waiting for measurement/m-dark.png b/test/goldens/ImpWidget/Waiting for measurement/m-dark.png new file mode 100644 index 0000000..55d1be6 Binary files /dev/null and b/test/goldens/ImpWidget/Waiting for measurement/m-dark.png differ diff --git a/test/goldens/ImpWidget/Waiting for measurement/m-light.png b/test/goldens/ImpWidget/Waiting for measurement/m-light.png new file mode 100644 index 0000000..0074aa7 Binary files /dev/null and b/test/goldens/ImpWidget/Waiting for measurement/m-light.png differ diff --git a/test/goldens/ImpWidget/Waiting for measurement/s-dark.png b/test/goldens/ImpWidget/Waiting for measurement/s-dark.png new file mode 100644 index 0000000..c691e64 Binary files /dev/null and b/test/goldens/ImpWidget/Waiting for measurement/s-dark.png differ diff --git a/test/goldens/ImpWidget/Waiting for measurement/s-light.png b/test/goldens/ImpWidget/Waiting for measurement/s-light.png new file mode 100644 index 0000000..7e9b3c3 Binary files /dev/null and b/test/goldens/ImpWidget/Waiting for measurement/s-light.png differ diff --git a/test/unit_tests/default_identification_ui_builder_test.dart b/test/imp_kit_test.dart similarity index 83% rename from test/unit_tests/default_identification_ui_builder_test.dart rename to test/imp_kit_test.dart index bd4146c..cad0048 100644 --- a/test/unit_tests/default_identification_ui_builder_test.dart +++ b/test/imp_kit_test.dart @@ -1,9 +1,9 @@ import 'package:flutter_test/flutter_test.dart'; void main() { - group('pChipKit', () { + group('ImpKit', () { test(' test suite example', () { expect(1, 1); }); }); -} +} \ No newline at end of file diff --git a/test/imp_widget_test.dart b/test/imp_widget_test.dart new file mode 100644 index 0000000..05a11e0 --- /dev/null +++ b/test/imp_widget_test.dart @@ -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, + ); + }); +} diff --git a/test/test_utils.dart b/test/test_utils.dart new file mode 100644 index 0000000..c2f5311 --- /dev/null +++ b/test/test_utils.dart @@ -0,0 +1,104 @@ + +import 'dart:async'; + +import 'package:mtrust_imp_kit/mtrust_imp_kit.dart'; +import 'package:mtrust_urp_virtual_strategy/mtrust_urp_virtual_strategy.dart'; + +final reader1 = FoundDevice( + name: 'IMP-000123', + type: UrpDeviceType.urpImp, + address: '00:00:00:00:00:00', +); + +final reader2 = FoundDevice( + name: 'IMP-000124', + type: UrpDeviceType.urpImp, + address: '00:00:00:00:00:01', +); + +final reader3 = FoundDevice( + name: 'IMP-000125', + type: UrpDeviceType.urpImp, + address: '00:00:00:00:00:02', +); + +class CompleterStrategy { + CompleterStrategy({bool withReaders = false}) { + strategy = UrpVirtualStrategy((UrpRequest request) async { + final payload = UrpImpCommandWrapper.fromBuffer(request.payload); + switch (payload.deviceCommand.command) { + case (UrpImpCommand.urpImpPrime): + primeCompleter = Completer(); + await primeCompleter.future; + + return UrpResponse(); + + case UrpImpCommand.urpImpStartMeasurement: + startMeasurementCompleter = Completer(); + try { + await startMeasurementCompleter.future; + } catch (e) { + return null; + } + + return UrpResponse( + payload: UrpImpMeasurementResult( + id: 123, + reads: 1, + ).writeToBuffer(), + ); + // ignore: no_default_cases + default: + return UrpResponse(); + } + }) + ..simulateDelays = false; + + if (withReaders) { + strategy + ..createVirtualReader(reader1) + ..createVirtualReader(reader2) + ..createVirtualReader(reader3) + ..createVirtualReader( + FoundDevice( + name: 'SEC-000123', + type: UrpDeviceType.urpSec, + address: '00:00:00:00:00:01', + ), + ); + } + } + Completer primeCompleter = Completer(); + Completer startMeasurementCompleter = Completer(); + late UrpVirtualStrategy strategy; +} + +class MockStorageAdapter extends StorageAdapter { + MockStorageAdapter() : super('mock_storage_adapter'); + + @override + Future clearPairedReader() { + return Future.value(); + } + + @override + Future clearPersistedReader() async {} + + @override + Future getLastConnectedReader() async { + return reader2; + } + + @override + Future getPairedReader() { + return Future.value(reader2); + } + + @override + Future persistLastConnectedReader(FoundDevice reader) async {} + + @override + Future persistPairedReader(FoundDevice reader) async { + return Future.value(); + } +}