Files
ch934x_serial/test/ch934x_serial_method_channel_test.dart
huangsheng aea4e4f1cd feat(usb): 完善CH934X串口服务功能
- 实现8端口逐一配置和流控开关控制
- 添加串口参数透传的单元测试验证
- 优化串口连接和重连逻辑,支持端口状态检查
- 添加ZTF串口响应数据解析和JSON提取功能
- 集成HapticFeedback振动反馈替代音频提示
- 统一将"震动"术语替换为更准确的"振动"
- 修复告警处理流程中的参数传递问题
- 优化数据库初始化和服务配置逻辑
2026-07-15 10:24:47 +08:00

152 lines
4.4 KiB
Dart

import 'package:ch934x_serial/ch934x_serial_method_channel.dart';
import 'package:ch934x_serial/src/models/models.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
const channel = MethodChannel('ch934x_serial');
late MethodChannelCh934xSerial platform;
setUp(() {
platform = MethodChannelCh934xSerial();
});
tearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null);
platform.dispose();
});
/// 显式替换 mock handler 并返回,后续 `await platform.xxx` 会触发该 handler。
void mockCall(Future<Object?> Function(MethodCall) handler) {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (MethodCall call) async {
return handler(call);
});
}
test('getDeviceList 解析为 Ch934xDeviceInfo 列表', () async {
mockCall((call) async {
expect(call.method, 'getDeviceList');
return <Map<String, Object>>[
<String, Object>{
'deviceId': 1,
'vendorId': 0x1a86,
'productId': 0xfe0c,
'deviceType': Ch934xDeviceType.ch9344,
'serialNumber': 'ABC',
'interfaceCount': 1,
'serialPorts': <Map<String, Object>>[
<String, Object>{'portIndex': 0},
],
},
];
});
final result = await platform.getDeviceList();
expect(result, hasLength(1));
expect(result.first.serialNumber, 'ABC');
expect(result.first.serialPorts.single.portIndex, 0);
});
test('read 透传 length 字段并返回字节', () async {
mockCall((call) async {
expect(call.method, 'read');
expect(call.arguments, <String, Object>{'length': 4});
return Uint8List.fromList([1, 2, 3, 4]);
});
final result = await platform.read(4);
expect(result, Uint8List.fromList([1, 2, 3, 4]));
});
test('read 接收非正长度直接返回空数组', () async {
// 未注册 mock handler,验证短路逻辑不调用原生层。
final result = await platform.read(0);
expect(result, isEmpty);
});
test('write 将数据写入原生层并回传字节数', () async {
mockCall((call) async {
expect(call.method, 'write');
expect(call.arguments, <String, Object>{
'data': Uint8List.fromList([9, 8, 7]),
});
return 3;
});
final written = await platform.write(Uint8List.fromList([9, 8, 7]));
expect(written, 3);
});
test('write 透传显式串口索引', () async {
mockCall((call) async {
expect(call.method, 'write');
expect(call.arguments, <String, Object>{
'data': Uint8List.fromList([9, 8, 7]),
'serialPortIndex': 2,
});
return 3;
});
final written = await platform.write(
Uint8List.fromList([9, 8, 7]),
serialPortIndex: 2,
);
expect(written, 3);
});
test('write 接收负串口索引直接返回零', () async {
final written = await platform.write(
Uint8List.fromList([9]),
serialPortIndex: -1,
);
expect(written, 0);
});
test('setSerialParameter 透传全部串口参数和流控开关', () async {
mockCall((call) async {
expect(call.method, 'setSerialParameter');
expect(call.arguments, <String, Object>{
'baud': 115200,
'dataBit': 8,
'stopBit': 1,
'parityBit': 0,
'flow': false,
});
return true;
});
expect(
await platform.setSerialParameter(
baud: 115200,
dataBit: 8,
stopBit: 1,
parityBit: 0,
flow: false,
),
isTrue,
);
});
test('getModemStatus 默认回退到 0', () async {
mockCall((_) async => null);
expect(await platform.getModemStatus(), 0);
});
test('dispatchException 推送给 setExceptionCallback 订阅者', () async {
mockCall((call) async {
expect(call.method, 'setExceptionCallback');
return null;
});
final received = <Ch934xException>[];
await platform.setExceptionCallback(received.add);
platform.dispatchException(
type: Ch934xExceptionType.deviceDetached,
message: 'detached',
);
// 等待 microtask 队列执行。
await Future<void>.delayed(Duration.zero);
expect(received, hasLength(1));
expect(received.first.message, 'detached');
});
}