editor_test_operations.dart 5.7 KB

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