2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								# Testing
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The directory structure of test files mirrors that of the code files, making it easy for us to map a file with the corresponding test and check if the test is updated.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								For an overview of testing best practices in Flutter applications, please refer to Flutter's [introduction to widget testing ](https://docs.flutter.dev/cookbook/testing/widget/introduction ) as well as their [introduction to unit testing ](https://docs.flutter.dev/cookbook/testing/unit/introduction ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								There you will learn how to do such things as such as simulate a click as well as leverage the `test`  and `expect`  functions.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Testing Basic Editor Functions
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The example code below shows how to construct a document that will be used in our testing.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const text = 'Welcome to Appflowy 😁';
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// Get the instance of the editor.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								final editor = tester.editor;
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// Insert an empty text node.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								editor.insertEmptyTextNode();
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// Insert a text node with the text string we defined earlier.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								editor.insertTextNode(text);
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// Insert the same text, but with the heading style.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								editor.insertTextNode(text, attributes: {
							 
						 
					
						
							
								
									
										
										
										
											2022-09-15 21:27:49 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    BuiltInAttributeKey.heading: BuiltInAttributeKey.h1,
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								});
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// Insert our text with the bulleted list style and the bold style.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// If you want to modify the style of the inserted text, you need to use the Delta parameter.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								editor.insertTextNode(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    '',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    attributes: {
							 
						 
					
						
							
								
									
										
										
										
											2022-09-15 21:27:49 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        BuiltInAttributeKey.subtype: BuiltInAttributeKey.bulletedList,
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    delta: Delta([
							 
						 
					
						
							
								
									
										
										
										
											2022-09-15 21:27:49 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        TextInsert(text, {BuiltInAttributeKey.bold: true}),
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    ]),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The `startTesting`  function of the editor must be called before you begin your test.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								await editor.startTesting();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Get the number of nodes in the document.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								final length = editor.documentLength;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								print(length);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Get the node of a defined path. In this case we are getting the first node of the document which is the text "Welcome to Appflowy 😁".
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								final firstTextNode = editor.nodeAtPath([0]) as TextNode;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Update the [Selection ](https://github.com/AppFlowy-IO/AppFlowy/blob/main/frontend/app_flowy/packages/appflowy_editor/lib/src/document/selection.dart ) so that our text "Welcome to Appflowy 😁" is selected. We will start our selection from the beginning of the string.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								await editor.updateSelection(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    Selection.single(path: firstTextNode.path, startOffset: 0),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Get the current selection.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								final selection = editor.documentSelection;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								print(selection);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Next we will simulate the input of a shortcut key being pressed that will select all the text.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// Meta + A.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								await editor.pressLogicKey(LogicalKeyboardKey.keyA, isMetaPressed: true);
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// Meta + shift + S.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								await editor.pressLogicKey(
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    LogicalKeyboardKey.keyS,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    isMetaPressed: true,
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    isShiftPressed: true,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								We will then simulate text input.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
									
										
										
										
											2022-08-17 17:02:54 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// Insert 'Hello World' at the beginning of the first node.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								editor.insertText(firstTextNode, 'Hello World', 0);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Once the text has been added, we can get information about the text node.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// Get the text of the first text node as plain text
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								final textAfterInserted = firstTextNode.toRawString();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								print(textAfterInserted);
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// Get the attributes of the text node
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								final attributes = firstTextNode.attributes;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								print(attributes);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								## A Complete Code Example
 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-17 21:36:21 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-02 00:18:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								In the example code below we are going to test `select_all_handler.dart`  by inserting 100 lines of text that read "Welcome to Appflowy 😁" and then simulating the "selectAll" shortcut key being pressed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Afterwards, we will `expect`  that the current selection of the editor is equal to the selection of all the lines that were generated.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```dart
							 
						 
					
						
							
								
									
										
										
										
											2022-08-17 17:23:20 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								import 'package:appflowy_editor/appflowy_editor.dart';
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								import 'package:flutter/services.dart';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								import 'package:flutter_test/flutter_test.dart';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								import '../../infra/test_editor.dart';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								void main() async {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  setUpAll(() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    TestWidgetsFlutterBinding.ensureInitialized();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  group('select_all_handler_test.dart', () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    testWidgets('Presses Command + A in the document', (tester) async {
							 
						 
					
						
							
								
									
										
										
										
											2022-08-17 17:02:54 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      const lines = 100;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      const text = 'Welcome to Appflowy 😁';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      final editor = tester.editor;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      for (var i = 0; i <  lines ;  i + + )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        editor.insertTextNode(text);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      await editor.startTesting();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      await editor.pressLogicKey(LogicalKeyboardKey.keyA, isMetaPressed: true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      expect(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        editor.documentSelection,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        Selection(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          start: Position(path: [0], offset: 0),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          end: Position(path: [lines - 1], offset: text.length),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        ),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      );
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    });
							 
						 
					
						
							
								
									
										
										
										
											2022-08-17 17:02:54 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  });
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 15:08:51 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```