示例工程 ch06-state
ch06-state/test/state_test.dart
import 'package:ch06_state/main.dart';
import 'package:ch06_state/src/lifecycle_probe.dart';
import 'package:ch06_state/src/state_demo_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
/// 把被测 widget 包进最小可用的 MaterialApp。
Widget host(Widget child) => MaterialApp(home: Scaffold(body: child));
void main() {
group('setState 与 build', () {
testWidgets('点击按钮改状态,build 重新执行', (WidgetTester tester) async {
await tester.pumpWidget(host(const CounterCard()));
expect(find.text('count = 0'), findsOneWidget);
expect(find.text('build 次数 = 1'), findsOneWidget);
await tester.tap(find.byKey(const Key('inc')));
await tester.pump();
expect(find.text('count = 1'), findsOneWidget);
expect(find.text('build 次数 = 2'), findsOneWidget);
// 同一帧内两次 setState 只会触发一次 build(框架把重建合批了)
await tester.tap(find.byKey(const Key('inc')));
await tester.tap(find.byKey(const Key('dec')));
await tester.pump();
expect(find.text('count = 1'), findsOneWidget);
expect(find.text('build 次数 = 3'), findsOneWidget);
await tester.tap(find.byKey(const Key('reset')));
await tester.pump();
expect(find.text('count = 0'), findsOneWidget);
});
testWidgets('父级改配置:State 保留,didUpdateWidget 生效', (WidgetTester tester) async {
final ValueNotifier<int> step = ValueNotifier<int>(1);
addTearDown(step.dispose);
await tester.pumpWidget(
host(
ValueListenableBuilder<int>(
valueListenable: step,
builder: (BuildContext context, int value, Widget? child) => CounterCard(step: value),
),
),
);
await tester.tap(find.byKey(const Key('inc')));
await tester.pump();
expect(find.text('count = 1'), findsOneWidget);
// 改变 step:同一个 State 被复用,_count 没有重置
step.value = 3;
await tester.pump();
await tester.tap(find.byKey(const Key('inc')));
await tester.pump();
expect(find.text('count = 4'), findsOneWidget);
});
});
group('用户输入', () {
testWidgets('添加与删除待办,controller 清空', (WidgetTester tester) async {
await tester.pumpWidget(host(const TodoCard()));
expect(find.byKey(const Key('todo-empty')), findsOneWidget);
await tester.enterText(find.byKey(const Key('todo-input')), '写 ch06 示例');
await tester.tap(find.byKey(const Key('todo-add')));
await tester.pump();
expect(find.text('写 ch06 示例'), findsOneWidget);
expect(find.byKey(const Key('todo-empty')), findsNothing);
// 输入框已被 controller.clear() 清空
final TextField input = tester.widget<TextField>(find.byKey(const Key('todo-input')));
expect(input.controller?.text, isEmpty);
await tester.enterText(find.byKey(const Key('todo-input')), '第二条');
await tester.tap(find.byKey(const Key('todo-add')));
await tester.pump();
expect(find.byType(ListTile), findsNWidgets(2));
await tester.tap(find.descendant(
of: find.byKey(const Key('todo-item-0')),
matching: find.byType(IconButton),
));
await tester.pump();
expect(find.text('写 ch06 示例'), findsNothing);
expect(find.text('第二条'), findsOneWidget);
});
testWidgets('空白输入不会添加', (WidgetTester tester) async {
await tester.pumpWidget(host(const TodoCard()));
await tester.enterText(find.byKey(const Key('todo-input')), ' ');
await tester.tap(find.byKey(const Key('todo-add')));
await tester.pump();
expect(find.byType(ListTile), findsNothing);
});
});
group('异步状态', () {
testWidgets('loading → data 三态切换', (WidgetTester tester) async {
await tester.pumpWidget(host(const AsyncCard()));
expect(find.byKey(const Key('async-loading')), findsOneWidget);
expect(find.byKey(const Key('async-data')), findsNothing);
await tester.pump(const Duration(milliseconds: 300));
await tester.pump();
expect(find.byKey(const Key('async-data')), findsOneWidget);
expect(find.text('服务器返回: 42'), findsOneWidget);
expect(find.byKey(const Key('async-loading')), findsNothing);
});
testWidgets('页面在 await 之前被销毁:mounted 检查兜住,不抛异常', (WidgetTester tester) async {
await tester.pumpWidget(host(const AsyncCard()));
expect(find.byKey(const Key('async-loading')), findsOneWidget);
// 还没等到 300ms 就把卡片换掉 → initState 里的 Future 稍后才完成
await tester.pumpWidget(host(const Text('已卸载')));
await tester.pump(const Duration(milliseconds: 400));
expect(tester.takeException(), isNull);
});
testWidgets('重新加载按钮回到 loading 再回到 data', (WidgetTester tester) async {
await tester.pumpWidget(host(const AsyncCard()));
await tester.pump(const Duration(milliseconds: 300));
await tester.pump();
expect(find.byKey(const Key('async-data')), findsOneWidget);
await tester.tap(find.byKey(const Key('async-reload')));
await tester.pump();
expect(find.byKey(const Key('async-loading')), findsOneWidget);
await tester.pump(const Duration(milliseconds: 300));
await tester.pump();
expect(find.byKey(const Key('async-data')), findsOneWidget);
});
});
group('Stream 订阅', () {
testWidgets('每秒更新,卸载后取消订阅', (WidgetTester tester) async {
await tester.pumpWidget(host(const TickerCard()));
expect(find.text('已过 0 秒'), findsOneWidget);
await tester.pump(const Duration(seconds: 1));
expect(find.text('已过 1 秒'), findsOneWidget);
await tester.pump(const Duration(seconds: 2));
expect(find.text('已过 3 秒'), findsOneWidget);
// 卸载:dispose 里 cancel。若忘了 cancel,下面的 pump 会让 setState 打到已销毁的 State。
await tester.pumpWidget(host(const Text('已卸载')));
await tester.pump(const Duration(seconds: 5));
expect(tester.takeException(), isNull);
});
});
group('生命周期顺序', () {
testWidgets('initState → didChangeDependencies → build,配置变化走 didUpdateWidget', (WidgetTester tester) async {
final log = <String>[];
await tester.pumpWidget(host(LifecycleProbe(log: log)));
expect(log, <String>[
'probe:initState',
'probe:didChangeDependencies',
'probe:build',
]);
log.clear();
await tester.pumpWidget(host(LifecycleProbe(log: log, label: 'renamed')));
// didUpdateWidget 里读到的 widget.label 已经是新值,oldWidget.label 才是旧的
expect(log, contains('renamed:didUpdateWidget(probe→renamed)'));
expect(log, contains('renamed:build'));
expect(log, isNot(contains('renamed:initState'))); // 同一个 State 被复用
log.clear();
await tester.pumpWidget(host(const Text('gone')));
expect(log, <String>['renamed:dispose']);
});
});
group('整页冒烟', () {
testWidgets('四张卡片都能渲染,无异常', (WidgetTester tester) async {
tester.view.physicalSize = const Size(500, 1400);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(const StateApp());
await tester.pumpAndSettle(const Duration(milliseconds: 400));
expect(find.byType(CounterCard), findsOneWidget);
expect(find.byType(TodoCard), findsOneWidget);
expect(find.byType(AsyncCard), findsOneWidget);
expect(find.byType(TickerCard), findsOneWidget);
expect(tester.takeException(), isNull);
});
});
}