1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import 'package:ch02_first_app/main.dart';
import 'package:ch02_first_app/src/home_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('第一个 Flutter App', () {
testWidgets('启动后渲染 AppBar 与所有知识卡片', (WidgetTester tester) async {
await tester.pumpWidget(const DartBookApp());
await tester.pumpAndSettle();
expect(find.byType(Scaffold), findsOneWidget);
expect(find.byType(AppBar), findsOneWidget);
expect(find.text('第一个 Flutter App'), findsOneWidget);
expect(find.byType(Card), findsNWidgets(facts.length));
expect(find.text('main() → runApp()'), findsOneWidget);
});
testWidgets('点击 FAB 弹出对话框,点"好"关闭', (WidgetTester tester) async {
await tester.pumpWidget(const DartBookApp());
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
await tester.tap(find.byKey(const Key('about-fab')));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.textContaining('第 2 章示例'), findsOneWidget);
await tester.tap(find.text('好'));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
});
testWidgets('窗口尺寸变化后布局不抛异常(桌面端会真的拉窗口)', (WidgetTester tester) async {
tester.view.physicalSize = const Size(420, 780);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(const DartBookApp());
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.byType(ListView), findsOneWidget);
tester.view.physicalSize = const Size(1280, 800);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
});
test('facts 常量表结构自洽', () {
expect(facts, isNotEmpty);
for (final Fact fact in facts) {
expect(fact.title, isNotEmpty);
expect(fact.body, isNotEmpty);
}
});
});
}