import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:pipelines_app/shell/shell_chat_shortcut.dart'; import 'package:pipelines_app/ui/input/keyboard_owner.dart'; const _k = KeyDownEvent( physicalKey: PhysicalKeyboardKey.keyK, logicalKey: LogicalKeyboardKey.keyK, timeStamp: Duration.zero, ); void main() { test( 'Ctrl+K, Cmd+K and Alt+K open Chat; other modifiers and plain K do not', () { bool chord(Set held, [KeyEvent event = _k]) => shellChatShortcut(event, _FakeKeyboard(held)); expect(chord({}), isFalse); expect(chord({LogicalKeyboardKey.controlLeft}), isTrue); expect(chord({LogicalKeyboardKey.altLeft}), isTrue); expect( chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft}), isFalse, ); expect( chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft}), isFalse, ); expect(chord({LogicalKeyboardKey.metaLeft}), isTrue); expect( chord({LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.shiftLeft}), isFalse, ); expect( chord({LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.altLeft}), isFalse, ); expect( chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.metaLeft}), isFalse, ); expect( shellChatShortcut( const KeyUpEvent( physicalKey: PhysicalKeyboardKey.keyK, logicalKey: LogicalKeyboardKey.keyK, timeStamp: Duration.zero, ), _FakeKeyboard({LogicalKeyboardKey.controlLeft}), ), isFalse, ); }, ); test('Option+K matches by key position though it types a symbol', () { // On a Mac, Option+K produces ˚: the logical key is not K. const ring = KeyDownEvent( physicalKey: PhysicalKeyboardKey.keyK, logicalKey: LogicalKeyboardKey(0x02da), character: '˚', timeStamp: Duration.zero, ); expect( shellChatShortcut(ring, _FakeKeyboard({LogicalKeyboardKey.altLeft})), isTrue, ); // Without Alt the same symbol is ordinary typing. expect(shellChatShortcut(ring, _FakeKeyboard({})), isFalse); }); test('labels name the Option chord on Apple platforms', () { expect(shellChatShortcutLabels(TargetPlatform.linux), ['Alt+K', 'Ctrl+K']); expect(shellChatShortcutLabels(TargetPlatform.macOS), [ 'Option+K', 'Cmd+K', 'Ctrl+K', ]); }); testWidgets('Ctrl+K, Alt+K and Cmd+K show Chat from another destination', ( tester, ) async { var opened = 0; await tester.pumpWidget( MaterialApp( home: AdaptiveShell( destinationBodies: { for (final destination in shellTabDestinations) destination: (_) => Text('${destination.name} body'), }, onOpenChat: () => opened++, ), ), ); expect(find.text('pipelines body'), isOnstage); // Chat is built on its first visit only. expect(find.text('chat body'), findsNothing); await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyK); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); await tester.pump(); // The outgoing tab fades out while still mounted, so Chat being built // and onstage is the switch. expect(find.text('chat body'), isOnstage); expect(opened, 1); await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyK); await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); await tester.pump(); expect(find.text('chat body'), isOnstage); // Already in Chat, the chord still hands the keyboard to the composer. expect(opened, 2); await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyK); await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft); await tester.pump(); expect(opened, 3); expect(tester.takeException(), isNull); }); testWidgets('a controlled shell reports the switch to Chat', (tester) async { final selected = []; await tester.pumpWidget( MaterialApp( home: AdaptiveShell( destination: AppDestination.runs, onDestinationSelected: selected.add, destinationBodies: { for (final destination in shellTabDestinations) destination: (_) => Text('${destination.name} body'), }, ), ), ); await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyK); await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); await tester.pump(); expect(selected, [AppDestination.chat]); }); testWidgets( 'terminal keeps Ctrl+K for kill-line but Alt+K and Cmd+K still open Chat', (tester) async { final terminal = FocusNode(); addTearDown(terminal.dispose); var opened = 0; await tester.pumpWidget( MaterialApp( home: AdaptiveShell( onOpenChat: () => opened++, destinationBodies: { AppDestination.pipelines: (_) => TypingSurface( child: Focus( focusNode: terminal, child: const Text('terminal'), ), ), AppDestination.chat: (_) => const Text('chat body'), }, ), ), ); terminal.requestFocus(); await tester.pump(); await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyK); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); await tester.pump(); expect(find.text('chat body'), findsNothing); expect(find.text('terminal'), isOnstage); expect(opened, 0); await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyK); await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); await tester.pump(); expect(find.text('chat body'), isOnstage); expect(opened, 1); await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyK); await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft); await tester.pump(); expect(opened, 2); expect(tester.takeException(), isNull); }, ); } class _FakeKeyboard extends HardwareKeyboard { _FakeKeyboard(this.pressed); final Set pressed; @override bool isLogicalKeyPressed(LogicalKeyboardKey key) => pressed.contains(key); }