common_operations.dart 9.2 KB

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