2023-06-23 08:35:33 +00:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:lightmeter/data/light_sensor_service.dart';
|
2023-07-05 14:29:33 +00:00
|
|
|
import 'package:mocktail/mocktail.dart';
|
|
|
|
import 'package:platform/platform.dart';
|
|
|
|
|
|
|
|
class _MockLocalPlatform extends Mock implements LocalPlatform {}
|
2023-06-23 08:35:33 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
2023-07-05 14:29:33 +00:00
|
|
|
late _MockLocalPlatform localPlatform;
|
2023-06-23 08:35:33 +00:00
|
|
|
late LightSensorService service;
|
|
|
|
|
|
|
|
const methodChannel = MethodChannel('system_feature');
|
|
|
|
// TODO: add event channel mock
|
|
|
|
//const eventChannel = EventChannel('light.eventChannel');
|
|
|
|
|
|
|
|
setUp(() {
|
2023-07-05 14:29:33 +00:00
|
|
|
localPlatform = _MockLocalPlatform();
|
|
|
|
service = LightSensorService(localPlatform);
|
2023-06-23 08:35:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() {
|
|
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
|
|
.setMockMethodCallHandler(methodChannel, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
group(
|
|
|
|
'hasSensor()',
|
|
|
|
() {
|
2023-07-05 14:29:33 +00:00
|
|
|
test('true - Android', () async {
|
|
|
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
2023-06-23 08:35:33 +00:00
|
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
|
|
.setMockMethodCallHandler(methodChannel, null);
|
|
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
|
|
.setMockMethodCallHandler(methodChannel, (methodCall) async {
|
|
|
|
switch (methodCall.method) {
|
|
|
|
case "sensor":
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expectLater(service.hasSensor(), completion(true));
|
|
|
|
});
|
|
|
|
|
2023-07-05 14:29:33 +00:00
|
|
|
test('false - Android', () async {
|
|
|
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
2023-06-23 08:35:33 +00:00
|
|
|
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));
|
|
|
|
});
|
2023-07-05 14:29:33 +00:00
|
|
|
|
|
|
|
test('null - Android', () async {
|
|
|
|
when(() => localPlatform.isAndroid).thenReturn(true);
|
2023-06-23 08:35:33 +00:00
|
|
|
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));
|
|
|
|
});
|
2023-07-05 14:29:33 +00:00
|
|
|
|
|
|
|
test('false - iOS', () async {
|
|
|
|
when(() => localPlatform.isAndroid).thenReturn(false);
|
|
|
|
expectLater(service.hasSensor(), completion(false));
|
|
|
|
});
|
2023-06-23 08:35:33 +00:00
|
|
|
},
|
|
|
|
);
|
2023-07-09 11:37:53 +00:00
|
|
|
|
|
|
|
group('luxStream', () {
|
|
|
|
// test('Android', () async {
|
|
|
|
// when(() => localPlatform.isAndroid).thenReturn(true);
|
|
|
|
// expect(service.luxStream(), const Stream.empty());
|
|
|
|
// });
|
|
|
|
|
|
|
|
test('iOS', () async {
|
|
|
|
when(() => localPlatform.isAndroid).thenReturn(false);
|
|
|
|
expect(service.luxStream(), const Stream<int>.empty());
|
|
|
|
});
|
|
|
|
});
|
2023-06-23 08:35:33 +00:00
|
|
|
}
|