- write方法新增可选的serialPortIndex参数,支持直接写入指定端口 - 传入端口索引时不会改变当前活跃端口,保持原有活跃端口行为 - 添加对负数端口索引的验证处理,直接返回零避免异常 - 更新文档说明多端口并发客户端应使用显式端口写入方式 - 增加相关单元测试覆盖新参数的各种使用场景 - 同步更新Android原生层实现以支持端口索引传递
127 lines
3.8 KiB
Dart
127 lines
3.8 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('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');
|
|
});
|
|
}
|