import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/screen.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:pipelines_app/ui/adaptive/pop_entrance.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'conversation/chat_thread_fixtures.dart'; class _Gateway extends FakeChatGateway { @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_sessions') { return Future.value({ 'chat_sessions': [ {'id': 'r', 'title': 'Recent', 'directory': '/w'}, {'id': 'z', 'title': 'Zed', 'directory': '/w'}, ], }); } if (type == 'chat_shells') return Future.value({'chat_shells': []}); return super.call(type, fields); } } Future _pumpShell( WidgetTester tester, Size size, { bool reduced = false, }) async { SharedPreferences.setMockInitialValues({}); // The chat tests run with the platform's reduced motion (see // flutter_test_config.dart); these opt back in, so controllers keep their // real durations, and choose reduced motion through the app's MediaQuery. tester.platformDispatcher.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures(); addTearDown( () => tester.platformDispatcher.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures(disableAnimations: true), ); tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway()..history = (_) async => chatPage(const []); addTearDown(gateway.close); await tester.pumpWidget( MaterialApp( theme: ThemeData(platform: TargetPlatform.android), builder: (context, child) => MediaQuery( data: MediaQuery.of(context).copyWith(disableAnimations: reduced), child: child!, ), home: AdaptiveShell( destination: AppDestination.chat, destinationBodies: { AppDestination.chat: (_) => ChatScreen(gateway: gateway), }, ), ), ); await tester.pumpAndSettle(); } Finder _composer(String id) => find.byKey(ValueKey('chat-composer-$id')); /// The composer pill's entrance. PopAnimations _pop(WidgetTester tester, String id) => tester .widget( find.descendant(of: _composer(id), matching: find.byType(PopTransition)), ) .animations; bool _playing(PopAnimations pop) => pop.opacity.value < 1; EditableText _field(WidgetTester tester, String id) => tester.widget( find.descendant(of: _composer(id), matching: find.byType(EditableText)), ); Future _ctrl(WidgetTester tester, LogicalKeyboardKey digit) async { await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); await tester.sendKeyEvent(digit); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); } /// Whether any piece of the top bar is still popping in. bool _barPlaying(WidgetTester tester) => tester .widgetList( find.descendant( of: find.byType(SegmentedBar), matching: find.byType(PopTransition), ), ) .any((transition) => _playing(transition.animations)); void main() { testWidgets('the composer pops in with the bar when a chat opens and ' 'again when another chat opens', (tester) async { await _pumpShell(tester, const Size(1280, 800)); await tester.tap(find.byKey(const Key('chat-row-r'))); await tester.pump(); await tester.pump(const Duration(milliseconds: 16)); expect(_playing(_pop(tester, 'r')), isTrue); expect(_barPlaying(tester), isTrue); await tester.pumpAndSettle(); expect(_pop(tester, 'r').opacity.value, 1); await tester.tap(find.byKey(const Key('chat-row-z'))); await tester.pump(); await tester.pump(const Duration(milliseconds: 16)); expect(_playing(_pop(tester, 'z')), isTrue); await tester.pumpAndSettle(); expect(_pop(tester, 'z').opacity.value, 1); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('typing, the keyboard and a sheet never replay it', ( tester, ) async { await _pumpShell(tester, const Size(390, 844)); await tester.tap(find.byKey(const Key('chat-row-r'))); await tester.pumpAndSettle(); final settled = _pop(tester, 'r'); expect(settled.opacity.value, 1); await tester.enterText( find.descendant(of: _composer('r'), matching: find.byType(EditableText)), 'hello', ); await tester.pump(); expect(_playing(_pop(tester, 'r')), isFalse); // The on-screen keyboard rises and falls. tester.view.viewInsets = const FakeViewPadding(bottom: 320); await tester.pump(); expect(_playing(_pop(tester, 'r')), isFalse); tester.view.resetViewInsets(); await tester.pump(); expect(_playing(_pop(tester, 'r')), isFalse); // A sheet over the chat opens and closes. showModalBottomSheet( context: tester.element(_composer('r')), builder: (_) => const SizedBox(height: 200, child: Text('sheet')), ); await tester.pumpAndSettle(); Navigator.of(tester.element(find.text('sheet'))).pop(); await tester.pump(); expect(_playing(_pop(tester, 'r')), isFalse); await tester.pumpAndSettle(); expect(identical(_pop(tester, 'r').pop, settled.pop), isTrue); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('under reduced motion the composer simply appears', ( tester, ) async { await _pumpShell(tester, const Size(1280, 800), reduced: true); await tester.tap(find.byKey(const Key('chat-row-r'))); await tester.pump(); expect(_pop(tester, 'r'), same(poppedIn)); await tester.pumpWidget(const SizedBox.shrink()); }); for (final size in [const Size(1280, 800), const Size(390, 844)]) { testWidgets('Ctrl+N focuses the composer while it is still popping in ' '(${size.width.toInt()} wide)', (tester) async { await _pumpShell(tester, size); await _ctrl(tester, LogicalKeyboardKey.digit2); // The switch frame, then the frame after it that takes the focus. await tester.pump(); await tester.pump(const Duration(milliseconds: 16)); await tester.pump(const Duration(milliseconds: 16)); expect(_playing(_pop(tester, 'z')), isTrue); expect(_field(tester, 'z').focusNode.hasPrimaryFocus, isTrue); tester.testTextInput.enterText('h'); await tester.pump(); expect(_field(tester, 'z').controller.text, 'h'); await tester.pumpAndSettle(); expect(_field(tester, 'z').focusNode.hasPrimaryFocus, isTrue); await tester.pumpWidget(const SizedBox.shrink()); }); } }