From d6f1593a20b4a5dfbcb758ffc9144ed51b8b4a80 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Sat, 13 Aug 2022 22:37:46 +0800 Subject: [PATCH] test: implement enter key test for styled text --- .../src/service/render_plugin_service.dart | 3 +- .../lib/src/service/selection_service.dart | 4 +- ...thout_shift_in_text_node_handler_test.dart | 110 ++++++++++++++++-- .../flowy_editor/test/infra/test_editor.dart | 9 +- 4 files changed, 108 insertions(+), 18 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/service/render_plugin_service.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/service/render_plugin_service.dart index e973d16d6a..b6f9414a9e 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/src/service/render_plugin_service.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/src/service/render_plugin_service.dart @@ -77,7 +77,8 @@ class FlowyRenderPlugin extends FlowyRenderPluginService { node.key = key; return _autoUpdateNodeWidget(builder, context); } else { - assert(false, 'Could not query the builder with this $name'); + assert(false, + 'Could not query the builder with this $name, or nodeValidator return false.'); // TODO: return a placeholder widget with tips. return Container(); } diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/service/selection_service.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/service/selection_service.dart index ecf013d0a4..a256d71f03 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/src/service/selection_service.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/src/service/selection_service.dart @@ -187,11 +187,11 @@ class _FlowySelectionState extends State if (selection != null) { if (selection.isCollapsed) { /// updates cursor area. - debugPrint('updating cursor'); + debugPrint('updating cursor, $selection'); _updateCursorAreas(selection.start); } else { // updates selection area. - debugPrint('updating selection'); + debugPrint('updating selection, $selection'); _updateSelectionAreas(selection); } } diff --git a/frontend/app_flowy/packages/flowy_editor/test/editor/key_event_tests/enter_without_shift_in_text_node_handler_test.dart b/frontend/app_flowy/packages/flowy_editor/test/editor/key_event_tests/enter_without_shift_in_text_node_handler_test.dart index 661862416b..dd722ec0d0 100644 --- a/frontend/app_flowy/packages/flowy_editor/test/editor/key_event_tests/enter_without_shift_in_text_node_handler_test.dart +++ b/frontend/app_flowy/packages/flowy_editor/test/editor/key_event_tests/enter_without_shift_in_text_node_handler_test.dart @@ -1,4 +1,5 @@ import 'package:flowy_editor/flowy_editor.dart'; +import 'package:flowy_editor/src/render/rich_text/rich_text_style.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import '../../infra/test_editor.dart'; @@ -8,11 +9,9 @@ void main() async { TestWidgetsFlutterBinding.ensureInitialized(); }); - group('Enter key without shift handler', () { - testWidgets('Pressing enter key in empty document', (tester) async { - final editor = tester.editor - ..initialize() - ..insertEmptyTextNode(); + group('enter_without_shift_in_text_node_handler.dart', () { + testWidgets('Presses enter key in empty document', (tester) async { + final editor = tester.editor..insertEmptyTextNode(); await editor.startTesting(); await editor.updateSelection( Selection.collapsed( @@ -30,19 +29,32 @@ void main() async { } }); - testWidgets('Pressing enter key in non-empty document', (tester) async { + testWidgets('Presses enter key in non-empty document', (tester) async { + // Before + // + // Welcome to Appflowy 😁 + // Welcome to Appflowy 😁 + // Welcome to Appflowy 😁 + // + // After + // + // Welcome to Appflowy 😁 + // Welcome to Appflowy 😁 + // [Empty Line] + // Welcome to Appflowy 😁 + // const text = 'Welcome to Appflowy 😁'; - var lines = 5; + var lines = 3; - final editor = tester.editor..initialize(); + final editor = tester.editor; for (var i = 1; i <= lines; i++) { - editor.insertTextNode(text: text); + editor.insertTextNode(text); } await editor.startTesting(); expect(editor.documentLength, lines); - // Pressing the enter key in last line. + // Presses the enter key in last line. await editor.updateSelection( Selection.collapsed( Position(path: [lines - 1], offset: 0), @@ -52,7 +64,6 @@ void main() async { LogicalKeyboardKey.enter, ); lines += 1; - await tester.pumpAndSettle(const Duration(microseconds: 500)); expect(editor.documentLength, lines); expect(editor.documentSelection, Selection.collapsed(Position(path: [lines - 1], offset: 0))); @@ -60,10 +71,87 @@ void main() async { expect(lastNode != null, true); expect(lastNode is TextNode, true); lastNode = lastNode as TextNode; + for (final node in editor.root.children) { + print( + 'path = ${node.path}, text = ${(node as TextNode).toRawString()}'); + } expect(lastNode.delta.toRawString(), text); expect((lastNode.previous as TextNode).delta.toRawString(), ''); expect( (lastNode.previous!.previous as TextNode).delta.toRawString(), text); }); + + // Before + // + // Welcome to Appflowy 😁 + // [Style] Welcome to Appflowy 😁 + // [Style] Welcome to Appflowy 😁 + // + // After + // + // Welcome to Appflowy 😁 + // [Empty Line] + // [Style] Welcome to Appflowy 😁 + // [Style] Welcome to Appflowy 😁 + // [Style] + testWidgets('Presses enter key in bulleted list', (tester) async { + await _testStyleNeedToBeCopy(tester, StyleKey.bulletedList); + }); + testWidgets('Presses enter key in numbered list', (tester) async { + await _testStyleNeedToBeCopy(tester, StyleKey.numberList); + }); + testWidgets('Presses enter key in checkbox styled text', (tester) async { + await _testStyleNeedToBeCopy(tester, StyleKey.checkbox); + }); + testWidgets('Presses enter key in quoted text', (tester) async { + await _testStyleNeedToBeCopy(tester, StyleKey.quote); + }); }); } + +Future _testStyleNeedToBeCopy(WidgetTester tester, String style) async { + const text = 'Welcome to Appflowy 😁'; + Attributes attributes = { + StyleKey.subtype: style, + }; + if (style == StyleKey.checkbox) { + attributes[StyleKey.checkbox] = false; + } else if (style == StyleKey.numberList) { + attributes[StyleKey.number] = 1; + } + final editor = tester.editor + ..insertTextNode(text) + ..insertTextNode(text, attributes: attributes) + ..insertTextNode(text, attributes: attributes); + + await editor.startTesting(); + await editor.updateSelection( + Selection.collapsed( + Position(path: [1], offset: 0), + ), + ); + await editor.pressLogicKey( + LogicalKeyboardKey.enter, + ); + expect(editor.documentSelection, + Selection.collapsed(Position(path: [2], offset: 0))); + + await editor.updateSelection( + Selection.collapsed( + Position(path: [3], offset: text.length), + ), + ); + await editor.pressLogicKey( + LogicalKeyboardKey.enter, + ); + expect(editor.documentSelection, + Selection.collapsed(Position(path: [4], offset: 0))); + expect(editor.nodeAtPath([4])?.subtype, style); + + await editor.pressLogicKey( + LogicalKeyboardKey.enter, + ); + expect(editor.documentSelection, + Selection.collapsed(Position(path: [4], offset: 0))); + expect(editor.nodeAtPath([4])?.subtype, null); +} diff --git a/frontend/app_flowy/packages/flowy_editor/test/infra/test_editor.dart b/frontend/app_flowy/packages/flowy_editor/test/infra/test_editor.dart index 19570ee0f8..3f780412c4 100644 --- a/frontend/app_flowy/packages/flowy_editor/test/infra/test_editor.dart +++ b/frontend/app_flowy/packages/flowy_editor/test/infra/test_editor.dart @@ -39,15 +39,15 @@ class EditorWidgetTester { _editorState = _createEmptyDocument(); } - insert(T node) { + void insert(T node) { _editorState.document.root.insert(node); } - insertEmptyTextNode() { + void insertEmptyTextNode() { insert(TextNode.empty()); } - insertTextNode({String? text, Attributes? attributes}) { + void insertTextNode(String? text, {Attributes? attributes}) { insert( TextNode( type: 'text', @@ -102,6 +102,7 @@ class EditorWidgetTester { } extension TestEditorExtension on WidgetTester { - EditorWidgetTester get editor => EditorWidgetTester(tester: this); + EditorWidgetTester get editor => + EditorWidgetTester(tester: this)..initialize(); EditorState get editorState => editor.editorState; }