From 1f88c7d9a4ef710af161657bc0aac0136aeaa8a6 Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 5 Jul 2023 16:29:33 +0200 Subject: [PATCH] used "platform" package to mock `isAndroid` --- lib/data/light_sensor_service.dart | 8 ++++++- lib/data/volume_events_service.dart | 11 +++++---- lib/interactors/metering_interactor.dart | 12 ++-------- lib/providers.dart | 9 ++++--- pubspec.yaml | 1 + test/data/light_sensor_service_test.dart | 23 ++++++++++++++---- test/data/volume_events_service_test.dart | 29 ++++++++++++++++++++--- 7 files changed, 65 insertions(+), 28 deletions(-) diff --git a/lib/data/light_sensor_service.dart b/lib/data/light_sensor_service.dart index 69b55c5..2fc94c5 100644 --- a/lib/data/light_sensor_service.dart +++ b/lib/data/light_sensor_service.dart @@ -1,9 +1,15 @@ import 'package:light_sensor/light_sensor.dart'; +import 'package:platform/platform.dart'; class LightSensorService { - const LightSensorService(); + final LocalPlatform localPlatform; + + const LightSensorService(this.localPlatform); Future hasSensor() async { + if (!localPlatform.isAndroid) { + return false; + } try { return await LightSensor.hasSensor ?? false; } catch (_) { diff --git a/lib/data/volume_events_service.dart b/lib/data/volume_events_service.dart index adb422e..d57936a 100644 --- a/lib/data/volume_events_service.dart +++ b/lib/data/volume_events_service.dart @@ -1,21 +1,22 @@ -import 'dart:io'; - import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; +import 'package:platform/platform.dart'; class VolumeEventsService { + final LocalPlatform localPlatform; + @visibleForTesting static const volumeHandlingChannel = MethodChannel("com.vodemn.lightmeter/volumeHandling"); @visibleForTesting static const volumeEventsChannel = EventChannel("com.vodemn.lightmeter/volumeEvents"); - const VolumeEventsService(); + const VolumeEventsService(this.localPlatform); /// If set to `false` we allow system to handle key events. /// Returns current status of volume handling. Future setVolumeHandling(bool enableHandling) async { - if (!Platform.isAndroid) { + if (!localPlatform.isAndroid) { return false; } return volumeHandlingChannel @@ -28,7 +29,7 @@ class VolumeEventsService { /// KEYCODE_VOLUME_DOWN = 25; /// pressed Stream volumeButtonsEventStream() { - if (!Platform.isAndroid) { + if (!localPlatform.isAndroid) { return const Stream.empty(); } return volumeEventsChannel diff --git a/lib/interactors/metering_interactor.dart b/lib/interactors/metering_interactor.dart index 02cbde9..34b03ec 100644 --- a/lib/interactors/metering_interactor.dart +++ b/lib/interactors/metering_interactor.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:app_settings/app_settings.dart'; import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; @@ -49,7 +47,7 @@ class MeteringInteractor { set film(Film value) => _userPreferencesService.film = value; VolumeAction get volumeAction => _userPreferencesService.volumeAction; - + /// Executes vibration if haptics are enabled in settings Future quickVibration() async { if (_userPreferencesService.haptics) await _hapticsService.quickVibration(); @@ -81,13 +79,7 @@ class MeteringInteractor { AppSettings.openAppSettings(); } - Future hasAmbientLightSensor() async { - if (Platform.isAndroid) { - return _lightSensorService.hasSensor(); - } else { - return false; - } - } + Future hasAmbientLightSensor() async => _lightSensorService.hasSensor(); Stream luxStream() => _lightSensorService.luxStream(); } diff --git a/lib/providers.dart b/lib/providers.dart index cede377..d7907ab 100644 --- a/lib/providers.dart +++ b/lib/providers.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:flutter/material.dart'; import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; @@ -15,6 +13,7 @@ import 'package:lightmeter/providers/stop_type_provider.dart'; import 'package:lightmeter/providers/supported_locale_provider.dart'; import 'package:lightmeter/providers/theme_provider.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:platform/platform.dart'; import 'package:shared_preferences/shared_preferences.dart'; class LightmeterProviders extends StatelessWidget { @@ -28,7 +27,7 @@ class LightmeterProviders extends StatelessWidget { return FutureBuilder( future: Future.wait([ SharedPreferences.getInstance(), - if (Platform.isAndroid) const LightSensorService().hasSensor() else Future.value(false), + const LightSensorService(LocalPlatform()).hasSensor(), ]), builder: (_, snapshot) { if (snapshot.data != null) { @@ -37,13 +36,13 @@ class LightmeterProviders extends StatelessWidget { child: InheritedWidgetBase( data: UserPreferencesService(snapshot.data![0] as SharedPreferences), child: InheritedWidgetBase( - data: const LightSensorService(), + data: const LightSensorService(LocalPlatform()), child: InheritedWidgetBase( data: const CaffeineService(), child: InheritedWidgetBase( data: const HapticsService(), child: InheritedWidgetBase( - data: const VolumeEventsService(), + data: const VolumeEventsService(LocalPlatform()), child: InheritedWidgetBase( data: const PermissionsService(), child: MeteringScreenLayoutProvider( diff --git a/pubspec.yaml b/pubspec.yaml index 8a230c8..bd72758 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,7 @@ dependencies: material_color_utilities: 0.2.0 package_info_plus: 4.0.1 permission_handler: 10.2.0 + platform: 3.1.0 shared_preferences: 2.1.1 url_launcher: 6.1.11 uuid: 3.0.7 diff --git a/test/data/light_sensor_service_test.dart b/test/data/light_sensor_service_test.dart index 1256e18..f30bf11 100644 --- a/test/data/light_sensor_service_test.dart +++ b/test/data/light_sensor_service_test.dart @@ -1,10 +1,15 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:lightmeter/data/light_sensor_service.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:platform/platform.dart'; + +class _MockLocalPlatform extends Mock implements LocalPlatform {} void main() { TestWidgetsFlutterBinding.ensureInitialized(); + late _MockLocalPlatform localPlatform; late LightSensorService service; const methodChannel = MethodChannel('system_feature'); @@ -12,7 +17,8 @@ void main() { //const eventChannel = EventChannel('light.eventChannel'); setUp(() { - service = const LightSensorService(); + localPlatform = _MockLocalPlatform(); + service = LightSensorService(localPlatform); }); tearDown(() { @@ -23,7 +29,8 @@ void main() { group( 'hasSensor()', () { - test('true', () async { + test('true - Android', () async { + when(() => localPlatform.isAndroid).thenReturn(true); TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .setMockMethodCallHandler(methodChannel, null); TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger @@ -38,7 +45,8 @@ void main() { expectLater(service.hasSensor(), completion(true)); }); - test('false', () async { + test('false - Android', () async { + when(() => localPlatform.isAndroid).thenReturn(true); TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .setMockMethodCallHandler(methodChannel, null); TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger @@ -52,7 +60,9 @@ void main() { }); expectLater(service.hasSensor(), completion(false)); }); - test('null', () async { + + test('null - Android', () async { + when(() => localPlatform.isAndroid).thenReturn(true); TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .setMockMethodCallHandler(methodChannel, null); TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger @@ -66,6 +76,11 @@ void main() { }); expectLater(service.hasSensor(), completion(false)); }); + + test('false - iOS', () async { + when(() => localPlatform.isAndroid).thenReturn(false); + expectLater(service.hasSensor(), completion(false)); + }); }, ); } diff --git a/test/data/volume_events_service_test.dart b/test/data/volume_events_service_test.dart index ab4f18a..a6f5ae7 100644 --- a/test/data/volume_events_service_test.dart +++ b/test/data/volume_events_service_test.dart @@ -1,11 +1,17 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:lightmeter/data/volume_events_service.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:platform/platform.dart'; + +class _MockLocalPlatform extends Mock implements LocalPlatform {} void main() { TestWidgetsFlutterBinding.ensureInitialized(); + late _MockLocalPlatform localPlatform; late VolumeEventsService service; + Future? methodCallSuccessHandler(MethodCall methodCall) async { switch (methodCall.method) { case "setVolumeHandling": @@ -16,7 +22,8 @@ void main() { } setUp(() { - service = const VolumeEventsService(); + localPlatform = _MockLocalPlatform(); + service = VolumeEventsService(localPlatform); TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( VolumeEventsService.volumeHandlingChannel, methodCallSuccessHandler, @@ -31,8 +38,24 @@ void main() { }); group('setVolumeHandling', () { - test('true', () async => expectLater(service.setVolumeHandling(true), completion(true))); + test('true - Android', () async { + when(() => localPlatform.isAndroid).thenReturn(true); + expectLater(service.setVolumeHandling(true), completion(true)); + }); - test('false', () async => expectLater(service.setVolumeHandling(false), completion(false))); + test('true - iOS', () async { + when(() => localPlatform.isAndroid).thenReturn(false); + expectLater(service.setVolumeHandling(true), completion(false)); + }); + + test('false - Android', () async { + when(() => localPlatform.isAndroid).thenReturn(true); + expectLater(service.setVolumeHandling(false), completion(false)); + }); + + test('false - iOS', () async { + when(() => localPlatform.isAndroid).thenReturn(false); + expectLater(service.setVolumeHandling(false), completion(false)); + }); }); }