m3_lightmeter/integration_test/utils/platform_channel_mock.dart

62 lines
1.7 KiB
Dart
Raw Normal View History

2023-10-17 09:58:48 +00:00
import 'dart:math';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
2023-10-18 21:58:57 +00:00
const _systemFeatureMethodChannel = MethodChannel('system_feature');
const _lightSensorMethodChannel = MethodChannel("light.eventChannel");
2023-10-18 13:18:59 +00:00
void setLightSensorAvilability({required bool hasSensor}) {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
2023-10-18 21:58:57 +00:00
_systemFeatureMethodChannel,
2023-10-18 13:18:59 +00:00
(methodCall) async {
switch (methodCall.method) {
case "sensor":
return hasSensor;
default:
return null;
}
},
2023-10-17 09:58:48 +00:00
);
}
2023-10-18 13:18:59 +00:00
void resetLightSensorAvilability() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
2023-10-18 21:58:57 +00:00
_systemFeatureMethodChannel,
2023-10-18 13:18:59 +00:00
null,
);
}
Future<void> sendMockIncidentEv(double ev) => sendMockLux((2.5 * pow(2, ev)).toInt());
Future<void> sendMockLux([int lux = 100]) async {
2023-10-17 09:58:48 +00:00
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
2023-10-18 21:58:57 +00:00
_lightSensorMethodChannel.name,
2023-10-18 13:18:59 +00:00
const StandardMethodCodec().encodeSuccessEnvelope(lux),
2023-10-17 09:58:48 +00:00
(ByteData? data) {},
);
}
2023-10-17 15:01:11 +00:00
2023-10-18 13:18:59 +00:00
void setupLightSensorStreamHandler() {
2023-10-17 15:01:11 +00:00
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
2023-10-18 21:58:57 +00:00
_lightSensorMethodChannel,
2023-10-17 15:01:11 +00:00
(methodCall) async {
switch (methodCall.method) {
2023-10-18 13:18:59 +00:00
case "listen":
return;
case "cancel":
return;
2023-10-17 15:01:11 +00:00
default:
return null;
}
},
);
}
2023-10-18 13:18:59 +00:00
void resetLightSensorStreamHandler() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
2023-10-18 21:58:57 +00:00
_lightSensorMethodChannel,
2023-10-18 13:18:59 +00:00
null,
);
}