Files
pipelines/app/test/features/terminal/terminal_shortcuts_test.dart
T
alan b44938d028 Open the on-screen keyboard for terminals on phones
On Android and iOS a tap on a terminal (a chat command card or the terminal
panel) now opens the soft keyboard and typing goes to the process, with no
autocorrect. Desktop keeps raw key events. Desktop keyboard tests now run
as macOS, since tests default to Android.
2026-11-10 00:19:46 +00:00

306 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.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_shortcuts.dart';
import 'package:pipelines_app/features/terminal/terminal_tab.dart';
import 'package:pipelines_app/ui/input/keyboard_owner.dart';
import 'package:xterm/xterm.dart';
import 'terminal_app.dart';
import 'terminal_fixtures.dart';
void main() {
late TerminalGateway mac;
late TerminalHub hub;
late TerminalLayout layout;
setUp(() {
mac = TerminalGateway()
..add('term_a', cwd: '/Users/op/src')
..add('term_b', cwd: '/Users/op/docs')
..add('term_c', cwd: '/Users/op/notes');
hub = TerminalHub(TestMachines([machine('mac', mac, name: 'Mac')]));
layout = TerminalLayout(store: MemoryLayoutStore());
});
tearDown(() async {
hub.dispose();
await mac.close();
});
String key(String id) => terminalKey('mac', id);
const toggles = {
'Alt+T': (LogicalKeyboardKey.keyT, LogicalKeyboardKey.altLeft),
'Ctrl+T': (LogicalKeyboardKey.keyT, LogicalKeyboardKey.controlLeft),
'Cmd+T': (LogicalKeyboardKey.keyT, LogicalKeyboardKey.metaLeft),
'Ctrl+`': (LogicalKeyboardKey.backquote, LogicalKeyboardKey.controlLeft),
};
for (final mode in TerminalMode.values) {
for (final MapEntry(key: name, value: (letter, modifier))
in toggles.entries) {
desktopTestWidgets(
'$name toggles the ${mode.label.toLowerCase()} from a text '
'field and from inside the terminal',
(tester) async {
layout.setMode(mode);
final surface = mode == TerminalMode.panel
? panelFinder
: floatFinder;
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, letter, [modifier]);
expect(surface, findsOneWidget);
expect(layout.visible, isTrue);
// The shortcut never types into the field it was pressed in.
expect(find.text('t'), findsNothing);
expect(terminalFocused(key('term_a')), isTrue);
// Inside the terminal the same keys hide it instead of reaching the
// shell (Ctrl+T would be transpose-chars there).
final typed = mac.bodies('terminal_input').length;
await chord(tester, letter, [modifier]);
expect(surface, findsNothing);
expect(layout.visible, isFalse);
expect(mac.bodies('terminal_input'), hasLength(typed));
expect(hub.tabs, hasLength(3));
},
);
}
}
test('Option+T on a Mac, which types †, toggles by key position', () {
const event = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.keyT,
logicalKey: LogicalKeyboardKey(0x2020),
character: '†',
timeStamp: Duration.zero,
);
expect(terminalToggleShortcut(event, _AltHeld()), isTrue);
});
desktopTestWidgets('holding the chord toggles once and types nothing', (
tester,
) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyT);
await tester.pumpAndSettle();
expect(terminalFocused(key('term_a')), isTrue);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.keyT);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.keyT);
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyT);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pumpAndSettle();
expect(layout.visible, isTrue);
expect(mac.bodies('terminal_input'), isEmpty);
});
desktopTestWidgets('Shift or a second modifier leaves T alone', (
tester,
) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
for (final modifiers in [
[LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft],
[LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft],
[LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.shiftLeft],
]) {
await chord(tester, LogicalKeyboardKey.keyT, modifiers);
expect(layout.visible, isFalse, reason: '$modifiers');
}
});
desktopTestWidgets(
'showing focuses the terminal; hiding gives the keyboard back '
'to the field that had it',
(tester) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
expect(appFieldFocused(tester), isTrue);
await altT(tester);
expect(terminalFocused(key('term_a')), isTrue);
expect(appFieldFocused(tester), isFalse);
// Typing goes straight to the shell.
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], 'x');
await altT(tester);
expect(appFieldFocused(tester), isTrue);
// The bar button focuses it as well.
await tester.tap(find.byIcon(Icons.terminal));
await tester.pumpAndSettle();
expect(terminalFocused(key('term_a')), isTrue);
},
);
desktopTestWidgets('a terminal left shown before a reload is focused once it '
'lists', (tester) async {
layout.show();
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.pumpAndSettle();
expect(panelFinder, findsOneWidget);
expect(terminalFocused(key('term_a')), isTrue);
// The emulator's size reaches the server after its debounce.
await tester.pump(const Duration(milliseconds: 300));
});
desktopTestWidgets('Ctrl+Tab and Ctrl+Shift+Tab move between terminal tabs, '
'wrapping, and focus each; hidden, they do nothing here', (tester) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await ctrlTab(tester);
expect(hub.active, isFalse);
await tester.tap(find.byKey(appField));
await altT(tester);
for (final id in ['term_b', 'term_c', 'term_a', 'term_b']) {
await ctrlTab(tester);
expect(hub.selected!.info.id, id);
expect(terminalFocused(key(id)), isTrue, reason: id);
}
for (final id in ['term_a', 'term_c']) {
await ctrlTab(tester, reverse: true);
expect(hub.selected!.info.id, id);
expect(terminalFocused(key(id)), isTrue, reason: id);
}
// From the field too: the terminal claims the keys while it is shown.
await tester.tap(find.byKey(appField));
await tester.pump();
await ctrlTab(tester);
expect(hub.selected!.info.id, 'term_a');
expect(terminalFocused(key('term_a')), isTrue);
await altT(tester);
await ctrlTab(tester);
expect(hub.selected!.info.id, 'term_a');
});
desktopTestWidgets('a tab picked by pointer takes the keyboard', (
tester,
) async {
layout.show();
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.pumpAndSettle();
await tester.tap(find.text('notes'));
await tester.pumpAndSettle();
expect(terminalFocused(key('term_c')), isTrue);
});
desktopTestWidgets('opening with no terminal starts one', (tester) async {
final empty = TerminalGateway();
final bare = TerminalHub(TestMachines([machine('mac', empty)]));
addTearDown(bare.dispose);
await pumpTerminalApp(tester, hub: bare, layout: layout);
await altT(tester);
expect(empty.bodies('terminal_open'), hasLength(1));
expect(bare.tabs, hasLength(1));
expect(terminalFocused(bare.tabs.single.key), isTrue);
await altT(tester);
await altT(tester);
expect(empty.bodies('terminal_open'), hasLength(1));
});
desktopTestWidgets(
'on a narrow window the keys open and close the full-screen '
'page, focused, and Ctrl+Tab moves between its tabs',
(tester) async {
await pumpTerminalApp(
tester,
hub: hub,
layout: layout,
size: const Size(390, 800),
);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, LogicalKeyboardKey.keyT, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget);
expect(terminalFocused(key('term_a')), isTrue);
await ctrlTab(tester, reverse: true);
expect(hub.selected!.info.id, 'term_c');
expect(terminalFocused(key('term_c')), isTrue);
await chord(tester, LogicalKeyboardKey.keyT, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsNothing);
expect(find.byType(TerminalView), findsNothing);
expect(appFieldFocused(tester), isTrue);
},
);
test('the listed shortcuts are the ones the platform delivers', () {
const mac = TerminalShortcutLabels(
web: false,
platform: TargetPlatform.macOS,
);
expect(mac.toggle, ['Cmd+T', 'Ctrl+T', 'Option+T', 'Ctrl+`']);
expect(mac.tabs, ['Ctrl+Tab', 'Ctrl+Shift+Tab']);
const linux = TerminalShortcutLabels(
web: false,
platform: TargetPlatform.linux,
);
expect(linux.toggle, ['Ctrl+T', 'Alt+T', 'Ctrl+`']);
const macWeb = TerminalShortcutLabels(
web: true,
platform: TargetPlatform.macOS,
);
expect(macWeb.toggle, ['Ctrl+T', 'Option+T', 'Ctrl+`']);
expect(macWeb.tabs, isEmpty);
expect(macWeb.summary, isNot(contains('Tab')));
const windowsWeb = TerminalShortcutLabels(
web: true,
platform: TargetPlatform.windows,
);
expect(windowsWeb.toggle, ['Alt+T', 'Ctrl+`']);
expect(windowsWeb.button, 'Terminal (Alt+T)');
});
desktopTestWidgets('typing / in the terminal stays in the terminal', (
tester,
) async {
layout.setMode(TerminalMode.panel);
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, LogicalKeyboardKey.keyT, [LogicalKeyboardKey.altLeft]);
expect(terminalFocused(key('term_a')), isTrue);
// App-wide shortcuts see a typing surface and leave the key alone.
expect(primaryFocusIsTypingSurface(), isTrue);
expect(primaryFocusTakesTyping(), isTrue);
await tester.sendKeyEvent(LogicalKeyboardKey.slash, character: '/');
await tester.pump();
expect(terminalFocused(key('term_a')), isTrue);
expect(
mac.bodies('terminal_input').map((b) => b['data']).join(),
contains('/'),
);
expect(
find.text('/'),
findsNothing,
reason: 'nothing typed into the field',
);
});
}
/// A keyboard on which only Alt (Option) is held.
class _AltHeld extends HardwareKeyboard {
@override
bool get isAltPressed => true;
@override
bool get isControlPressed => false;
@override
bool get isMetaPressed => false;
@override
bool get isShiftPressed => false;
}
/// Hardware keys reach the terminal on desktop; phones type through the
/// on-screen keyboard instead. These tests drive desktop key events.
void desktopTestWidgets(String description, WidgetTesterCallback callback) =>
testWidgets(
description,
callback,
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);