Skip to content

Commit

Permalink
test: improve coverage for arrow_keys_handler.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Xazin committed Apr 10, 2023
1 parent 47911fc commit 1b014c2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 38 deletions.
60 changes: 23 additions & 37 deletions lib/src/service/internal_key_event_handlers/arrow_keys_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ShortcutEventHandler cursorUpSelect = (editorState, event) {
if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}
final end = _goUp(editorState);
final end = _moveVertical(editorState);
if (end == null) {
return KeyEventResult.ignored;
}
Expand All @@ -55,7 +55,7 @@ ShortcutEventHandler cursorDownSelect = (editorState, event) {
if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}
final end = _goDown(editorState);
final end = _moveVertical(editorState, upwards: false);
if (end == null) {
return KeyEventResult.ignored;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ ShortcutEventHandler cursorUp = (editorState, event) {
return KeyEventResult.ignored;
}

final upPosition = _goUp(editorState);
final upPosition = _moveVertical(editorState);
editorState.updateCursorSelection(
upPosition == null ? null : Selection.collapsed(upPosition),
);
Expand All @@ -241,7 +241,7 @@ ShortcutEventHandler cursorDown = (editorState, event) {
return KeyEventResult.ignored;
}

final downPosition = _goDown(editorState);
final downPosition = _moveVertical(editorState, upwards: false);
editorState.updateCursorSelection(
downPosition == null ? null : Selection.collapsed(downPosition),
);
Expand Down Expand Up @@ -448,9 +448,9 @@ extension on Position {
path: path,
offset: node.delta.prevRunePosition(offset),
);
} else {
return Position(path: path, offset: offset);
}

return Position(path: path, offset: offset);
case _SelectionRange.word:
if (node is TextNode) {
final result = node.selectable?.getWordBoundaryInPosition(
Expand All @@ -462,11 +462,10 @@ extension on Position {
if (result != null) {
return result.start;
}
} else {
return Position(path: path, offset: offset);
}

return Position(path: path, offset: offset);
}
return null;
}

Position? goRight(
Expand All @@ -490,61 +489,48 @@ extension on Position {
path: path,
offset: node.delta.nextRunePosition(offset),
);
} else {
return Position(path: path, offset: offset);
}

return Position(path: path, offset: offset);
case _SelectionRange.word:
if (node is TextNode) {
final result = node.selectable?.getWordBoundaryInPosition(this);
if (result != null) {
return result.end;
}
} else {
return Position(path: path, offset: offset);
}

return Position(path: path, offset: offset);
}
return null;
}
}

Position? _goUp(EditorState editorState) {
Position? _moveVertical(
EditorState editorState, {
bool upwards = true,
}) {
final selection = editorState.service.selectionService.currentSelection.value;
final rects = editorState.service.selectionService.selectionRects;
if (rects.isEmpty || selection == null) {
return null;
}
Offset offset;
if (selection.isBackward) {
final rect = rects.reduce(
(current, next) => current.bottom >= next.bottom ? current : next,
);
offset = rect.topRight.translate(0, -rect.height);
} else {
final rect = rects.reduce(
(current, next) => current.top <= next.top ? current : next,
);
offset = rect.topLeft.translate(0, -rect.height);
}
return editorState.service.selectionService.getPositionInOffset(offset);
}

Position? _goDown(EditorState editorState) {
final selection = editorState.service.selectionService.currentSelection.value;
final rects = editorState.service.selectionService.selectionRects;
if (rects.isEmpty || selection == null) {
return null;
}
Offset offset;
if (selection.isBackward) {
final rect = rects.reduce(
(current, next) => current.bottom >= next.bottom ? current : next,
);
offset = rect.bottomRight.translate(0, rect.height);
offset = upwards
? rect.topRight.translate(0, -rect.height)
: rect.bottomRight.translate(0, rect.height);
} else {
final rect = rects.reduce(
(current, next) => current.top <= next.top ? current : next,
);
offset = rect.bottomLeft.translate(0, rect.height);
offset = upwards
? rect.topLeft.translate(0, -rect.height)
: rect.bottomLeft.translate(0, rect.height);
}

return editorState.service.selectionService.getPositionInOffset(offset);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:io';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../infra/test_editor.dart';
Expand Down Expand Up @@ -50,6 +49,69 @@ void main() async {
});
});

testWidgets('Cursor up/down', (tester) async {
final editor = tester.editor
..insertTextNode("Welcome")
..insertTextNode("Welcome to AppFlowy");
await editor.startTesting();

await editor.updateSelection(Selection.single(path: [1], startOffset: 19));

await editor.pressLogicKey(key: LogicalKeyboardKey.arrowUp);

expect(
editor.documentSelection,
Selection.single(path: [0], startOffset: 7),
);

await editor.pressLogicKey(key: LogicalKeyboardKey.arrowDown);

expect(
editor.documentSelection,
Selection.single(path: [1], startOffset: 7),
);
});

testWidgets('Cursor top/bottom select', (tester) async {
const text = 'Welcome to Appflowy';
final editor = tester.editor
..insertTextNode(text)
..insertTextNode(text)
..insertTextNode(text);
await editor.startTesting();

Future<void> select(bool isTop) async {
await editor.pressLogicKey(
key: isTop ? LogicalKeyboardKey.arrowUp : LogicalKeyboardKey.arrowDown,
isMetaPressed: Platform.isMacOS,
isControlPressed: !Platform.isMacOS,
isShiftPressed: true,
);
}

await editor.updateSelection(Selection.single(path: [1], startOffset: 7));

await select(true);

expect(
editor.documentSelection,
Selection(
start: Position(path: [1], offset: 7),
end: Position(path: [0]),
),
);

await select(false);

expect(
editor.documentSelection,
Selection(
start: Position(path: [1], offset: 7),
end: Position(path: [2], offset: 19),
),
);
});

testWidgets('Presses alt + arrow right key, move the cursor one word right',
(tester) async {
const text = 'Welcome to Appflowy';
Expand Down

0 comments on commit 1b014c2

Please sign in to comment.