298 lines
9.7 KiB
Dart
298 lines
9.7 KiB
Dart
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:printer/printer_method_channel.dart';
|
|
import 'package:printer/enums/multi_byte_encoding.dart';
|
|
import 'package:printer/enums/printer_alignment.dart';
|
|
import 'package:printer/enums/serial_flow_control.dart';
|
|
import 'package:printer/enums/serial_parity.dart';
|
|
import 'package:printer/enums/serial_stop_bits.dart';
|
|
import 'package:printer/models/printer_exception.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
MethodChannelPrinter platform = MethodChannelPrinter();
|
|
const MethodChannel channel = MethodChannel('printer');
|
|
|
|
setUp(() {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'getPlatformVersion') {
|
|
return '42';
|
|
}
|
|
if (methodCall.method == 'openComPort') {
|
|
return 1;
|
|
}
|
|
if (methodCall.method == 'openUsbPort') {
|
|
return 2;
|
|
}
|
|
if (methodCall.method == 'closePort') {
|
|
return true;
|
|
}
|
|
if (methodCall.method == 'isPortOpened') {
|
|
return true;
|
|
}
|
|
if (methodCall.method == 'enumComPorts') {
|
|
return ['/dev/ttyS0', '/dev/ttyS1'];
|
|
}
|
|
if (methodCall.method == 'enumUsbPorts') {
|
|
return ['USB_Printer_0'];
|
|
}
|
|
if (methodCall.method == 'setMultiByteMode') return true;
|
|
if (methodCall.method == 'setMultiByteEncoding') return true;
|
|
if (methodCall.method == 'printText') return true;
|
|
if (methodCall.method == 'setAlignment') return true;
|
|
if (methodCall.method == 'setTextScale') return true;
|
|
if (methodCall.method == 'setTextBold') return true;
|
|
if (methodCall.method == 'setTextUnderline') return true;
|
|
if (methodCall.method == 'feedLine') return true;
|
|
if (methodCall.method == 'feedDot') return true;
|
|
if (methodCall.method == 'halfCutPaper') return true;
|
|
if (methodCall.method == 'fullCutPaper') return true;
|
|
return null;
|
|
});
|
|
});
|
|
|
|
tearDown(() {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, null);
|
|
});
|
|
|
|
test('getPlatformVersion', () async {
|
|
expect(await platform.getPlatformVersion(), '42');
|
|
});
|
|
|
|
test('openComPort returns handle', () async {
|
|
final handle = await platform.openComPort(
|
|
portName: '/dev/ttyS0',
|
|
baudRate: 115200,
|
|
parity: SerialParity.none,
|
|
stopBits: SerialStopBits.one,
|
|
flowControl: SerialFlowControl.none,
|
|
);
|
|
|
|
expect(handle, 1);
|
|
});
|
|
|
|
test('openComPort passes correct parameters', () async {
|
|
late Map<dynamic, dynamic> capturedArgs;
|
|
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'openComPort') {
|
|
capturedArgs = methodCall.arguments as Map<dynamic, dynamic>;
|
|
return 42;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
await platform.openComPort(
|
|
portName: '/dev/ttyS0',
|
|
baudRate: 9600,
|
|
dataBits: 7,
|
|
parity: SerialParity.even,
|
|
stopBits: SerialStopBits.two,
|
|
flowControl: SerialFlowControl.rtsCts,
|
|
autoReplyMode: false,
|
|
);
|
|
|
|
expect(capturedArgs['portName'], '/dev/ttyS0');
|
|
expect(capturedArgs['baudRate'], 9600);
|
|
expect(capturedArgs['dataBits'], 7);
|
|
expect(capturedArgs['parity'], 2); // SerialParity.even
|
|
expect(capturedArgs['stopBits'], 2); // SerialStopBits.two
|
|
expect(capturedArgs['flowControl'], 2); // SerialFlowControl.rtsCts
|
|
expect(capturedArgs['autoReplyMode'], 0); // false
|
|
});
|
|
|
|
test('openUsbPort returns handle', () async {
|
|
final handle = await platform.openUsbPort(portName: 'USB_Printer_0');
|
|
|
|
expect(handle, 2);
|
|
});
|
|
|
|
test('closePort returns true', () async {
|
|
final result = await platform.closePort(1);
|
|
|
|
expect(result, true);
|
|
});
|
|
|
|
test('isPortOpened returns true', () async {
|
|
final result = await platform.isPortOpened(1);
|
|
|
|
expect(result, true);
|
|
});
|
|
|
|
test('enumComPorts returns list of strings', () async {
|
|
final ports = await platform.enumComPorts();
|
|
|
|
expect(ports, isA<List<String>>());
|
|
expect(ports, contains('/dev/ttyS0'));
|
|
expect(ports, contains('/dev/ttyS1'));
|
|
});
|
|
|
|
test('enumUsbPorts returns list of strings', () async {
|
|
final ports = await platform.enumUsbPorts();
|
|
|
|
expect(ports, isA<List<String>>());
|
|
expect(ports, contains('USB_Printer_0'));
|
|
});
|
|
|
|
test('PlatformException converts to PrinterException on openComPort', () async {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'openComPort') {
|
|
throw PlatformException(
|
|
code: 'PORT_OPEN_FAILED',
|
|
message: 'Failed to open port',
|
|
details: {'port': '/dev/ttyS0'},
|
|
);
|
|
}
|
|
return null;
|
|
});
|
|
|
|
expect(
|
|
() async => platform.openComPort(
|
|
portName: '/dev/ttyS0',
|
|
baudRate: 115200,
|
|
),
|
|
throwsA(isA<PrinterException>()
|
|
.having((e) => e.code, 'code', 'PORT_OPEN_FAILED')
|
|
.having((e) => e.message, 'message', 'Failed to open port')),
|
|
);
|
|
});
|
|
|
|
test('PlatformException converts to PrinterException on closePort', () async {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'closePort') {
|
|
throw PlatformException(
|
|
code: 'PORT_CLOSE_FAILED',
|
|
message: 'Failed to close port',
|
|
);
|
|
}
|
|
return null;
|
|
});
|
|
|
|
expect(
|
|
() async => platform.closePort(1),
|
|
throwsA(isA<PrinterException>()
|
|
.having((e) => e.code, 'code', 'PORT_CLOSE_FAILED')),
|
|
);
|
|
});
|
|
|
|
test('printText passes text parameter', () async {
|
|
late Map<dynamic, dynamic> capturedArgs;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'printText') {
|
|
capturedArgs = methodCall.arguments as Map<dynamic, dynamic>;
|
|
return true;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
await platform.printText(1, 'Hello 中文');
|
|
expect(capturedArgs['handle'], 1);
|
|
expect(capturedArgs['text'], 'Hello 中文');
|
|
});
|
|
|
|
test('setAlignment passes alignment.value', () async {
|
|
late Map<dynamic, dynamic> capturedArgs;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'setAlignment') {
|
|
capturedArgs = methodCall.arguments as Map<dynamic, dynamic>;
|
|
return true;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
await platform.setAlignment(1, PrinterAlignment.center);
|
|
expect(capturedArgs['alignment'], 1); // PrinterAlignment.center.value
|
|
});
|
|
|
|
test('setMultiByteEncoding passes encoding.value', () async {
|
|
late Map<dynamic, dynamic> capturedArgs;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'setMultiByteEncoding') {
|
|
capturedArgs = methodCall.arguments as Map<dynamic, dynamic>;
|
|
return true;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
await platform.setMultiByteEncoding(1, MultiByteEncoding.utf8);
|
|
expect(capturedArgs['encoding'], 1); // MultiByteEncoding.utf8.value
|
|
});
|
|
|
|
test('setTextBold converts bool to int 0/1', () async {
|
|
late Map<dynamic, dynamic> capturedArgs;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'setTextBold') {
|
|
capturedArgs = methodCall.arguments as Map<dynamic, dynamic>;
|
|
return true;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
await platform.setTextBold(1, true);
|
|
expect(capturedArgs['bold'], 1);
|
|
|
|
await platform.setTextBold(1, false);
|
|
expect(capturedArgs['bold'], 0);
|
|
});
|
|
|
|
test('feedDot passes numDots parameter', () async {
|
|
late Map<dynamic, dynamic> capturedArgs;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'feedDot') {
|
|
capturedArgs = methodCall.arguments as Map<dynamic, dynamic>;
|
|
return true;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
await platform.feedDot(1, 100);
|
|
expect(capturedArgs['handle'], 1);
|
|
expect(capturedArgs['numDots'], 100);
|
|
});
|
|
|
|
test('fullCutPaper passes handle parameter', () async {
|
|
late Map<dynamic, dynamic> capturedArgs;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'fullCutPaper') {
|
|
capturedArgs = methodCall.arguments as Map<dynamic, dynamic>;
|
|
return true;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
await platform.fullCutPaper(1);
|
|
expect(capturedArgs['handle'], 1);
|
|
});
|
|
|
|
test('PlatformException converts to PrinterException on printText', () async {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
if (methodCall.method == 'printText') {
|
|
throw PlatformException(
|
|
code: 'PRINT_FAILED',
|
|
message: 'Failed to print text',
|
|
);
|
|
}
|
|
return null;
|
|
});
|
|
|
|
expect(
|
|
() async => platform.printText(1, 'test'),
|
|
throwsA(isA<PrinterException>()
|
|
.having((e) => e.code, 'code', 'PRINT_FAILED')),
|
|
);
|
|
});
|
|
}
|