editor_test_operations.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import 'dart:ui';
  2. import 'package:appflowy/generated/locale_keys.g.dart';
  3. import 'package:appflowy/plugins/document/presentation/editor_plugins/actions/block_action_add_button.dart';
  4. import 'package:appflowy/plugins/document/presentation/editor_plugins/header/cover_editor.dart';
  5. import 'package:appflowy/plugins/document/presentation/editor_plugins/header/custom_cover_picker.dart';
  6. import 'package:appflowy/plugins/document/presentation/editor_plugins/header/document_header_node_widget.dart';
  7. import 'package:appflowy/plugins/document/presentation/editor_plugins/header/emoji_icon_widget.dart';
  8. import 'package:appflowy/plugins/document/presentation/editor_plugins/header/emoji_popover.dart';
  9. import 'package:appflowy/plugins/inline_actions/widgets/inline_actions_handler.dart';
  10. import 'package:appflowy_editor/appflowy_editor.dart' hide Log;
  11. import 'package:easy_localization/easy_localization.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter/services.dart';
  14. import 'package:flutter_test/flutter_test.dart';
  15. import 'util.dart';
  16. extension EditorWidgetTester on WidgetTester {
  17. EditorOperations get editor => EditorOperations(this);
  18. }
  19. class EditorOperations {
  20. const EditorOperations(this.tester);
  21. final WidgetTester tester;
  22. EditorState getCurrentEditorState() {
  23. return tester
  24. .widget<AppFlowyEditor>(find.byType(AppFlowyEditor))
  25. .editorState;
  26. }
  27. /// Tap the line of editor at [index]
  28. Future<void> tapLineOfEditorAt(int index) async {
  29. final textBlocks = find.byType(AppFlowyRichText);
  30. index = index.clamp(0, textBlocks.evaluate().length - 1);
  31. await tester.tapAt(tester.getTopRight(textBlocks.at(index)));
  32. await tester.pumpAndSettle();
  33. }
  34. /// Hover on cover plugin button above the document
  35. Future<void> hoverOnCoverToolbar() async {
  36. final coverToolbar = find.byType(DocumentHeaderToolbar);
  37. await tester.startGesture(
  38. tester.getBottomLeft(coverToolbar).translate(5, -5),
  39. kind: PointerDeviceKind.mouse,
  40. );
  41. await tester.pumpAndSettle();
  42. }
  43. /// Taps on the 'Add Icon' button in the cover toolbar
  44. Future<void> tapAddIconButton() async {
  45. await tester.tapButtonWithName(
  46. LocaleKeys.document_plugins_cover_addIcon.tr(),
  47. );
  48. expect(find.byType(EmojiPopover), findsOneWidget);
  49. }
  50. /// Taps the 'Remove Icon' button in the cover toolbar and the icon popover
  51. Future<void> tapRemoveIconButton({bool isInPicker = false}) async {
  52. Finder button =
  53. find.text(LocaleKeys.document_plugins_cover_removeIcon.tr());
  54. if (isInPicker) {
  55. button = find.descendant(of: find.byType(EmojiPopover), matching: button);
  56. }
  57. await tester.tapButton(button);
  58. }
  59. /// Requires that the document must already have an icon. This opens the icon
  60. /// picker
  61. Future<void> tapOnIconWidget() async {
  62. final iconWidget = find.byType(EmojiIconWidget);
  63. await tester.tapButton(iconWidget);
  64. }
  65. Future<void> tapOnAddCover() async {
  66. await tester.tapButtonWithName(
  67. LocaleKeys.document_plugins_cover_addCover.tr(),
  68. );
  69. }
  70. Future<void> tapOnChangeCover() async {
  71. await tester.tapButtonWithName(
  72. LocaleKeys.document_plugins_cover_changeCover.tr(),
  73. );
  74. }
  75. Future<void> switchSolidColorBackground() async {
  76. final findPurpleButton = find.byWidgetPredicate(
  77. (widget) => widget is ColorItem && widget.option.name == 'Purple',
  78. );
  79. await tester.tapButton(findPurpleButton);
  80. }
  81. Future<void> addNetworkImageCover(String imageUrl) async {
  82. final findNewImageButton = find.byType(NewCustomCoverButton);
  83. await tester.tapButton(findNewImageButton);
  84. final imageUrlTextField = find.descendant(
  85. of: find.byType(NetworkImageUrlInput),
  86. matching: find.byType(TextField),
  87. );
  88. await tester.enterText(imageUrlTextField, imageUrl);
  89. await tester.tapButtonWithName(
  90. LocaleKeys.document_plugins_cover_add.tr(),
  91. );
  92. await tester.tapButtonWithName(
  93. LocaleKeys.document_plugins_cover_saveToGallery.tr(),
  94. );
  95. }
  96. Future<void> switchNetworkImageCover(String imageUrl) async {
  97. final image = find.byWidgetPredicate(
  98. (widget) => widget is ImageGridItem,
  99. );
  100. await tester.tapButton(image);
  101. }
  102. Future<void> tapOnRemoveCover() async {
  103. await tester.tapButton(find.byType(DeleteCoverButton));
  104. }
  105. /// A cover must be present in the document to function properly since this
  106. /// catches all cover types collectively
  107. Future<void> hoverOnCover() async {
  108. final cover = find.byType(DocumentCover);
  109. await tester.startGesture(
  110. tester.getCenter(cover),
  111. kind: PointerDeviceKind.mouse,
  112. );
  113. await tester.pumpAndSettle();
  114. }
  115. Future<void> dismissCoverPicker() async {
  116. await tester.sendKeyEvent(LogicalKeyboardKey.escape);
  117. await tester.pumpAndSettle();
  118. }
  119. /// trigger the slash command (selection menu)
  120. Future<void> showSlashMenu() async {
  121. await tester.ime.insertCharacter('/');
  122. }
  123. /// trigger the slash command (selection menu)
  124. Future<void> showAtMenu() async {
  125. await tester.ime.insertCharacter('@');
  126. }
  127. /// Tap the slash menu item with [name]
  128. ///
  129. /// Must call [showSlashMenu] first.
  130. Future<void> tapSlashMenuItemWithName(String name) async {
  131. final slashMenuItem = find.text(name, findRichText: true);
  132. await tester.tapButton(slashMenuItem);
  133. }
  134. /// Tap the at menu item with [name]
  135. ///
  136. /// Must call [showAtMenu] first.
  137. Future<void> tapAtMenuItemWithName(String name) async {
  138. final atMenuItem = find.descendant(
  139. of: find.byType(InlineActionsHandler),
  140. matching: find.text(name, findRichText: true),
  141. );
  142. await tester.tapButton(atMenuItem);
  143. }
  144. /// Update the editor's selection
  145. Future<void> updateSelection(Selection selection) async {
  146. final editorState = getCurrentEditorState();
  147. editorState.updateSelectionWithReason(
  148. selection,
  149. reason: SelectionUpdateReason.uiEvent,
  150. );
  151. await tester.pumpAndSettle(const Duration(milliseconds: 200));
  152. }
  153. /// hover and click on the + button beside the block component.
  154. Future<void> hoverAndClickOptionAddButton(
  155. Path path,
  156. bool withModifiedKey, // alt on windows or linux, option on macos
  157. ) async {
  158. final optionAddButton = find.byWidgetPredicate(
  159. (widget) =>
  160. widget is BlockComponentActionWrapper &&
  161. widget.node.path.equals(path),
  162. );
  163. await tester.hoverOnWidget(
  164. optionAddButton,
  165. onHover: () async {
  166. if (withModifiedKey) {
  167. await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
  168. }
  169. await tester.tapButton(
  170. find.byWidgetPredicate(
  171. (widget) =>
  172. widget is BlockAddButton &&
  173. widget.blockComponentContext.node.path.equals(path),
  174. ),
  175. );
  176. if (withModifiedKey) {
  177. await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
  178. }
  179. },
  180. );
  181. }
  182. }