switch_folder_test.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import 'package:appflowy/generated/locale_keys.g.dart';
  2. import 'package:appflowy/plugins/document/presentation/plugins/base/built_in_page_widget.dart';
  3. import 'package:appflowy/user/presentation/folder/folder_widget.dart';
  4. import 'package:appflowy_editor/appflowy_editor.dart';
  5. import 'package:easy_localization/easy_localization.dart';
  6. import 'package:flowy_infra_ui/style_widget/text_field.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:flutter_test/flutter_test.dart';
  9. import 'package:integration_test/integration_test.dart';
  10. import 'util/mock/mock_file_picker.dart';
  11. import 'util/util.dart';
  12. void main() {
  13. IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  14. group('customize the folder path', () {
  15. const location = 'appflowy';
  16. setUp(() async {
  17. await TestFolder.cleanTestLocation(location);
  18. await TestFolder.setTestLocation(location);
  19. });
  20. tearDown(() async {
  21. await TestFolder.cleanTestLocation(location);
  22. });
  23. tearDownAll(() async {
  24. await TestFolder.cleanTestLocation(null);
  25. });
  26. testWidgets(
  27. 'customize folder name and path when launching app in first time',
  28. (tester) async {
  29. const folderName = 'appflowy';
  30. await TestFolder.cleanTestLocation(folderName);
  31. await tester.initializeAppFlowy();
  32. // Click create button
  33. await tester.tapCreateButton();
  34. // Set directory
  35. final cfw = find.byType(CreateFolderWidget);
  36. expect(cfw, findsOneWidget);
  37. final state = tester.state(cfw) as CreateFolderWidgetState;
  38. final dir = await TestFolder.testLocation(null);
  39. state.directory = dir.path;
  40. // input folder name
  41. final ftf = find.byType(FlowyTextField);
  42. expect(ftf, findsOneWidget);
  43. await tester.enterText(ftf, 'appflowy');
  44. // Click create button again
  45. await tester.tapCreateButton();
  46. await tester.expectToSeeWelcomePage();
  47. await TestFolder.cleanTestLocation(folderName);
  48. });
  49. testWidgets('open a new folder when launching app in first time',
  50. (tester) async {
  51. const folderName = 'appflowy';
  52. await TestFolder.cleanTestLocation(folderName);
  53. await TestFolder.setTestLocation(folderName);
  54. await tester.initializeAppFlowy();
  55. // tap open button
  56. await mockGetDirectoryPath(folderName);
  57. await tester.tapOpenFolderButton();
  58. await tester.wait(1000);
  59. await tester.expectToSeeWelcomePage();
  60. await TestFolder.cleanTestLocation(folderName);
  61. });
  62. testWidgets('switch to B from A, then switch to A again', (tester) async {
  63. const String userA = 'userA';
  64. const String userB = 'userB';
  65. await TestFolder.cleanTestLocation(userA);
  66. await TestFolder.setTestLocation(userA);
  67. await tester.initializeAppFlowy();
  68. await tester.tapGoButton();
  69. await tester.expectToSeeWelcomePage();
  70. // switch to user B
  71. {
  72. await tester.openSettings();
  73. await tester.openSettingsPage(SettingsPage.user);
  74. await tester.enterUserName(userA);
  75. await tester.openSettingsPage(SettingsPage.files);
  76. await tester.pumpAndSettle();
  77. // mock the file_picker result
  78. await mockGetDirectoryPath(userB);
  79. await tester.tapCustomLocationButton();
  80. await tester.pumpAndSettle();
  81. await tester.expectToSeeWelcomePage();
  82. }
  83. // switch to the userA
  84. {
  85. await tester.openSettings();
  86. await tester.openSettingsPage(SettingsPage.user);
  87. await tester.enterUserName(userB);
  88. await tester.openSettingsPage(SettingsPage.files);
  89. await tester.pumpAndSettle();
  90. // mock the file_picker result
  91. await mockGetDirectoryPath(userA);
  92. await tester.tapCustomLocationButton();
  93. await tester.pumpAndSettle();
  94. await tester.expectToSeeWelcomePage();
  95. expect(find.textContaining(userA), findsOneWidget);
  96. }
  97. // switch to the userB again
  98. {
  99. await tester.openSettings();
  100. await tester.openSettingsPage(SettingsPage.files);
  101. await tester.pumpAndSettle();
  102. // mock the file_picker result
  103. await mockGetDirectoryPath(userB);
  104. await tester.tapCustomLocationButton();
  105. await tester.pumpAndSettle();
  106. await tester.expectToSeeWelcomePage();
  107. expect(find.textContaining(userB), findsOneWidget);
  108. }
  109. await TestFolder.cleanTestLocation(userA);
  110. await TestFolder.cleanTestLocation(userB);
  111. });
  112. testWidgets('reset to default location', (tester) async {
  113. await tester.initializeAppFlowy();
  114. await tester.tapGoButton();
  115. // home and readme document
  116. await tester.expectToSeeWelcomePage();
  117. // open settings and restore the location
  118. await tester.openSettings();
  119. await tester.openSettingsPage(SettingsPage.files);
  120. await tester.restoreLocation();
  121. expect(
  122. await TestFolder.defaultDevelopmentLocation(),
  123. await TestFolder.currentLocation(),
  124. );
  125. });
  126. testWidgets('/board shortcut creates a new board', (tester) async {
  127. const folderName = 'appflowy';
  128. await TestFolder.cleanTestLocation(folderName);
  129. await TestFolder.setTestLocation(folderName);
  130. await tester.initializeAppFlowy();
  131. // tap open button
  132. await mockGetDirectoryPath(folderName);
  133. await tester.tapOpenFolderButton();
  134. await tester.wait(1000);
  135. await tester.expectToSeeWelcomePage();
  136. final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  137. // Necessary for being able to enterText when not in debug mode
  138. binding.testTextInput.register();
  139. // Needs tab to obtain focus for the app flowy editor.
  140. // by default the tap appears at the center of the widget.
  141. final Finder editor = find.byType(AppFlowyEditor);
  142. await tester.tap(editor);
  143. await tester.pumpAndSettle();
  144. // tester.sendText() cannot be used since the editor
  145. // does not contain any EditableText widgets.
  146. // to interact with the app during an integration test,
  147. // simulate physical keyboard events.
  148. await simulateKeyDownEvent(LogicalKeyboardKey.enter);
  149. await tester.pumpAndSettle();
  150. await simulateKeyDownEvent(LogicalKeyboardKey.enter);
  151. await tester.pumpAndSettle();
  152. await simulateKeyDownEvent(LogicalKeyboardKey.arrowLeft);
  153. await tester.pumpAndSettle();
  154. await simulateKeyDownEvent(LogicalKeyboardKey.slash);
  155. await tester.pumpAndSettle();
  156. await simulateKeyDownEvent(LogicalKeyboardKey.keyB);
  157. await tester.pumpAndSettle();
  158. await simulateKeyDownEvent(LogicalKeyboardKey.keyO);
  159. await tester.pumpAndSettle();
  160. await simulateKeyDownEvent(LogicalKeyboardKey.keyA);
  161. await tester.pumpAndSettle();
  162. await simulateKeyDownEvent(LogicalKeyboardKey.keyR);
  163. await tester.pumpAndSettle();
  164. await simulateKeyDownEvent(LogicalKeyboardKey.keyD);
  165. await tester.pumpAndSettle();
  166. await simulateKeyDownEvent(LogicalKeyboardKey.arrowDown);
  167. await tester.pumpAndSettle();
  168. // Checks whether the options in the selection menu
  169. // for /board exist.
  170. expect(find.byType(SelectionMenuItemWidget), findsAtLeastNWidgets(2));
  171. // Finalizes the slash command that creates the board.
  172. await simulateKeyDownEvent(LogicalKeyboardKey.enter);
  173. await tester.pumpAndSettle();
  174. // Checks whether new board is referenced and properly on the page.
  175. expect(find.byType(BuiltInPageWidget), findsOneWidget);
  176. // Checks whether the new board is in the side bar.
  177. final sidebarLabel = LocaleKeys.newPageText.tr();
  178. expect(find.text(sidebarLabel), findsOneWidget);
  179. });
  180. });
  181. }