Files
pipelines/app/test/features/terminal/terminal_surface_test.dart
T

253 lines
9.3 KiB
Dart

import 'dart:convert';
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_keys.dart';
import 'package:pipelines_app/features/settings/settings_terminal_section.dart';
import 'package:pipelines_app/features/terminal/terminal_float.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:xterm/xterm.dart';
import 'terminal_fixtures.dart';
void main() {
late TerminalGateway mac;
late TerminalHub hub;
late MemoryLayoutStore store;
late TerminalLayout layout;
setUp(() {
mac = TerminalGateway()
..add('term_a', cwd: '/Users/op/src', text: 'alpha\r\n')
..add('term_b', cwd: '/Users/op/docs', text: 'beta\r\n');
hub = TerminalHub(TestMachines([machine('mac', mac, name: 'Mac')]));
store = MemoryLayoutStore();
layout = TerminalLayout(store: store);
});
tearDown(() async {
hub.dispose();
await mac.close();
});
Future<void> pumpApp(
WidgetTester tester, {
Size size = const Size(1200, 800),
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
MaterialApp(
home: TerminalSurface(
hub: hub,
layout: layout,
cupertino: false,
child: Scaffold(
appBar: AppBar(
title: const Text('App'),
actions: [TerminalToggleButton(layout: layout, cupertino: false)],
),
body: const TextField(key: Key('app-field')),
),
),
),
);
await tester.pump();
}
Future<void> ctrlBacktick(WidgetTester tester) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.backquote);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
}
Map<String, dynamic> saved() =>
jsonDecode(store.saved!) as Map<String, dynamic>;
testWidgets('Ctrl+` and the bar button toggle the bottom panel; hiding '
'keeps every shell', (tester) async {
await pumpApp(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
await ctrlBacktick(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsOneWidget);
expect(find.text('src'), findsOneWidget);
expect(find.text('docs'), findsOneWidget);
expect(saved()['visible'], isTrue);
// The terminal has the keyboard, yet the shortcut still hides it.
await tester.tap(find.byType(TerminalView));
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], 'x');
await ctrlBacktick(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
expect(hub.tabs, hasLength(2));
expect(mac.bodies('terminal_close'), isEmpty);
await tester.tap(find.byTooltip(TerminalToggleButton.label));
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('terminal-panel')), findsOneWidget);
// Reopening draws the same emulator, output and all.
expect(find.byType(TerminalView), findsOneWidget);
});
testWidgets('the panel resizes from its top edge and remembers its height', (
tester,
) async {
await pumpApp(tester);
await ctrlBacktick(tester);
final before = tester
.getSize(find.byKey(const ValueKey('terminal-panel')))
.height;
expect(before, terminalPanelDefaultHeight);
await tester.drag(
find.byKey(const ValueKey('terminal-panel-resize')),
const Offset(0, -120),
);
await tester.pumpAndSettle();
final after = tester
.getSize(find.byKey(const ValueKey('terminal-panel')))
.height;
expect(after, closeTo(before + 120, 1));
expect(saved()['panel_height'], closeTo(after, 1));
// The app above shrinks rather than hiding under it.
final field = tester.getRect(find.byType(Scaffold).first);
expect(field.bottom, closeTo(800 - after, 1));
final restored = TerminalLayout(store: store);
await restored.restore();
expect(restored.panelHeight, closeTo(after, 1));
expect(restored.visible, isTrue);
// The resized terminal tells the server its new size.
await tester.pump(const Duration(milliseconds: 200));
expect(mac.bodies('terminal_resize'), isNotEmpty);
});
testWidgets('the floating window moves, resizes, minimises and maximises', (
tester,
) async {
layout
..setMode(TerminalMode.float)
..show();
await pumpApp(tester);
await tester.pump();
final window = find.byKey(const ValueKey('terminal-float'));
expect(window, findsOneWidget);
final start = layout.floatRect;
await tester.drag(
find.byKey(const ValueKey('terminal-float-title')),
const Offset(60, 40),
);
await tester.pumpAndSettle();
expect(layout.floatRect.topLeft, start.topLeft + const Offset(60, 40));
await tester.drag(
find.byKey(const ValueKey('terminal-float-resize')),
const Offset(80, 50),
);
await tester.pumpAndSettle();
expect(layout.floatRect.size, start.size + const Offset(80, 50));
expect(saved()['float'][2], closeTo(start.width + 80, 1));
await tester.tap(find.byTooltip('Minimise'));
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsNothing);
expect(hub.tabs, hasLength(2));
await tester.tap(find.byTooltip('Restore'));
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsOneWidget);
await tester.tap(find.byTooltip('Maximise'));
await tester.pumpAndSettle();
expect(
tester.getRect(
find.descendant(of: window, matching: find.byType(Material)).first,
),
const Rect.fromLTWH(0, 0, 1200, 800).deflate(terminalFloatMargin),
);
await tester.tap(find.byTooltip('Restore size'));
await tester.pumpAndSettle();
expect(layout.maximized, isFalse);
});
testWidgets('tabs select and close their terminals; New terminal opens one', (
tester,
) async {
layout.show();
await pumpApp(tester);
await tester.pumpAndSettle();
expect(hub.selected!.info.id, 'term_a');
await tester.tap(find.text('docs'));
await tester.pumpAndSettle();
expect(hub.selected!.info.id, 'term_b');
await tester.tap(find.byTooltip('New terminal'));
await tester.pumpAndSettle();
expect(mac.bodies('terminal_open'), hasLength(1));
expect(hub.tabs, hasLength(3));
expect(hub.selected!.info.id, 'term_1');
await tester.tap(find.byTooltip('Close terminal').first);
await tester.pumpAndSettle();
expect(mac.bodies('terminal_close').single['terminal_id'], 'term_a');
expect(find.text('src'), findsNothing);
});
testWidgets('the options menu switches between panel and window', (
tester,
) async {
layout.show();
await pumpApp(tester);
await tester.tap(find.byTooltip('Terminal options'));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('terminal-mode-float')));
await tester.pumpAndSettle();
expect(layout.mode, TerminalMode.float);
expect(find.byKey(const ValueKey('terminal-float')), findsOneWidget);
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
expect(saved()['mode'], 'float');
await tester.tap(find.byTooltip('Terminal options'));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('terminal-hide')));
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('terminal-float')), findsNothing);
});
testWidgets('Settings chooses the mode', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: SettingsTerminalSection(layout: layout),
),
),
),
);
expect(find.text('Terminal'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('settings-terminal-float')));
await tester.pumpAndSettle();
expect(layout.mode, TerminalMode.float);
await tester.tap(find.byKey(const ValueKey('settings-terminal-panel')));
await tester.pumpAndSettle();
expect(layout.mode, TerminalMode.panel);
});
testWidgets('a narrow window opens the terminal full screen with the key '
'toolbar', (tester) async {
layout.show();
await pumpApp(tester, size: const Size(390, 800));
// The panel is not drawn on a phone even when left shown.
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
await tester.tap(find.byTooltip(TerminalToggleButton.label));
await tester.pumpAndSettle();
expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget);
expect(find.byType(TerminalView), findsOneWidget);
expect(find.byType(ChatShellTerminalKeys), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-shell-key-ctrl-c')));
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], '\x03');
await tester.pageBack();
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsNothing);
expect(hub.tabs, hasLength(2));
});
}