implemented platform mocks for unit tests

This commit is contained in:
Vadim 2023-10-19 19:19:54 +02:00
parent 3c4d959cc6
commit 9baf1da157
6 changed files with 98 additions and 83 deletions

View file

@ -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<int>.empty();
}
return LightSensor.lightSensorStream;
return LightSensor.luxStream();
}
}

View file

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

View file

@ -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<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 {
when(() => localPlatform.isAndroid).thenReturn(false);

View file

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

View file

@ -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<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 {
when(() => localPlatform.isAndroid).thenReturn(false);

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