255 lines
9.5 KiB
Dart
255 lines
9.5 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: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) {
|
|
testWidgets('$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);
|
|
});
|
|
|
|
testWidgets('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);
|
|
});
|
|
|
|
testWidgets('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');
|
|
}
|
|
});
|
|
|
|
testWidgets('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);
|
|
});
|
|
|
|
testWidgets('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));
|
|
});
|
|
|
|
testWidgets('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');
|
|
});
|
|
|
|
testWidgets('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);
|
|
});
|
|
|
|
testWidgets('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));
|
|
});
|
|
|
|
testWidgets('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)');
|
|
});
|
|
}
|
|
|
|
/// 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;
|
|
}
|