import 'package:ch16_bookmarks/main.dart'; import 'package:ch16_bookmarks/src/data/database.dart'; import 'package:ch16_bookmarks/src/application/providers.dart'; import 'package:ch16_bookmarks/src/domain/bookmark_repository.dart'; import 'package:ch16_bookmarks/src/presentation/router.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'repository_test.dart' show FakeRemoteApi, sampleDrafts; /// 整个 App 的可测试装配:只替换"基础设施层",UI/路由/状态逻辑全部走真实实现。 Widget testApp({required AppDatabase db, required BookmarkRemoteApi remote}) => ProviderScope( overrides: [ databaseProvider.overrideWithValue(db), remoteApiProvider.overrideWithValue(remote), ], child: const BookmarksApp(), ); void main() { late AppDatabase db; setUp(() { db = AppDatabase.memory(); }); Future pumpApp(WidgetTester tester, {BookmarkRemoteApi? remote}) async { tester.view.physicalSize = const Size(800, 1200); tester.view.devicePixelRatio = 1.0; addTearDown(tester.view.reset); await tester.pumpWidget(testApp(db: db, remote: remote ?? FakeRemoteApi())); await tester.pumpAndSettle(); } Future unmount(WidgetTester tester) async { // 卸载 → 释放 drift 的 stream watcher(否则会报 "Timer 仍挂起") await tester.pumpWidget(const SizedBox.shrink()); await tester.pumpAndSettle(); await db.close(); } testWidgets('空状态 → 新建 → 列表显示', (WidgetTester tester) async { await pumpApp(tester); expect(find.byKey(const Key('empty-message')), findsOneWidget); expect(find.text('还没有书签'), findsOneWidget); await tester.tap(find.byKey(const Key('add'))); await tester.pumpAndSettle(); expect(find.text('新建书签'), findsOneWidget); await tester.enterText(find.byKey(const Key('title')), 'Dart 中文社区'); await tester.enterText(find.byKey(const Key('url')), 'https://dart.cn'); await tester.enterText(find.byKey(const Key('note')), '中文文档'); await tester.tap(find.byKey(const Key('save'))); await tester.pumpAndSettle(); // 回到首页,列表里有条目 expect(find.byKey(const Key('bookmark-list')), findsOneWidget); expect(find.text('Dart 中文社区'), findsOneWidget); expect(find.text('中文文档'), findsOneWidget); await unmount(tester); }); testWidgets('表单校验:空标题/非法链接不保存', (WidgetTester tester) async { await pumpApp(tester); await tester.tap(find.byKey(const Key('add'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('save'))); await tester.pumpAndSettle(); expect(find.text('标题不能为空'), findsOneWidget); expect(find.text('需要 http(s) 链接'), findsOneWidget); await tester.enterText(find.byKey(const Key('title')), 'x'); await tester.enterText(find.byKey(const Key('url')), 'javascript:alert(1)'); await tester.tap(find.byKey(const Key('save'))); await tester.pumpAndSettle(); expect(find.text('需要 http(s) 链接'), findsOneWidget); // 仍在编辑页,没有跳回列表 expect(find.text('新建书签'), findsOneWidget); expect(find.byKey(const Key('bookmark-list')), findsNothing); await unmount(tester); }); testWidgets('搜索:命中与空结果文案区分', (WidgetTester tester) async { await pumpApp(tester, remote: FakeRemoteApi(drafts: sampleDrafts)); await tester.tap(find.byKey(const Key('sync'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('bookmark-list')), findsOneWidget); // 防抖:150ms 内不过滤 await tester.enterText(find.byKey(const Key('search')), 'flutter'); await tester.pump(const Duration(milliseconds: 50)); expect(find.text('Dart 官网'), findsOneWidget); // 还在,尚未过滤 await tester.pump(const Duration(milliseconds: 200)); expect(find.text('Flutter 官网'), findsOneWidget); expect(find.text('Dart 官网'), findsNothing); await tester.enterText(find.byKey(const Key('search')), 'zzz'); await tester.pump(const Duration(milliseconds: 200)); expect(find.byKey(const Key('empty-message')), findsOneWidget); expect(find.textContaining('没有匹配「zzz」'), findsOneWidget); await tester.enterText(find.byKey(const Key('search')), ''); await tester.pump(const Duration(milliseconds: 200)); expect(find.text('Flutter 官网'), findsOneWidget); expect(find.text('Dart 官网'), findsOneWidget); await unmount(tester); }); testWidgets('收藏切换与详情页删除', (WidgetTester tester) async { await pumpApp(tester, remote: FakeRemoteApi(drafts: sampleDrafts)); await tester.tap(find.byKey(const Key('sync'))); await tester.pumpAndSettle(); // 找到"Flutter 官网"那条的收藏按钮 final Finder flutterTile = find.ancestor( of: find.text('Flutter 官网'), matching: find.byType(ListTile), ); final Finder favButton = find.descendant(of: flutterTile, matching: find.byType(IconButton)); await tester.tap(favButton); await tester.pumpAndSettle(); expect(find.byIcon(Icons.star), findsWidgets); // 至少有一个已收藏 // 进详情页 await tester.tap(find.text('Flutter 官网')); await tester.pumpAndSettle(); expect(find.byKey(const Key('detail-title')), findsOneWidget); expect(find.text('https://flutter.dev'), findsOneWidget); // 删除(含确认框) await tester.tap(find.byKey(const Key('delete'))); await tester.pumpAndSettle(); expect(find.text('删除这条书签?'), findsOneWidget); await tester.tap(find.byKey(const Key('confirm-delete'))); await tester.pumpAndSettle(); expect(find.text('Flutter 官网'), findsNothing); expect(find.text('Dart 官网'), findsOneWidget); // 只剩一条 await unmount(tester); }); testWidgets('同步失败显示错误,不污染列表', (WidgetTester tester) async { await pumpApp(tester, remote: FakeRemoteApi(error: const SyncException('服务器 500'))); await tester.tap(find.byKey(const Key('sync'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('sync-error')), findsOneWidget); expect(find.textContaining('服务器 500'), findsOneWidget); expect(find.byKey(const Key('empty-message')), findsOneWidget); await unmount(tester); }); testWidgets('重复同步不会产生重复条目', (WidgetTester tester) async { await pumpApp(tester, remote: FakeRemoteApi(drafts: sampleDrafts)); await tester.tap(find.byKey(const Key('sync'))); await tester.pumpAndSettle(); expect(find.text('本次导入 2 条'), findsOneWidget); await tester.tap(find.byKey(const Key('sync'))); await tester.pumpAndSettle(); expect(find.text('本次导入 0 条'), findsOneWidget); expect(find.byType(ListTile), findsNWidgets(2)); await unmount(tester); }); testWidgets('未知路径显示 404 页', (WidgetTester tester) async { await pumpApp(tester); // 直接改路由(等价于深链接一个不存在的路径) final ProviderContainer container = ProviderScope.containerOf( tester.element(find.byKey(const Key('add'))), ); container.read(routerProvider).go('/no-such-page'); await tester.pumpAndSettle(); expect(find.byKey(const Key('not-found')), findsOneWidget); await unmount(tester); }); }