diff --git a/integration_test/generate_screenshots.dart b/integration_test/generate_screenshots.dart index 2146d4d..a44c42b 100644 --- a/integration_test/generate_screenshots.dart +++ b/integration_test/generate_screenshots.dart @@ -136,13 +136,6 @@ extension on WidgetTester { await pumpAndSettle(); } - Future takePhoto() async { - await tap(find.byType(MeteringMeasureButton)); - await pump(const Duration(seconds: 2)); // wait for circular progress indicator - await pump(const Duration(seconds: 1)); // wait for circular progress indicator - await pumpAndSettle(); - } - Future tapListTile(String title) async { final listTile = find.byWidgetPredicate( (widget) => widget is ListTile && widget.title is Text && (widget.title as Text?)?.data == title, diff --git a/integration_test/metering_screen_test.dart b/integration_test/metering_screen_test.dart index 543d993..c0b7412 100644 --- a/integration_test/metering_screen_test.dart +++ b/integration_test/metering_screen_test.dart @@ -1,24 +1,14 @@ +import 'dart:io'; + import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; -import 'package:lightmeter/application.dart'; -import 'package:lightmeter/data/caffeine_service.dart'; -import 'package:lightmeter/data/haptics_service.dart'; -import 'package:lightmeter/data/light_sensor_service.dart'; import 'package:lightmeter/data/models/ev_source_type.dart'; -import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; -import 'package:lightmeter/data/models/supported_locale.dart'; -import 'package:lightmeter/data/models/theme_type.dart'; -import 'package:lightmeter/data/models/volume_action.dart'; -import 'package:lightmeter/data/permissions_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; -import 'package:lightmeter/data/volume_events_service.dart'; -import 'package:lightmeter/environment.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:lightmeter/providers/services_provider.dart'; -import 'package:lightmeter/providers/user_preferences_provider.dart'; -import 'package:lightmeter/res/theme.dart'; import 'package:lightmeter/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.dart'; +import 'package:lightmeter/screens/metering/components/light_sensor_container/widget_container_light_sensor.dart'; import 'package:lightmeter/screens/metering/components/shared/exposure_pairs_list/widget_list_exposure_pairs.dart'; import 'package:lightmeter/screens/metering/components/shared/readings_container/components/extreme_exposure_pairs_container/widget_container_extreme_exposure_pairs.dart'; import 'package:lightmeter/screens/metering/components/shared/readings_container/components/film_picker/widget_picker_film.dart'; @@ -30,116 +20,66 @@ import 'package:lightmeter/screens/shared/icon_placeholder/widget_icon_placehold import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:mocktail/mocktail.dart'; -import 'package:permission_handler/permission_handler.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'mocks/paid_features_mock.dart'; import 'utils/expectations.dart'; +import 'utils/platform_channel_mock.dart'; import 'utils/widget_tester_actions.dart'; -class _MockUserPreferencesService extends Mock implements UserPreferencesService {} - -class _MockCaffeineService extends Mock implements CaffeineService {} - -class _MockHapticsService extends Mock implements HapticsService {} - -class _MockPermissionsService extends Mock implements PermissionsService {} - -class _MockLightSensorService extends Mock implements LightSensorService {} - -class _MockVolumeEventsService extends Mock implements VolumeEventsService {} - -const _defaultIsoValue = IsoValue(400, StopType.full); +const defaultIsoValue = IsoValue(400, StopType.full); +const mockPhotoEv100 = 8.3; +const fastestExposurePairMockPhoto = "f/1.0 - 1/320"; //https://stackoverflow.com/a/67186625/13167574 void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); - late _MockUserPreferencesService mockUserPreferencesService; - late _MockCaffeineService mockCaffeineService; - late _MockHapticsService mockHapticsService; - late _MockPermissionsService mockPermissionsService; - late _MockLightSensorService mockLightSensorService; - late _MockVolumeEventsService mockVolumeEventsService; + group( + '[Light sensor availability]', + () { + testWidgets( + 'Android - has sensor', + (tester) async { + SharedPreferences.setMockInitialValues({UserPreferencesService.evSourceTypeKey: EvSourceType.sensor.index}); + setLightSensorAvilability(hasSensor: true); + await tester.pumpApplication(productStatus: IAPProductStatus.purchasable); - setUpAll(() { - mockUserPreferencesService = _MockUserPreferencesService(); - when(() => mockUserPreferencesService.evSourceType).thenReturn(EvSourceType.sensor); - when(() => mockUserPreferencesService.stopType).thenReturn(StopType.third); - when(() => mockUserPreferencesService.locale).thenReturn(SupportedLocale.en); - when(() => mockUserPreferencesService.caffeine).thenReturn(true); - when(() => mockUserPreferencesService.volumeAction).thenReturn(VolumeAction.shutter); - when(() => mockUserPreferencesService.cameraEvCalibration).thenReturn(0.0); - when(() => mockUserPreferencesService.lightSensorEvCalibration).thenReturn(0.0); - when(() => mockUserPreferencesService.iso).thenReturn(_defaultIsoValue); - when(() => mockUserPreferencesService.ndFilter).thenReturn(NdValue.values.first); - when(() => mockUserPreferencesService.haptics).thenReturn(true); - when(() => mockUserPreferencesService.meteringScreenLayout).thenReturn({ - MeteringScreenLayoutFeature.equipmentProfiles: true, - MeteringScreenLayoutFeature.extremeExposurePairs: true, - MeteringScreenLayoutFeature.filmPicker: true, - MeteringScreenLayoutFeature.histogram: false, - }); - when(() => mockUserPreferencesService.themeType).thenReturn(ThemeType.light); - when(() => mockUserPreferencesService.primaryColor).thenReturn(primaryColorsList[5]); - when(() => mockUserPreferencesService.dynamicColor).thenReturn(false); + // verify no button to switch to the incident light mode + expect(find.byType(LightSensorContainer), findsOneWidget); + }, + skip: Platform.isIOS, + ); - mockCaffeineService = _MockCaffeineService(); - when(() => mockCaffeineService.isKeepScreenOn()).thenAnswer((_) async => false); - when(() => mockCaffeineService.keepScreenOn(true)).thenAnswer((_) async => true); - when(() => mockCaffeineService.keepScreenOn(false)).thenAnswer((_) async => false); + testWidgets( + 'Android - no sensor', + (tester) async { + SharedPreferences.setMockInitialValues({UserPreferencesService.evSourceTypeKey: EvSourceType.sensor.index}); + setLightSensorAvilability(hasSensor: false); + await tester.pumpApplication(productStatus: IAPProductStatus.purchasable); - mockHapticsService = _MockHapticsService(); - when(() => mockHapticsService.quickVibration()).thenAnswer((_) async {}); - when(() => mockHapticsService.responseVibration()).thenAnswer((_) async {}); - when(() => mockHapticsService.errorVibration()).thenAnswer((_) async {}); + // verify no button to switch to the incident light mode + expect(find.byTooltip(S.current.tooltipUseLightSensor), findsNothing); + }, + skip: Platform.isIOS, + ); - mockPermissionsService = _MockPermissionsService(); - when(() => mockPermissionsService.requestCameraPermission()).thenAnswer((_) async => PermissionStatus.granted); - when(() => mockPermissionsService.checkCameraPermission()).thenAnswer((_) async => PermissionStatus.granted); + testWidgets( + 'iOS - no sensor', + (tester) async { + SharedPreferences.setMockInitialValues({UserPreferencesService.evSourceTypeKey: EvSourceType.sensor.index}); + await tester.pumpApplication(productStatus: IAPProductStatus.purchasable); - mockLightSensorService = _MockLightSensorService(); - when(() => mockLightSensorService.hasSensor()).thenAnswer((_) async => true); - when(() => mockLightSensorService.luxStream()).thenAnswer((_) => Stream.fromIterable([100])); - - mockVolumeEventsService = _MockVolumeEventsService(); - when(() => mockVolumeEventsService.setVolumeHandling(true)).thenAnswer((_) async => true); - when(() => mockVolumeEventsService.setVolumeHandling(false)).thenAnswer((_) async => false); - when(() => mockVolumeEventsService.volumeButtonsEventStream()).thenAnswer((_) => const Stream.empty()); - - when(() => mockHapticsService.quickVibration()).thenAnswer((_) async {}); - when(() => mockHapticsService.responseVibration()).thenAnswer((_) async {}); - }); - - Future pumpApplication( - WidgetTester tester, - IAPProductStatus purchaseStatus, { - String selectedEquipmentProfileId = '', - Film selectedFilm = const Film.other(), - }) async { - await tester.pumpWidget( - MockIAPProviders( - purchaseStatus: purchaseStatus, - selectedEquipmentProfileId: selectedEquipmentProfileId, - selectedFilm: selectedFilm, - child: ServicesProvider( - environment: const Environment.prod().copyWith(hasLightSensor: true), - userPreferencesService: mockUserPreferencesService, - caffeineService: mockCaffeineService, - hapticsService: mockHapticsService, - permissionsService: mockPermissionsService, - lightSensorService: mockLightSensorService, - volumeEventsService: mockVolumeEventsService, - child: const UserPreferencesProvider( - child: Application(), - ), - ), - ), - ); - await tester.pumpAndSettle(); - } + // verify no button to switch to the incident light mode + expect(find.byTooltip(S.current.tooltipUseLightSensor), findsNothing); + }, + skip: Platform.isAndroid, + ); + }, + ); group( - 'Match extreme exposure pairs & pairs list edge values', + '[Match extreme exposure pairs & pairs list edge values]', () { void expectExposurePairsListItem(int index, String aperture, String shutterSpeed) { final firstPairRow = find.byWidgetPredicate( @@ -156,14 +96,13 @@ void main() { } setUpAll(() { - when(() => mockUserPreferencesService.evSourceType).thenReturn(EvSourceType.sensor); - when(() => mockLightSensorService.luxStream()).thenAnswer((_) => Stream.fromIterable([100])); + SharedPreferences.setMockInitialValues({UserPreferencesService.evSourceTypeKey: EvSourceType.camera.index}); }); testWidgets( 'No exposure pairs', (tester) async { - await pumpApplication(tester, IAPProductStatus.purchasable); + await tester.pumpApplication(productStatus: IAPProductStatus.purchasable); final pickerFinder = find.byType(ExtremeExposurePairsContainer); expect(pickerFinder, findsOneWidget); @@ -195,14 +134,15 @@ void main() { testWidgets( 'Multiple exposure pairs w/o reciprocity', (tester) async { - await pumpApplication(tester, IAPProductStatus.purchasable); - await tester.toggleIncidentMetering(); - expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); - expectExposurePairsListItem(0, 'f/1.0', '1/160'); - expectMeasureButton(7.3); + await tester.pumpApplication(productStatus: IAPProductStatus.purchasable); + await tester.takePhoto(); + + expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 6"'); + expectExposurePairsListItem(0, 'f/1.0', '1/320'); + expectMeasureButton(mockPhotoEv100); final exposurePairs = MeteringContainerBuidler.buildExposureValues( - 7.3, + mockPhotoEv100, StopType.third, defaultEquipmentProfile, ); @@ -216,22 +156,23 @@ void main() { matching: find.byType(Scrollable), ), ); - expectExposurePairsListItem(exposurePairs.length - 1, 'f/45', '13"'); - expectMeasureButton(7.3); + expectExposurePairsListItem(exposurePairs.length - 1, 'f/45', '6"'); + expectMeasureButton(mockPhotoEv100); }, ); testWidgets( 'Multiple exposure pairs w/ reciprocity', (tester) async { - await pumpApplication(tester, IAPProductStatus.purchased, selectedFilm: mockFilms.first); - await tester.toggleIncidentMetering(); - expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 26"'); - expectExposurePairsListItem(0, 'f/1.0', '1/160'); - expectMeasureButton(7.3); + await tester.pumpApplication(selectedFilm: mockFilms.first); + await tester.takePhoto(); + + expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 12"'); + expectExposurePairsListItem(0, 'f/1.0', '1/320'); + expectMeasureButton(mockPhotoEv100); final exposurePairs = MeteringContainerBuidler.buildExposureValues( - 7.3, + mockPhotoEv100, StopType.third, defaultEquipmentProfile, ); @@ -245,48 +186,48 @@ void main() { matching: find.byType(Scrollable), ), ); - expectExposurePairsListItem(exposurePairs.length - 1, 'f/45', '26"'); - expectMeasureButton(7.3); + expectExposurePairsListItem(exposurePairs.length - 1, 'f/45', '12"'); + expectMeasureButton(mockPhotoEv100); }, ); }, - skip: true, ); group( - 'Pickers tests', + '[Pickers tests]', () { group('Select film', () { testWidgets( 'with the same ISO', (tester) async { - await pumpApplication(tester, IAPProductStatus.purchased); - expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); - expectMeasureButton(7.3); + await tester.pumpApplication(productStatus: IAPProductStatus.purchased); + expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"'); + expectMeasureButton(mockPhotoEv100); await tester.openAnimatedPicker(); expect(find.byType(DialogPicker), findsOneWidget); - await tester.tapRadioListTile('2'); await tester.tapSelectButton(); - expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); - expectMeasureButton(7.3); + expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"'); + expectMeasureButton(mockPhotoEv100); /// Make sure, that nothing is changed await tester.tap(find.byType(MeteringMeasureButton)); await tester.tap(find.byType(MeteringMeasureButton)); await tester.pumpAndSettle(); - expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); - expectMeasureButton(7.3); + expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"'); + expectMeasureButton(mockPhotoEv100); }, ); + + /// Select film with iso > current }); testWidgets( 'Select ISO +1 EV', (tester) async { - await pumpApplication(tester, IAPProductStatus.purchased); - expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); - expectMeasureButton(7.3); + await tester.pumpApplication(productStatus: IAPProductStatus.purchased); + expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"'); + expectMeasureButton(mockPhotoEv100); await tester.openAnimatedPicker(); expect(find.byType(DialogPicker), findsOneWidget); @@ -307,9 +248,9 @@ void main() { testWidgets( 'Select ND -1 EV', (tester) async { - await pumpApplication(tester, IAPProductStatus.purchased); - expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); - expectMeasureButton(7.3); + await tester.pumpApplication(productStatus: IAPProductStatus.purchased); + expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"'); + expectMeasureButton(mockPhotoEv100); await tester.openAnimatedPicker(); expect(find.byType(DialogPicker), findsOneWidget); diff --git a/integration_test/run_integration_tests.sh b/integration_test/run_integration_tests.sh new file mode 100644 index 0000000..526c9fb --- /dev/null +++ b/integration_test/run_integration_tests.sh @@ -0,0 +1,17 @@ +# https://github.com/flutter/flutter/issues/86295#issuecomment-1192766368 +devices=$(adb devices) +devicesIds=$(echo $devices | grep -Eo '[A-Z0-9]{2,}') +firstDeviceId=$(echo $devicesIds | cut -d " " -f 1) +# adb -s $firstDeviceId shell pm grant com.vodemn.lightmeter.dev android.permission.CAMERA + +flutter drive \ + --dart-define="cameraPreviewAspectRatio=240/320" \ + --dart-define="cameraStubImage=assets/camera_stub_image.jpg" \ + --driver=test_driver/integration_driver.dart \ + --target=integration_test/metering_screen_test.dart \ + --profile \ + --flavor=dev \ + --no-dds \ + --endless-trace-buffer \ + --purge-persistent-cache \ + -d $firstDeviceId \ No newline at end of file diff --git a/integration_test/utils/expectations.dart b/integration_test/utils/expectations.dart index 3fe633b..29be39e 100644 --- a/integration_test/utils/expectations.dart +++ b/integration_test/utils/expectations.dart @@ -1,3 +1,4 @@ +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.dart'; @@ -37,10 +38,15 @@ void expectMeasureButton(double ev) { void expectExposurePairsContainer(String fastest, String slowest) { final pickerFinder = find.byType(ExtremeExposurePairsContainer); expect(pickerFinder, findsOneWidget); - expect(find.descendant(of: pickerFinder, matching: find.text(S.current.fastestExposurePair)), - findsOneWidget); + expect(find.descendant(of: pickerFinder, matching: find.text(S.current.fastestExposurePair)), findsOneWidget); expect(find.descendant(of: pickerFinder, matching: find.text(fastest)), findsOneWidget); - expect(find.descendant(of: pickerFinder, matching: find.text(S.current.slowestExposurePair)), - findsOneWidget); + expect(find.descendant(of: pickerFinder, matching: find.text(S.current.slowestExposurePair)), findsOneWidget); expect(find.descendant(of: pickerFinder, matching: find.text(slowest)), findsOneWidget); } + +void expectRadioListTile(String text, {bool isSelected = false}) { + expect( + find.descendant(of: find.byWidgetPredicate((widget) => widget is RadioListTile), matching: find.text(text)), + findsOneWidget, + ); +} diff --git a/integration_test/utils/platform_channel_mock.dart b/integration_test/utils/platform_channel_mock.dart index f50a8f7..cb57cc9 100644 --- a/integration_test/utils/platform_channel_mock.dart +++ b/integration_test/utils/platform_channel_mock.dart @@ -18,3 +18,17 @@ Future sendMockIncidentEv(double ev) async { (ByteData? data) {}, ); } + +void setLightSensorAvilability({required bool hasSensor}) { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('system_feature'), + (methodCall) async { + switch (methodCall.method) { + case "sensor": + return hasSensor; + default: + return null; + } + }, + ); +} diff --git a/integration_test/utils/widget_tester_actions.dart b/integration_test/utils/widget_tester_actions.dart index 22c1e20..8abc4ce 100644 --- a/integration_test/utils/widget_tester_actions.dart +++ b/integration_test/utils/widget_tester_actions.dart @@ -10,6 +10,7 @@ import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import '../mocks/paid_features_mock.dart'; +import 'platform_channel_mock.dart'; extension WidgetTesterCommonActions on WidgetTester { Future pumpApplication({ @@ -33,8 +34,16 @@ extension WidgetTesterCommonActions on WidgetTester { await pumpAndSettle(); } - Future toggleIncidentMetering() async { + Future takePhoto() async { await tap(find.byType(MeteringMeasureButton)); + await pump(const Duration(seconds: 2)); // wait for circular progress indicator + await pump(const Duration(seconds: 1)); // wait for circular progress indicator + await pumpAndSettle(); + } + + Future toggleIncidentMetering(double ev) async { + await tap(find.byType(MeteringMeasureButton)); + await sendMockIncidentEv(ev); await tap(find.byType(MeteringMeasureButton)); await pumpAndSettle(); }