From 1dd6a233585b87693a4330052f8aa3dfedad0abb Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Tue, 30 Aug 2022 09:25:00 -0700 Subject: [PATCH] Refactor macOS text editing shortcuts (#110289) --- .../default_text_editing_shortcuts.dart | 137 +++--- .../default_text_editing_shortcuts_test.dart | 439 ++++++++++++++++++ 2 files changed, 510 insertions(+), 66 deletions(-) create mode 100644 packages/flutter/test/widgets/default_text_editing_shortcuts_test.dart diff --git a/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart b/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart index 439153bfe1..11b6fc95e8 100644 --- a/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart +++ b/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart @@ -258,32 +258,7 @@ class DefaultTextEditingShortcuts extends StatelessWidget { // macOS document shortcuts: https://support.apple.com/en-us/HT201236. // The macOS shortcuts uses different word/line modifiers than most other // platforms. - static final Map _macShortcuts = { - const SingleActivator(LogicalKeyboardKey.keyX, meta: true): const CopySelectionTextIntent.cut(SelectionChangedCause.keyboard), - const SingleActivator(LogicalKeyboardKey.keyC, meta: true): CopySelectionTextIntent.copy, - const SingleActivator(LogicalKeyboardKey.keyV, meta: true): const PasteTextIntent(SelectionChangedCause.keyboard), - const SingleActivator(LogicalKeyboardKey.keyA, meta: true): const SelectAllTextIntent(SelectionChangedCause.keyboard), - const SingleActivator(LogicalKeyboardKey.keyZ, meta: true): const UndoTextIntent(SelectionChangedCause.keyboard), - const SingleActivator(LogicalKeyboardKey.keyZ, shift: true, meta: true): const RedoTextIntent(SelectionChangedCause.keyboard), - - // On desktop these keys should go to the IME when a field is focused, not to other - // Shortcuts. - if (!kIsWeb) ...{ - const SingleActivator(LogicalKeyboardKey.arrowLeft): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowUp): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowDown): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowUp, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowDown, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.escape): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.space): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.enter): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.tab): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.tab, shift: true): const DoNothingAndStopPropagationTextIntent(), - }, - }; + static final Map _macShortcuts = _iOSShortcuts; // There is no complete documentation of iOS shortcuts. static final Map _iOSShortcuts = { @@ -399,42 +374,7 @@ class DefaultTextEditingShortcuts extends StatelessWidget { SingleActivator(LogicalKeyboardKey.backspace, meta: true, shift: pressShift): const DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.delete, meta: true, shift: pressShift): const DoNothingAndStopPropagationTextIntent(), }, - const SingleActivator(LogicalKeyboardKey.arrowDown, alt: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowUp, alt: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, alt: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, alt: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, alt: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, alt: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowDown): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowUp): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft, control: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight, control: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, control: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, control: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.end): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.home): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowDown, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowUp, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, meta: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowDown, shift: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.end, shift: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.home, shift: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.end, control: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.home, control: true): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.space): const DoNothingAndStopPropagationTextIntent(), - const SingleActivator(LogicalKeyboardKey.enter): const DoNothingAndStopPropagationTextIntent(), + ..._commonDisablingTextShortcuts, const SingleActivator(LogicalKeyboardKey.keyX, control: true): const DoNothingAndStopPropagationTextIntent(), const SingleActivator(LogicalKeyboardKey.keyX, meta: true): const DoNothingAndStopPropagationTextIntent(), const SingleActivator(LogicalKeyboardKey.keyC, control: true): const DoNothingAndStopPropagationTextIntent(), @@ -445,6 +385,53 @@ class DefaultTextEditingShortcuts extends StatelessWidget { const SingleActivator(LogicalKeyboardKey.keyA, meta: true): const DoNothingAndStopPropagationTextIntent(), }; + + static const Map _commonDisablingTextShortcuts = { + SingleActivator(LogicalKeyboardKey.arrowDown, alt: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowRight, alt: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowUp, alt: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowDown, meta: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowRight, meta: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowUp, meta: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowDown, shift: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.end, shift: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.home, shift: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowDown): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowLeft): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowRight): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowUp): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowLeft, control: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowRight, control: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, control: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, control: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.end): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.home): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.end, control: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.home, control: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.space): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.enter): DoNothingAndStopPropagationTextIntent(), + }; + + static final Map _macDisablingTextShortcuts = { + ..._commonDisablingTextShortcuts, + const SingleActivator(LogicalKeyboardKey.escape): const DoNothingAndStopPropagationTextIntent(), + const SingleActivator(LogicalKeyboardKey.tab): const DoNothingAndStopPropagationTextIntent(), + const SingleActivator(LogicalKeyboardKey.tab, shift: true): const DoNothingAndStopPropagationTextIntent(), + }; + static Map get _shortcuts { switch (defaultTargetPlatform) { case TargetPlatform.android: @@ -462,21 +449,39 @@ class DefaultTextEditingShortcuts extends StatelessWidget { } } + Map? _getDisablingShortcut() { + if (kIsWeb) { + return _webDisablingTextShortcuts; + } + switch (defaultTargetPlatform) { + + case TargetPlatform.android: + case TargetPlatform.fuchsia: + case TargetPlatform.iOS: + case TargetPlatform.linux: + case TargetPlatform.windows: + return null; + case TargetPlatform.macOS: + return _macDisablingTextShortcuts; + } + } + @override Widget build(BuildContext context) { Widget result = child; - if (kIsWeb) { - // On the web, these shortcuts make sure of the following: + final Map? disablingShortcut = _getDisablingShortcut(); + if (disablingShortcut != null) { + // These shortcuts make sure of the following: // // 1. Shortcuts fired when an EditableText is focused are ignored and - // forwarded to the browser by the EditableText's Actions, because it + // forwarded to the platform by the EditableText's Actions, because it // maps DoNothingAndStopPropagationTextIntent to DoNothingAction. // 2. Shortcuts fired when no EditableText is focused will still trigger // _shortcuts assuming DoNothingAndStopPropagationTextIntent is // unhandled elsewhere. result = Shortcuts( debugLabel: '', - shortcuts: _webDisablingTextShortcuts, + shortcuts: disablingShortcut, child: result ); } diff --git a/packages/flutter/test/widgets/default_text_editing_shortcuts_test.dart b/packages/flutter/test/widgets/default_text_editing_shortcuts_test.dart new file mode 100644 index 0000000000..3fedcdd1cf --- /dev/null +++ b/packages/flutter/test/widgets/default_text_editing_shortcuts_test.dart @@ -0,0 +1,439 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; + +Future sendKeyCombination( + WidgetTester tester, + SingleActivator activator, +) async { + final List modifiers = [ + if (activator.control) LogicalKeyboardKey.control, + if (activator.shift) LogicalKeyboardKey.shift, + if (activator.alt) LogicalKeyboardKey.alt, + if (activator.meta) LogicalKeyboardKey.meta, + ]; + for (final LogicalKeyboardKey modifier in modifiers) { + await tester.sendKeyDownEvent(modifier); + } + await tester.sendKeyDownEvent(activator.trigger); + await tester.sendKeyUpEvent(activator.trigger); + await tester.pump(); + for (final LogicalKeyboardKey modifier in modifiers.reversed) { + await tester.sendKeyUpEvent(modifier); + } +} + +void main() { + Widget buildSpyAboveEditableText({ + required FocusNode editableFocusNode, + required FocusNode spyFocusNode, + }) { + return MaterialApp( + home: Align( + alignment: Alignment.topLeft, + child: SizedBox( + // Softwrap at exactly 20 characters. + width: 201, + height: 200, + child: ActionSpy( + focusNode: spyFocusNode, + child: EditableText( + controller: TextEditingController(text: 'dummy text'), + showSelectionHandles: true, + autofocus: true, + focusNode: editableFocusNode, + style: const TextStyle(fontSize: 10.0), + textScaleFactor: 1, + // Avoid the cursor from taking up width. + cursorWidth: 0, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + selectionControls: materialTextSelectionControls, + keyboardType: TextInputType.text, + maxLines: null, + textAlign: TextAlign.left, + ), + ), + ), + ), + ); + } + group('macOS does not accept shortcuts if focus under EditableText', () { + final TargetPlatformVariant macOSOnly = TargetPlatformVariant.only(TargetPlatform.macOS); + + testWidgets('word modifier + arrowLeft', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + editable.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true)); + await tester.pump(); + + expect(state.lastIntent, isNull); + }, variant: macOSOnly); + + testWidgets('word modifier + arrowRight', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + editable.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true)); + await tester.pump(); + + expect(state.lastIntent, isNull); + }, variant: macOSOnly); + + testWidgets('line modifier + arrowLeft', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + editable.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true)); + await tester.pump(); + + expect(state.lastIntent, isNull); + }, variant: macOSOnly); + + testWidgets('line modifier + arrowRight', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + editable.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true)); + await tester.pump(); + + expect(state.lastIntent, isNull); + }, variant: macOSOnly); + + testWidgets('word modifier + arrow key movement', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + editable.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true)); + await tester.pump(); + + expect(state.lastIntent, isNull); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true)); + await tester.pump(); + expect(state.lastIntent, isNull); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true)); + await tester.pump(); + expect(state.lastIntent, isNull); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true)); + await tester.pump(); + expect(state.lastIntent, isNull); + }, variant: macOSOnly); + + testWidgets('line modifier + arrow key movement', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + editable.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true)); + await tester.pump(); + expect(state.lastIntent, isNull); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true)); + await tester.pump(); + expect(state.lastIntent, isNull); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true)); + await tester.pump(); + expect(state.lastIntent, isNull); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true)); + await tester.pump(); + expect(state.lastIntent, isNull); + }, variant: macOSOnly); + }); + + group('macOS does accept shortcuts if focus above EditableText', () { + final TargetPlatformVariant macOSOnly = TargetPlatformVariant.only(TargetPlatform.macOS); + + testWidgets('word modifier + arrowLeft', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + spy.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true)); + await tester.pump(); + + expect(state.lastIntent, isA()); + }, variant: macOSOnly); + + testWidgets('word modifier + arrowRight', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + spy.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true)); + await tester.pump(); + + expect(state.lastIntent, isA()); + }, variant: macOSOnly); + + testWidgets('line modifier + arrowLeft', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + spy.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true)); + await tester.pump(); + + expect(state.lastIntent, isA()); + }, variant: macOSOnly); + + testWidgets('line modifier + arrowRight', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + spy.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true)); + await tester.pump(); + + expect(state.lastIntent, isA()); + }, variant: macOSOnly); + + testWidgets('word modifier + arrow key movement', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + spy.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true)); + await tester.pump(); + expect(state.lastIntent, isA()); + + state.lastIntent = null; + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true)); + await tester.pump(); + expect(state.lastIntent, isA()); + + state.lastIntent = null; + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true)); + await tester.pump(); + expect(state.lastIntent, isA()); + + state.lastIntent = null; + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true)); + await tester.pump(); + expect(state.lastIntent, isA()); + }, variant: macOSOnly); + + testWidgets('line modifier + arrow key movement', (WidgetTester tester) async { + tester.binding.testTextInput.unregister(); + addTearDown((){ + tester.binding.testTextInput.register(); + }); + final FocusNode editable = FocusNode(); + final FocusNode spy = FocusNode(); + await tester.pumpWidget( + buildSpyAboveEditableText( + editableFocusNode: editable, + spyFocusNode: spy, + ), + ); + spy.requestFocus(); + await tester.pump(); + final ActionSpyState state = tester.state(find.byType(ActionSpy)); + + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true)); + await tester.pump(); + expect(state.lastIntent, isA()); + + state.lastIntent = null; + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true)); + await tester.pump(); + expect(state.lastIntent, isA()); + + state.lastIntent = null; + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true)); + await tester.pump(); + expect(state.lastIntent, isA()); + + state.lastIntent = null; + await sendKeyCombination(tester, const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true)); + await tester.pump(); + expect(state.lastIntent, isA()); + }, variant: macOSOnly); + }, skip: kIsWeb); // [intended] specific tests target non-web. +} + +class ActionSpy extends StatefulWidget { + const ActionSpy({super.key, required this.focusNode, required this.child}); + final FocusNode focusNode; + final Widget child; + + @override + State createState() => ActionSpyState(); +} + +class ActionSpyState extends State { + Intent? lastIntent; + late final Map> _actions = >{ + ExtendSelectionByCharacterIntent: CallbackAction(onInvoke: _captureIntent), + ExtendSelectionToNextWordBoundaryIntent: CallbackAction(onInvoke: _captureIntent), + ExtendSelectionToLineBreakIntent: CallbackAction(onInvoke: _captureIntent), + ExpandSelectionToLineBreakIntent: CallbackAction(onInvoke: _captureIntent), + ExpandSelectionToDocumentBoundaryIntent: CallbackAction(onInvoke: _captureIntent), + ExtendSelectionVerticallyToAdjacentLineIntent: CallbackAction(onInvoke: _captureIntent), + ExtendSelectionToDocumentBoundaryIntent: CallbackAction(onInvoke: _captureIntent), + ExtendSelectionToNextWordBoundaryOrCaretLocationIntent: CallbackAction(onInvoke: _captureIntent), + }; + + // ignore: use_setters_to_change_properties + void _captureIntent(Intent intent) { + lastIntent = intent; + } + + @override + Widget build(BuildContext context) { + return Actions( + actions: _actions, + child: Focus( + focusNode: widget.focusNode, + child: widget.child, + ), + ); + } +}