feat(desktop): 支持粘贴配对并修复串流与设备切换
- 支持 Ctrl+V 粘贴配对文本和二维码图片,预览确认后识别校验 - 修复控制发送阻塞、TLS 读写争锁、设备切换沿用旧连接及串流误关窗 - 优化媒体加密上下文、UDP 接收缓冲和异步诊断日志 - 安装器按路径停止主控与信令进程,统一递增版本、打包和清理旧产物 - 发布 0.1.3+4,通过三端编译、20 项 Flutter 测试和 58 项 Rust 单元测试
This commit is contained in:
1 parent
f6b7730684
commit
3cd72d70e1
38 files changed
+1173
-196
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:ttstd_sender/core/diagnostic_log.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'asynchronous logs preserve order, redact tokens and finish after dispose',
|
||||
() async {
|
||||
final log = DiagnosticLog(
|
||||
'ttstd_log_test_${DateTime.now().microsecondsSinceEpoch}.log');
|
||||
log.start();
|
||||
final file = File(log.path);
|
||||
addTearDown(() async {
|
||||
await log.flush();
|
||||
if (await file.exists()) await file.delete();
|
||||
});
|
||||
log.write('first #token=secret');
|
||||
log.write('second');
|
||||
log.dispose();
|
||||
await log.flush();
|
||||
final text = await file.readAsString();
|
||||
expect(text, contains('first #token=[redacted]'));
|
||||
expect(text, isNot(contains('secret')));
|
||||
expect(text.indexOf('first'), lessThan(text.indexOf('second')));
|
||||
expect(text.indexOf('second'), lessThan(text.indexOf('=== stopped ===')));
|
||||
});
|
||||
|
||||
test('log rotation and failed writes do not prevent later writes', () async {
|
||||
final log = DiagnosticLog(
|
||||
'ttstd_log_test_${DateTime.now().microsecondsSinceEpoch}.log');
|
||||
log.start();
|
||||
final file = File(log.path);
|
||||
addTearDown(() async {
|
||||
log.dispose();
|
||||
await log.flush();
|
||||
if (await file.exists()) await file.delete();
|
||||
});
|
||||
await file.writeAsString('x' * (2 * 1024 * 1024 + 1));
|
||||
log.write('after rotation');
|
||||
await log.flush();
|
||||
expect(
|
||||
await file.readAsString(),
|
||||
allOf(contains('log rotated:'), contains('after rotation'),
|
||||
isNot(startsWith('xxx'))));
|
||||
await file.delete();
|
||||
log.write('unavailable file');
|
||||
await log.flush();
|
||||
await file.writeAsString('recovered\n');
|
||||
log.write('after recovery');
|
||||
await log.flush();
|
||||
expect(await file.readAsString(), contains('after recovery'));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:ttstd_sender/core/pairing_input.dart';
|
||||
import '../fixtures/pairing_qr.dart';
|
||||
|
||||
void main() {
|
||||
test('parses pairing links in copied text and validates identity fields', () {
|
||||
expect(parsePairingText('配对信息:\n$pairingText\n').ip, '192.168.100.141');
|
||||
for (final invalid in [
|
||||
'hello',
|
||||
'$pairingText\n$pairingText',
|
||||
pairingText.replaceFirst('41001', '70000'),
|
||||
pairingText.replaceFirst('192.168.100.141', 'invalid'),
|
||||
'lancast://pair?ip=127.0.0.1&port=41001',
|
||||
'$pairingText&tls=invalid'
|
||||
]) {
|
||||
expect(() => parsePairingText(invalid), throwsFormatException);
|
||||
}
|
||||
});
|
||||
test('decodes color, grayscale and rotated QR images', () {
|
||||
for (final bytes in [
|
||||
pairingQr(),
|
||||
pairingQr(grayscale: true),
|
||||
pairingQr(rotate: true)
|
||||
]) {
|
||||
final result = parsePairingImage(bytes);
|
||||
expect(result.deviceId, 'test-qr');
|
||||
expect(result.ip, '192.168.100.141');
|
||||
}
|
||||
});
|
||||
test('rejects malformed or oversized image data', () {
|
||||
expect(() => parsePairingImage(Uint8List.fromList([1, 2, 3])),
|
||||
throwsFormatException);
|
||||
expect(() => parsePairingImage(Uint8List(maxPairingImageBytes + 1)),
|
||||
throwsFormatException);
|
||||
});
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:image/image.dart' as img;
|
||||
import 'package:zxing2/qrcode.dart';
|
||||
|
||||
final pairingText =
|
||||
'lancast://pair?ip=192.168.100.141&port=41001&fp=${'ab' * 32}&id=test-qr&name=Receiver';
|
||||
|
||||
Uint8List pairingQr({bool rotate = false, bool grayscale = false}) {
|
||||
final matrix = Encoder.encode(pairingText, ErrorCorrectionLevel.m).matrix!;
|
||||
const scale = 5, margin = 4;
|
||||
var image = img.Image(
|
||||
width: (matrix.width + margin * 2) * scale,
|
||||
height: (matrix.height + margin * 2) * scale,
|
||||
numChannels: grayscale ? 1 : 4);
|
||||
img.fill(image, color: img.ColorRgba8(255, 255, 255, 255));
|
||||
for (var y = 0; y < matrix.height; y++) {
|
||||
for (var x = 0; x < matrix.width; x++) {
|
||||
if (matrix.get(x, y) == 1) {
|
||||
img.fillRect(image,
|
||||
x1: (x + margin) * scale,
|
||||
y1: (y + margin) * scale,
|
||||
x2: (x + margin + 1) * scale - 1,
|
||||
y2: (y + margin + 1) * scale - 1,
|
||||
color: img.ColorRgba8(0, 0, 0, 255));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rotate) image = img.copyRotate(image, angle: 90);
|
||||
return img.encodePng(image);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:ttstd_sender/core/pairing.dart';
|
||||
import 'package:ttstd_sender/core/pairing_input.dart';
|
||||
import 'package:ttstd_sender/features/discovery/pairing_paste_dialog.dart';
|
||||
import 'fixtures/pairing_qr.dart';
|
||||
|
||||
void main() {
|
||||
Future<void> open(WidgetTester tester, PairingPasteDialog dialog,
|
||||
void Function(PairingUri?) result) async {
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: Builder(
|
||||
builder: (context) => TextButton(
|
||||
onPressed: () async => result(await showDialog<PairingUri>(
|
||||
context: context, builder: (_) => dialog)),
|
||||
child: const Text('open'),
|
||||
))));
|
||||
await tester.tap(find.text('open'));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
testWidgets('Ctrl+V previews text; only confirmation returns a pairing',
|
||||
(tester) async {
|
||||
PairingUri? result;
|
||||
await open(
|
||||
tester,
|
||||
PairingPasteDialog(
|
||||
readClipboard: () async =>
|
||||
PairingClipboardContent(text: pairingText)),
|
||||
(value) => result = value);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyV);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text(pairingText), findsOneWidget);
|
||||
expect(result, isNull);
|
||||
await tester.tap(find.text('确认配对'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(result?.ip, '192.168.100.141');
|
||||
});
|
||||
|
||||
testWidgets('pasting image does not decode until confirmation',
|
||||
(tester) async {
|
||||
var decodes = 0;
|
||||
PairingUri? result;
|
||||
await open(
|
||||
tester,
|
||||
PairingPasteDialog(
|
||||
readClipboard: () async =>
|
||||
PairingClipboardContent(image: pairingQr()),
|
||||
decodeImage: (_) async {
|
||||
decodes++;
|
||||
return parsePairingText(pairingText);
|
||||
},
|
||||
),
|
||||
(value) => result = value);
|
||||
await tester.tap(find.text('粘贴'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(Image), findsOneWidget);
|
||||
expect(decodes, 0);
|
||||
expect(result, isNull);
|
||||
await tester.tap(find.text('确认配对'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(decodes, 1);
|
||||
expect(result?.deviceId, 'test-qr');
|
||||
});
|
||||
|
||||
testWidgets('invalid text remains in dialog and can be replaced or cancelled',
|
||||
(tester) async {
|
||||
var text = 'invalid text';
|
||||
PairingUri? result;
|
||||
await open(
|
||||
tester,
|
||||
PairingPasteDialog(
|
||||
readClipboard: () async => PairingClipboardContent(text: text)),
|
||||
(value) => result = value);
|
||||
await tester.tap(find.text('粘贴'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.textContaining('配对链接无效'), findsNothing);
|
||||
await tester.tap(find.text('确认配对'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.textContaining('配对链接无效'), findsOneWidget);
|
||||
expect(find.byType(PairingPasteDialog), findsOneWidget);
|
||||
text = pairingText;
|
||||
await tester.tap(find.text('粘贴'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.textContaining('配对链接无效'), findsNothing);
|
||||
await tester.tap(find.text('取消'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(result, isNull);
|
||||
});
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:ttstd_sender/core/models.dart';
|
||||
import 'package:ttstd_sender/main.dart';
|
||||
import 'package:ttstd_sender/features/session/connect_bar.dart';
|
||||
import 'package:ttstd_sender/features/session/mirror_bar.dart';
|
||||
|
||||
const MethodChannel _methodChannel = MethodChannel('ttstd/sender');
|
||||
const EventChannel _eventChannel = EventChannel('ttstd/sender/events');
|
||||
@@ -22,7 +26,8 @@ DeviceInfo _testDevice() => const DeviceInfo(
|
||||
);
|
||||
|
||||
void _mockChannels({List<DeviceInfo> devices = const [], String? pairingUri}) {
|
||||
final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
messenger.setMockMethodCallHandler(_methodChannel, (call) async {
|
||||
switch (call.method) {
|
||||
case 'discover':
|
||||
@@ -41,11 +46,69 @@ void _mockChannels({List<DeviceInfo> devices = const [], String? pairingUri}) {
|
||||
|
||||
void main() {
|
||||
tearDown(() {
|
||||
final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
messenger.setMockMethodCallHandler(_methodChannel, null);
|
||||
messenger.setMockStreamHandler(_eventChannel, null);
|
||||
});
|
||||
|
||||
testWidgets('切换设备先断开旧连接,发现刷新不替换活动设备', (tester) async {
|
||||
final first = _testDevice();
|
||||
const second = DeviceInfo(
|
||||
id: 'second-device',
|
||||
name: '另一设备',
|
||||
ip: '192.168.100.141',
|
||||
mediaPort: 41000,
|
||||
controlPort: 41001,
|
||||
fingerprint: 'second-fingerprint',
|
||||
);
|
||||
final devices = [first, second];
|
||||
final disconnected = Completer<void>();
|
||||
var disconnectCalls = 0;
|
||||
late void Function(Object?) emitPhase;
|
||||
_mockChannels(devices: devices);
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
messenger.setMockMethodCallHandler(_methodChannel, (call) async {
|
||||
if (call.method == 'discover')
|
||||
return devices.map((device) => device.toJson()).toList();
|
||||
if (call.method == 'disconnect') {
|
||||
disconnectCalls++;
|
||||
await disconnected.future;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
messenger.setMockStreamHandler(_eventChannel, MockStreamHandler.inline(
|
||||
onListen: (arguments, events) {
|
||||
emitPhase = events.success;
|
||||
},
|
||||
));
|
||||
await tester.pumpWidget(const TTSTDConnectApp());
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
emitPhase('direct');
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
expect(tester.widget<MirrorBar>(find.byType(MirrorBar)).connected, isTrue);
|
||||
devices.remove(first);
|
||||
await tester.tap(find.byIcon(Icons.refresh));
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
expect(tester.widget<ConnectBar>(find.byType(ConnectBar)).device?.id,
|
||||
first.id);
|
||||
|
||||
await tester.tap(find.text('另一设备'));
|
||||
await tester.pump();
|
||||
expect(disconnectCalls, 1);
|
||||
expect(tester.widget<MirrorBar>(find.byType(MirrorBar)).connected, isFalse);
|
||||
expect(tester.widget<ConnectBar>(find.byType(ConnectBar)).device?.id,
|
||||
first.id);
|
||||
disconnected.complete();
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
final bar = tester.widget<ConnectBar>(find.byType(ConnectBar));
|
||||
expect(bar.device?.ip, '192.168.100.141');
|
||||
expect(bar.phase, ConnectionPhase.idle);
|
||||
expect(tester.widget<MirrorBar>(find.byType(MirrorBar)).connected, isFalse);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
});
|
||||
|
||||
testWidgets('首页无设备时显示空态', (tester) async {
|
||||
_mockChannels();
|
||||
await tester.pumpWidget(const TTSTDConnectApp());
|
||||
@@ -69,28 +132,22 @@ void main() {
|
||||
expect(find.text('可连接设备'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('扫码配对对话框展示原生返回的 pairing URI', (tester) async {
|
||||
const uri = 'lancast://pair?ip=10.0.0.2&port=41001&fp=test-fingerprint';
|
||||
_mockChannels(pairingUri: uri);
|
||||
await tester.pumpWidget(const TTSTDConnectApp());
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
|
||||
await tester.tap(find.byTooltip('扫码配对').first);
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
|
||||
expect(find.text('扫码配对'), findsOneWidget);
|
||||
expect(find.text(uri), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('配对码不可用时提示而不展示假 URI', (tester) async {
|
||||
testWidgets('扫码配对入口打开粘贴窗口,不依赖原生扫码', (tester) async {
|
||||
_mockChannels();
|
||||
await tester.pumpWidget(const TTSTDConnectApp());
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
|
||||
await tester.tap(find.byTooltip('扫码配对').first);
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
|
||||
expect(find.text('当前平台不支持扫码配对'), findsOneWidget);
|
||||
expect(find.text('确认配对'), findsOneWidget);
|
||||
expect(find.textContaining('Ctrl+V'), findsOneWidget);
|
||||
expect(find.text('等待粘贴…'), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<FilledButton>(find.widgetWithText(FilledButton, '确认配对'))
|
||||
.onPressed,
|
||||
isNull);
|
||||
await tester.tap(find.text('取消'));
|
||||
await tester.pumpAndSettle(_settleTimeout);
|
||||
});
|
||||
|
||||
testWidgets('设置页可切换本机信令与外部信令', (tester) async {
|
||||
|
||||
Reference in new issue
Block a user