mirror of
https://github.com/vodemn/m3_lightmeter.git
synced 2024-11-26 01:10:39 +00:00
used "platform" package to mock isAndroid
This commit is contained in:
parent
d17894d4a5
commit
1f88c7d9a4
7 changed files with 65 additions and 28 deletions
|
@ -1,9 +1,15 @@
|
||||||
import 'package:light_sensor/light_sensor.dart';
|
import 'package:light_sensor/light_sensor.dart';
|
||||||
|
import 'package:platform/platform.dart';
|
||||||
|
|
||||||
class LightSensorService {
|
class LightSensorService {
|
||||||
const LightSensorService();
|
final LocalPlatform localPlatform;
|
||||||
|
|
||||||
|
const LightSensorService(this.localPlatform);
|
||||||
|
|
||||||
Future<bool> hasSensor() async {
|
Future<bool> hasSensor() async {
|
||||||
|
if (!localPlatform.isAndroid) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return await LightSensor.hasSensor ?? false;
|
return await LightSensor.hasSensor ?? false;
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
import 'dart:io';
|
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:platform/platform.dart';
|
||||||
|
|
||||||
class VolumeEventsService {
|
class VolumeEventsService {
|
||||||
|
final LocalPlatform localPlatform;
|
||||||
|
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
static const volumeHandlingChannel = MethodChannel("com.vodemn.lightmeter/volumeHandling");
|
static const volumeHandlingChannel = MethodChannel("com.vodemn.lightmeter/volumeHandling");
|
||||||
|
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
static const volumeEventsChannel = EventChannel("com.vodemn.lightmeter/volumeEvents");
|
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.
|
/// If set to `false` we allow system to handle key events.
|
||||||
/// Returns current status of volume handling.
|
/// Returns current status of volume handling.
|
||||||
Future<bool> setVolumeHandling(bool enableHandling) async {
|
Future<bool> setVolumeHandling(bool enableHandling) async {
|
||||||
if (!Platform.isAndroid) {
|
if (!localPlatform.isAndroid) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return volumeHandlingChannel
|
return volumeHandlingChannel
|
||||||
|
@ -28,7 +29,7 @@ class VolumeEventsService {
|
||||||
/// KEYCODE_VOLUME_DOWN = 25;
|
/// KEYCODE_VOLUME_DOWN = 25;
|
||||||
/// pressed
|
/// pressed
|
||||||
Stream<int> volumeButtonsEventStream() {
|
Stream<int> volumeButtonsEventStream() {
|
||||||
if (!Platform.isAndroid) {
|
if (!localPlatform.isAndroid) {
|
||||||
return const Stream.empty();
|
return const Stream.empty();
|
||||||
}
|
}
|
||||||
return volumeEventsChannel
|
return volumeEventsChannel
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import 'dart:io';
|
|
||||||
|
|
||||||
import 'package:app_settings/app_settings.dart';
|
import 'package:app_settings/app_settings.dart';
|
||||||
import 'package:lightmeter/data/caffeine_service.dart';
|
import 'package:lightmeter/data/caffeine_service.dart';
|
||||||
import 'package:lightmeter/data/haptics_service.dart';
|
import 'package:lightmeter/data/haptics_service.dart';
|
||||||
|
@ -81,13 +79,7 @@ class MeteringInteractor {
|
||||||
AppSettings.openAppSettings();
|
AppSettings.openAppSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> hasAmbientLightSensor() async {
|
Future<bool> hasAmbientLightSensor() async => _lightSensorService.hasSensor();
|
||||||
if (Platform.isAndroid) {
|
|
||||||
return _lightSensorService.hasSensor();
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream<int> luxStream() => _lightSensorService.luxStream();
|
Stream<int> luxStream() => _lightSensorService.luxStream();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import 'dart:io';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:lightmeter/data/caffeine_service.dart';
|
import 'package:lightmeter/data/caffeine_service.dart';
|
||||||
import 'package:lightmeter/data/haptics_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/supported_locale_provider.dart';
|
||||||
import 'package:lightmeter/providers/theme_provider.dart';
|
import 'package:lightmeter/providers/theme_provider.dart';
|
||||||
import 'package:lightmeter/utils/inherited_generics.dart';
|
import 'package:lightmeter/utils/inherited_generics.dart';
|
||||||
|
import 'package:platform/platform.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class LightmeterProviders extends StatelessWidget {
|
class LightmeterProviders extends StatelessWidget {
|
||||||
|
@ -28,7 +27,7 @@ class LightmeterProviders extends StatelessWidget {
|
||||||
return FutureBuilder(
|
return FutureBuilder(
|
||||||
future: Future.wait([
|
future: Future.wait([
|
||||||
SharedPreferences.getInstance(),
|
SharedPreferences.getInstance(),
|
||||||
if (Platform.isAndroid) const LightSensorService().hasSensor() else Future.value(false),
|
const LightSensorService(LocalPlatform()).hasSensor(),
|
||||||
]),
|
]),
|
||||||
builder: (_, snapshot) {
|
builder: (_, snapshot) {
|
||||||
if (snapshot.data != null) {
|
if (snapshot.data != null) {
|
||||||
|
@ -37,13 +36,13 @@ class LightmeterProviders extends StatelessWidget {
|
||||||
child: InheritedWidgetBase<UserPreferencesService>(
|
child: InheritedWidgetBase<UserPreferencesService>(
|
||||||
data: UserPreferencesService(snapshot.data![0] as SharedPreferences),
|
data: UserPreferencesService(snapshot.data![0] as SharedPreferences),
|
||||||
child: InheritedWidgetBase<LightSensorService>(
|
child: InheritedWidgetBase<LightSensorService>(
|
||||||
data: const LightSensorService(),
|
data: const LightSensorService(LocalPlatform()),
|
||||||
child: InheritedWidgetBase<CaffeineService>(
|
child: InheritedWidgetBase<CaffeineService>(
|
||||||
data: const CaffeineService(),
|
data: const CaffeineService(),
|
||||||
child: InheritedWidgetBase<HapticsService>(
|
child: InheritedWidgetBase<HapticsService>(
|
||||||
data: const HapticsService(),
|
data: const HapticsService(),
|
||||||
child: InheritedWidgetBase<VolumeEventsService>(
|
child: InheritedWidgetBase<VolumeEventsService>(
|
||||||
data: const VolumeEventsService(),
|
data: const VolumeEventsService(LocalPlatform()),
|
||||||
child: InheritedWidgetBase<PermissionsService>(
|
child: InheritedWidgetBase<PermissionsService>(
|
||||||
data: const PermissionsService(),
|
data: const PermissionsService(),
|
||||||
child: MeteringScreenLayoutProvider(
|
child: MeteringScreenLayoutProvider(
|
||||||
|
|
|
@ -30,6 +30,7 @@ dependencies:
|
||||||
material_color_utilities: 0.2.0
|
material_color_utilities: 0.2.0
|
||||||
package_info_plus: 4.0.1
|
package_info_plus: 4.0.1
|
||||||
permission_handler: 10.2.0
|
permission_handler: 10.2.0
|
||||||
|
platform: 3.1.0
|
||||||
shared_preferences: 2.1.1
|
shared_preferences: 2.1.1
|
||||||
url_launcher: 6.1.11
|
url_launcher: 6.1.11
|
||||||
uuid: 3.0.7
|
uuid: 3.0.7
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:lightmeter/data/light_sensor_service.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() {
|
void main() {
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
late _MockLocalPlatform localPlatform;
|
||||||
late LightSensorService service;
|
late LightSensorService service;
|
||||||
|
|
||||||
const methodChannel = MethodChannel('system_feature');
|
const methodChannel = MethodChannel('system_feature');
|
||||||
|
@ -12,7 +17,8 @@ void main() {
|
||||||
//const eventChannel = EventChannel('light.eventChannel');
|
//const eventChannel = EventChannel('light.eventChannel');
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
service = const LightSensorService();
|
localPlatform = _MockLocalPlatform();
|
||||||
|
service = LightSensorService(localPlatform);
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDown(() {
|
tearDown(() {
|
||||||
|
@ -23,7 +29,8 @@ void main() {
|
||||||
group(
|
group(
|
||||||
'hasSensor()',
|
'hasSensor()',
|
||||||
() {
|
() {
|
||||||
test('true', () async {
|
test('true - Android', () async {
|
||||||
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||||
.setMockMethodCallHandler(methodChannel, null);
|
.setMockMethodCallHandler(methodChannel, null);
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||||
|
@ -38,7 +45,8 @@ void main() {
|
||||||
expectLater(service.hasSensor(), completion(true));
|
expectLater(service.hasSensor(), completion(true));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('false', () async {
|
test('false - Android', () async {
|
||||||
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||||
.setMockMethodCallHandler(methodChannel, null);
|
.setMockMethodCallHandler(methodChannel, null);
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||||
|
@ -52,7 +60,9 @@ void main() {
|
||||||
});
|
});
|
||||||
expectLater(service.hasSensor(), completion(false));
|
expectLater(service.hasSensor(), completion(false));
|
||||||
});
|
});
|
||||||
test('null', () async {
|
|
||||||
|
test('null - Android', () async {
|
||||||
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||||
.setMockMethodCallHandler(methodChannel, null);
|
.setMockMethodCallHandler(methodChannel, null);
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||||
|
@ -66,6 +76,11 @@ void main() {
|
||||||
});
|
});
|
||||||
expectLater(service.hasSensor(), completion(false));
|
expectLater(service.hasSensor(), completion(false));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('false - iOS', () async {
|
||||||
|
when(() => localPlatform.isAndroid).thenReturn(false);
|
||||||
|
expectLater(service.hasSensor(), completion(false));
|
||||||
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:lightmeter/data/volume_events_service.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() {
|
void main() {
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
late _MockLocalPlatform localPlatform;
|
||||||
late VolumeEventsService service;
|
late VolumeEventsService service;
|
||||||
|
|
||||||
Future<Object?>? methodCallSuccessHandler(MethodCall methodCall) async {
|
Future<Object?>? methodCallSuccessHandler(MethodCall methodCall) async {
|
||||||
switch (methodCall.method) {
|
switch (methodCall.method) {
|
||||||
case "setVolumeHandling":
|
case "setVolumeHandling":
|
||||||
|
@ -16,7 +22,8 @@ void main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
service = const VolumeEventsService();
|
localPlatform = _MockLocalPlatform();
|
||||||
|
service = VolumeEventsService(localPlatform);
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||||
VolumeEventsService.volumeHandlingChannel,
|
VolumeEventsService.volumeHandlingChannel,
|
||||||
methodCallSuccessHandler,
|
methodCallSuccessHandler,
|
||||||
|
@ -31,8 +38,24 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
group('setVolumeHandling', () {
|
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));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue