From 7b81c99a4d4b066261964434b95b783ea797db96 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Fri, 5 Sep 2025 22:18:15 +0200 Subject: [PATCH] fixed unit tests --- .../mocks/paid_features_mock.dart | 42 ++++++- .../equipment_profile_provider_test.dart | 106 +++++++++++++----- .../equipment_profile_picker_test.dart | 68 ++++++++++- .../listener_equipment_profiles_test.dart | 5 +- 4 files changed, 181 insertions(+), 40 deletions(-) diff --git a/integration_test/mocks/paid_features_mock.dart b/integration_test/mocks/paid_features_mock.dart index f7a4bdb..5a33209 100644 --- a/integration_test/mocks/paid_features_mock.dart +++ b/integration_test/mocks/paid_features_mock.dart @@ -15,6 +15,7 @@ class _MockGeolocationService extends Mock implements GeolocationService {} class MockIAPProviders extends StatefulWidget { final TogglableMap equipmentProfiles; + final TogglableMap pinholeEquipmentProfiles; final String selectedEquipmentProfileId; final TogglableMap predefinedFilms; final TogglableMap customFilms; @@ -23,6 +24,7 @@ class MockIAPProviders extends StatefulWidget { MockIAPProviders({ TogglableMap? equipmentProfiles, + TogglableMap? pinholeEquipmentProfiles, this.selectedEquipmentProfileId = '', TogglableMap? predefinedFilms, TogglableMap? customFilms, @@ -30,6 +32,7 @@ class MockIAPProviders extends StatefulWidget { required this.child, super.key, }) : equipmentProfiles = equipmentProfiles ?? mockEquipmentProfiles.toTogglableMap(), + pinholeEquipmentProfiles = pinholeEquipmentProfiles ?? mockPinholeEquipmentProfiles.toTogglableMap(), predefinedFilms = predefinedFilms ?? mockFilms.toTogglableMap(), customFilms = customFilms ?? mockFilms.toTogglableMap(), selectedFilmId = selectedFilmId ?? const FilmStub().id; @@ -46,15 +49,20 @@ class _MockIAPProvidersState extends State { void initState() { super.initState(); registerFallbackValue(defaultEquipmentProfile); + registerFallbackValue(mockPinholeEquipmentProfiles.first); registerFallbackValue(defaultCustomPhotos.first); registerFallbackValue(ApertureValue.values.first); registerFallbackValue(ShutterSpeedValue.values.first); mockIapStorageService = _MockIapStorageService(); when(() => mockIapStorageService.init()).thenAnswer((_) async {}); - when(() => mockIapStorageService.getEquipmentProfiles()).thenAnswer((_) => Future.value(widget.equipmentProfiles)); when(() => mockIapStorageService.selectedEquipmentProfileId).thenReturn(widget.selectedEquipmentProfileId); + when(() => mockIapStorageService.getEquipmentProfiles()).thenAnswer((_) => Future.value(widget.equipmentProfiles)); + when(() => mockIapStorageService.getPinholeEquipmentProfiles()) + .thenAnswer((_) => Future.value(widget.pinholeEquipmentProfiles)); when(() => mockIapStorageService.addEquipmentProfile(any())).thenAnswer((_) async {}); + when(() => mockIapStorageService.addPinholeEquipmentProfile(any())) + .thenAnswer((_) async {}); when( () => mockIapStorageService.updateEquipmentProfile( id: any(named: 'id'), @@ -62,7 +70,14 @@ class _MockIAPProvidersState extends State { isUsed: any(named: 'isUsed'), ), ).thenAnswer((_) async {}); + when( + () => mockIapStorageService.updatePinholeEquipmentProfile( + id: any(named: 'id'), + name: any(named: 'name'), + ), + ).thenAnswer((_) async {}); when(() => mockIapStorageService.deleteEquipmentProfile(any())).thenAnswer((_) async {}); + when(() => mockIapStorageService.deletePinholeEquipmentProfile(any())).thenAnswer((_) async {}); when(() => mockIapStorageService.getPredefinedFilms()).thenAnswer((_) => Future.value(widget.predefinedFilms)); when(() => mockIapStorageService.getCustomFilms()).thenAnswer((_) => Future.value(widget.customFilms)); @@ -175,6 +190,31 @@ final mockEquipmentProfiles = [ ), ]; +final mockPinholeEquipmentProfiles = [ + PinholeEquipmentProfile( + id: '3', + name: 'Pinhole Camera f/64', + aperture: 64.0, + isoValues: [ + IsoValue.values[1], + IsoValue.values[2], + IsoValue.values[3], + ], + ndValues: const [NdValue(0)], + ), + PinholeEquipmentProfile( + id: '4', + name: 'Pinhole Camera f/128', + aperture: 128.0, + isoValues: [ + IsoValue.values[1], + IsoValue.values[2], + IsoValue.values[3], + ], + ndValues: const [NdValue(0)], + ), +]; + const mockFilms = [ _FilmMultiplying(id: '1', name: 'Mock film 1', iso: 100, reciprocityMultiplier: 2), _FilmMultiplying(id: '2', name: 'Mock film 2', iso: 400, reciprocityMultiplier: 2), diff --git a/test/providers/equipment_profile_provider_test.dart b/test/providers/equipment_profile_provider_test.dart index 8aacb8b..b243bee 100644 --- a/test/providers/equipment_profile_provider_test.dart +++ b/test/providers/equipment_profile_provider_test.dart @@ -16,7 +16,7 @@ void main() { }); setUp(() { - registerFallbackValue(_customProfiles.first); + registerFallbackValue(_mockEquipmentProfiles.first); when(() => storageService.addEquipmentProfile(any())).thenAnswer((_) async {}); when( () => storageService.updateEquipmentProfile( @@ -26,7 +26,10 @@ void main() { ), ).thenAnswer((_) async {}); when(() => storageService.deleteEquipmentProfile(any())).thenAnswer((_) async {}); - when(() => storageService.getEquipmentProfiles()).thenAnswer((_) => Future.value(_customProfiles.toTogglableMap())); + when(() => storageService.getEquipmentProfiles()) + .thenAnswer((_) => Future.value(_mockEquipmentProfiles.toTogglableMap())); + when(() => storageService.getPinholeEquipmentProfiles()) + .thenAnswer((_) => Future.value(_mockPinholeEquipmentProfiles.toTogglableMap())); }); tearDown(() { @@ -62,17 +65,15 @@ void main() { 'EquipmentProfilesProvider dependency on IAPProductStatus', () { setUp(() { - when(() => storageService.selectedEquipmentProfileId).thenReturn(_customProfiles.first.id); - when(() => storageService.getEquipmentProfiles()) - .thenAnswer((_) => Future.value(_customProfiles.toTogglableMap())); + when(() => storageService.selectedEquipmentProfileId).thenReturn(_mockProfiles.first.id); }); testWidgets( 'Pro - show all saved profiles', (tester) async { await pumpTestWidget(tester, true); - expectEquipmentProfilesCount(_customProfiles.length + 1); - expectSelectedEquipmentProfileName(_customProfiles.first.name); + expectEquipmentProfilesCount(_mockProfiles.length + 1); + expectSelectedEquipmentProfileName(_mockProfiles.first.name); }, ); @@ -90,19 +91,19 @@ void main() { testWidgets( 'toggleProfile', (tester) async { - when(() => storageService.selectedEquipmentProfileId).thenReturn(_customProfiles.first.id); + when(() => storageService.selectedEquipmentProfileId).thenReturn(_mockEquipmentProfiles.first.id); await pumpTestWidget(tester, true); - expectEquipmentProfilesCount(_customProfiles.length + 1); - expectEquipmentProfilesInUseCount(_customProfiles.length + 1); - expectSelectedEquipmentProfileName(_customProfiles.first.name); + expectEquipmentProfilesCount(_mockProfiles.length + 1); + expectEquipmentProfilesInUseCount(_mockProfiles.length + 1); + expectSelectedEquipmentProfileName(_mockEquipmentProfiles.first.name); - await tester.equipmentProfilesProvider.toggleProfile(_customProfiles.first, false); + await tester.equipmentProfilesProvider.toggleProfile(_mockEquipmentProfiles.first.id, false); await tester.pump(); - expectEquipmentProfilesCount(_customProfiles.length + 1); - expectEquipmentProfilesInUseCount(_customProfiles.length + 1 - 1); + expectEquipmentProfilesCount(_mockProfiles.length + 1); + expectEquipmentProfilesInUseCount(_mockProfiles.length + 1 - 1); expectSelectedEquipmentProfileName(''); - verify(() => storageService.updateEquipmentProfile(id: _customProfiles.first.id, isUsed: false)).called(1); + verify(() => storageService.updateEquipmentProfile(id: _mockEquipmentProfiles.first.id, isUsed: false)).called(1); verify(() => storageService.selectedEquipmentProfileId = '').called(1); }, ); @@ -111,6 +112,7 @@ void main() { 'EquipmentProfilesProvider CRUD', (tester) async { when(() => storageService.getEquipmentProfiles()).thenAnswer((_) async => {}); + when(() => storageService.getPinholeEquipmentProfiles()).thenAnswer((_) async => {}); when(() => storageService.selectedEquipmentProfileId).thenReturn(''); await pumpTestWidget(tester, true); @@ -118,44 +120,45 @@ void main() { expectSelectedEquipmentProfileName(''); /// Add first profile and verify - await tester.equipmentProfilesProvider.addProfile(_customProfiles.first); + await tester.equipmentProfilesProvider.addProfile(_mockEquipmentProfiles.first); await tester.pump(); expectEquipmentProfilesCount(2); expectSelectedEquipmentProfileName(''); verify(() => storageService.addEquipmentProfile(any())).called(1); /// Add the other profiles and select the 1st one - for (final profile in _customProfiles.skip(1)) { + for (final profile in _mockEquipmentProfiles.skip(1)) { await tester.equipmentProfilesProvider.addProfile(profile); } - tester.equipmentProfilesProvider.selectProfile(_customProfiles.first); + tester.equipmentProfilesProvider.selectProfile(_mockEquipmentProfiles.first.id); await tester.pump(); - expectEquipmentProfilesCount(1 + _customProfiles.length); - expectSelectedEquipmentProfileName(_customProfiles.first.name); + expectEquipmentProfilesCount(1 + _mockEquipmentProfiles.length); + expectSelectedEquipmentProfileName(_mockEquipmentProfiles.first.name); /// Edit the selected profile - final updatedName = "${_customProfiles.first} updated"; - await tester.equipmentProfilesProvider.updateProfile(_customProfiles.first.copyWith(name: updatedName)); + final updatedName = "${_mockEquipmentProfiles.first} updated"; + await tester.equipmentProfilesProvider.updateProfile(_mockEquipmentProfiles.first.copyWith(name: updatedName)); await tester.pump(); - expectEquipmentProfilesCount(1 + _customProfiles.length); + expectEquipmentProfilesCount(1 + _mockEquipmentProfiles.length); expectSelectedEquipmentProfileName(updatedName); - verify(() => storageService.updateEquipmentProfile(id: _customProfiles.first.id, name: updatedName)).called(1); + verify(() => storageService.updateEquipmentProfile(id: _mockEquipmentProfiles.first.id, name: updatedName)) + .called(1); /// Delete a non-selected profile - await tester.equipmentProfilesProvider.deleteProfile(_customProfiles.last); + await tester.equipmentProfilesProvider.deleteProfile(_mockEquipmentProfiles.last); await tester.pump(); - expectEquipmentProfilesCount(1 + _customProfiles.length - 1); + expectEquipmentProfilesCount(1 + _mockEquipmentProfiles.length - 1); expectSelectedEquipmentProfileName(updatedName); verifyNever(() => storageService.selectedEquipmentProfileId = ''); - verify(() => storageService.deleteEquipmentProfile(_customProfiles.last.id)).called(1); + verify(() => storageService.deleteEquipmentProfile(_mockEquipmentProfiles.last.id)).called(1); /// Delete the selected profile - await tester.equipmentProfilesProvider.deleteProfile(_customProfiles.first); + await tester.equipmentProfilesProvider.deleteProfile(_mockEquipmentProfiles.first); await tester.pump(); - expectEquipmentProfilesCount(1 + _customProfiles.length - 2); + expectEquipmentProfilesCount(1 + _mockEquipmentProfiles.length - 2); expectSelectedEquipmentProfileName(''); verify(() => storageService.selectedEquipmentProfileId = '').called(1); - verify(() => storageService.deleteEquipmentProfile(_customProfiles.first.id)).called(1); + verify(() => storageService.deleteEquipmentProfile(_mockEquipmentProfiles.first.id)).called(1); }, ); } @@ -221,7 +224,9 @@ class _SelectedEquipmentProfile extends StatelessWidget { } } -final List _customProfiles = [ +final List _mockProfiles = [..._mockEquipmentProfiles, ..._mockPinholeEquipmentProfiles]; + +final List _mockEquipmentProfiles = [ const EquipmentProfile( id: '1', name: 'Test 1', @@ -291,3 +296,42 @@ final List _customProfiles = [ ], ), ]; + +final _mockPinholeEquipmentProfiles = [ + const PinholeEquipmentProfile( + id: '3', + name: 'Pinhole Camera f/64', + aperture: 64.0, + isoValues: [ + IsoValue(100, StopType.full), + IsoValue(200, StopType.full), + IsoValue(400, StopType.full), + IsoValue(800, StopType.full), + ], + ndValues: [ + NdValue(0), + NdValue(2), + NdValue(4), + ], + ), + const PinholeEquipmentProfile( + id: '4', + name: 'Pinhole Camera f/128', + aperture: 128.0, + isoValues: [ + IsoValue(50, StopType.full), + IsoValue(100, StopType.full), + IsoValue(200, StopType.full), + IsoValue(400, StopType.full), + IsoValue(800, StopType.full), + IsoValue(1600, StopType.full), + ], + ndValues: [ + NdValue(0), + NdValue(1), + NdValue(2), + NdValue(4), + NdValue(8), + ], + ), +]; diff --git a/test/screens/metering/components/shared/readings_container/equipment_profile_picker_test.dart b/test/screens/metering/components/shared/readings_container/equipment_profile_picker_test.dart index 6b44fd6..ac21632 100644 --- a/test/screens/metering/components/shared/readings_container/equipment_profile_picker_test.dart +++ b/test/screens/metering/components/shared/readings_container/equipment_profile_picker_test.dart @@ -19,7 +19,10 @@ void main() { setUpAll(() { storageService = _MockEquipmentProfilesStorageService(); - when(() => storageService.getEquipmentProfiles()).thenAnswer((_) async => _mockEquipmentProfiles.toTogglableMap()); + when(() => storageService.getEquipmentProfiles()) + .thenAnswer((_) async => _mockEquipmentProfiles.toList().toTogglableMap()); + when(() => storageService.getPinholeEquipmentProfiles()) + .thenAnswer((_) async => _mockPinholeEquipmentProfiles.toList().toTogglableMap()); when(() => storageService.selectedEquipmentProfileId).thenReturn(''); }); @@ -48,7 +51,7 @@ void main() { expectReadingValueContainerText(S.current.equipmentProfile); await tester.openAnimatedPicker(); expect(find.byIcon(Icons.camera_alt_outlined), findsOneWidget); - expectDialogPickerText(S.current.equipmentProfile); + expectDialogPickerText(S.current.equipmentProfile); }, ); @@ -62,7 +65,7 @@ void main() { await pumpApplication(tester); expectReadingValueContainerText(S.current.none); await tester.openAnimatedPicker(); - expectRadioListTile(S.current.none, isSelected: true); + expectRadioListTile(S.current.none, isSelected: true); }, ); @@ -73,7 +76,18 @@ void main() { await pumpApplication(tester); expectReadingValueContainerText(_mockEquipmentProfiles.first.name); await tester.openAnimatedPicker(); - expectRadioListTile(_mockEquipmentProfiles.first.name, isSelected: true); + expectRadioListTile(_mockEquipmentProfiles.first.name, isSelected: true); + }, + ); + + testWidgets( + 'Pinhole Camera f/64', + (tester) async { + when(() => storageService.selectedEquipmentProfileId).thenReturn(_mockPinholeEquipmentProfiles.first.id); + await pumpApplication(tester); + expectReadingValueContainerText(_mockPinholeEquipmentProfiles.first.name); + await tester.openAnimatedPicker(); + expectRadioListTile(_mockPinholeEquipmentProfiles.first.name, isSelected: true); }, ); }, @@ -84,10 +98,13 @@ void main() { (tester) async { when(() => storageService.getEquipmentProfiles()) .thenAnswer((_) async => _mockEquipmentProfiles.skip(1).toList().toTogglableMap()); + when(() => storageService.getPinholeEquipmentProfiles()) + .thenAnswer((_) async => _mockPinholeEquipmentProfiles.skip(1).toList().toTogglableMap()); await pumpApplication(tester); await tester.openAnimatedPicker(); - expectRadioListTile(S.current.none, isSelected: true); - expectRadioListTile(_mockEquipmentProfiles[1].name); + expectRadioListTile(S.current.none, isSelected: true); + expectRadioListTile(_mockEquipmentProfiles[1].name); + expectRadioListTile(_mockPinholeEquipmentProfiles[1].name); }, ); } @@ -126,3 +143,42 @@ final _mockEquipmentProfiles = [ isoValues: IsoValue.values, ), ]; + +final _mockPinholeEquipmentProfiles = [ + const PinholeEquipmentProfile( + id: '3', + name: 'Pinhole Camera f/64', + aperture: 64.0, + isoValues: [ + IsoValue(100, StopType.full), + IsoValue(200, StopType.full), + IsoValue(400, StopType.full), + IsoValue(800, StopType.full), + ], + ndValues: [ + NdValue(0), + NdValue(2), + NdValue(4), + ], + ), + const PinholeEquipmentProfile( + id: '4', + name: 'Pinhole Camera f/128', + aperture: 128.0, + isoValues: [ + IsoValue(50, StopType.full), + IsoValue(100, StopType.full), + IsoValue(200, StopType.full), + IsoValue(400, StopType.full), + IsoValue(800, StopType.full), + IsoValue(1600, StopType.full), + ], + ndValues: [ + NdValue(0), + NdValue(1), + NdValue(2), + NdValue(4), + NdValue(8), + ], + ), +]; diff --git a/test/screens/metering/utils/listener_equipment_profiles_test.dart b/test/screens/metering/utils/listener_equipment_profiles_test.dart index cf04ef4..11da028 100644 --- a/test/screens/metering/utils/listener_equipment_profiles_test.dart +++ b/test/screens/metering/utils/listener_equipment_profiles_test.dart @@ -13,7 +13,7 @@ class _MockEquipmentProfilesStorageService extends Mock implements IapStorageSer void main() { TestWidgetsFlutterBinding.ensureInitialized(); final storageService = _MockEquipmentProfilesStorageService(); - final onDidChangeDependencies = MockValueChanged(); + final onDidChangeDependencies = MockValueChanged(); setUp(() { registerFallbackValue(_customProfiles.first); @@ -26,6 +26,7 @@ void main() { ).thenAnswer((_) async {}); when(() => storageService.deleteEquipmentProfile(any())).thenAnswer((_) async {}); when(() => storageService.getEquipmentProfiles()).thenAnswer((_) => Future.value(_customProfiles.toTogglableMap())); + when(() => storageService.getPinholeEquipmentProfiles()).thenAnswer((_) => Future.value({})); }); tearDown(() { @@ -56,7 +57,7 @@ void main() { when(() => storageService.selectedEquipmentProfileId).thenReturn(''); await pumpTestWidget(tester); - tester.equipmentProfilesProvider.selectProfile(_customProfiles[0]); + tester.equipmentProfilesProvider.selectProfile(_customProfiles[0].id); await tester.pump(); verify(() => onDidChangeDependencies.onChanged(_customProfiles[0])).called(1); },