This commit is contained in:
2026-08-04 17:04:18 +08:00
parent c7ebb5a205
commit 5746d773de
15 changed files with 187 additions and 56 deletions
+65
View File
@@ -0,0 +1,65 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:slogan_app/shared/app_toast.dart';
void main() {
testWidgets('showToast 显示在屏幕正中心', (tester) async {
await tester.pumpWidget(MaterialApp(
navigatorKey: appNavigatorKey,
home: const Scaffold(body: Center(child: Text('页面'))),
));
showToast('上传成功');
await tester.pump();
final text = find.text('上传成功');
expect(text, findsOneWidget);
final center = tester.getCenter(text);
final screen = tester.getSize(find.byType(MaterialApp));
expect(center.dx, closeTo(screen.width / 2, 1));
expect(center.dy, closeTo(screen.height / 2, 1));
// 等 toast 消失,避免测试结束时残留 Timer
await tester.pump(const Duration(seconds: 3));
});
testWidgets('短文本 2 秒后消失', (tester) async {
await tester.pumpWidget(MaterialApp(
navigatorKey: appNavigatorKey,
home: const Scaffold(body: Center(child: Text('页面'))),
));
showToast('上传成功');
await tester.pump();
expect(find.text('上传成功'), findsOneWidget);
// 2 秒整之前仍在
await tester.pump(const Duration(milliseconds: 1900));
expect(find.text('上传成功'), findsOneWidget);
// 超过 2 秒后消失
await tester.pump(const Duration(milliseconds: 200));
expect(find.text('上传成功'), findsNothing);
});
testWidgets('长文本 4 秒后消失', (tester) async {
await tester.pumpWidget(MaterialApp(
navigatorKey: appNavigatorKey,
home: const Scaffold(body: Center(child: Text('页面'))),
));
showToast('这是一个超过二十个字符的非常长的提示消息内容展示测试');
await tester.pump();
expect(find.text('这是一个超过二十个字符的非常长的提示消息内容展示测试'),
findsOneWidget);
await tester.pump(const Duration(seconds: 3));
expect(find.text('这是一个超过二十个字符的非常长的提示消息内容展示测试'),
findsOneWidget);
await tester.pump(const Duration(seconds: 1, milliseconds: 500));
expect(find.text('这是一个超过二十个字符的非常长的提示消息内容展示测试'),
findsNothing);
});
}