CaffeineService tests

This commit is contained in:
Vadim 2023-06-21 12:49:18 +02:00
parent edf2b953c2
commit 41431ba0b8
2 changed files with 78 additions and 2 deletions

View file

@ -9,7 +9,7 @@ class CaffeineService {
return _methodChannel.invokeMethod<bool>("isKeepScreenOn").then((value) => value!); return _methodChannel.invokeMethod<bool>("isKeepScreenOn").then((value) => value!);
} }
Future<void> keepScreenOn(bool keep) async { Future<bool> keepScreenOn(bool keep) async {
await _methodChannel.invokeMethod<bool>("setKeepScreenOn", keep); return _methodChannel.invokeMethod<bool>("setKeepScreenOn", keep).then((value) => value!);
} }
} }

View file

@ -0,0 +1,76 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:lightmeter/data/caffeine_service.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late CaffeineService service;
const methodChannel = MethodChannel('com.vodemn.lightmeter/keepScreenOn');
Future<Object?>? cameraMethodCallSuccessHandler(MethodCall methodCall) async {
switch (methodCall.method) {
case "isKeepScreenOn":
return true;
case "setKeepScreenOn":
return methodCall.arguments as bool;
default:
return null;
}
}
setUp(() {
service = const CaffeineService();
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, cameraMethodCallSuccessHandler);
});
tearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, null);
});
group(
'isKeepScreenOn()',
() {
test('true', () async {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, null);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, (methodCall) async {
switch (methodCall.method) {
case "isKeepScreenOn":
return true;
default:
return null;
}
});
expectLater(service.isKeepScreenOn(), completion(true));
});
test('false', () async {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, null);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, (methodCall) async {
switch (methodCall.method) {
case "isKeepScreenOn":
return false;
default:
return null;
}
});
expectLater(service.isKeepScreenOn(), completion(false));
});
},
);
group(
'keepScreenOn()',
() {
test('true', () async => expectLater(service.keepScreenOn(true), completion(true)));
test('false', () async => expectLater(service.keepScreenOn(false), completion(false)));
},
);
}