common_operations.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_backend/protobuf/flowy-folder2/view.pb.dart';
  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. Future<void> Function()? onHover,
  101. }) async {
  102. try {
  103. final gesture = await createGesture(kind: PointerDeviceKind.mouse);
  104. await gesture.addPointer(location: Offset.zero);
  105. await pump();
  106. await gesture.moveTo(offset ?? getCenter(finder));
  107. await pumpAndSettle();
  108. await onHover?.call();
  109. await gesture.removePointer();
  110. } catch (err) {
  111. Log.error('hoverOnWidget error: $err');
  112. }
  113. }
  114. /// Hover on the page name.
  115. Future<void> hoverOnPageName(
  116. String name, {
  117. Future<void> Function()? onHover,
  118. bool useLast = true,
  119. }) async {
  120. if (useLast) {
  121. await hoverOnWidget(findPageName(name).last, onHover: onHover);
  122. } else {
  123. await hoverOnWidget(findPageName(name).first, onHover: onHover);
  124. }
  125. }
  126. /// Tap the ... button beside the page name.
  127. ///
  128. /// Must call [hoverOnPageName] first.
  129. Future<void> tapPageOptionButton() async {
  130. final optionButton = find.byType(ViewDisclosureButton);
  131. await tapButton(optionButton);
  132. }
  133. /// Tap the delete page button.
  134. Future<void> tapDeletePageButton() async {
  135. await tapPageOptionButton();
  136. await tapButtonWithName(ViewDisclosureAction.delete.name);
  137. }
  138. /// Tap the rename page button.
  139. Future<void> tapRenamePageButton() async {
  140. await tapPageOptionButton();
  141. await tapButtonWithName(ViewDisclosureAction.rename.name);
  142. }
  143. /// Rename the page.
  144. Future<void> renamePage(String name) async {
  145. await tapRenamePageButton();
  146. await enterText(find.byType(TextFormField), name);
  147. await tapOKButton();
  148. }
  149. Future<void> tapOKButton() async {
  150. final okButton = find.byWidgetPredicate(
  151. (widget) =>
  152. widget is PrimaryTextButton &&
  153. widget.label == LocaleKeys.button_OK.tr(),
  154. );
  155. await tapButton(okButton);
  156. }
  157. /// Tap the restore button.
  158. ///
  159. /// the restore button will show after the current page is deleted.
  160. Future<void> tapRestoreButton() async {
  161. final restoreButton = find.textContaining(
  162. LocaleKeys.deletePagePrompt_restore.tr(),
  163. );
  164. await tapButton(restoreButton);
  165. }
  166. /// Tap the delete permanently button.
  167. ///
  168. /// the restore button will show after the current page is deleted.
  169. Future<void> tapDeletePermanentlyButton() async {
  170. final restoreButton = find.textContaining(
  171. LocaleKeys.deletePagePrompt_deletePermanent.tr(),
  172. );
  173. await tapButton(restoreButton);
  174. }
  175. /// Tap the share button above the document page.
  176. Future<void> tapShareButton() async {
  177. final shareButton = find.byWidgetPredicate(
  178. (widget) => widget is DocumentShareButton,
  179. );
  180. await tapButton(shareButton);
  181. }
  182. /// Tap the export markdown button
  183. ///
  184. /// Must call [tapShareButton] first.
  185. Future<void> tapMarkdownButton() async {
  186. final markdownButton = find.textContaining(
  187. LocaleKeys.shareAction_markdown.tr(),
  188. );
  189. await tapButton(markdownButton);
  190. }
  191. Future<void> createNewPageWithName(ViewLayoutPB layout, String name) async {
  192. // create a new page
  193. await tapAddButton();
  194. await tapButtonWithName(layout.menuName);
  195. await pumpAndSettle();
  196. // hover on it and change it's name
  197. await hoverOnPageName(
  198. LocaleKeys.menuAppHeader_defaultNewPageName.tr(),
  199. onHover: () async {
  200. await renamePage(name);
  201. await pumpAndSettle();
  202. },
  203. );
  204. await pumpAndSettle();
  205. }
  206. }
  207. extension ViewLayoutPBTest on ViewLayoutPB {
  208. String get menuName {
  209. switch (this) {
  210. case ViewLayoutPB.Grid:
  211. return LocaleKeys.grid_menuName.tr();
  212. case ViewLayoutPB.Board:
  213. return LocaleKeys.board_menuName.tr();
  214. case ViewLayoutPB.Document:
  215. return LocaleKeys.document_menuName.tr();
  216. case ViewLayoutPB.Calendar:
  217. return LocaleKeys.calendar_menuName.tr();
  218. default:
  219. throw UnsupportedError('Unsupported layout: $this');
  220. }
  221. }
  222. String get referencedMenuName {
  223. switch (this) {
  224. case ViewLayoutPB.Grid:
  225. return LocaleKeys.document_plugins_referencedGrid.tr();
  226. case ViewLayoutPB.Board:
  227. return LocaleKeys.document_plugins_referencedBoard.tr();
  228. case ViewLayoutPB.Calendar:
  229. return LocaleKeys.document_plugins_referencedCalendar.tr();
  230. default:
  231. throw UnsupportedError('Unsupported layout: $this');
  232. }
  233. }
  234. }