mirror of
https://github.com/vodemn/m3_lightmeter.git
synced 2025-05-09 09:40:42 +00:00
implemented platform mocks for unit tests
This commit is contained in:
parent
3c4d959cc6
commit
9baf1da157
6 changed files with 98 additions and 83 deletions
|
@ -11,7 +11,7 @@ class LightSensorService {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return await LightSensor.hasSensor ?? false;
|
return await LightSensor.hasSensor();
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,6 @@ class LightSensorService {
|
||||||
if (!localPlatform.isAndroid) {
|
if (!localPlatform.isAndroid) {
|
||||||
return const Stream<int>.empty();
|
return const Stream<int>.empty();
|
||||||
}
|
}
|
||||||
return LightSensor.lightSensorStream;
|
return LightSensor.luxStream();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ dependencies:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
intl: 0.18.0
|
intl: 0.18.0
|
||||||
intl_utils: 2.8.2
|
intl_utils: 2.8.2
|
||||||
light_sensor: 2.0.2
|
light_sensor: 3.0.0
|
||||||
m3_lightmeter_iap:
|
m3_lightmeter_iap:
|
||||||
git:
|
git:
|
||||||
url: "https://github.com/vodemn/m3_lightmeter_iap"
|
url: "https://github.com/vodemn/m3_lightmeter_iap"
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.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:lightmeter/data/light_sensor_service.dart';
|
||||||
import 'package:mocktail/mocktail.dart';
|
import 'package:mocktail/mocktail.dart';
|
||||||
import 'package:platform/platform.dart';
|
import 'package:platform/platform.dart';
|
||||||
|
|
||||||
|
import '../event_channel_mock.dart';
|
||||||
|
|
||||||
class _MockLocalPlatform extends Mock implements LocalPlatform {}
|
class _MockLocalPlatform extends Mock implements LocalPlatform {}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
@ -12,68 +14,44 @@ void main() {
|
||||||
late _MockLocalPlatform localPlatform;
|
late _MockLocalPlatform localPlatform;
|
||||||
late LightSensorService service;
|
late LightSensorService service;
|
||||||
|
|
||||||
const methodChannel = MethodChannel('system_feature');
|
|
||||||
// TODO: add event channel mock
|
|
||||||
//const eventChannel = EventChannel('light.eventChannel');
|
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
localPlatform = _MockLocalPlatform();
|
localPlatform = _MockLocalPlatform();
|
||||||
service = LightSensorService(localPlatform);
|
service = LightSensorService(localPlatform);
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDown(() {
|
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
||||||
.setMockMethodCallHandler(methodChannel, null);
|
|
||||||
});
|
|
||||||
|
|
||||||
group(
|
group(
|
||||||
'hasSensor()',
|
'hasSensor()',
|
||||||
() {
|
() {
|
||||||
test('true - Android', () async {
|
void setMockSensorAvailability({required bool hasSensor}) {
|
||||||
when(() => localPlatform.isAndroid).thenReturn(true);
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
LightSensor.methodChannel,
|
||||||
.setMockMethodCallHandler(methodChannel, null);
|
(methodCall) async {
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
||||||
.setMockMethodCallHandler(methodChannel, (methodCall) async {
|
|
||||||
switch (methodCall.method) {
|
switch (methodCall.method) {
|
||||||
case "sensor":
|
case "sensor":
|
||||||
return true;
|
return hasSensor;
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
tearDown(() {
|
||||||
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||||
|
LightSensor.methodChannel,
|
||||||
|
null,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('true - Android', () async {
|
||||||
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
||||||
|
setMockSensorAvailability(hasSensor: true);
|
||||||
expectLater(service.hasSensor(), completion(true));
|
expectLater(service.hasSensor(), completion(true));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('false - Android', () async {
|
test('false - Android', () async {
|
||||||
when(() => localPlatform.isAndroid).thenReturn(true);
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
||||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
setMockSensorAvailability(hasSensor: false);
|
||||||
.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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
expectLater(service.hasSensor(), completion(false));
|
expectLater(service.hasSensor(), completion(false));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -85,10 +63,18 @@ void main() {
|
||||||
);
|
);
|
||||||
|
|
||||||
group('luxStream', () {
|
group('luxStream', () {
|
||||||
// test('Android', () async {
|
test('Android', () async {
|
||||||
// when(() => localPlatform.isAndroid).thenReturn(true);
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
||||||
// expect(service.luxStream(), const Stream.empty());
|
final stream = service.luxStream();
|
||||||
// });
|
final List<int> 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 {
|
test('iOS', () async {
|
||||||
when(() => localPlatform.isAndroid).thenReturn(false);
|
when(() => localPlatform.isAndroid).thenReturn(false);
|
||||||
|
|
|
@ -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/metering_screen_layout_config.dart';
|
||||||
import 'package:lightmeter/data/models/supported_locale.dart';
|
import 'package:lightmeter/data/models/supported_locale.dart';
|
||||||
import 'package:lightmeter/data/models/theme_type.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/data/shared_prefs_service.dart';
|
||||||
import 'package:lightmeter/res/theme.dart';
|
import 'package:lightmeter/res/theme.dart';
|
||||||
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
||||||
|
@ -99,8 +100,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('set', () {
|
test('set', () {
|
||||||
when(() => sharedPreferences.setInt(UserPreferencesService.isoKey, 200))
|
when(() => sharedPreferences.setInt(UserPreferencesService.isoKey, 200)).thenAnswer((_) => Future.value(true));
|
||||||
.thenAnswer((_) => Future.value(true));
|
|
||||||
service.iso = const IsoValue(200, StopType.full);
|
service.iso = const IsoValue(200, StopType.full);
|
||||||
verify(() => sharedPreferences.setInt(UserPreferencesService.isoKey, 200)).called(1);
|
verify(() => sharedPreferences.setInt(UserPreferencesService.isoKey, 200)).called(1);
|
||||||
});
|
});
|
||||||
|
@ -118,8 +118,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('set', () {
|
test('set', () {
|
||||||
when(() => sharedPreferences.setInt(UserPreferencesService.ndFilterKey, 0))
|
when(() => sharedPreferences.setInt(UserPreferencesService.ndFilterKey, 0)).thenAnswer((_) => Future.value(true));
|
||||||
.thenAnswer((_) => Future.value(true));
|
|
||||||
service.ndFilter = const NdValue(0);
|
service.ndFilter = const NdValue(0);
|
||||||
verify(() => sharedPreferences.setInt(UserPreferencesService.ndFilterKey, 0)).called(1);
|
verify(() => sharedPreferences.setInt(UserPreferencesService.ndFilterKey, 0)).called(1);
|
||||||
});
|
});
|
||||||
|
@ -175,8 +174,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('set', () {
|
test('set', () {
|
||||||
when(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0))
|
when(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0)).thenAnswer((_) => Future.value(true));
|
||||||
.thenAnswer((_) => Future.value(true));
|
|
||||||
service.stopType = StopType.full;
|
service.stopType = StopType.full;
|
||||||
verify(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0)).called(1);
|
verify(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0)).called(1);
|
||||||
});
|
});
|
||||||
|
@ -253,6 +251,26 @@ void main() {
|
||||||
verify(() => sharedPreferences.setBool(UserPreferencesService.hapticsKey, false)).called(1);
|
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', () {
|
group('locale', () {
|
||||||
test('get default', () {
|
test('get default', () {
|
||||||
|
@ -261,8 +279,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get', () {
|
test('get', () {
|
||||||
when(() => sharedPreferences.getString(UserPreferencesService.localeKey))
|
when(() => sharedPreferences.getString(UserPreferencesService.localeKey)).thenReturn('SupportedLocale.ru');
|
||||||
.thenReturn('SupportedLocale.ru');
|
|
||||||
expect(service.locale, SupportedLocale.ru);
|
expect(service.locale, SupportedLocale.ru);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -279,14 +296,12 @@ void main() {
|
||||||
|
|
||||||
group('cameraEvCalibration', () {
|
group('cameraEvCalibration', () {
|
||||||
test('get default', () {
|
test('get default', () {
|
||||||
when(() => sharedPreferences.getDouble(UserPreferencesService.cameraEvCalibrationKey))
|
when(() => sharedPreferences.getDouble(UserPreferencesService.cameraEvCalibrationKey)).thenReturn(null);
|
||||||
.thenReturn(null);
|
|
||||||
expect(service.cameraEvCalibration, 0.0);
|
expect(service.cameraEvCalibration, 0.0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get', () {
|
test('get', () {
|
||||||
when(() => sharedPreferences.getDouble(UserPreferencesService.cameraEvCalibrationKey))
|
when(() => sharedPreferences.getDouble(UserPreferencesService.cameraEvCalibrationKey)).thenReturn(2.0);
|
||||||
.thenReturn(2.0);
|
|
||||||
expect(service.cameraEvCalibration, 2.0);
|
expect(service.cameraEvCalibration, 2.0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -303,14 +318,12 @@ void main() {
|
||||||
|
|
||||||
group('lightSensorEvCalibration', () {
|
group('lightSensorEvCalibration', () {
|
||||||
test('get default', () {
|
test('get default', () {
|
||||||
when(() => sharedPreferences.getDouble(UserPreferencesService.lightSensorEvCalibrationKey))
|
when(() => sharedPreferences.getDouble(UserPreferencesService.lightSensorEvCalibrationKey)).thenReturn(null);
|
||||||
.thenReturn(null);
|
|
||||||
expect(service.lightSensorEvCalibration, 0.0);
|
expect(service.lightSensorEvCalibration, 0.0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get', () {
|
test('get', () {
|
||||||
when(() => sharedPreferences.getDouble(UserPreferencesService.lightSensorEvCalibrationKey))
|
when(() => sharedPreferences.getDouble(UserPreferencesService.lightSensorEvCalibrationKey)).thenReturn(2.0);
|
||||||
.thenReturn(2.0);
|
|
||||||
expect(service.lightSensorEvCalibration, 2.0);
|
expect(service.lightSensorEvCalibration, 2.0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -354,8 +367,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get', () {
|
test('get', () {
|
||||||
when(() => sharedPreferences.getInt(UserPreferencesService.primaryColorKey))
|
when(() => sharedPreferences.getInt(UserPreferencesService.primaryColorKey)).thenReturn(0xff9c27b0);
|
||||||
.thenReturn(0xff9c27b0);
|
|
||||||
expect(service.primaryColor, primaryColorsList[2]);
|
expect(service.primaryColor, primaryColorsList[2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -372,14 +384,12 @@ void main() {
|
||||||
|
|
||||||
group('dynamicColor', () {
|
group('dynamicColor', () {
|
||||||
test('get default', () {
|
test('get default', () {
|
||||||
when(() => sharedPreferences.getBool(UserPreferencesService.dynamicColorKey))
|
when(() => sharedPreferences.getBool(UserPreferencesService.dynamicColorKey)).thenReturn(null);
|
||||||
.thenReturn(null);
|
|
||||||
expect(service.dynamicColor, false);
|
expect(service.dynamicColor, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get', () {
|
test('get', () {
|
||||||
when(() => sharedPreferences.getBool(UserPreferencesService.dynamicColorKey))
|
when(() => sharedPreferences.getBool(UserPreferencesService.dynamicColorKey)).thenReturn(true);
|
||||||
.thenReturn(true);
|
|
||||||
expect(service.dynamicColor, true);
|
expect(service.dynamicColor, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -387,8 +397,7 @@ void main() {
|
||||||
when(() => sharedPreferences.setBool(UserPreferencesService.dynamicColorKey, false))
|
when(() => sharedPreferences.setBool(UserPreferencesService.dynamicColorKey, false))
|
||||||
.thenAnswer((_) => Future.value(true));
|
.thenAnswer((_) => Future.value(true));
|
||||||
service.dynamicColor = false;
|
service.dynamicColor = false;
|
||||||
verify(() => sharedPreferences.setBool(UserPreferencesService.dynamicColorKey, false))
|
verify(() => sharedPreferences.setBool(UserPreferencesService.dynamicColorKey, false)).called(1);
|
||||||
.called(1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import 'package:lightmeter/data/volume_events_service.dart';
|
||||||
import 'package:mocktail/mocktail.dart';
|
import 'package:mocktail/mocktail.dart';
|
||||||
import 'package:platform/platform.dart';
|
import 'package:platform/platform.dart';
|
||||||
|
|
||||||
|
import '../event_channel_mock.dart';
|
||||||
|
|
||||||
class _MockLocalPlatform extends Mock implements LocalPlatform {}
|
class _MockLocalPlatform extends Mock implements LocalPlatform {}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
@ -60,10 +62,18 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
group('volumeButtonsEventStream', () {
|
group('volumeButtonsEventStream', () {
|
||||||
// test('Android', () async {
|
test('Android', () async {
|
||||||
// when(() => localPlatform.isAndroid).thenReturn(true);
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
||||||
// expect(service.volumeButtonsEventStream(), const Stream.empty());
|
final stream = service.volumeButtonsEventStream();
|
||||||
// });
|
final List<int> 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 {
|
test('iOS', () async {
|
||||||
when(() => localPlatform.isAndroid).thenReturn(false);
|
when(() => localPlatform.isAndroid).thenReturn(false);
|
||||||
|
|
10
test/event_channel_mock.dart
Normal file
10
test/event_channel_mock.dart
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
Future<void> sendMockVolumeAction(String channelName, int keyCode) async {
|
||||||
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
|
||||||
|
channelName,
|
||||||
|
const StandardMethodCodec().encodeSuccessEnvelope(keyCode),
|
||||||
|
(ByteData? data) {},
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in a new issue