import 'package:flutter/cupertino.dart'; 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/search/search_screen.dart'; import 'package:pipelines_app/features/search/ui/search_bar_controls.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; class _Gateway implements ApiGateway { @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async => { 'search_results': {'results': [], 'truncated': false}, }; } EditableText _field(WidgetTester tester) => tester.widget( find.descendant( of: find.byKey(searchFieldKey), matching: find.byType(EditableText), ), ); Future _press(WidgetTester tester, LogicalKeyboardKey key) async { await tester.sendKeyEvent(key); await tester.pumpAndSettle(); } Future _search(WidgetTester tester) async { await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); await tester.sendKeyEvent(LogicalKeyboardKey.slash); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); await tester.pumpAndSettle(); } Future _pump( WidgetTester tester, AppDestination from, { bool cupertino = false, }) async { tester.view.physicalSize = const Size(1200, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final shell = AdaptiveShell( cupertino: cupertino, destination: from, destinationBodies: { for (final destination in AppDestination.values) destination: (_) => Text('Body ${destination.name}'), AppDestination.search: (_) => SearchScreen(gateway: _Gateway(), onOpen: (_) {}, embedded: true), }, ); await tester.pumpWidget( cupertino ? CupertinoApp(home: shell) : MaterialApp(home: shell), ); await tester.pumpAndSettle(); } void main() { for (final from in [ AppDestination.chat, AppDestination.runs, AppDestination.pipelines, ]) { testWidgets( 'Escape focuses Search, then returns to ${from.name} without clearing query', (tester) async { await _pump(tester, from); await _search(tester); await tester.enterText(find.byKey(searchFieldKey), 'release'); await tester.pumpAndSettle(); await _press(tester, LogicalKeyboardKey.tab); expect(_field(tester).focusNode.hasPrimaryFocus, isFalse); await _press(tester, LogicalKeyboardKey.escape); expect(_field(tester).focusNode.hasPrimaryFocus, isTrue); expect(_field(tester).controller.text, 'release'); await _press(tester, LogicalKeyboardKey.escape); expect(find.text('Body ${from.name}'), findsOneWidget); await _search(tester); expect(_field(tester).controller.text, 'release'); expect(_field(tester).focusNode.hasPrimaryFocus, isTrue); await tester.pumpWidget(const SizedBox.shrink()); }, ); } testWidgets( 'Cupertino Escape preserves native Search focus handoff and previous tab', (tester) async { await _pump(tester, AppDestination.runs, cupertino: true); await _search(tester); await tester.enterText(find.byKey(searchFieldKey), 'query'); await tester.pumpAndSettle(); _field(tester).focusNode.unfocus(); await tester.pumpAndSettle(); await _press(tester, LogicalKeyboardKey.escape); expect(_field(tester).focusNode.hasPrimaryFocus, isTrue); expect(_field(tester).controller.text, 'query'); await _press(tester, LogicalKeyboardKey.escape); expect(find.text('Body runs'), findsOneWidget); await tester.pumpWidget(const SizedBox.shrink()); }, ); testWidgets('Escape dismisses a Search dialog before changing tabs', ( tester, ) async { await _pump(tester, AppDestination.chat); await _search(tester); await tester.tap(find.byKey(searchHelpKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsOneWidget); await _press(tester, LogicalKeyboardKey.escape); expect(find.byType(AlertDialog), findsNothing); expect(find.byKey(searchFieldKey), findsOneWidget); expect(find.text('Body chat'), findsNothing); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('Escape dismisses a Search menu before changing tabs', ( tester, ) async { await _pump(tester, AppDestination.chat); await _search(tester); await tester.tap(find.byKey(searchKindKey)); await tester.pumpAndSettle(); expect( find.byWidgetPredicate((widget) => widget is PopupMenuEntry), findsWidgets, ); await _press(tester, LogicalKeyboardKey.escape); expect( find.byWidgetPredicate((widget) => widget is PopupMenuEntry), findsNothing, ); expect(find.byKey(searchFieldKey), findsOneWidget); expect(find.text('Body chat'), findsNothing); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('modified Escape and hidden Search never consume tab history', ( tester, ) async { await _pump(tester, AppDestination.pipelines); await _search(tester); for (final modifier in [ LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft, LogicalKeyboardKey.altLeft, LogicalKeyboardKey.metaLeft, ]) { await tester.sendKeyDownEvent(modifier); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.sendKeyUpEvent(modifier); await tester.pumpAndSettle(); expect(find.byKey(searchFieldKey), findsOneWidget); } await _search(tester); await _press(tester, LogicalKeyboardKey.escape); expect(find.text('Body pipelines'), findsOneWidget); await _press(tester, LogicalKeyboardKey.escape); expect(find.text('Body pipelines'), findsOneWidget); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('initial Search with no previous tab stays open on Escape', ( tester, ) async { await _pump(tester, AppDestination.search); await _search(tester); await tester.enterText(find.byKey(searchFieldKey), 'keep'); await tester.pumpAndSettle(); await _press(tester, LogicalKeyboardKey.escape); expect(find.byKey(searchFieldKey), findsOneWidget); expect(_field(tester).controller.text, 'keep'); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('holding Escape does not perform the second back action', ( tester, ) async { await _pump(tester, AppDestination.chat); await _search(tester); _field(tester).focusNode.unfocus(); await tester.pumpAndSettle(); await tester.sendKeyDownEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(_field(tester).focusNode.hasPrimaryFocus, isTrue); await tester.sendKeyRepeatEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.byKey(searchFieldKey), findsOneWidget); expect(find.text('Body chat'), findsNothing); await tester.sendKeyUpEvent(LogicalKeyboardKey.escape); await tester.pumpWidget(const SizedBox.shrink()); }); }