From aa3d9ddec1d1880a7a012bab6e05b13c70649b6c Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Tue, 12 Aug 2025 13:36:53 +0200 Subject: [PATCH] added test for guard Pro tap --- integration_test/guard_pro_tap_test.dart | 79 +++++++++++++++++++ integration_test/run_all_tests.dart | 2 + .../utils/widget_tester_actions.dart | 4 +- .../lightmeter_pro/screen_lightmeter_pro.dart | 4 +- lib/utils/guard_pro_tap.dart | 7 +- 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 integration_test/guard_pro_tap_test.dart diff --git a/integration_test/guard_pro_tap_test.dart b/integration_test/guard_pro_tap_test.dart new file mode 100644 index 0000000..cb2a292 --- /dev/null +++ b/integration_test/guard_pro_tap_test.dart @@ -0,0 +1,79 @@ +import 'dart:convert'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:lightmeter/data/models/ev_source_type.dart'; +import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; +import 'package:lightmeter/data/shared_prefs_service.dart'; +import 'package:lightmeter/generated/l10n.dart'; +import 'package:lightmeter/screens/equipment_profile_edit/screen_equipment_profile_edit.dart'; +import 'package:lightmeter/screens/lightmeter_pro/screen_lightmeter_pro.dart'; +import 'package:lightmeter/screens/logbook_photos/screen_logbook_photos.dart'; +import 'package:lightmeter/screens/settings/screen_settings.dart'; +import 'package:lightmeter/utils/platform_utils.dart'; +import 'package:meta/meta.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import '../integration_test/utils/widget_tester_actions.dart'; +import 'mocks/iap_products_mock.dart'; + +@isTest +void testGuardProTap(String description) { + testWidgets( + description, + (tester) async { + SharedPreferences.setMockInitialValues({ + /// Metering values + UserPreferencesService.evSourceTypeKey: EvSourceType.camera.index, + UserPreferencesService.meteringScreenLayoutKey: json.encode( + { + MeteringScreenLayoutFeature.equipmentProfiles: true, + MeteringScreenLayoutFeature.extremeExposurePairs: true, + MeteringScreenLayoutFeature.filmPicker: true, + }.toJson(), + ), + UserPreferencesService.seenChangelogVersionKey: await const PlatformUtils().version, + }); + + await tester.pumpApplication(isPro: false); + await tester.openSettings(); + await tester.tapDescendantTextOf(S.current.equipmentProfiles); + + /// Try adding a new equipment profile + await tester.tap(find.byIcon(Icons.add_outlined).first); + await tester.pumpAndSettle(); + expect(find.byType(LightmeterProScreen), findsOneWidget); + + /// Purchase Pro + (tester.state(find.byType(MockIAPProductsProvider)) as MockIAPProductsProviderState).buy(); + await tester.navigatorPop(true); + await tester.pumpAndSettle(); + expect(find.byType(LightmeterProScreen), findsNothing); + expect(find.byType(EquipmentProfileEditScreen), findsOneWidget); + await tester.navigatorPop(); + await tester.navigatorPop(); + + /// Refund + (tester.state(find.byType(MockIAPProductsProvider)) as MockIAPProductsProviderState).clearPurchases(); + await tester.pumpAndSettle(); + await tester.tapDescendantTextOf(S.current.logbook); + + /// Try enabling logbook + await tester.tap(find.text(S.current.saveNewPhotos)); + await tester.pumpAndSettle(); + expect(find.byType(LightmeterProScreen), findsOneWidget); + (tester.state(find.byType(MockIAPProductsProvider)) as MockIAPProductsProviderState).buy(); + await tester.navigatorPop(true); + await tester.pumpAndSettle(); + expect(find.byType(LightmeterProScreen), findsNothing); + await tester.pumpAndSettle(); + expect( + find.descendant( + of: find.byType(LogbookPhotosScreen), + matching: find.byWidgetPredicate((widget) => widget is SwitchListTile && widget.value), + ), + findsOneWidget, + ); + }, + ); +} diff --git a/integration_test/run_all_tests.dart b/integration_test/run_all_tests.dart index 1910cb0..718a565 100644 --- a/integration_test/run_all_tests.dart +++ b/integration_test/run_all_tests.dart @@ -2,6 +2,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; import 'e2e_test.dart'; +import 'guard_pro_tap_test.dart'; import 'logbook_test.dart'; import 'metering_screen_layout_test.dart'; import 'purchases_test.dart'; @@ -19,6 +20,7 @@ void main() { }); testPurchases('Purchase & refund premium features'); + testGuardProTap('Guard Pro tap'); testToggleLayoutFeatures('Toggle metering screen layout features'); testLogbook('Logbook'); testE2E('e2e'); diff --git a/integration_test/utils/widget_tester_actions.dart b/integration_test/utils/widget_tester_actions.dart index 7754bb4..477f209 100644 --- a/integration_test/utils/widget_tester_actions.dart +++ b/integration_test/utils/widget_tester_actions.dart @@ -68,8 +68,8 @@ extension WidgetTesterCommonActions on WidgetTester { await pumpAndSettle(); } - Future navigatorPop() async { - (state(find.byType(Navigator)) as NavigatorState).pop(); + Future navigatorPop([Object? result]) async { + (state(find.byType(Navigator)) as NavigatorState).pop(result); await pumpAndSettle(Dimens.durationML); } } diff --git a/lib/screens/lightmeter_pro/screen_lightmeter_pro.dart b/lib/screens/lightmeter_pro/screen_lightmeter_pro.dart index 80ca149..aa7cfa5 100644 --- a/lib/screens/lightmeter_pro/screen_lightmeter_pro.dart +++ b/lib/screens/lightmeter_pro/screen_lightmeter_pro.dart @@ -115,7 +115,7 @@ class _LightmeterProScreenState extends State { try { final isPro = await IAPProductsProvider.of(context).restorePurchases(); if (mounted && isPro) { - Navigator.of(context).pop(); + Navigator.of(context).pop(true); } } on PlatformException catch (e) { _showSnackbar(e.message ?? ''); @@ -131,7 +131,7 @@ class _LightmeterProScreenState extends State { try { final isPro = await IAPProductsProvider.of(context).buyPro(product); if (mounted && isPro) { - Navigator.of(context).pop(); + Navigator.of(context).pop(true); } } on PlatformException catch (e) { _showSnackbar(e.message ?? ''); diff --git a/lib/utils/guard_pro_tap.dart b/lib/utils/guard_pro_tap.dart index 624bd05..6132537 100644 --- a/lib/utils/guard_pro_tap.dart +++ b/lib/utils/guard_pro_tap.dart @@ -2,10 +2,13 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/navigation/routes.dart'; import 'package:lightmeter/utils/context_utils.dart'; -void guardProTap(BuildContext context, VoidCallback callback) { +Future guardProTap(BuildContext context, VoidCallback callback) async { if (context.isPro) { callback(); } else { - Navigator.of(context).pushNamed(NavigationRoutes.proFeaturesScreen.name); + final isPro = await Navigator.of(context).pushNamed(NavigationRoutes.proFeaturesScreen.name); + if (isPro == true) { + callback(); + } } }