fixed metering screen tests

This commit is contained in:
Vadim 2023-10-17 17:01:11 +02:00
parent 0e5762b88e
commit 5e8f02afb2
6 changed files with 134 additions and 154 deletions

View file

@ -136,13 +136,6 @@ extension on WidgetTester {
await pumpAndSettle(); await pumpAndSettle();
} }
Future<void> 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<void> tapListTile(String title) async { Future<void> tapListTile(String title) async {
final listTile = find.byWidgetPredicate( final listTile = find.byWidgetPredicate(
(widget) => widget is ListTile && widget.title is Text && (widget.title as Text?)?.data == title, (widget) => widget is ListTile && widget.title is Text && (widget.title as Text?)?.data == title,

View file

@ -1,24 +1,14 @@
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_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/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/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/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/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/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/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'; 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_iap/m3_lightmeter_iap.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:mocktail/mocktail.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 'mocks/paid_features_mock.dart';
import 'utils/expectations.dart'; import 'utils/expectations.dart';
import 'utils/platform_channel_mock.dart';
import 'utils/widget_tester_actions.dart'; import 'utils/widget_tester_actions.dart';
class _MockUserPreferencesService extends Mock implements UserPreferencesService {} const defaultIsoValue = IsoValue(400, StopType.full);
const mockPhotoEv100 = 8.3;
class _MockCaffeineService extends Mock implements CaffeineService {} const fastestExposurePairMockPhoto = "f/1.0 - 1/320";
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);
//https://stackoverflow.com/a/67186625/13167574 //https://stackoverflow.com/a/67186625/13167574
void main() { void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); IntegrationTestWidgetsFlutterBinding.ensureInitialized();
late _MockUserPreferencesService mockUserPreferencesService; group(
late _MockCaffeineService mockCaffeineService; '[Light sensor availability]',
late _MockHapticsService mockHapticsService; () {
late _MockPermissionsService mockPermissionsService; testWidgets(
late _MockLightSensorService mockLightSensorService; 'Android - has sensor',
late _MockVolumeEventsService mockVolumeEventsService; (tester) async {
SharedPreferences.setMockInitialValues({UserPreferencesService.evSourceTypeKey: EvSourceType.sensor.index});
setLightSensorAvilability(hasSensor: true);
await tester.pumpApplication(productStatus: IAPProductStatus.purchasable);
setUpAll(() { // verify no button to switch to the incident light mode
mockUserPreferencesService = _MockUserPreferencesService(); expect(find.byType(LightSensorContainer), findsOneWidget);
when(() => mockUserPreferencesService.evSourceType).thenReturn(EvSourceType.sensor); },
when(() => mockUserPreferencesService.stopType).thenReturn(StopType.third); skip: Platform.isIOS,
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);
mockCaffeineService = _MockCaffeineService(); testWidgets(
when(() => mockCaffeineService.isKeepScreenOn()).thenAnswer((_) async => false); 'Android - no sensor',
when(() => mockCaffeineService.keepScreenOn(true)).thenAnswer((_) async => true); (tester) async {
when(() => mockCaffeineService.keepScreenOn(false)).thenAnswer((_) async => false); SharedPreferences.setMockInitialValues({UserPreferencesService.evSourceTypeKey: EvSourceType.sensor.index});
setLightSensorAvilability(hasSensor: false);
await tester.pumpApplication(productStatus: IAPProductStatus.purchasable);
mockHapticsService = _MockHapticsService(); // verify no button to switch to the incident light mode
when(() => mockHapticsService.quickVibration()).thenAnswer((_) async {}); expect(find.byTooltip(S.current.tooltipUseLightSensor), findsNothing);
when(() => mockHapticsService.responseVibration()).thenAnswer((_) async {}); },
when(() => mockHapticsService.errorVibration()).thenAnswer((_) async {}); skip: Platform.isIOS,
);
mockPermissionsService = _MockPermissionsService(); testWidgets(
when(() => mockPermissionsService.requestCameraPermission()).thenAnswer((_) async => PermissionStatus.granted); 'iOS - no sensor',
when(() => mockPermissionsService.checkCameraPermission()).thenAnswer((_) async => PermissionStatus.granted); (tester) async {
SharedPreferences.setMockInitialValues({UserPreferencesService.evSourceTypeKey: EvSourceType.sensor.index});
await tester.pumpApplication(productStatus: IAPProductStatus.purchasable);
mockLightSensorService = _MockLightSensorService(); // verify no button to switch to the incident light mode
when(() => mockLightSensorService.hasSensor()).thenAnswer((_) async => true); expect(find.byTooltip(S.current.tooltipUseLightSensor), findsNothing);
when(() => mockLightSensorService.luxStream()).thenAnswer((_) => Stream.fromIterable([100])); },
skip: Platform.isAndroid,
mockVolumeEventsService = _MockVolumeEventsService(); );
when(() => mockVolumeEventsService.setVolumeHandling(true)).thenAnswer((_) async => true); },
when(() => mockVolumeEventsService.setVolumeHandling(false)).thenAnswer((_) async => false); );
when(() => mockVolumeEventsService.volumeButtonsEventStream()).thenAnswer((_) => const Stream<int>.empty());
when(() => mockHapticsService.quickVibration()).thenAnswer((_) async {});
when(() => mockHapticsService.responseVibration()).thenAnswer((_) async {});
});
Future<void> 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();
}
group( 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) { void expectExposurePairsListItem(int index, String aperture, String shutterSpeed) {
final firstPairRow = find.byWidgetPredicate( final firstPairRow = find.byWidgetPredicate(
@ -156,14 +96,13 @@ void main() {
} }
setUpAll(() { setUpAll(() {
when(() => mockUserPreferencesService.evSourceType).thenReturn(EvSourceType.sensor); SharedPreferences.setMockInitialValues({UserPreferencesService.evSourceTypeKey: EvSourceType.camera.index});
when(() => mockLightSensorService.luxStream()).thenAnswer((_) => Stream.fromIterable([100]));
}); });
testWidgets( testWidgets(
'No exposure pairs', 'No exposure pairs',
(tester) async { (tester) async {
await pumpApplication(tester, IAPProductStatus.purchasable); await tester.pumpApplication(productStatus: IAPProductStatus.purchasable);
final pickerFinder = find.byType(ExtremeExposurePairsContainer); final pickerFinder = find.byType(ExtremeExposurePairsContainer);
expect(pickerFinder, findsOneWidget); expect(pickerFinder, findsOneWidget);
@ -195,14 +134,15 @@ void main() {
testWidgets( testWidgets(
'Multiple exposure pairs w/o reciprocity', 'Multiple exposure pairs w/o reciprocity',
(tester) async { (tester) async {
await pumpApplication(tester, IAPProductStatus.purchasable); await tester.pumpApplication(productStatus: IAPProductStatus.purchasable);
await tester.toggleIncidentMetering(); await tester.takePhoto();
expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"');
expectExposurePairsListItem(0, 'f/1.0', '1/160'); expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 6"');
expectMeasureButton(7.3); expectExposurePairsListItem(0, 'f/1.0', '1/320');
expectMeasureButton(mockPhotoEv100);
final exposurePairs = MeteringContainerBuidler.buildExposureValues( final exposurePairs = MeteringContainerBuidler.buildExposureValues(
7.3, mockPhotoEv100,
StopType.third, StopType.third,
defaultEquipmentProfile, defaultEquipmentProfile,
); );
@ -216,22 +156,23 @@ void main() {
matching: find.byType(Scrollable), matching: find.byType(Scrollable),
), ),
); );
expectExposurePairsListItem(exposurePairs.length - 1, 'f/45', '13"'); expectExposurePairsListItem(exposurePairs.length - 1, 'f/45', '6"');
expectMeasureButton(7.3); expectMeasureButton(mockPhotoEv100);
}, },
); );
testWidgets( testWidgets(
'Multiple exposure pairs w/ reciprocity', 'Multiple exposure pairs w/ reciprocity',
(tester) async { (tester) async {
await pumpApplication(tester, IAPProductStatus.purchased, selectedFilm: mockFilms.first); await tester.pumpApplication(selectedFilm: mockFilms.first);
await tester.toggleIncidentMetering(); await tester.takePhoto();
expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 26"');
expectExposurePairsListItem(0, 'f/1.0', '1/160'); expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 12"');
expectMeasureButton(7.3); expectExposurePairsListItem(0, 'f/1.0', '1/320');
expectMeasureButton(mockPhotoEv100);
final exposurePairs = MeteringContainerBuidler.buildExposureValues( final exposurePairs = MeteringContainerBuidler.buildExposureValues(
7.3, mockPhotoEv100,
StopType.third, StopType.third,
defaultEquipmentProfile, defaultEquipmentProfile,
); );
@ -245,48 +186,48 @@ void main() {
matching: find.byType(Scrollable), matching: find.byType(Scrollable),
), ),
); );
expectExposurePairsListItem(exposurePairs.length - 1, 'f/45', '26"'); expectExposurePairsListItem(exposurePairs.length - 1, 'f/45', '12"');
expectMeasureButton(7.3); expectMeasureButton(mockPhotoEv100);
}, },
); );
}, },
skip: true,
); );
group( group(
'Pickers tests', '[Pickers tests]',
() { () {
group('Select film', () { group('Select film', () {
testWidgets( testWidgets(
'with the same ISO', 'with the same ISO',
(tester) async { (tester) async {
await pumpApplication(tester, IAPProductStatus.purchased); await tester.pumpApplication(productStatus: IAPProductStatus.purchased);
expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"');
expectMeasureButton(7.3); expectMeasureButton(mockPhotoEv100);
await tester.openAnimatedPicker<FilmPicker>(); await tester.openAnimatedPicker<FilmPicker>();
expect(find.byType(DialogPicker<Film>), findsOneWidget); expect(find.byType(DialogPicker<Film>), findsOneWidget);
await tester.tapRadioListTile<Film>('2');
await tester.tapSelectButton(); await tester.tapSelectButton();
expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"');
expectMeasureButton(7.3); expectMeasureButton(mockPhotoEv100);
/// Make sure, that nothing is changed /// Make sure, that nothing is changed
await tester.tap(find.byType(MeteringMeasureButton)); await tester.tap(find.byType(MeteringMeasureButton));
await tester.tap(find.byType(MeteringMeasureButton)); await tester.tap(find.byType(MeteringMeasureButton));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"');
expectMeasureButton(7.3); expectMeasureButton(mockPhotoEv100);
}, },
); );
/// Select film with iso > current
}); });
testWidgets( testWidgets(
'Select ISO +1 EV', 'Select ISO +1 EV',
(tester) async { (tester) async {
await pumpApplication(tester, IAPProductStatus.purchased); await tester.pumpApplication(productStatus: IAPProductStatus.purchased);
expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"');
expectMeasureButton(7.3); expectMeasureButton(mockPhotoEv100);
await tester.openAnimatedPicker<IsoValuePicker>(); await tester.openAnimatedPicker<IsoValuePicker>();
expect(find.byType(DialogPicker<IsoValue>), findsOneWidget); expect(find.byType(DialogPicker<IsoValue>), findsOneWidget);
@ -307,9 +248,9 @@ void main() {
testWidgets( testWidgets(
'Select ND -1 EV', 'Select ND -1 EV',
(tester) async { (tester) async {
await pumpApplication(tester, IAPProductStatus.purchased); await tester.pumpApplication(productStatus: IAPProductStatus.purchased);
expectExposurePairsContainer('f/1.0 - 1/160', 'f/45 - 13"'); expectExposurePairsContainer('f/1.0 - 1/320', 'f/45 - 13"');
expectMeasureButton(7.3); expectMeasureButton(mockPhotoEv100);
await tester.openAnimatedPicker<NdValuePicker>(); await tester.openAnimatedPicker<NdValuePicker>();
expect(find.byType(DialogPicker<NdValue>), findsOneWidget); expect(find.byType(DialogPicker<NdValue>), findsOneWidget);

View file

@ -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

View file

@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.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) { void expectExposurePairsContainer(String fastest, String slowest) {
final pickerFinder = find.byType(ExtremeExposurePairsContainer); final pickerFinder = find.byType(ExtremeExposurePairsContainer);
expect(pickerFinder, findsOneWidget); expect(pickerFinder, findsOneWidget);
expect(find.descendant(of: pickerFinder, matching: find.text(S.current.fastestExposurePair)), expect(find.descendant(of: pickerFinder, matching: find.text(S.current.fastestExposurePair)), findsOneWidget);
findsOneWidget);
expect(find.descendant(of: pickerFinder, matching: find.text(fastest)), findsOneWidget); expect(find.descendant(of: pickerFinder, matching: find.text(fastest)), findsOneWidget);
expect(find.descendant(of: pickerFinder, matching: find.text(S.current.slowestExposurePair)), expect(find.descendant(of: pickerFinder, matching: find.text(S.current.slowestExposurePair)), findsOneWidget);
findsOneWidget);
expect(find.descendant(of: pickerFinder, matching: find.text(slowest)), findsOneWidget); expect(find.descendant(of: pickerFinder, matching: find.text(slowest)), findsOneWidget);
} }
void expectRadioListTile<T>(String text, {bool isSelected = false}) {
expect(
find.descendant(of: find.byWidgetPredicate((widget) => widget is RadioListTile<T>), matching: find.text(text)),
findsOneWidget,
);
}

View file

@ -18,3 +18,17 @@ Future<void> sendMockIncidentEv(double ev) async {
(ByteData? data) {}, (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;
}
},
);
}

View file

@ -10,6 +10,7 @@ import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import '../mocks/paid_features_mock.dart'; import '../mocks/paid_features_mock.dart';
import 'platform_channel_mock.dart';
extension WidgetTesterCommonActions on WidgetTester { extension WidgetTesterCommonActions on WidgetTester {
Future<void> pumpApplication({ Future<void> pumpApplication({
@ -33,8 +34,16 @@ extension WidgetTesterCommonActions on WidgetTester {
await pumpAndSettle(); await pumpAndSettle();
} }
Future<void> toggleIncidentMetering() async { Future<void> takePhoto() async {
await tap(find.byType(MeteringMeasureButton)); 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<void> toggleIncidentMetering(double ev) async {
await tap(find.byType(MeteringMeasureButton));
await sendMockIncidentEv(ev);
await tap(find.byType(MeteringMeasureButton)); await tap(find.byType(MeteringMeasureButton));
await pumpAndSettle(); await pumpAndSettle();
} }