From 2571c6b1bf20e7a0e0584bdd86b1069b01091c3f Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 6 Sep 2022 12:58:37 +0800 Subject: [PATCH] test: add shortcut_event / keybinding test --- .../built_in_shortcut_events.dart | 2 +- .../test/infra/test_raw_key_event.dart | 3 + .../shortcut_event/keybinding_test.dart | 82 +++++++++++++++++++ .../shortcut_event/shortcut_event_test.dart | 75 +++++++++++++++++ 4 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 frontend/app_flowy/packages/appflowy_editor/test/service/shortcut_event/keybinding_test.dart create mode 100644 frontend/app_flowy/packages/appflowy_editor/test/service/shortcut_event/shortcut_event_test.dart diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dart index d313a33dc8..e619246069 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dart @@ -58,7 +58,7 @@ List builtInShortcutEvents = [ key: 'Move cursor top', command: 'meta+arrow up', windowsCommand: 'ctrl+arrow up', - handler: cursorBegin, + handler: cursorTop, ), ShortcutEvent( key: 'Move cursor bottom', diff --git a/frontend/app_flowy/packages/appflowy_editor/test/infra/test_raw_key_event.dart b/frontend/app_flowy/packages/appflowy_editor/test/infra/test_raw_key_event.dart index 47cacc18b1..451f203e8b 100644 --- a/frontend/app_flowy/packages/appflowy_editor/test/infra/test_raw_key_event.dart +++ b/frontend/app_flowy/packages/appflowy_editor/test/infra/test_raw_key_event.dart @@ -115,6 +115,9 @@ extension on LogicalKeyboardKey { if (this == LogicalKeyboardKey.keyB) { return PhysicalKeyboardKey.keyB; } + if (this == LogicalKeyboardKey.keyC) { + return PhysicalKeyboardKey.keyC; + } if (this == LogicalKeyboardKey.keyI) { return PhysicalKeyboardKey.keyI; } diff --git a/frontend/app_flowy/packages/appflowy_editor/test/service/shortcut_event/keybinding_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/service/shortcut_event/keybinding_test.dart new file mode 100644 index 0000000000..48b335c6c7 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/test/service/shortcut_event/keybinding_test.dart @@ -0,0 +1,82 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:appflowy_editor/appflowy_editor.dart'; + +void main() async { + setUpAll(() { + TestWidgetsFlutterBinding.ensureInitialized(); + }); + + group('keybinding_test.dart', () { + test('keybinding parse(cmd+shift+alt+ctrl+a)', () { + const command = 'cmd+shift+alt+ctrl+a'; + final keybinding = Keybinding.parse(command); + expect(keybinding.isAltPressed, true); + expect(keybinding.isShiftPressed, true); + expect(keybinding.isMetaPressed, true); + expect(keybinding.isControlPressed, true); + expect(keybinding.keyLabel, 'a'); + }); + + test('keybinding parse(cmd+shift+alt+a)', () { + const command = 'cmd+shift+alt+a'; + final keybinding = Keybinding.parse(command); + expect(keybinding.isAltPressed, true); + expect(keybinding.isShiftPressed, true); + expect(keybinding.isMetaPressed, true); + expect(keybinding.isControlPressed, false); + expect(keybinding.keyLabel, 'a'); + }); + + test('keybinding parse(cmd+shift+ctrl+a)', () { + const command = 'cmd+shift+ctrl+a'; + final keybinding = Keybinding.parse(command); + expect(keybinding.isAltPressed, false); + expect(keybinding.isShiftPressed, true); + expect(keybinding.isMetaPressed, true); + expect(keybinding.isControlPressed, true); + expect(keybinding.keyLabel, 'a'); + }); + + test('keybinding parse(cmd+alt+ctrl+a)', () { + const command = 'cmd+alt+ctrl+a'; + final keybinding = Keybinding.parse(command); + expect(keybinding.isAltPressed, true); + expect(keybinding.isShiftPressed, false); + expect(keybinding.isMetaPressed, true); + expect(keybinding.isControlPressed, true); + expect(keybinding.keyLabel, 'a'); + }); + + test('keybinding parse(shift+alt+ctrl+a)', () { + const command = 'shift+alt+ctrl+a'; + final keybinding = Keybinding.parse(command); + expect(keybinding.isAltPressed, true); + expect(keybinding.isShiftPressed, true); + expect(keybinding.isMetaPressed, false); + expect(keybinding.isControlPressed, true); + expect(keybinding.keyLabel, 'a'); + }); + + test('keybinding copyWith', () { + const command = 'shift+alt+ctrl+a'; + final keybinding = + Keybinding.parse(command).copyWith(isMetaPressed: true); + expect(keybinding.isAltPressed, true); + expect(keybinding.isShiftPressed, true); + expect(keybinding.isMetaPressed, true); + expect(keybinding.isControlPressed, true); + expect(keybinding.keyLabel, 'a'); + }); + + test('keybinding equal', () { + const command = 'cmd+shift+alt+ctrl+a'; + expect(Keybinding.parse(command), Keybinding.parse(command)); + }); + + test('keybinding toMap', () { + const command = 'cmd+shift+alt+ctrl+a'; + final keybinding = Keybinding.parse(command); + expect(keybinding, Keybinding.fromMap(keybinding.toMap())); + }); + }); +} diff --git a/frontend/app_flowy/packages/appflowy_editor/test/service/shortcut_event/shortcut_event_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/service/shortcut_event/shortcut_event_test.dart new file mode 100644 index 0000000000..1a479ad086 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/test/service/shortcut_event/shortcut_event_test.dart @@ -0,0 +1,75 @@ +import 'dart:io'; + +import 'package:appflowy_editor/src/service/shortcut_event/built_in_shortcut_events.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:appflowy_editor/appflowy_editor.dart'; +import '../../infra/test_editor.dart'; + +void main() async { + setUpAll(() { + TestWidgetsFlutterBinding.ensureInitialized(); + }); + + group('shortcut_event.dart', () { + test('redefine shortcut event command', () { + final shortcutEvent = ShortcutEvent( + key: 'Sample', + command: 'cmd+shift+alt+ctrl+a', + handler: (editorState, event) { + return KeyEventResult.handled; + }, + ); + shortcutEvent.updateCommand('cmd+shift+alt+ctrl+b'); + expect(shortcutEvent.keybindings.length, 1); + expect(shortcutEvent.keybindings.first.isMetaPressed, true); + expect(shortcutEvent.keybindings.first.isShiftPressed, true); + expect(shortcutEvent.keybindings.first.isAltPressed, true); + expect(shortcutEvent.keybindings.first.isControlPressed, true); + expect(shortcutEvent.keybindings.first.keyLabel, 'b'); + }); + + testWidgets('redefine move cursor begin command', (tester) async { + const text = 'Welcome to Appflowy 😁'; + final editor = tester.editor + ..insertTextNode(text) + ..insertTextNode(text); + await editor.startTesting(); + await editor.updateSelection( + Selection.single(path: [1], startOffset: text.length), + ); + if (Platform.isWindows) { + await editor.pressLogicKey( + LogicalKeyboardKey.arrowLeft, + isControlPressed: true, + ); + } else { + await editor.pressLogicKey( + LogicalKeyboardKey.arrowLeft, + isMetaPressed: true, + ); + } + expect( + editor.documentSelection, + Selection.single(path: [1], startOffset: 0), + ); + await editor.updateSelection( + Selection.single(path: [1], startOffset: text.length), + ); + for (final event in builtInShortcutEvents) { + if (event.key == 'Move cursor begin') { + event.updateCommand('alt+arrow left'); + } + } + await editor.pressLogicKey( + LogicalKeyboardKey.arrowLeft, + isAltPressed: true, + ); + expect( + editor.documentSelection, + Selection.single(path: [1], startOffset: 0), + ); + }); + }); +}