Files
pipelines/app/test/features/settings/keyboard_shortcuts_help_test.dart
T
alan c3c2c1bbc9 Open Chat with Cmd+K as well as Ctrl+K
Cmd is the natural modifier on a Mac. Like Option+K, it also opens Chat
from the terminal, where Ctrl+K stays kill-line.
2026-11-13 16:02:31 +00:00

190 lines
6.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/settings/keyboard_shortcut_keys.dart';
import 'package:pipelines_app/features/settings/keyboard_shortcuts_help.dart';
import 'package:pipelines_app/features/settings/keyboard_shortcuts_registry.dart';
void main() {
const registry = KeyboardShortcutRegistry(
platform: TargetPlatform.macOS,
web: true,
);
test('registry uses platform labels and browser caveats', () {
final entries = registry.entries;
final terminal = entries.firstWhere(
(entry) => entry.action == 'Show or hide terminal',
);
final search = entries.firstWhere((entry) => entry.action == 'Open Search');
expect(terminal.keys, contains('Option+T'));
expect(terminal.keys, isNot(contains('Cmd+T')));
expect(search.keys, 'Option+/, Cmd+/, Ctrl+/');
expect(search.caveat, contains('browser'));
});
test(
'Open Chat lists Option+K or Alt+K with Ctrl+K and the terminal rule',
() {
KeyboardShortcutEntry openChat(TargetPlatform platform) =>
KeyboardShortcutRegistry(
platform: platform,
web: false,
).entries.firstWhere((entry) => entry.action == 'Open Chat');
expect(openChat(TargetPlatform.macOS).keys, 'Option+K, Cmd+K, Ctrl+K');
final linux = openChat(TargetPlatform.linux);
expect(linux.keys, 'Alt+K, Ctrl+K');
expect(linux.caveat, contains('kill-line'));
expect(openChat(TargetPlatform.windows).keys, 'Alt+K, Ctrl+K');
},
);
test('Runs and Pipelines switch is Ctrl+A on every platform', () {
for (final platform in TargetPlatform.values) {
final entry = KeyboardShortcutRegistry(platform: platform, web: false)
.entries
.firstWhere(
(entry) => entry.action == 'Switch between Runs and Pipelines',
);
expect(entry.keys, 'Ctrl+A', reason: platform.name);
expect(entry.caveat, contains('Pipelines'));
expect(entry.caveat, contains('text field'));
}
});
test('uses the shell primary modifier rule on iOS', () {
final entries = const KeyboardShortcutRegistry(
platform: TargetPlatform.iOS,
web: false,
).entries;
expect(
entries
.firstWhere((entry) => entry.action == 'Open a chat list item')
.keys,
'Ctrl+1…9',
);
expect(
entries.firstWhere((entry) => entry.action == 'Open Search').keys,
'Option+/, Ctrl+/',
);
expect(
entries
.firstWhere((entry) => entry.action == 'Show or hide floating chats')
.caveat,
contains('focuses its message input'),
);
});
Future<void> pumpHelp(
WidgetTester tester, {
bool focusSearch = false,
Size size = const Size(720, 900),
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: KeyboardShortcutsHelp(
registry: registry,
focusSearch: focusSearch,
),
),
),
);
await tester.pump();
}
Finder search() => find.byKey(const Key('keyboard-shortcuts-search'));
testWidgets('help is readable and keyboard traversable', (tester) async {
// Tall enough that the lazily built list lays out every group.
await pumpHelp(tester, size: const Size(720, 4000));
expect(find.text('Keyboard shortcuts'), findsOneWidget);
expect(find.text('Show or hide floating chats'), findsOneWidget);
// Keys are drawn as keycaps, one per key of each chord.
expect(find.text('Option'), findsWidgets);
expect(find.byType(ShortcutKeycap), findsWidgets);
expect(
find.bySemanticsLabel(
RegExp(r'^Show or hide floating chats: Option\+Y, Ctrl\+Y, Cmd\+Y'),
),
findsOneWidget,
);
expect(
tester.getSemantics(find.bySemanticsLabel('Keyboard shortcuts help')),
matchesSemantics(
scopesRoute: true,
namesRoute: true,
label: 'Keyboard shortcuts help',
),
);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
expect(find.byTooltip('Close keyboard shortcuts'), findsOneWidget);
});
testWidgets('the search field is not focused unless asked', (tester) async {
await pumpHelp(tester);
expect(tester.widget<SearchBar>(search()).focusNode!.hasFocus, isFalse);
});
testWidgets('the search field starts focused when asked', (tester) async {
await pumpHelp(tester, focusSearch: true);
await tester.pump();
expect(tester.widget<SearchBar>(search()).focusNode!.hasFocus, isTrue);
});
testWidgets('typing filters fuzzily and highlights the action', (
tester,
) async {
await pumpHelp(tester, focusSearch: true);
await tester.enterText(search(), 'flt');
await tester.pump();
expect(find.textContaining('floating', findRichText: true), findsWidgets);
expect(find.text('Open Search'), findsNothing);
expect(find.text('Leave Search'), findsNothing);
// Only the groups that still have a match are shown.
expect(find.text('Windows'), findsOneWidget);
expect(find.text('Navigation'), findsNothing);
});
testWidgets('keys can be searched as written', (tester) async {
await pumpHelp(tester);
await tester.enterText(search(), 'option k');
await tester.pump();
expect(find.text('Open Chat'), findsOneWidget);
expect(find.text('Leave Search'), findsNothing);
});
testWidgets('no match says so and Clear restores the list', (tester) async {
await pumpHelp(tester);
await tester.enterText(search(), 'zzqx');
await tester.pump();
expect(find.text('No shortcuts match “zzqx”'), findsOneWidget);
await tester.tap(find.byTooltip('Clear search'));
await tester.pump();
expect(find.text('Navigation'), findsOneWidget);
expect(find.textContaining('No shortcuts match'), findsNothing);
});
testWidgets('phone-sized help scrolls its list under a fixed header', (
tester,
) async {
await pumpHelp(tester, size: const Size(400, 640));
final list = find.byKey(const Key('keyboard-shortcuts-list'));
expect(list, findsOneWidget);
final before = tester.getTopLeft(search()).dy;
await tester.drag(list, const Offset(0, -400));
await tester.pump();
expect(tester.getTopLeft(search()).dy, before);
});
}