import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:yx_net_inspector/src/widgets/floating_ball.dart'; import 'package:yx_net_inspector/src/controller/yx_net_inspector_controller.dart'; import 'package:yx_net_inspector/src/models/inspector_config.dart'; import 'package:yx_net_inspector/src/models/inspector_theme.dart'; void main() { group('YxFloatingBall Widget Tests', () { late YxNetInspectorController controller; late YxNetInspectorConfig config; late YxNetInspectorTheme theme; setUp(() { controller = YxNetInspectorController.instance; controller.clearLogs(); config = const YxNetInspectorConfig(); theme = const YxNetInspectorTheme(); controller.initialize(config); }); tearDown(() { controller.clearLogs(); }); testWidgets('应该渲染悬浮球组件', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: config, theme: theme, controller: controller, ), ), ), ); // 验证悬浮球是否存在 expect(find.byType(YxFloatingBall), findsOneWidget); expect(find.byType(Positioned), findsOneWidget); }); testWidgets('应该显示网络图标', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: config, theme: theme, controller: controller, ), ), ), ); // 查找网络图标 expect(find.byIcon(Icons.network_check), findsOneWidget); }); testWidgets('应该显示请求数量徽章', (WidgetTester tester) async { // 添加一些请求 controller.logRequest( id: 'test-1', method: 'GET', url: 'https://example.com'); controller.logRequest( id: 'test-2', method: 'POST', url: 'https://example.com'); await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: config, theme: theme, controller: controller, ), ), ), ); await tester.pump(); // 等待状态更新 // 验证徽章显示请求数量 expect(find.text('2'), findsOneWidget); }); testWidgets('应该显示错误数量徽章', (WidgetTester tester) async { // 添加请求和错误 controller.logRequest( id: 'test-1', method: 'GET', url: 'https://example.com'); controller.logError(id: 'test-1', error: '网络错误'); await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: config, theme: theme, controller: controller, ), ), ), ); await tester.pump(); // 等待状态更新 // 验证错误徽章 expect(find.text('1'), findsWidgets); // 应该有请求数和错误数的徽章 }); testWidgets('点击悬浮球应该打开检查器面板', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: config, theme: theme, controller: controller, ), ), ), ); // 点击悬浮球 await tester.tap(find.byType(GestureDetector)); await tester.pumpAndSettle(); // 验证是否打开了对话框 expect(find.byType(Dialog), findsOneWidget); }); testWidgets('悬浮球应该可以拖拽', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: config, theme: theme, controller: controller, ), ), ), ); // 获取初始位置 final initialFinder = find.byType(Positioned); expect(initialFinder, findsOneWidget); // 执行拖拽操作 await tester.drag(find.byType(GestureDetector), const Offset(100, 50)); await tester.pumpAndSettle(); // 验证位置已改变(这里我们主要验证没有报错,因为具体位置计算较复杂) expect(find.byType(Positioned), findsOneWidget); }); testWidgets('不可拖拽配置时悬浮球不应该移动', (WidgetTester tester) async { final nonDraggableConfig = config.copyWith(draggable: false); await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: nonDraggableConfig, theme: theme, controller: controller, ), ), ), ); // 尝试拖拽 await tester.drag(find.byType(GestureDetector), const Offset(100, 50)); await tester.pumpAndSettle(); // 验证组件仍然存在且没有报错 expect(find.byType(YxFloatingBall), findsOneWidget); }); testWidgets('隐藏徽章配置时不应该显示徽章', (WidgetTester tester) async { final noBadgeConfig = config.copyWith(showBadge: false); // 添加一些请求 controller.logRequest( id: 'test-1', method: 'GET', url: 'https://example.com'); await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: noBadgeConfig, theme: theme, controller: controller, ), ), ), ); await tester.pump(); // 验证没有显示数字徽章 expect(find.text('1'), findsNothing); }); testWidgets('自定义颜色应该正确应用', (WidgetTester tester) async { final customConfig = config.copyWith(ballColor: Colors.red); await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: customConfig, theme: theme, controller: controller, ), ), ), ); // 查找容器组件(这里我们主要验证没有报错) expect(find.byType(Container), findsWidgets); }); testWidgets('自定义大小应该正确应用', (WidgetTester tester) async { final customSizeConfig = config.copyWith(ballSize: 80.0); await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: customSizeConfig, theme: theme, controller: controller, ), ), ), ); // 验证组件正常渲染 expect(find.byType(YxFloatingBall), findsOneWidget); }); testWidgets('长按悬浮球应该有反馈', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp( home: Scaffold( body: YxFloatingBall( config: config, theme: theme, controller: controller, ), ), ), ); // 执行长按操作 await tester.longPress(find.byType(GestureDetector)); await tester.pumpAndSettle(); // 验证没有报错(具体反馈行为可能需要根据实际实现调整) expect(find.byType(YxFloatingBall), findsOneWidget); }); }); }