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_actions_shortcut.dart'; import 'package:pipelines_app/ui/input/keyboard_owner.dart'; KeyDownEvent _down(PhysicalKeyboardKey physical, LogicalKeyboardKey logical) => KeyDownEvent( physicalKey: physical, logicalKey: logical, timeStamp: Duration.zero, ); final _n = _down(PhysicalKeyboardKey.keyN, LogicalKeyboardKey.keyN); final _j = _down(PhysicalKeyboardKey.keyJ, LogicalKeyboardKey.keyJ); Future _alt(WidgetTester tester, LogicalKeyboardKey key) async { await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyEvent(key); await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); await tester.pump(); } void main() { test('Alt+N is New chat and Alt+J the next chat needing you, alone', () { ShellChatAction? chord(Set held, KeyEvent event) => shellChatActionShortcut(event, _FakeKeyboard(held)); const alt = LogicalKeyboardKey.altLeft; expect(chord({alt}, _n), ShellChatAction.newChat); expect(chord({alt}, _j), ShellChatAction.nextAttention); expect(chord({}, _n), isNull); expect(chord({alt, LogicalKeyboardKey.shiftLeft}, _n), isNull); expect(chord({alt, LogicalKeyboardKey.controlLeft}, _j), isNull); expect(chord({alt, LogicalKeyboardKey.metaLeft}, _j), isNull); expect(chord({LogicalKeyboardKey.controlLeft}, _n), isNull); // Option+N is a dead key (˜) on a Mac: matched by position. final dead = _down( PhysicalKeyboardKey.keyN, const LogicalKeyboardKey(0x02dc), ); expect(chord({alt}, dead), ShellChatAction.newChat); expect( shellChatActionLabel(ShellChatAction.newChat, TargetPlatform.macOS), 'Option+N', ); expect( shellChatActionLabel(ShellChatAction.nextAttention, TargetPlatform.linux), 'Alt+J', ); }); testWidgets('the chords show Chat and report the action; the terminal ' 'keeps them', (tester) async { final terminal = FocusNode(); addTearDown(terminal.dispose); final actions = []; await tester.pumpWidget( MaterialApp( home: AdaptiveShell( onChatAction: actions.add, destinationBodies: { AppDestination.pipelines: (_) => TypingSurface( child: Focus(focusNode: terminal, child: const Text('terminal')), ), AppDestination.chat: (_) => const Text('chat body'), }, ), ), ); terminal.requestFocus(); await tester.pump(); await _alt(tester, LogicalKeyboardKey.keyN); expect(actions, isEmpty); expect(find.text('chat body'), findsNothing); terminal.unfocus(); await tester.pump(); await _alt(tester, LogicalKeyboardKey.keyJ); expect(actions, [ShellChatAction.nextAttention]); expect(find.text('chat body'), isOnstage); await _alt(tester, LogicalKeyboardKey.keyN); expect(actions, [ShellChatAction.nextAttention, ShellChatAction.newChat]); }); testWidgets('a dialog keeps the chords', (tester) async { final actions = []; await tester.pumpWidget( MaterialApp( home: AdaptiveShell( onChatAction: actions.add, destinationBodies: { AppDestination.pipelines: (context) => TextButton( onPressed: () => showDialog( context: context, builder: (_) => const AlertDialog(content: TextField(autofocus: true)), ), child: const Text('dialog'), ), AppDestination.chat: (_) => const Text('chat body'), }, ), ), ); await tester.tap(find.text('dialog')); await tester.pumpAndSettle(); await _alt(tester, LogicalKeyboardKey.keyN); expect(actions, isEmpty); }); } class _FakeKeyboard extends HardwareKeyboard { _FakeKeyboard(this.pressed); final Set pressed; @override bool isLogicalKeyPressed(LogicalKeyboardKey key) => pressed.contains(key); }