common_operations.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 create grid button.
  43. ///
  44. /// Must call [tapAddButton] first.
  45. Future<void> tapCreateCalendarButton() async {
  46. await tapButtonWithName(LocaleKeys.calendar_menuName.tr());
  47. }
  48. /// Tap the import button.
  49. ///
  50. /// Must call [tapAddButton] first.
  51. Future<void> tapImportButton() async {
  52. await tapButtonWithName(LocaleKeys.moreAction_import.tr());
  53. }
  54. /// Tap the import from text & markdown button.
  55. ///
  56. /// Must call [tapImportButton] first.
  57. Future<void> tapTextAndMarkdownButton() async {
  58. await tapButtonWithName(LocaleKeys.importPanel_textAndMarkdown.tr());
  59. }
  60. /// Tap the LanguageSelectorOnWelcomePage widget on the launch page.
  61. Future<void> tapLanguageSelectorOnWelcomePage() async {
  62. final languageSelector = find.byType(LanguageSelectorOnWelcomePage);
  63. await tapButton(languageSelector);
  64. }
  65. /// Tap languageItem on LanguageItemsListView.
  66. ///
  67. /// [scrollDelta] is the distance to scroll the ListView.
  68. /// Default value is 100
  69. ///
  70. /// If it is positive -> scroll down.
  71. ///
  72. /// If it is negative -> scroll up.
  73. Future<void> tapLanguageItem({
  74. required String languageCode,
  75. String? countryCode,
  76. double? scrollDelta,
  77. }) async {
  78. final languageItemsListView = find.descendant(
  79. of: find.byType(ListView),
  80. matching: find.byType(Scrollable),
  81. );
  82. final languageItem = find.byWidgetPredicate(
  83. (widget) =>
  84. widget is LanguageItem &&
  85. widget.locale.languageCode == languageCode &&
  86. widget.locale.countryCode == countryCode,
  87. );
  88. // scroll the ListView until zHCNLanguageItem shows on the screen.
  89. await scrollUntilVisible(
  90. languageItem,
  91. scrollDelta ?? 100,
  92. scrollable: languageItemsListView,
  93. // maxHeight of LanguageItemsListView
  94. maxScrolls: 400,
  95. );
  96. try {
  97. await tapButton(languageItem);
  98. } on FlutterError catch (e) {
  99. Log.warn('tapLanguageItem error: $e');
  100. }
  101. }
  102. /// Hover on the widget.
  103. Future<void> hoverOnWidget(
  104. Finder finder, {
  105. Offset? offset,
  106. Future<void> Function()? onHover,
  107. }) async {
  108. try {
  109. final gesture = await createGesture(kind: PointerDeviceKind.mouse);
  110. await gesture.addPointer(location: Offset.zero);
  111. await pump();
  112. await gesture.moveTo(offset ?? getCenter(finder));
  113. await pumpAndSettle();
  114. await onHover?.call();
  115. await gesture.removePointer();
  116. } catch (err) {
  117. Log.error('hoverOnWidget error: $err');
  118. }
  119. }
  120. /// Hover on the page name.
  121. Future<void> hoverOnPageName(
  122. String name, {
  123. Future<void> Function()? onHover,
  124. bool useLast = true,
  125. }) async {
  126. if (useLast) {
  127. await hoverOnWidget(findPageName(name).last, onHover: onHover);
  128. } else {
  129. await hoverOnWidget(findPageName(name).first, onHover: onHover);
  130. }
  131. }
  132. /// open the page with given name.
  133. Future<void> openPage(String name) async {
  134. final finder = findPageName(name);
  135. expect(finder, findsOneWidget);
  136. await tapButton(finder);
  137. }
  138. /// Tap the ... button beside the page name.
  139. ///
  140. /// Must call [hoverOnPageName] first.
  141. Future<void> tapPageOptionButton() async {
  142. final optionButton = find.byType(ViewDisclosureButton);
  143. await tapButton(optionButton);
  144. }
  145. /// Tap the delete page button.
  146. Future<void> tapDeletePageButton() async {
  147. await tapPageOptionButton();
  148. await tapButtonWithName(ViewDisclosureAction.delete.name);
  149. }
  150. /// Tap the rename page button.
  151. Future<void> tapRenamePageButton() async {
  152. await tapPageOptionButton();
  153. await tapButtonWithName(ViewDisclosureAction.rename.name);
  154. }
  155. /// Rename the page.
  156. Future<void> renamePage(String name) async {
  157. await tapRenamePageButton();
  158. await enterText(find.byType(TextFormField), name);
  159. await tapOKButton();
  160. }
  161. Future<void> tapOKButton() async {
  162. final okButton = find.byWidgetPredicate(
  163. (widget) =>
  164. widget is PrimaryTextButton &&
  165. widget.label == LocaleKeys.button_OK.tr(),
  166. );
  167. await tapButton(okButton);
  168. }
  169. /// Tap the restore button.
  170. ///
  171. /// the restore button will show after the current page is deleted.
  172. Future<void> tapRestoreButton() async {
  173. final restoreButton = find.textContaining(
  174. LocaleKeys.deletePagePrompt_restore.tr(),
  175. );
  176. await tapButton(restoreButton);
  177. }
  178. /// Tap the delete permanently button.
  179. ///
  180. /// the restore button will show after the current page is deleted.
  181. Future<void> tapDeletePermanentlyButton() async {
  182. final restoreButton = find.textContaining(
  183. LocaleKeys.deletePagePrompt_deletePermanent.tr(),
  184. );
  185. await tapButton(restoreButton);
  186. }
  187. /// Tap the share button above the document page.
  188. Future<void> tapShareButton() async {
  189. final shareButton = find.byWidgetPredicate(
  190. (widget) => widget is DocumentShareButton,
  191. );
  192. await tapButton(shareButton);
  193. }
  194. /// Tap the export markdown button
  195. ///
  196. /// Must call [tapShareButton] first.
  197. Future<void> tapMarkdownButton() async {
  198. final markdownButton = find.textContaining(
  199. LocaleKeys.shareAction_markdown.tr(),
  200. );
  201. await tapButton(markdownButton);
  202. }
  203. Future<void> createNewPageWithName(ViewLayoutPB layout, String name) async {
  204. // create a new page
  205. await tapAddButton();
  206. await tapButtonWithName(layout.menuName);
  207. await pumpAndSettle();
  208. // hover on it and change it's name
  209. await hoverOnPageName(
  210. LocaleKeys.menuAppHeader_defaultNewPageName.tr(),
  211. onHover: () async {
  212. await renamePage(name);
  213. await pumpAndSettle();
  214. },
  215. );
  216. await pumpAndSettle();
  217. }
  218. }
  219. extension ViewLayoutPBTest on ViewLayoutPB {
  220. String get menuName {
  221. switch (this) {
  222. case ViewLayoutPB.Grid:
  223. return LocaleKeys.grid_menuName.tr();
  224. case ViewLayoutPB.Board:
  225. return LocaleKeys.board_menuName.tr();
  226. case ViewLayoutPB.Document:
  227. return LocaleKeys.document_menuName.tr();
  228. case ViewLayoutPB.Calendar:
  229. return LocaleKeys.calendar_menuName.tr();
  230. default:
  231. throw UnsupportedError('Unsupported layout: $this');
  232. }
  233. }
  234. String get referencedMenuName {
  235. switch (this) {
  236. case ViewLayoutPB.Grid:
  237. return LocaleKeys.document_plugins_referencedGrid.tr();
  238. case ViewLayoutPB.Board:
  239. return LocaleKeys.document_plugins_referencedBoard.tr();
  240. case ViewLayoutPB.Calendar:
  241. return LocaleKeys.document_plugins_referencedCalendar.tr();
  242. default:
  243. throw UnsupportedError('Unsupported layout: $this');
  244. }
  245. }
  246. }