From d26da843b39f8dab334ea454c100ffa545c36e84 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Wed, 1 Nov 2023 21:09:56 +0100 Subject: [PATCH] `EquipmentProfileListener` tests --- test/function_mock.dart | 7 + .../shared/dialog_picker_test.dart | 9 +- .../listsner_equipment_profiles_test.dart | 124 ++++++++++++++++++ .../utils/notifier_volume_keys_test.dart | 10 +- 4 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 test/function_mock.dart create mode 100644 test/screens/metering/utils/listsner_equipment_profiles_test.dart diff --git a/test/function_mock.dart b/test/function_mock.dart new file mode 100644 index 0000000..8a4abfe --- /dev/null +++ b/test/function_mock.dart @@ -0,0 +1,7 @@ +import 'package:mocktail/mocktail.dart'; + +class _ValueChanged { + void onChanged(T value) {} +} + +class MockValueChanged extends Mock implements _ValueChanged {} diff --git a/test/screens/metering/components/shared/readings_container/shared/dialog_picker_test.dart b/test/screens/metering/components/shared/readings_container/shared/dialog_picker_test.dart index 39c38ba..bd037af 100644 --- a/test/screens/metering/components/shared/readings_container/shared/dialog_picker_test.dart +++ b/test/screens/metering/components/shared/readings_container/shared/dialog_picker_test.dart @@ -7,16 +7,11 @@ import 'package:lightmeter/screens/metering/components/shared/readings_container import 'package:mocktail/mocktail.dart'; import '../../../../../../application_mock.dart'; +import '../../../../../../function_mock.dart'; import '../utils.dart'; -class _ValueChanged { - void onChanged(T value) {} -} - -class _MockValueChanged extends Mock implements _ValueChanged {} - void main() { - final functions = _MockValueChanged(); + final functions = MockValueChanged(); group( 'onChanged', diff --git a/test/screens/metering/utils/listsner_equipment_profiles_test.dart b/test/screens/metering/utils/listsner_equipment_profiles_test.dart new file mode 100644 index 0000000..f1c11aa --- /dev/null +++ b/test/screens/metering/utils/listsner_equipment_profiles_test.dart @@ -0,0 +1,124 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:lightmeter/providers/equipment_profile_provider.dart'; +import 'package:lightmeter/screens/metering/utils/listsner_equipment_profiles.dart'; +import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; +import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; +import 'package:mocktail/mocktail.dart'; + +import '../../../function_mock.dart'; + +class _MockIAPStorageService extends Mock implements IAPStorageService {} + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + late _MockIAPStorageService storageService; + final equipmentProfileProviderKey = GlobalKey(); + + setUpAll(() { + storageService = _MockIAPStorageService(); + }); + + tearDown(() { + reset(storageService); + }); + + Future pumpTestWidget( + WidgetTester tester, + ValueChanged onDidChangeDependencies, + ) async { + await tester.pumpWidget( + IAPProducts( + products: [ + IAPProduct( + storeId: IAPProductType.paidFeatures.storeId, + status: IAPProductStatus.purchased, + ), + ], + child: EquipmentProfileProvider( + key: equipmentProfileProviderKey, + storageService: storageService, + child: MaterialApp( + home: EquipmentProfileListener( + onDidChangeDependencies: onDidChangeDependencies, + child: Builder(builder: (context) => Text(EquipmentProfiles.selectedOf(context).name)), + ), + ), + ), + ), + ); + } + + testWidgets( + 'Listen to equipment profile selection and edit', + (tester) async { + when(() => storageService.equipmentProfiles).thenReturn(List.from(_customProfiles)); + when(() => storageService.selectedEquipmentProfileId).thenReturn(''); + final onDidChangeDependencies = MockValueChanged(); + + await pumpTestWidget(tester, onDidChangeDependencies.onChanged); + + /// Verify that selecting a new profile triggers the callback + equipmentProfileProviderKey.currentState!.setProfile(_customProfiles[0]); + await tester.pump(); + verify(() => onDidChangeDependencies.onChanged(_customProfiles[0])).called(1); + + /// Verify that updating the current profile triggers the callback + final updatedProfile1 = _customProfiles[0].copyWith(name: 'Test 1 updated'); + equipmentProfileProviderKey.currentState!.updateProdile(updatedProfile1); + await tester.pump(); + verify(() => onDidChangeDependencies.onChanged(updatedProfile1)).called(1); + + /// Verify that updating the not selected profile doesn't trigger the callback + final updatedProfile2 = _customProfiles[1].copyWith(name: 'Test 2 updated'); + equipmentProfileProviderKey.currentState!.updateProdile(updatedProfile2); + await tester.pump(); + verifyNever(() => onDidChangeDependencies.onChanged(updatedProfile2)); + }, + ); +} + +final List _customProfiles = [ + const EquipmentProfile( + id: '1', + name: 'Test 1', + apertureValues: [ + ApertureValue(4.0, StopType.full), + ApertureValue(4.5, StopType.third), + ApertureValue(4.8, StopType.half), + ApertureValue(5.0, StopType.third), + ApertureValue(5.6, StopType.full), + ApertureValue(6.3, StopType.third), + ApertureValue(6.7, StopType.half), + ApertureValue(7.1, StopType.third), + ApertureValue(8, StopType.full), + ], + ndValues: [ + NdValue(0), + NdValue(2), + NdValue(4), + NdValue(8), + NdValue(16), + NdValue(32), + NdValue(64), + ], + shutterSpeedValues: ShutterSpeedValue.values, + isoValues: [ + IsoValue(100, StopType.full), + IsoValue(125, StopType.third), + IsoValue(160, StopType.third), + IsoValue(200, StopType.full), + IsoValue(250, StopType.third), + IsoValue(320, StopType.third), + IsoValue(400, StopType.full), + ], + ), + const EquipmentProfile( + id: '2', + name: 'Test 2', + apertureValues: ApertureValue.values, + ndValues: NdValue.values, + shutterSpeedValues: ShutterSpeedValue.values, + isoValues: IsoValue.values, + ), +]; diff --git a/test/screens/metering/utils/notifier_volume_keys_test.dart b/test/screens/metering/utils/notifier_volume_keys_test.dart index 47332c8..7bb6458 100644 --- a/test/screens/metering/utils/notifier_volume_keys_test.dart +++ b/test/screens/metering/utils/notifier_volume_keys_test.dart @@ -6,14 +6,10 @@ import 'package:lightmeter/screens/metering/utils/notifier_volume_keys.dart'; import 'package:mocktail/mocktail.dart'; import 'package:test/test.dart'; +import '../../../function_mock.dart'; + class _MockVolumeEventsService extends Mock implements VolumeEventsService {} -class _ValueChanged { - void onChanged(VolumeKey value) {} -} - -class _MockValueChanged extends Mock implements _ValueChanged {} - void main() { late _MockVolumeEventsService mockVolumeEventsService; @@ -28,7 +24,7 @@ void main() { when(() => mockVolumeEventsService.volumeButtonsEventStream()).thenAnswer((_) => volumeButtonsEvents.stream); final volumeKeysNotifier = VolumeKeysNotifier(mockVolumeEventsService); - final functions = _MockValueChanged(); + final functions = MockValueChanged(); volumeKeysNotifier.addListener(() => functions.onChanged(volumeKeysNotifier.value)); expect(volumeKeysNotifier.value, VolumeKey.up);