Compare commits

...

6 commits

Author SHA1 Message Date
Vadim
73ebc671f2 showBuyProDialog tests 2023-11-01 21:38:14 +01:00
Vadim
a41add2f97 split EquipmentProfileListener tests 2023-11-01 21:15:55 +01:00
Vadim
6507df2ad6 typo 2023-11-01 21:10:27 +01:00
Vadim
d26da843b3 EquipmentProfileListener tests 2023-11-01 21:09:56 +01:00
Vadim
ee7ebd585a import 2023-11-01 20:46:29 +01:00
Vadim
002b7c985a VolumeKeysNotifier tests 2023-11-01 20:45:48 +01:00
18 changed files with 279 additions and 31 deletions

View file

@ -3,7 +3,7 @@ import 'package:flutter/services.dart';
import 'package:platform/platform.dart';
class VolumeEventsService {
final LocalPlatform localPlatform;
final LocalPlatform _localPlatform;
@visibleForTesting
static const volumeHandlingChannel = MethodChannel("com.vodemn.lightmeter/volumeHandling");
@ -11,12 +11,12 @@ class VolumeEventsService {
@visibleForTesting
static const volumeEventsChannel = EventChannel("com.vodemn.lightmeter/volumeEvents");
const VolumeEventsService(this.localPlatform);
const VolumeEventsService(this._localPlatform);
/// If set to `false` we allow system to handle key events.
/// Returns current status of volume handling.
Future<bool> setVolumeHandling(bool enableHandling) async {
if (!localPlatform.isAndroid) {
if (!_localPlatform.isAndroid) {
return false;
}
return volumeHandlingChannel
@ -29,7 +29,7 @@ class VolumeEventsService {
/// KEYCODE_VOLUME_DOWN = 25;
/// pressed
Stream<int> volumeButtonsEventStream() {
if (!localPlatform.isAndroid) {
if (!_localPlatform.isAndroid) {
return const Stream.empty();
}
return volumeEventsChannel

View file

@ -54,9 +54,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
_defaultProfile,
if (IAPProducts.isPurchased(context, IAPProductType.paidFeatures)) ..._customProfiles,
],
selected: IAPProducts.isPurchased(context, IAPProductType.paidFeatures)
? _selectedProfile
: _defaultProfile,
selected: IAPProducts.isPurchased(context, IAPProductType.paidFeatures) ? _selectedProfile : _defaultProfile,
child: widget.child,
);
}
@ -85,7 +83,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
_refreshSavedProfiles();
}
void updateProdile(EquipmentProfile data) {
void updateProfile(EquipmentProfile data) {
final indexToUpdate = _customProfiles.indexWhere((element) => element.id == data.id);
if (indexToUpdate >= 0) {
_customProfiles[indexToUpdate] = data;
@ -118,13 +116,14 @@ class EquipmentProfiles extends SelectableInheritedModel<EquipmentProfile> {
/// [_defaultProfile] + profiles created by the user
static List<EquipmentProfile> of(BuildContext context) {
return InheritedModel.inheritFrom<EquipmentProfiles>(context, aspect: SelectableAspect.list)!
.values;
return InheritedModel.inheritFrom<EquipmentProfiles>(context, aspect: SelectableAspect.list)!.values;
}
static EquipmentProfile selectedOf(BuildContext context) {
return InheritedModel.inheritFrom<EquipmentProfiles>(context,
aspect: SelectableAspect.selected,)!
return InheritedModel.inheritFrom<EquipmentProfiles>(
context,
aspect: SelectableAspect.selected,
)!
.selected;
}
}

View file

@ -10,9 +10,9 @@ import 'package:lightmeter/screens/metering/communication/event_communication_me
as communication_events;
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart'
as communication_states;
import 'package:lightmeter/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart';
import 'package:lightmeter/screens/metering/event_metering.dart';
import 'package:lightmeter/screens/metering/state_metering.dart';
import 'package:lightmeter/screens/metering/utils/notifier_volume_keys.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {

View file

@ -4,8 +4,8 @@ import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/screens/metering/bloc_metering.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
import 'package:lightmeter/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart';
import 'package:lightmeter/screens/metering/screen_metering.dart';
import 'package:lightmeter/screens/metering/utils/notifier_volume_keys.dart';
class MeteringFlow extends StatefulWidget {
const MeteringFlow({super.key});

View file

@ -13,7 +13,7 @@ import 'package:lightmeter/screens/metering/components/camera_container/provider
import 'package:lightmeter/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart';
import 'package:lightmeter/screens/metering/event_metering.dart';
import 'package:lightmeter/screens/metering/state_metering.dart';
import 'package:lightmeter/screens/metering/utils/listsner_equipment_profiles.dart';
import 'package:lightmeter/screens/metering/utils/listener_equipment_profiles.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class MeteringScreen extends StatelessWidget {

View file

@ -5,12 +5,12 @@ import 'package:lightmeter/data/models/volume_action.dart';
import 'package:lightmeter/data/volume_events_service.dart';
class VolumeKeysNotifier extends ChangeNotifier with RouteAware {
final VolumeEventsService volumeEventsService;
final VolumeEventsService _volumeEventsService;
late final StreamSubscription<VolumeKey> _volumeKeysSubscription;
VolumeKey _value = VolumeKey.up;
VolumeKeysNotifier(this.volumeEventsService) {
_volumeKeysSubscription = volumeEventsService
VolumeKeysNotifier(this._volumeEventsService) {
_volumeKeysSubscription = _volumeEventsService
.volumeButtonsEventStream()
.map((event) => event == 24 ? VolumeKey.up : VolumeKey.down)
.listen((event) {
@ -19,6 +19,8 @@ class VolumeKeysNotifier extends ChangeNotifier with RouteAware {
}
VolumeKey get value => _value;
@protected
set value(VolumeKey newValue) {
_value = newValue;
notifyListeners();

View file

@ -4,7 +4,7 @@ import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/remote_config_provider.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/settings/components/utils/show_buy_pro_dialog.dart';
import 'package:lightmeter/screens/settings/utils/show_buy_pro_dialog.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
class BuyProListTile extends StatelessWidget {

View file

@ -90,7 +90,7 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
}
void _updateProfileAt(EquipmentProfile data) {
EquipmentProfileProvider.of(context).updateProdile(data);
EquipmentProfileProvider.of(context).updateProfile(data);
}
void _removeProfileAt(EquipmentProfile data) {

View file

@ -28,7 +28,7 @@ Future<void> showBuyProDialog(BuildContext context) {
FilledButton(
onPressed: () {
Navigator.of(context).pop();
IAPProductsProvider.of(context).buy(IAPProductType.paidFeatures);
IAPProductsProvider.maybeOf(context)?.buy(IAPProductType.paidFeatures);
},
child: Text(unlockFeaturesEnabled ? S.of(context).unlock : S.of(context).buy),
),

View file

@ -28,7 +28,7 @@ dependencies:
m3_lightmeter_iap:
git:
url: "https://github.com/vodemn/m3_lightmeter_iap"
ref: v0.6.3
ref: v0.7.0
m3_lightmeter_resources:
git:
url: "https://github.com/vodemn/m3_lightmeter_resources"

7
test/function_mock.dart Normal file
View file

@ -0,0 +1,7 @@
import 'package:mocktail/mocktail.dart';
class _ValueChanged<T> {
void onChanged(T value) {}
}
class MockValueChanged<T> extends Mock implements _ValueChanged<T> {}

View file

@ -260,7 +260,7 @@ class _Application extends StatelessWidget {
ElevatedButton(
key: updateProfileButtonKey(profile.id),
onPressed: () {
EquipmentProfileProvider.of(context).updateProdile(
EquipmentProfileProvider.of(context).updateProfile(
profile.copyWith(
name: '${profile.name} updated',
isoValues: _customProfiles.first.isoValues,

View file

@ -7,9 +7,9 @@ import 'package:lightmeter/screens/metering/communication/event_communication_me
as communication_events;
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart'
as communication_states;
import 'package:lightmeter/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart';
import 'package:lightmeter/screens/metering/event_metering.dart';
import 'package:lightmeter/screens/metering/state_metering.dart';
import 'package:lightmeter/screens/metering/utils/notifier_volume_keys.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';

View file

@ -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>(T value) {}
}
class _MockValueChanged extends Mock implements _ValueChanged {}
void main() {
final functions = _MockValueChanged();
final functions = MockValueChanged<int>();
group(
'onChanged',

View file

@ -0,0 +1,138 @@
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/listener_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();
final storageService = _MockIAPStorageService();
final equipmentProfileProviderKey = GlobalKey<EquipmentProfileProviderState>();
final onDidChangeDependencies = MockValueChanged<EquipmentProfile>();
tearDown(() {
reset(onDidChangeDependencies);
reset(storageService);
});
Future<void> pumpTestWidget(WidgetTester tester) 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.onChanged,
child: Builder(builder: (context) => Text(EquipmentProfiles.selectedOf(context).name)),
),
),
),
),
);
}
testWidgets(
'Trigger `onDidChangeDependencies` by selecting a new profile',
(tester) async {
when(() => storageService.equipmentProfiles).thenReturn(List.from(_customProfiles));
when(() => storageService.selectedEquipmentProfileId).thenReturn('');
await pumpTestWidget(tester);
equipmentProfileProviderKey.currentState!.setProfile(_customProfiles[0]);
await tester.pump();
verify(() => onDidChangeDependencies.onChanged(_customProfiles[0])).called(1);
},
);
testWidgets(
'Trigger `onDidChangeDependencies` by updating the selected profile',
(tester) async {
when(() => storageService.equipmentProfiles).thenReturn(List.from(_customProfiles));
when(() => storageService.selectedEquipmentProfileId).thenReturn(_customProfiles[0].id);
await pumpTestWidget(tester);
final updatedProfile1 = _customProfiles[0].copyWith(name: 'Test 1 updated');
equipmentProfileProviderKey.currentState!.updateProfile(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!.updateProfile(updatedProfile2);
await tester.pump();
verifyNever(() => onDidChangeDependencies.onChanged(updatedProfile2));
},
);
testWidgets(
"Don't trigger `onDidChangeDependencies` by updating the unselected profile",
(tester) async {
when(() => storageService.equipmentProfiles).thenReturn(List.from(_customProfiles));
when(() => storageService.selectedEquipmentProfileId).thenReturn(_customProfiles[0].id);
await pumpTestWidget(tester);
final updatedProfile2 = _customProfiles[1].copyWith(name: 'Test 2 updated');
equipmentProfileProviderKey.currentState!.updateProfile(updatedProfile2);
await tester.pump();
verifyNever(() => onDidChangeDependencies.onChanged(updatedProfile2));
},
);
}
final List<EquipmentProfile> _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,
),
];

View file

@ -0,0 +1,46 @@
import 'dart:async';
import 'package:lightmeter/data/models/volume_action.dart';
import 'package:lightmeter/data/volume_events_service.dart';
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 {}
void main() {
late _MockVolumeEventsService mockVolumeEventsService;
setUp(() {
mockVolumeEventsService = _MockVolumeEventsService();
});
test(
'Listen to `volumeButtonsEventStream()`',
() async {
final StreamController<int> volumeButtonsEvents = StreamController<int>();
when(() => mockVolumeEventsService.volumeButtonsEventStream()).thenAnswer((_) => volumeButtonsEvents.stream);
final volumeKeysNotifier = VolumeKeysNotifier(mockVolumeEventsService);
final functions = MockValueChanged<VolumeKey>();
volumeKeysNotifier.addListener(() => functions.onChanged(volumeKeysNotifier.value));
expect(volumeKeysNotifier.value, VolumeKey.up);
volumeButtonsEvents.add(25);
volumeButtonsEvents.add(25);
volumeButtonsEvents.add(25);
volumeButtonsEvents.add(24);
volumeButtonsEvents.add(24);
volumeButtonsEvents.add(25);
await Future.delayed(Duration.zero);
verify(() => functions.onChanged(VolumeKey.up)).called(2);
verify(() => functions.onChanged(VolumeKey.down)).called(4);
volumeKeysNotifier.removeListener(() => functions.onChanged(volumeKeysNotifier.value));
await volumeKeysNotifier.dispose();
await volumeButtonsEvents.close();
},
);
}

View file

@ -0,0 +1,61 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:lightmeter/data/models/feature.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/remote_config_provider.dart';
import 'package:lightmeter/screens/settings/utils/show_buy_pro_dialog.dart';
import '../../../application_mock.dart';
void main() {
Future<void> pumpApplication(WidgetTester tester) async {
await tester.pumpWidget(
RemoteConfig(
config: const {Feature.unlockProFeaturesText: false},
child: WidgetTestApplicationMock(
child: Builder(
builder: (context) => ElevatedButton(
onPressed: () => showBuyProDialog(context),
child: const SizedBox(),
),
),
),
),
);
await tester.pumpAndSettle();
}
testWidgets(
'`showBuyProDialog` and buy',
(tester) async {
await pumpApplication(tester);
await tester.tap(find.byType(ElevatedButton));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text(S.current.lightmeterPro), findsOneWidget);
expect(find.text(S.current.cancel), findsOneWidget);
expect(find.text(S.current.buy), findsOneWidget);
await tester.tap(find.text(S.current.buy));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
},
);
testWidgets(
'`showBuyProDialog` and cancel',
(tester) async {
await pumpApplication(tester);
await tester.tap(find.byType(ElevatedButton));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text(S.current.lightmeterPro), findsOneWidget);
expect(find.text(S.current.cancel), findsOneWidget);
expect(find.text(S.current.buy), findsOneWidget);
await tester.tap(find.text(S.current.cancel));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
},
);
}