m3_lightmeter/test/data/light_sensor_service_test.dart
Vadim 27d56d1061
ML-166 Golden tests (#167)
* setup golden toolkit

* implemented `GoldenTestApplicationMock`

* added devices with dark theme

* implemented MeteringScreen golden test

* moved platform channel logic to app mock

* implemented SettingsScreen golden test

* gitignore golden tests failures

* Create dart_test.yaml

* adjusted `RulerSlider` ticks height

* set master screenshots

* run golden tests on ci

* fixed `LightSensorService` tests

* removed golden workflow call from PR check

* Update pr_check.yml
2024-04-12 08:07:20 +02:00

84 lines
2.6 KiB
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() {
TestWidgetsFlutterBinding.ensureInitialized();
late _MockLocalPlatform localPlatform;
late LightSensorService service;
setUp(() {
localPlatform = _MockLocalPlatform();
service = LightSensorService(localPlatform);
});
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.isIOS).thenReturn(false);
setMockSensorAvailability(hasSensor: true);
expectLater(service.hasSensor(), completion(true));
});
test('false - Android', () async {
when(() => localPlatform.isIOS).thenReturn(false);
setMockSensorAvailability(hasSensor: false);
expectLater(service.hasSensor(), completion(false));
});
test('false - iOS', () async {
when(() => localPlatform.isIOS).thenReturn(true);
expectLater(service.hasSensor(), completion(false));
});
},
);
group('luxStream', () {
test('Android', () async {
when(() => localPlatform.isIOS).thenReturn(false);
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.isIOS).thenReturn(true);
expect(service.luxStream(), const Stream<int>.empty());
});
});
}