editor_test_operations.dart 5.7 KB

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