浏览代码

test: add shortcut_event / keybinding test

Lucas.Xu 2 年之前
父节点
当前提交
2571c6b1bf

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/service/shortcut_event/built_in_shortcut_events.dart

@@ -58,7 +58,7 @@ List<ShortcutEvent> builtInShortcutEvents = [
     key: 'Move cursor top',
     command: 'meta+arrow up',
     windowsCommand: 'ctrl+arrow up',
-    handler: cursorBegin,
+    handler: cursorTop,
   ),
   ShortcutEvent(
     key: 'Move cursor bottom',

+ 3 - 0
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;
     }

+ 82 - 0
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()));
+    });
+  });
+}

+ 75 - 0
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),
+      );
+    });
+  });
+}