import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/chat_stop_icon.dart'; import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart'; import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart'; import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; import 'package:pipelines_app/ui/motion/motion.dart'; import 'chat_composer_bar_fixtures.dart'; class _CountPaint extends CustomPainter { _CountPaint(this.onPaint); final VoidCallback onPaint; @override void paint(Canvas canvas, Size size) => onPaint(); @override bool shouldRepaint(_CountPaint oldDelegate) => false; } void main() { setUp(() { TestWidgetsFlutterBinding.ensureInitialized() .platformDispatcher .accessibilityFeaturesTestValue = const FakeAccessibilityFeatures( disableAnimations: false, ); }); tearDown(() { TestWidgetsFlutterBinding.ensureInitialized() .platformDispatcher .accessibilityFeaturesTestValue = const FakeAccessibilityFeatures( disableAnimations: true, ); }); testWidgets('rotation repaints only the icon, not the surrounding chat', ( tester, ) async { var paints = 0; await tester.pumpWidget( MaterialApp( home: Scaffold( body: CustomPaint( painter: _CountPaint(() => paints++), child: const ChatStopIcon(child: Icon(Icons.stop)), ), ), ), ); final before = paints; await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1)); expect(paints, before); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets( 'Stop rotates linearly and continuously once every eight seconds', (tester) async { await tester.pumpWidget( const MaterialApp( home: Scaffold(body: ChatStopIcon(child: Icon(Icons.stop))), ), ); RotationTransition rotation() => tester.widget(find.byKey(const Key('chat-stop-rotation'))); await tester.pump(const Duration(seconds: 2)); expect(rotation().turns.value, closeTo(0.25, 0.001)); await tester.pump(const Duration(seconds: 2)); expect(rotation().turns.value, closeTo(0.5, 0.001)); await tester.pump(const Duration(seconds: 8)); expect(rotation().turns.value, closeTo(0.5, 0.001)); await tester.pumpWidget(const SizedBox.shrink()); expect(tester.binding.transientCallbackCount, 0); }, ); testWidgets('reduced motion stops/reset rotation and resumes when enabled', ( tester, ) async { final reduce = ValueNotifier(true); addTearDown(reduce.dispose); await tester.pumpWidget( MaterialApp( home: ValueListenableBuilder( valueListenable: reduce, builder: (context, value, _) => MediaQuery( data: MediaQuery.of(context).copyWith(disableAnimations: value), child: const ChatStopIcon(child: Icon(Icons.stop)), ), ), ), ); RotationTransition rotation() => tester.widget(find.byKey(const Key('chat-stop-rotation'))); await tester.pumpAndSettle(); expect(rotation().turns.value, 0); expect(tester.binding.transientCallbackCount, 0); reduce.value = false; await tester.pump(); await tester.pump(const Duration(seconds: 1)); expect(rotation().turns.value, closeTo(0.125, 0.001)); reduce.value = true; await tester.pumpAndSettle(); expect(rotation().turns.value, 0); expect(tester.binding.transientCallbackCount, 0); }); testWidgets('TickerMode silences hidden Stop animation', (tester) async { await tester.pumpWidget( const MaterialApp( home: TickerMode( enabled: false, child: ChatStopIcon(child: Icon(Icons.stop)), ), ), ); await tester.pumpAndSettle(); expect(tester.binding.transientCallbackCount, 0); await tester.pumpWidget(const SizedBox.shrink()); }); for (final cupertino in [false, true]) { testWidgets( 'rotating Stop keeps its target and interrupt action; Send stays available ($cupertino)', (tester) async { final gateway = ComposerBarGateway(); addTearDown(gateway.close); final composer = ChatComposerController( gateway, 's', drafts: MemoryChatDraftStore(), ); addTearDown(composer.dispose); final running = ValueNotifier(true); addTearDown(running.dispose); final bar = ValueListenableBuilder( valueListenable: running, builder: (_, value, _) => ChatComposerBar(composer: composer, running: value), ); await tester.pumpWidget( cupertino ? CupertinoApp( home: CupertinoPageScaffold(child: Center(child: bar)), ) : MaterialApp( home: Scaffold(body: Center(child: bar)), ), ); // The composer pops in first; Stop's target is measured once it has. await tester.pump(Motion.tabEntranceDuration); final stop = find.byKey(const Key('chat-composer-stop')); final target = tester.getRect(stop); await tester.pump(const Duration(seconds: 3)); expect(tester.getRect(stop), target); expect(find.byKey(const Key('chat-composer-primary')), findsOneWidget); expect( tester .widget( find.byKey(const Key('chat-stop-rotation')), ) .turns .value, greaterThan(0), ); await tester.tap(stop); await tester.pump(); await tester.pump(); expect(gateway.interruptBodies, [ {'session_id': 's'}, ]); running.value = false; await tester.pumpAndSettle(); expect(find.byType(ChatStopIcon), findsNothing); expect(tester.binding.transientCallbackCount, 0); }, ); } }