2bbc737604
Provide a platform-independent destination shortcut that preserves Ctrl+A for text fields and the terminal. Document the shortcut in help and cover navigation and focus exceptions with regression tests.
209 lines
7.0 KiB
Dart
209 lines
7.0 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
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_runs_pipelines_shortcut.dart';
|
|
import 'package:pipelines_app/ui/input/keyboard_owner.dart';
|
|
|
|
const _a = KeyDownEvent(
|
|
physicalKey: PhysicalKeyboardKey.keyA,
|
|
logicalKey: LogicalKeyboardKey.keyA,
|
|
timeStamp: Duration.zero,
|
|
);
|
|
|
|
void main() {
|
|
test('Ctrl+A matches only with Ctrl alone', () {
|
|
bool chord(Set<LogicalKeyboardKey> held, [KeyEvent event = _a]) =>
|
|
shellRunsPipelinesShortcut(event, _FakeKeyboard(held));
|
|
expect(chord({}), isFalse);
|
|
expect(chord({LogicalKeyboardKey.controlLeft}), isTrue);
|
|
expect(chord({LogicalKeyboardKey.controlRight}), isTrue);
|
|
expect(
|
|
chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft}),
|
|
isFalse,
|
|
);
|
|
expect(
|
|
chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft}),
|
|
isFalse,
|
|
);
|
|
expect(
|
|
chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.metaLeft}),
|
|
isFalse,
|
|
);
|
|
expect(chord({LogicalKeyboardKey.altLeft}), isFalse);
|
|
expect(chord({LogicalKeyboardKey.metaLeft}), isFalse);
|
|
expect(
|
|
shellRunsPipelinesShortcut(
|
|
const KeyUpEvent(
|
|
physicalKey: PhysicalKeyboardKey.keyA,
|
|
logicalKey: LogicalKeyboardKey.keyA,
|
|
timeStamp: Duration.zero,
|
|
),
|
|
_FakeKeyboard({LogicalKeyboardKey.controlLeft}),
|
|
),
|
|
isFalse,
|
|
);
|
|
});
|
|
|
|
test('Runs goes to Pipelines; every other destination goes to Runs', () {
|
|
expect(
|
|
shellRunsPipelinesTarget(AppDestination.runs),
|
|
AppDestination.pipelines,
|
|
);
|
|
expect(
|
|
shellRunsPipelinesTarget(AppDestination.pipelines),
|
|
AppDestination.runs,
|
|
);
|
|
expect(shellRunsPipelinesTarget(AppDestination.chat), AppDestination.runs);
|
|
expect(
|
|
shellRunsPipelinesTarget(AppDestination.search),
|
|
AppDestination.runs,
|
|
);
|
|
expect(
|
|
shellRunsPipelinesTarget(AppDestination.settings),
|
|
AppDestination.runs,
|
|
);
|
|
});
|
|
|
|
testWidgets('Ctrl+A bounces Chat, Runs, Pipelines, Runs', (tester) async {
|
|
await _onAndroid(() async {
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: AdaptiveShell(
|
|
destinationBodies: {
|
|
for (final destination in shellTabDestinations)
|
|
destination: (_) => Text('${destination.name} body'),
|
|
},
|
|
),
|
|
),
|
|
);
|
|
expect(find.text('pipelines body'), isOnstage);
|
|
|
|
Future<void> pressCtrl(LogicalKeyboardKey key) async {
|
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.sendKeyEvent(key);
|
|
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
// Reach Chat first with Ctrl+K, then start the Ctrl+A sequence.
|
|
await pressCtrl(LogicalKeyboardKey.keyK);
|
|
expect(find.text('chat body'), isOnstage);
|
|
expect(find.text('pipelines body'), findsNothing);
|
|
|
|
await pressCtrl(LogicalKeyboardKey.keyA);
|
|
expect(find.text('runs body'), isOnstage);
|
|
expect(find.text('chat body'), findsNothing);
|
|
|
|
await pressCtrl(LogicalKeyboardKey.keyA);
|
|
expect(find.text('pipelines body'), isOnstage);
|
|
expect(find.text('runs body'), findsNothing);
|
|
|
|
await pressCtrl(LogicalKeyboardKey.keyA);
|
|
expect(find.text('runs body'), isOnstage);
|
|
expect(find.text('pipelines body'), findsNothing);
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
});
|
|
|
|
testWidgets('Ctrl+A is left to a focused text field', (tester) async {
|
|
await _onAndroid(() async {
|
|
final field = TextEditingController(text: 'draft message');
|
|
final focus = FocusNode();
|
|
addTearDown(field.dispose);
|
|
addTearDown(focus.dispose);
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: AdaptiveShell(
|
|
destinationBodies: {
|
|
AppDestination.pipelines: (_) =>
|
|
TextField(controller: field, focusNode: focus),
|
|
AppDestination.runs: (_) => const Text('runs body'),
|
|
},
|
|
),
|
|
),
|
|
);
|
|
focus.requestFocus();
|
|
await tester.pump();
|
|
expect(focus.hasPrimaryFocus, isTrue);
|
|
|
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
|
|
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.pumpAndSettle();
|
|
|
|
// Destination unchanged; the field took the chord (select all on this
|
|
// platform), so the text is still there and fully selected.
|
|
expect(find.text('runs body'), findsNothing);
|
|
expect(field.text, 'draft message');
|
|
expect(field.selection.baseOffset, 0);
|
|
expect(field.selection.extentOffset, field.text.length);
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
});
|
|
|
|
testWidgets('Ctrl+A is left to the terminal while it has focus', (
|
|
tester,
|
|
) async {
|
|
await _onAndroid(() async {
|
|
final terminal = FocusNode();
|
|
addTearDown(terminal.dispose);
|
|
final received = <KeyEvent>[];
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: AdaptiveShell(
|
|
destinationBodies: {
|
|
AppDestination.pipelines: (_) => TypingSurface(
|
|
child: Focus(
|
|
focusNode: terminal,
|
|
onKeyEvent: (node, event) {
|
|
if (event.logicalKey == LogicalKeyboardKey.keyA) {
|
|
received.add(event);
|
|
}
|
|
return KeyEventResult.ignored;
|
|
},
|
|
child: const Text('terminal'),
|
|
),
|
|
),
|
|
AppDestination.runs: (_) => const Text('runs body'),
|
|
},
|
|
),
|
|
),
|
|
);
|
|
terminal.requestFocus();
|
|
await tester.pump();
|
|
|
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
|
|
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('runs body'), findsNothing);
|
|
expect(find.text('terminal'), isOnstage);
|
|
expect(received.whereType<KeyDownEvent>(), hasLength(1));
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
});
|
|
}
|
|
|
|
/// Runs [body] with the platform pinned to Android, so Ctrl+A in a text field
|
|
/// means select all everywhere. flutter_test requires the debug override to be
|
|
/// cleared before the test body returns, hence the `finally`.
|
|
Future<void> _onAndroid(Future<void> Function() body) async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
|
try {
|
|
await body();
|
|
} finally {
|
|
debugDefaultTargetPlatformOverride = null;
|
|
}
|
|
}
|
|
|
|
class _FakeKeyboard extends HardwareKeyboard {
|
|
_FakeKeyboard(this.pressed);
|
|
final Set<LogicalKeyboardKey> pressed;
|
|
@override
|
|
bool isLogicalKeyPressed(LogicalKeyboardKey key) => pressed.contains(key);
|
|
}
|