editor_test_operations.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/document_header_node_widget.dart';
  5. import 'package:appflowy/plugins/document/presentation/editor_plugins/header/emoji_icon_widget.dart';
  6. import 'package:appflowy/plugins/document/presentation/editor_plugins/header/emoji_popover.dart';
  7. import 'package:appflowy_editor/appflowy_editor.dart' hide Log;
  8. import 'package:easy_localization/easy_localization.dart';
  9. import 'package:flutter/services.dart';
  10. import 'package:flutter_test/flutter_test.dart';
  11. import 'ime.dart';
  12. import 'util.dart';
  13. extension EditorWidgetTester on WidgetTester {
  14. EditorOperations get editor => EditorOperations(this);
  15. }
  16. class EditorOperations {
  17. const EditorOperations(this.tester);
  18. final WidgetTester tester;
  19. EditorState getCurrentEditorState() {
  20. return tester
  21. .widget<AppFlowyEditor>(find.byType(AppFlowyEditor))
  22. .editorState;
  23. }
  24. /// Tap the line of editor at [index]
  25. Future<void> tapLineOfEditorAt(int index) async {
  26. final textBlocks = find.byType(TextBlockComponentWidget);
  27. await tester.tapAt(tester.getTopRight(textBlocks.at(index)));
  28. await tester.pumpAndSettle();
  29. }
  30. /// Hover on cover plugin button above the document
  31. Future<void> hoverOnCoverToolbar() async {
  32. final coverToolbar = find.byType(DocumentHeaderToolbar);
  33. await tester.startGesture(
  34. tester.getBottomLeft(coverToolbar).translate(5, -5),
  35. kind: PointerDeviceKind.mouse,
  36. );
  37. await tester.pumpAndSettle();
  38. }
  39. /// Taps on the 'Add Icon' button in the cover toolbar
  40. Future<void> tapAddIconButton() async {
  41. await tester.tapButtonWithName(
  42. LocaleKeys.document_plugins_cover_addIcon.tr(),
  43. );
  44. expect(find.byType(EmojiPopover), findsOneWidget);
  45. }
  46. /// Taps the 'Remove Icon' button in the cover toolbar and the icon popover
  47. Future<void> tapRemoveIconButton({bool isInPicker = false}) async {
  48. Finder button =
  49. find.text(LocaleKeys.document_plugins_cover_removeIcon.tr());
  50. if (isInPicker) {
  51. button = find.descendant(of: find.byType(EmojiPopover), matching: button);
  52. }
  53. await tester.tapButton(button);
  54. }
  55. /// Requires that the document must already have an icon. This opens the icon
  56. /// picker
  57. Future<void> tapOnIconWidget() async {
  58. final iconWidget = find.byType(EmojiIconWidget);
  59. await tester.tapButton(iconWidget);
  60. }
  61. Future<void> tapOnAddCover() async {
  62. await tester.tapButtonWithName(
  63. LocaleKeys.document_plugins_cover_addCover.tr(),
  64. );
  65. }
  66. Future<void> tapOnChangeCover() async {
  67. await tester.tapButtonWithName(
  68. LocaleKeys.document_plugins_cover_changeCover.tr(),
  69. );
  70. }
  71. Future<void> switchSolidColorBackground() async {
  72. final findPurpleButton = find.byWidgetPredicate(
  73. (widget) => widget is ColorItem && widget.option.colorHex == "ffe8e0ff",
  74. );
  75. await tester.tapButton(findPurpleButton);
  76. }
  77. Future<void> tapOnRemoveCover() async {
  78. await tester.tapButton(find.byType(DeleteCoverButton));
  79. }
  80. /// A cover must be present in the document to function properly since this
  81. /// catches all cover types collectively
  82. Future<void> hoverOnCover() async {
  83. final cover = find.byType(DocumentCover);
  84. await tester.startGesture(
  85. tester.getCenter(cover),
  86. kind: PointerDeviceKind.mouse,
  87. );
  88. await tester.pumpAndSettle();
  89. }
  90. Future<void> dismissCoverPicker() async {
  91. await tester.sendKeyEvent(LogicalKeyboardKey.escape);
  92. await tester.pumpAndSettle();
  93. }
  94. /// trigger the slash command (selection menu)
  95. Future<void> showSlashMenu() async {
  96. await tester.ime.insertCharacter('/');
  97. }
  98. /// trigger the slash command (selection menu)
  99. Future<void> showAtMenu() async {
  100. await tester.ime.insertCharacter('@');
  101. }
  102. /// Tap the slash menu item with [name]
  103. ///
  104. /// Must call [showSlashMenu] first.
  105. Future<void> tapSlashMenuItemWithName(String name) async {
  106. final slashMenuItem = find.text(name, findRichText: true);
  107. await tester.tapButton(slashMenuItem);
  108. }
  109. /// Tap the at menu item with [name]
  110. ///
  111. /// Must call [showAtMenu] first.
  112. Future<void> tapAtMenuItemWithName(String name) async {
  113. final atMenuItem = find.descendant(
  114. of: find.byType(SelectionMenuWidget),
  115. matching: find.text(name, findRichText: true),
  116. );
  117. await tester.tapButton(atMenuItem);
  118. }
  119. }