common_operations.dart 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /// open the page with given name.
  127. Future<void> openPage(String name) async {
  128. await tapButton(findPageName(name));
  129. }
  130. /// Tap the ... button beside the page name.
  131. ///
  132. /// Must call [hoverOnPageName] first.
  133. Future<void> tapPageOptionButton() async {
  134. final optionButton = find.byType(ViewDisclosureButton);
  135. await tapButton(optionButton);
  136. }
  137. /// Tap the delete page button.
  138. Future<void> tapDeletePageButton() async {
  139. await tapPageOptionButton();
  140. await tapButtonWithName(ViewDisclosureAction.delete.name);
  141. }
  142. /// Tap the rename page button.
  143. Future<void> tapRenamePageButton() async {
  144. await tapPageOptionButton();
  145. await tapButtonWithName(ViewDisclosureAction.rename.name);
  146. }
  147. /// Rename the page.
  148. Future<void> renamePage(String name) async {
  149. await tapRenamePageButton();
  150. await enterText(find.byType(TextFormField), name);
  151. await tapOKButton();
  152. }
  153. Future<void> tapOKButton() async {
  154. final okButton = find.byWidgetPredicate(
  155. (widget) =>
  156. widget is PrimaryTextButton &&
  157. widget.label == LocaleKeys.button_OK.tr(),
  158. );
  159. await tapButton(okButton);
  160. }
  161. /// Tap the restore button.
  162. ///
  163. /// the restore button will show after the current page is deleted.
  164. Future<void> tapRestoreButton() async {
  165. final restoreButton = find.textContaining(
  166. LocaleKeys.deletePagePrompt_restore.tr(),
  167. );
  168. await tapButton(restoreButton);
  169. }
  170. /// Tap the delete permanently button.
  171. ///
  172. /// the restore button will show after the current page is deleted.
  173. Future<void> tapDeletePermanentlyButton() async {
  174. final restoreButton = find.textContaining(
  175. LocaleKeys.deletePagePrompt_deletePermanent.tr(),
  176. );
  177. await tapButton(restoreButton);
  178. }
  179. /// Tap the share button above the document page.
  180. Future<void> tapShareButton() async {
  181. final shareButton = find.byWidgetPredicate(
  182. (widget) => widget is DocumentShareButton,
  183. );
  184. await tapButton(shareButton);
  185. }
  186. /// Tap the export markdown button
  187. ///
  188. /// Must call [tapShareButton] first.
  189. Future<void> tapMarkdownButton() async {
  190. final markdownButton = find.textContaining(
  191. LocaleKeys.shareAction_markdown.tr(),
  192. );
  193. await tapButton(markdownButton);
  194. }
  195. Future<void> createNewPageWithName(ViewLayoutPB layout, String name) async {
  196. // create a new page
  197. await tapAddButton();
  198. await tapButtonWithName(layout.menuName);
  199. await pumpAndSettle();
  200. // hover on it and change it's name
  201. await hoverOnPageName(
  202. LocaleKeys.menuAppHeader_defaultNewPageName.tr(),
  203. onHover: () async {
  204. await renamePage(name);
  205. await pumpAndSettle();
  206. },
  207. );
  208. await pumpAndSettle();
  209. }
  210. }
  211. extension ViewLayoutPBTest on ViewLayoutPB {
  212. String get menuName {
  213. switch (this) {
  214. case ViewLayoutPB.Grid:
  215. return LocaleKeys.grid_menuName.tr();
  216. case ViewLayoutPB.Board:
  217. return LocaleKeys.board_menuName.tr();
  218. case ViewLayoutPB.Document:
  219. return LocaleKeys.document_menuName.tr();
  220. case ViewLayoutPB.Calendar:
  221. return LocaleKeys.calendar_menuName.tr();
  222. default:
  223. throw UnsupportedError('Unsupported layout: $this');
  224. }
  225. }
  226. String get referencedMenuName {
  227. switch (this) {
  228. case ViewLayoutPB.Grid:
  229. return LocaleKeys.document_plugins_referencedGrid.tr();
  230. case ViewLayoutPB.Board:
  231. return LocaleKeys.document_plugins_referencedBoard.tr();
  232. case ViewLayoutPB.Calendar:
  233. return LocaleKeys.document_plugins_referencedCalendar.tr();
  234. default:
  235. throw UnsupportedError('Unsupported layout: $this');
  236. }
  237. }
  238. }