Files
photo-live/apps/mobile/test/pages/share_qr_dialog_test.dart
T
2026-09-19 01:56:38 +08:00

73 lines
2.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 分享二维码对话框 widget 测试(P3 计划 Task 4)。
///
/// 覆盖:QrImageView 以 viewer URL({VIEWER_BASE}/a/{token})渲染、
/// URL 文本可见、复制按钮写入剪贴板并提示。
library;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:photo_live/core/config.dart';
import 'package:photo_live/l10n/app_localizations.dart';
import 'package:photo_live/pages/share_qr_dialog.dart';
import 'package:qr_flutter/qr_flutter.dart';
void main() {
const token = 'sharetoken000000000001';
Future<void> pumpDialog(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
// 复制提示走 ScaffoldMessenger:包一层 Scaffold 供 SnackBar 挂载。
home: const Scaffold(
body: Center(child: ShareQrDialog(publicToken: token)),
),
),
);
await tester.pumpAndSettle();
}
testWidgets('渲染 QrImageView,data 为观众链接;URL 文本可见', (tester) async {
await pumpDialog(tester);
final url = ApiConfig.viewerAlbumUrl(token);
// qr_flutter 4.1 未公开 QrImageView.data(私有 _data),以同源的
// SelectableText 文本断言 URL 构造(二维码与文本同用该局部变量)。
expect(find.byType(QrImageView), findsOneWidget);
expect(
tester.widget<SelectableText>(find.byType(SelectableText)).data,
url,
);
expect(url, 'http://10.0.2.2:5175/a/$token');
expect(find.text('Scan the code or open the link to view'), findsOneWidget);
});
testWidgets('复制按钮 → 剪贴板收到 URL + 已复制提示', (tester) async {
final clipboardCalls = <MethodCall>[];
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async {
clipboardCalls.add(call);
return null;
},
);
await pumpDialog(tester);
await tester.tap(find.text('Copy link'));
await tester.pumpAndSettle();
final url = ApiConfig.viewerAlbumUrl(token);
expect(
clipboardCalls.any(
(c) =>
c.method == 'Clipboard.setData' &&
(c.arguments as Map<String, dynamic>)['text'] == url,
),
isTrue,
);
expect(find.text('Copied'), findsOneWidget);
});
}