used "platform" package to mock isAndroid

This commit is contained in:
Vadim 2023-07-05 16:29:33 +02:00
parent d17894d4a5
commit 1f88c7d9a4
7 changed files with 65 additions and 28 deletions

View file

@ -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<bool> hasSensor() async {
if (!localPlatform.isAndroid) {
return false;
}
try {
return await LightSensor.hasSensor ?? false;
} catch (_) {

View file

@ -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<bool> 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<int> volumeButtonsEventStream() {
if (!Platform.isAndroid) {
if (!localPlatform.isAndroid) {
return const Stream.empty();
}
return volumeEventsChannel

View file

@ -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<void> quickVibration() async {
if (_userPreferencesService.haptics) await _hapticsService.quickVibration();
@ -81,13 +79,7 @@ class MeteringInteractor {
AppSettings.openAppSettings();
}
Future<bool> hasAmbientLightSensor() async {
if (Platform.isAndroid) {
return _lightSensorService.hasSensor();
} else {
return false;
}
}
Future<bool> hasAmbientLightSensor() async => _lightSensorService.hasSensor();
Stream<int> luxStream() => _lightSensorService.luxStream();
}

View file

@ -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<UserPreferencesService>(
data: UserPreferencesService(snapshot.data![0] as SharedPreferences),
child: InheritedWidgetBase<LightSensorService>(
data: const LightSensorService(),
data: const LightSensorService(LocalPlatform()),
child: InheritedWidgetBase<CaffeineService>(
data: const CaffeineService(),
child: InheritedWidgetBase<HapticsService>(
data: const HapticsService(),
child: InheritedWidgetBase<VolumeEventsService>(
data: const VolumeEventsService(),
data: const VolumeEventsService(LocalPlatform()),
child: InheritedWidgetBase<PermissionsService>(
data: const PermissionsService(),
child: MeteringScreenLayoutProvider(

View file

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

View file

@ -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));
});
},
);
}

View file

@ -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<Object?>? 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));
});
});
}