diff --git a/lib/data/light_sensor_service.dart b/lib/data/light_sensor_service.dart index c837dde..a4b95d7 100644 --- a/lib/data/light_sensor_service.dart +++ b/lib/data/light_sensor_service.dart @@ -11,7 +11,7 @@ class LightSensorService { return false; } try { - return await LightSensor.hasSensor ?? false; + return await LightSensor.hasSensor(); } catch (_) { return false; } @@ -21,6 +21,6 @@ class LightSensorService { if (!localPlatform.isAndroid) { return const Stream.empty(); } - return LightSensor.lightSensorStream; + return LightSensor.luxStream(); } } diff --git a/pubspec.yaml b/pubspec.yaml index 934bfbd..215f802 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -22,7 +22,7 @@ dependencies: sdk: flutter intl: 0.18.0 intl_utils: 2.8.2 - light_sensor: 2.0.2 + light_sensor: 3.0.0 m3_lightmeter_iap: git: url: "https://github.com/vodemn/m3_lightmeter_iap" diff --git a/test/data/light_sensor_service_test.dart b/test/data/light_sensor_service_test.dart index d29b50f..f8be45b 100644 --- a/test/data/light_sensor_service_test.dart +++ b/test/data/light_sensor_service_test.dart @@ -1,9 +1,11 @@ -import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:light_sensor/light_sensor.dart'; import 'package:lightmeter/data/light_sensor_service.dart'; import 'package:mocktail/mocktail.dart'; import 'package:platform/platform.dart'; +import '../event_channel_mock.dart'; + class _MockLocalPlatform extends Mock implements LocalPlatform {} void main() { @@ -12,68 +14,44 @@ void main() { late _MockLocalPlatform localPlatform; late LightSensorService service; - const methodChannel = MethodChannel('system_feature'); - // TODO: add event channel mock - //const eventChannel = EventChannel('light.eventChannel'); - setUp(() { localPlatform = _MockLocalPlatform(); service = LightSensorService(localPlatform); }); - tearDown(() { - TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger - .setMockMethodCallHandler(methodChannel, null); - }); - group( 'hasSensor()', () { + void setMockSensorAvailability({required bool hasSensor}) { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + LightSensor.methodChannel, + (methodCall) async { + switch (methodCall.method) { + case "sensor": + return hasSensor; + default: + return null; + } + }, + ); + } + + tearDown(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + LightSensor.methodChannel, + null, + ); + }); + test('true - Android', () async { when(() => localPlatform.isAndroid).thenReturn(true); - TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger - .setMockMethodCallHandler(methodChannel, null); - TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger - .setMockMethodCallHandler(methodChannel, (methodCall) async { - switch (methodCall.method) { - case "sensor": - return true; - default: - return null; - } - }); + setMockSensorAvailability(hasSensor: true); expectLater(service.hasSensor(), completion(true)); }); test('false - Android', () async { when(() => localPlatform.isAndroid).thenReturn(true); - TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger - .setMockMethodCallHandler(methodChannel, null); - TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger - .setMockMethodCallHandler(methodChannel, (methodCall) async { - switch (methodCall.method) { - case "sensor": - return false; - default: - return null; - } - }); - expectLater(service.hasSensor(), completion(false)); - }); - - test('null - Android', () async { - when(() => localPlatform.isAndroid).thenReturn(true); - TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger - .setMockMethodCallHandler(methodChannel, null); - TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger - .setMockMethodCallHandler(methodChannel, (methodCall) async { - switch (methodCall.method) { - case "sensor": - return null; - default: - return null; - } - }); + setMockSensorAvailability(hasSensor: false); expectLater(service.hasSensor(), completion(false)); }); @@ -85,10 +63,18 @@ void main() { ); group('luxStream', () { - // test('Android', () async { - // when(() => localPlatform.isAndroid).thenReturn(true); - // expect(service.luxStream(), const Stream.empty()); - // }); + test('Android', () async { + when(() => localPlatform.isAndroid).thenReturn(true); + final stream = service.luxStream(); + final List result = []; + final subscription = stream.listen(result.add); + await sendMockVolumeAction(LightSensor.eventChannel.name, 100); + await sendMockVolumeAction(LightSensor.eventChannel.name, 150); + await sendMockVolumeAction(LightSensor.eventChannel.name, 150); + await sendMockVolumeAction(LightSensor.eventChannel.name, 200); + expect(result, [100, 150, 150, 200]); + subscription.cancel(); + }); test('iOS', () async { when(() => localPlatform.isAndroid).thenReturn(false); diff --git a/test/data/shared_prefs_service_test.dart b/test/data/shared_prefs_service_test.dart index 0896626..8ec42e1 100644 --- a/test/data/shared_prefs_service_test.dart +++ b/test/data/shared_prefs_service_test.dart @@ -4,6 +4,7 @@ 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/shared_prefs_service.dart'; import 'package:lightmeter/res/theme.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; @@ -99,8 +100,7 @@ void main() { }); test('set', () { - when(() => sharedPreferences.setInt(UserPreferencesService.isoKey, 200)) - .thenAnswer((_) => Future.value(true)); + when(() => sharedPreferences.setInt(UserPreferencesService.isoKey, 200)).thenAnswer((_) => Future.value(true)); service.iso = const IsoValue(200, StopType.full); verify(() => sharedPreferences.setInt(UserPreferencesService.isoKey, 200)).called(1); }); @@ -118,8 +118,7 @@ void main() { }); test('set', () { - when(() => sharedPreferences.setInt(UserPreferencesService.ndFilterKey, 0)) - .thenAnswer((_) => Future.value(true)); + when(() => sharedPreferences.setInt(UserPreferencesService.ndFilterKey, 0)).thenAnswer((_) => Future.value(true)); service.ndFilter = const NdValue(0); verify(() => sharedPreferences.setInt(UserPreferencesService.ndFilterKey, 0)).called(1); }); @@ -175,8 +174,7 @@ void main() { }); test('set', () { - when(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0)) - .thenAnswer((_) => Future.value(true)); + when(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0)).thenAnswer((_) => Future.value(true)); service.stopType = StopType.full; verify(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0)).called(1); }); @@ -253,6 +251,26 @@ void main() { verify(() => sharedPreferences.setBool(UserPreferencesService.hapticsKey, false)).called(1); }); }); + group('volumeAction', () { + test('get default', () { + when(() => sharedPreferences.getBool(UserPreferencesService.volumeActionKey)).thenReturn(null); + expect(service.volumeAction, VolumeAction.shutter); + }); + + test('get', () { + when(() => sharedPreferences.getString(UserPreferencesService.volumeActionKey)) + .thenReturn(VolumeAction.shutter.toString()); + expect(service.volumeAction, VolumeAction.shutter); + }); + + test('set', () { + when(() => sharedPreferences.setString(UserPreferencesService.volumeActionKey, VolumeAction.shutter.toString())) + .thenAnswer((_) => Future.value(true)); + service.volumeAction = VolumeAction.shutter; + verify(() => sharedPreferences.setString(UserPreferencesService.volumeActionKey, VolumeAction.shutter.toString())) + .called(1); + }); + }); group('locale', () { test('get default', () { @@ -261,8 +279,7 @@ void main() { }); test('get', () { - when(() => sharedPreferences.getString(UserPreferencesService.localeKey)) - .thenReturn('SupportedLocale.ru'); + when(() => sharedPreferences.getString(UserPreferencesService.localeKey)).thenReturn('SupportedLocale.ru'); expect(service.locale, SupportedLocale.ru); }); @@ -279,14 +296,12 @@ void main() { group('cameraEvCalibration', () { test('get default', () { - when(() => sharedPreferences.getDouble(UserPreferencesService.cameraEvCalibrationKey)) - .thenReturn(null); + when(() => sharedPreferences.getDouble(UserPreferencesService.cameraEvCalibrationKey)).thenReturn(null); expect(service.cameraEvCalibration, 0.0); }); test('get', () { - when(() => sharedPreferences.getDouble(UserPreferencesService.cameraEvCalibrationKey)) - .thenReturn(2.0); + when(() => sharedPreferences.getDouble(UserPreferencesService.cameraEvCalibrationKey)).thenReturn(2.0); expect(service.cameraEvCalibration, 2.0); }); @@ -303,14 +318,12 @@ void main() { group('lightSensorEvCalibration', () { test('get default', () { - when(() => sharedPreferences.getDouble(UserPreferencesService.lightSensorEvCalibrationKey)) - .thenReturn(null); + when(() => sharedPreferences.getDouble(UserPreferencesService.lightSensorEvCalibrationKey)).thenReturn(null); expect(service.lightSensorEvCalibration, 0.0); }); test('get', () { - when(() => sharedPreferences.getDouble(UserPreferencesService.lightSensorEvCalibrationKey)) - .thenReturn(2.0); + when(() => sharedPreferences.getDouble(UserPreferencesService.lightSensorEvCalibrationKey)).thenReturn(2.0); expect(service.lightSensorEvCalibration, 2.0); }); @@ -354,8 +367,7 @@ void main() { }); test('get', () { - when(() => sharedPreferences.getInt(UserPreferencesService.primaryColorKey)) - .thenReturn(0xff9c27b0); + when(() => sharedPreferences.getInt(UserPreferencesService.primaryColorKey)).thenReturn(0xff9c27b0); expect(service.primaryColor, primaryColorsList[2]); }); @@ -372,14 +384,12 @@ void main() { group('dynamicColor', () { test('get default', () { - when(() => sharedPreferences.getBool(UserPreferencesService.dynamicColorKey)) - .thenReturn(null); + when(() => sharedPreferences.getBool(UserPreferencesService.dynamicColorKey)).thenReturn(null); expect(service.dynamicColor, false); }); test('get', () { - when(() => sharedPreferences.getBool(UserPreferencesService.dynamicColorKey)) - .thenReturn(true); + when(() => sharedPreferences.getBool(UserPreferencesService.dynamicColorKey)).thenReturn(true); expect(service.dynamicColor, true); }); @@ -387,8 +397,7 @@ void main() { when(() => sharedPreferences.setBool(UserPreferencesService.dynamicColorKey, false)) .thenAnswer((_) => Future.value(true)); service.dynamicColor = false; - verify(() => sharedPreferences.setBool(UserPreferencesService.dynamicColorKey, false)) - .called(1); + verify(() => sharedPreferences.setBool(UserPreferencesService.dynamicColorKey, false)).called(1); }); }); } diff --git a/test/data/volume_events_service_test.dart b/test/data/volume_events_service_test.dart index f9ef3d6..f574e50 100644 --- a/test/data/volume_events_service_test.dart +++ b/test/data/volume_events_service_test.dart @@ -4,6 +4,8 @@ import 'package:lightmeter/data/volume_events_service.dart'; import 'package:mocktail/mocktail.dart'; import 'package:platform/platform.dart'; +import '../event_channel_mock.dart'; + class _MockLocalPlatform extends Mock implements LocalPlatform {} void main() { @@ -60,10 +62,18 @@ void main() { }); group('volumeButtonsEventStream', () { - // test('Android', () async { - // when(() => localPlatform.isAndroid).thenReturn(true); - // expect(service.volumeButtonsEventStream(), const Stream.empty()); - // }); + test('Android', () async { + when(() => localPlatform.isAndroid).thenReturn(true); + final stream = service.volumeButtonsEventStream(); + final List result = []; + final subscription = stream.listen(result.add); + await sendMockVolumeAction(VolumeEventsService.volumeEventsChannel.name, 24); + await sendMockVolumeAction(VolumeEventsService.volumeEventsChannel.name, 25); + await sendMockVolumeAction(VolumeEventsService.volumeEventsChannel.name, 20); + await sendMockVolumeAction(VolumeEventsService.volumeEventsChannel.name, 24); + expect(result, [24, 25, 24]); + subscription.cancel(); + }); test('iOS', () async { when(() => localPlatform.isAndroid).thenReturn(false); diff --git a/test/event_channel_mock.dart b/test/event_channel_mock.dart new file mode 100644 index 0000000..ee8cbc7 --- /dev/null +++ b/test/event_channel_mock.dart @@ -0,0 +1,10 @@ +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; + +Future sendMockVolumeAction(String channelName, int keyCode) async { + await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage( + channelName, + const StandardMethodCodec().encodeSuccessEnvelope(keyCode), + (ByteData? data) {}, + ); +}