import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_view.dart'; import 'package:pipelines_app/features/terminal/terminal_hub.dart'; import 'package:pipelines_app/features/terminal/terminal_layout.dart'; import 'package:pipelines_app/features/terminal/terminal_surface.dart'; import 'package:pipelines_app/features/terminal/terminal_toggle_button.dart'; import 'package:pipelines_app/features/terminal/terminal_window.dart'; import 'package:pipelines_app/shell/windows/window_host.dart'; import 'package:pipelines_app/shell/windows/window_kind.dart'; import 'package:pipelines_app/shell/windows/window_layer.dart'; import 'package:pipelines_app/shell/windows/window_manager.dart'; const appField = Key('app-field'); /// An app with a text field under the terminal surface, [size] logical /// pixels, optionally asking for reduced motion. Future pumpTerminalApp( WidgetTester tester, { required TerminalHub hub, required TerminalLayout layout, Size size = const Size(1200, 800), bool reduceMotion = false, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( MaterialApp( builder: (context, child) => MediaQuery( data: MediaQuery.of(context).copyWith(disableAnimations: reduceMotion), child: child!, ), home: TerminalTestApp(hub: hub, layout: layout), ), ); await tester.pump(); } /// The terminal surface around an app with a text field, with the app's /// window system as the real app wires it: the terminal's floating mode is a /// window in the host above the surface's stage. class TerminalTestApp extends StatefulWidget { const TerminalTestApp({ super.key, required this.hub, required this.layout, this.shortcutHere = true, }); final TerminalHub hub; final TerminalLayout layout; final bool shortcutHere; @override State createState() => TerminalTestAppState(); } class TerminalTestAppState extends State { final WindowManager manager = WindowManager(kinds: WindowKinds()); late final TerminalWindows floating = TerminalWindows( manager: manager, hub: widget.hub, layout: widget.layout, ); @override void initState() { super.initState(); floating ..start() ..ready(); } @override void dispose() { floating.dispose(); manager.dispose(); super.dispose(); } @override Widget build(BuildContext context) => TerminalSurface( hub: widget.hub, layout: widget.layout, shortcutHere: widget.shortcutHere, floating: floating, windows: WindowHost(manager: manager), child: Scaffold( appBar: AppBar( title: const Text('App'), actions: [ TerminalToggleButton(layout: widget.layout), ], ), body: const TextField(key: appField), ), ); } /// The test app's window manager. WindowManager terminalWindowManager(WidgetTester tester) => tester.state(find.byType(TerminalTestApp)).manager; /// Presses [key] with [modifiers] held, then lets the motion finish unless /// [settle] is false. Future chord( WidgetTester tester, LogicalKeyboardKey key, List modifiers, { bool settle = true, }) async { for (final modifier in modifiers) { await tester.sendKeyDownEvent(modifier); } await tester.sendKeyEvent(key); for (final modifier in modifiers.reversed) { await tester.sendKeyUpEvent(modifier); } if (settle) { await tester.pumpAndSettle(); } else { await tester.pump(); } } Future altT(WidgetTester tester, {bool settle = true}) => chord( tester, LogicalKeyboardKey.keyT, [LogicalKeyboardKey.altLeft], settle: settle, ); Future ctrlTab(WidgetTester tester, {bool reverse = false}) => chord( tester, LogicalKeyboardKey.tab, [LogicalKeyboardKey.controlLeft, if (reverse) LogicalKeyboardKey.shiftLeft], ); /// The terminal whose emulator has the keyboard, or null. ChatShellTerminalView? focusedTerminal() => FocusManager .instance .primaryFocus ?.context ?.findAncestorWidgetOfExactType(); /// Whether [tab]'s emulator has the keyboard. bool terminalFocused(String key) => focusedTerminal()?.key == ValueKey('terminal-view-$key'); /// Whether the app's text field has the keyboard. bool appFieldFocused(WidgetTester tester) => tester .widget( find.descendant( of: find.byKey(appField), matching: find.byType(EditableText), ), ) .focusNode .hasFocus; final Finder panelFinder = find.byKey(const ValueKey('terminal-panel')); /// The terminal's floating window, shown or hiding. final Finder floatLayer = find.byWidgetPredicate( (w) => w is WindowLayer && w.entry.kind == terminalWindowKind, skipOffstage: false, ); /// The terminal's floating window while it is shown. final Finder floatFinder = find.byWidgetPredicate( (w) => w is WindowLayer && w.entry.kind == terminalWindowKind && w.visible, );