1
This commit is contained in:
31
test/ch34_method_channel_test.dart
Normal file
31
test/ch34_method_channel_test.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:ch34/src/ch34_method_channel.dart';
|
||||
import 'package:ch34/src/ch34_platform_interface.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
MethodChannelCh34.registerDefault();
|
||||
MethodChannelCh34 platform = Ch34Platform.instance as MethodChannelCh34;
|
||||
const MethodChannel channel = MethodChannel('ch34');
|
||||
|
||||
setUp(() {
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(
|
||||
channel,
|
||||
(MethodCall methodCall) async {
|
||||
return '42';
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, null);
|
||||
});
|
||||
|
||||
test('getPlatformVersion', () async {
|
||||
expect(await platform.getPlatformVersion(), '42');
|
||||
});
|
||||
}
|
||||
205
test/ch34_test.dart
Normal file
205
test/ch34_test.dart
Normal file
@@ -0,0 +1,205 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:ch34/ch34.dart';
|
||||
import 'package:ch34/src/ch34_method_channel.dart';
|
||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||
|
||||
class MockCh34Platform
|
||||
with MockPlatformInterfaceMixin
|
||||
implements Ch34Platform {
|
||||
|
||||
@override
|
||||
Future<String?> getPlatformVersion() => Future.value('42');
|
||||
|
||||
@override
|
||||
Future<List<UsbDeviceInfo>> enumDevice() => Future.value([]);
|
||||
|
||||
@override
|
||||
Future<String?> getChipType(String deviceName) => Future.value('CH340');
|
||||
|
||||
@override
|
||||
Future<bool> openDevice(String deviceName) => Future.value(true);
|
||||
|
||||
@override
|
||||
Future<bool> requestPermission(String deviceName) => Future.value(true);
|
||||
|
||||
@override
|
||||
void setUsbStateListener(void Function(String, bool) onStateChanged) {}
|
||||
|
||||
@override
|
||||
void removeUsbStateListener() {}
|
||||
|
||||
@override
|
||||
Future<int> getSerialCount(String deviceName) => Future.value(1);
|
||||
|
||||
@override
|
||||
Future<int> getSerialBaud(String deviceName, int serialNumber) =>
|
||||
Future.value(115200);
|
||||
|
||||
@override
|
||||
Future<ChipMasterFrequency> getChipMasterFrequency(String deviceName) =>
|
||||
Future.value(const ChipMasterFrequency(
|
||||
frequency: 12000000,
|
||||
switchEnable: false,
|
||||
coStatus: 0,
|
||||
));
|
||||
|
||||
@override
|
||||
Future<bool> enableSerial(String deviceName, int serialNumber, bool enable) =>
|
||||
Future.value(true);
|
||||
|
||||
@override
|
||||
Future<bool> setSerialParameter(
|
||||
String deviceName,
|
||||
int serialNumber,
|
||||
SerialParameter parameter,
|
||||
) => Future.value(true);
|
||||
|
||||
@override
|
||||
Future<int> writeData(
|
||||
String deviceName,
|
||||
int serialNumber,
|
||||
Uint8List data, {
|
||||
int timeout = 0,
|
||||
}) => Future.value(data.length);
|
||||
|
||||
@override
|
||||
Future<void> asyncWriteData(
|
||||
String deviceName,
|
||||
int serialNumber,
|
||||
Uint8List data,
|
||||
) => Future.value();
|
||||
|
||||
@override
|
||||
Future<Uint8List> readData(String deviceName, int serialNumber) =>
|
||||
Future.value(Uint8List(0));
|
||||
|
||||
@override
|
||||
Future<Uint8List> readDataWithTimeout(
|
||||
String deviceName,
|
||||
int serialNumber,
|
||||
int vTime,
|
||||
int vMin,
|
||||
) => Future.value(Uint8List(0));
|
||||
|
||||
@override
|
||||
Future<void> registerDataCallback(
|
||||
String deviceName,
|
||||
int serialNumber,
|
||||
void Function(Uint8List) onData,
|
||||
) => Future.value();
|
||||
|
||||
@override
|
||||
void removeDataCallback(String deviceName) {}
|
||||
|
||||
@override
|
||||
Future<bool> isConnected(String deviceName) => Future.value(true);
|
||||
|
||||
@override
|
||||
Future<List<String>> getConnectedDevices() => Future.value([]);
|
||||
|
||||
@override
|
||||
Future<void> disconnect(String deviceName) => Future.value();
|
||||
|
||||
@override
|
||||
Future<void> close() => Future.value();
|
||||
|
||||
@override
|
||||
Future<bool> isSupportGpio(String deviceName) => Future.value(false);
|
||||
|
||||
@override
|
||||
Future<int> queryGpioCount(String deviceName) => Future.value(0);
|
||||
|
||||
@override
|
||||
Future<GpioStatus> queryGpioStatus(String deviceName, int gpioIndex) =>
|
||||
throw const Ch34Exception('Not supported');
|
||||
|
||||
@override
|
||||
Future<List<GpioStatus>> queryAllGpioStatus(String deviceName) =>
|
||||
Future.value([]);
|
||||
|
||||
@override
|
||||
Future<bool> enableGpio(
|
||||
String deviceName,
|
||||
int gpioIndex,
|
||||
bool enable,
|
||||
GpioDirection direction,
|
||||
) => Future.value(false);
|
||||
|
||||
@override
|
||||
Future<bool> setGpioVal(
|
||||
String deviceName,
|
||||
int gpioIndex,
|
||||
GpioValue value,
|
||||
) => Future.value(false);
|
||||
|
||||
@override
|
||||
Future<GpioValue> getGpioVal(String deviceName, int gpioIndex) =>
|
||||
Future.value(GpioValue.low);
|
||||
|
||||
@override
|
||||
Future<bool> setDtr(String deviceName, int serialNumber, bool valid) =>
|
||||
Future.value(true);
|
||||
|
||||
@override
|
||||
Future<bool> setRts(String deviceName, int serialNumber, bool valid) =>
|
||||
Future.value(true);
|
||||
|
||||
@override
|
||||
Future<bool> setBreakSignal(
|
||||
String deviceName, int serialNumber, bool valid) =>
|
||||
Future.value(true);
|
||||
|
||||
@override
|
||||
Future<void> registerModemStatusCallback(
|
||||
String deviceName,
|
||||
void Function(ModemStatus) onModemStatus,
|
||||
) => Future.value();
|
||||
|
||||
@override
|
||||
void removeModemStatusCallback(String deviceName) {}
|
||||
|
||||
@override
|
||||
Future<int> querySerialErrorCount(
|
||||
String deviceName,
|
||||
int serialNumber,
|
||||
SerialErrorType errorType,
|
||||
) => Future.value(0);
|
||||
|
||||
@override
|
||||
Future<void> setReadTimeout(int timeout) => Future.value();
|
||||
|
||||
@override
|
||||
Future<void> addNewHardware(int vid, int pid, String chipType) => Future.value();
|
||||
|
||||
@override
|
||||
Future<void> setDebug(bool enabled) => Future.value();
|
||||
|
||||
@override
|
||||
Future<bool> isDebugMode() => Future.value(false);
|
||||
}
|
||||
|
||||
void main() {
|
||||
MethodChannelCh34.registerDefault();
|
||||
final Ch34Platform initialPlatform = Ch34Platform.instance;
|
||||
|
||||
test('$MethodChannelCh34 is the default instance', () {
|
||||
expect(initialPlatform, isInstanceOf<MethodChannelCh34>());
|
||||
});
|
||||
|
||||
test('getPlatformVersion', () async {
|
||||
MockCh34Platform fakePlatform = MockCh34Platform();
|
||||
Ch34Platform.instance = fakePlatform;
|
||||
|
||||
expect(await Ch34Manager.getPlatformVersion(), '42');
|
||||
});
|
||||
|
||||
test('enumDevice returns empty list', () async {
|
||||
MockCh34Platform fakePlatform = MockCh34Platform();
|
||||
Ch34Platform.instance = fakePlatform;
|
||||
|
||||
final devices = await Ch34Manager.enumDevice();
|
||||
expect(devices, isEmpty);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user