common_operations.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import 'dart:ui';
  2. import 'package:appflowy_backend/log.dart';
  3. import 'package:appflowy/generated/locale_keys.g.dart';
  4. import 'package:appflowy/plugins/document/presentation/share/share_button.dart';
  5. import 'package:appflowy/user/presentation/skip_log_in_screen.dart';
  6. import 'package:appflowy/workspace/presentation/home/menu/app/header/add_button.dart';
  7. import 'package:appflowy/workspace/presentation/home/menu/app/section/item.dart';
  8. import 'package:appflowy/workspace/presentation/settings/widgets/settings_language_view.dart';
  9. import 'package:appflowy_editor/appflowy_editor.dart' hide Log;
  10. import 'package:easy_localization/easy_localization.dart';
  11. import 'package:flowy_infra_ui/widget/buttons/primary_button.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter_test/flutter_test.dart';
  14. import 'util.dart';
  15. extension CommonOperations on WidgetTester {
  16. /// Get current file location of AppFlowy.
  17. Future<String> currentFileLocation() async {
  18. return TestFolder.currentLocation();
  19. }
  20. /// Tap the GetStart button on the launch page.
  21. Future<void> tapGoButton() async {
  22. final goButton = find.byType(GoButton);
  23. await tapButton(goButton);
  24. }
  25. /// Tap the + button on the home page.
  26. Future<void> tapAddButton() async {
  27. final addButton = find.byType(AddButton);
  28. await tapButton(addButton);
  29. }
  30. /// Tap the create document button.
  31. ///
  32. /// Must call [tapAddButton] first.
  33. Future<void> tapCreateDocumentButton() async {
  34. await tapButtonWithName(LocaleKeys.document_menuName.tr());
  35. }
  36. /// Tap the create grid button.
  37. ///
  38. /// Must call [tapAddButton] first.
  39. Future<void> tapCreateGridButton() async {
  40. await tapButtonWithName(LocaleKeys.grid_menuName.tr());
  41. }
  42. /// Tap the import button.
  43. ///
  44. /// Must call [tapAddButton] first.
  45. Future<void> tapImportButton() async {
  46. await tapButtonWithName(LocaleKeys.moreAction_import.tr());
  47. }
  48. /// Tap the import from text & markdown button.
  49. ///
  50. /// Must call [tapImportButton] first.
  51. Future<void> tapTextAndMarkdownButton() async {
  52. await tapButtonWithName(LocaleKeys.importPanel_textAndMarkdown.tr());
  53. }
  54. /// Tap the LanguageSelectorOnWelcomePage widget on the launch page.
  55. Future<void> tapLanguageSelectorOnWelcomePage() async {
  56. final languageSelector = find.byType(LanguageSelectorOnWelcomePage);
  57. await tapButton(languageSelector);
  58. }
  59. /// Tap languageItem on LanguageItemsListView.
  60. ///
  61. /// [scrollDelta] is the distance to scroll the ListView.
  62. /// Default value is 100
  63. ///
  64. /// If it is positive -> scroll down.
  65. ///
  66. /// If it is negative -> scroll up.
  67. Future<void> tapLanguageItem({
  68. required String languageCode,
  69. String? countryCode,
  70. double? scrollDelta,
  71. }) async {
  72. final languageItemsListView = find.descendant(
  73. of: find.byType(ListView),
  74. matching: find.byType(Scrollable),
  75. );
  76. final languageItem = find.byWidgetPredicate(
  77. (widget) =>
  78. widget is LanguageItem &&
  79. widget.locale.languageCode == languageCode &&
  80. widget.locale.countryCode == countryCode,
  81. );
  82. // scroll the ListView until zHCNLanguageItem shows on the screen.
  83. await scrollUntilVisible(
  84. languageItem,
  85. scrollDelta ?? 100,
  86. scrollable: languageItemsListView,
  87. // maxHeight of LanguageItemsListView
  88. maxScrolls: 400,
  89. );
  90. try {
  91. await tapButton(languageItem);
  92. } on FlutterError catch (e) {
  93. Log.warn('tapLanguageItem error: $e');
  94. }
  95. }
  96. /// Hover on the widget.
  97. Future<void> hoverOnWidget(
  98. Finder finder, {
  99. Offset? offset,
  100. }) async {
  101. try {
  102. final gesture = await createGesture(kind: PointerDeviceKind.mouse);
  103. await gesture.addPointer(location: Offset.zero);
  104. addTearDown(gesture.removePointer);
  105. await pump();
  106. await gesture.moveTo(offset ?? getCenter(finder));
  107. await pumpAndSettle();
  108. } catch (_) {}
  109. }
  110. /// Hover on the page name.
  111. Future<void> hoverOnPageName(String name) async {
  112. await hoverOnWidget(findPageName(name));
  113. }
  114. /// Tap the ... button beside the page name.
  115. ///
  116. /// Must call [hoverOnPageName] first.
  117. Future<void> tapPageOptionButton() async {
  118. final optionButton = find.byType(ViewDisclosureButton);
  119. await tapButton(optionButton);
  120. }
  121. /// Tap the delete page button.
  122. ///
  123. /// Must call [tapPageOptionButton] first.
  124. Future<void> tapDeletePageButton() async {
  125. await tapPageOptionButton();
  126. await tapButtonWithName(ViewDisclosureAction.delete.name);
  127. }
  128. /// Tap the rename page button.
  129. ///
  130. /// Must call [tapPageOptionButton] first.
  131. Future<void> tapRenamePageButton() async {
  132. await tapPageOptionButton();
  133. await tapButtonWithName(ViewDisclosureAction.rename.name);
  134. }
  135. /// Rename the page.
  136. ///
  137. /// Must call [tapPageOptionButton] first.
  138. Future<void> renamePage(String name) async {
  139. await tapRenamePageButton();
  140. await enterText(find.byType(TextFormField), name);
  141. await tapOKButton();
  142. }
  143. Future<void> tapOKButton() async {
  144. final okButton = find.byWidgetPredicate(
  145. (widget) =>
  146. widget is PrimaryTextButton &&
  147. widget.label == LocaleKeys.button_OK.tr(),
  148. );
  149. await tapButton(okButton);
  150. }
  151. /// Tap the restore button.
  152. ///
  153. /// the restore button will show after the current page is deleted.
  154. Future<void> tapRestoreButton() async {
  155. final restoreButton = find.textContaining(
  156. LocaleKeys.deletePagePrompt_restore.tr(),
  157. );
  158. await tapButton(restoreButton);
  159. }
  160. /// Tap the delete permanently button.
  161. ///
  162. /// the restore button will show after the current page is deleted.
  163. Future<void> tapDeletePermanentlyButton() async {
  164. final restoreButton = find.textContaining(
  165. LocaleKeys.deletePagePrompt_deletePermanent.tr(),
  166. );
  167. await tapButton(restoreButton);
  168. }
  169. /// Tap the share button above the document page.
  170. Future<void> tapShareButton() async {
  171. final shareButton = find.byWidgetPredicate(
  172. (widget) => widget is DocumentShareButton,
  173. );
  174. await tapButton(shareButton);
  175. }
  176. /// Tap the export markdown button
  177. ///
  178. /// Must call [tapShareButton] first.
  179. Future<void> tapMarkdownButton() async {
  180. final markdownButton = find.textContaining(
  181. LocaleKeys.shareAction_markdown.tr(),
  182. );
  183. await tapButton(markdownButton);
  184. }
  185. /// Hover on cover plugin button above the document
  186. Future<void> hoverOnCoverPluginAddButton() async {
  187. final editor = find.byWidgetPredicate(
  188. (widget) => widget is AppFlowyEditor,
  189. );
  190. await hoverOnWidget(
  191. editor,
  192. offset: getTopLeft(editor).translate(20, 20),
  193. );
  194. }
  195. }