base.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:appflowy/startup/entry_point.dart';
  4. import 'package:appflowy/startup/startup.dart';
  5. import 'package:appflowy/user/presentation/presentation.dart';
  6. import 'package:appflowy/workspace/application/settings/prelude.dart';
  7. import 'package:flowy_infra/uuid.dart';
  8. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  9. import 'package:flutter/gestures.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter/services.dart';
  12. import 'package:flutter_test/flutter_test.dart';
  13. import 'package:path/path.dart' as p;
  14. import 'package:path_provider/path_provider.dart';
  15. class FlowyTestContext {
  16. FlowyTestContext({
  17. required this.applicationDataDirectory,
  18. });
  19. final String applicationDataDirectory;
  20. }
  21. extension AppFlowyTestBase on WidgetTester {
  22. Future<FlowyTestContext> initializeAppFlowy({
  23. // use to append after the application data directory
  24. String? pathExtension,
  25. Size windowsSize = const Size(1600, 1200),
  26. }) async {
  27. binding.setSurfaceSize(windowsSize);
  28. mockHotKeyManagerHandlers();
  29. final directory = await mockApplicationDataStorage(
  30. pathExtension: pathExtension,
  31. );
  32. WidgetsFlutterBinding.ensureInitialized();
  33. await FlowyRunner.run(
  34. FlowyApp(),
  35. IntegrationMode.integrationTest,
  36. );
  37. await waitUntilSignInPageShow();
  38. return FlowyTestContext(
  39. applicationDataDirectory: directory,
  40. );
  41. }
  42. void mockHotKeyManagerHandlers() {
  43. TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
  44. .setMockMethodCallHandler(const MethodChannel('hotkey_manager'),
  45. (MethodCall methodCall) async {
  46. if (methodCall.method == 'unregisterAll') {
  47. // do nothing
  48. }
  49. return;
  50. });
  51. }
  52. Future<String> mockApplicationDataStorage({
  53. // use to append after the application data directory
  54. String? pathExtension,
  55. }) async {
  56. final dir = await getTemporaryDirectory();
  57. // Use a random uuid to avoid conflict.
  58. String path = p.join(dir.path, 'appflowy_integration_test', uuid());
  59. if (pathExtension != null && pathExtension.isNotEmpty) {
  60. path = '$path/$pathExtension';
  61. }
  62. final directory = Directory(path);
  63. if (!directory.existsSync()) {
  64. await directory.create(recursive: true);
  65. }
  66. MockApplicationDataStorage.initialPath = directory.path;
  67. return directory.path;
  68. }
  69. Future<void> waitUntilSignInPageShow() async {
  70. final finder = find.byType(GoButton);
  71. await pumpUntilFound(finder);
  72. expect(finder, findsOneWidget);
  73. }
  74. Future<void> pumpUntilFound(
  75. Finder finder, {
  76. Duration timeout = const Duration(seconds: 10),
  77. }) async {
  78. bool timerDone = false;
  79. final timer = Timer(timeout, () => timerDone = true);
  80. while (timerDone != true) {
  81. await pump();
  82. if (any(finder)) {
  83. timerDone = true;
  84. }
  85. }
  86. timer.cancel();
  87. }
  88. Future<void> tapButton(
  89. Finder finder, {
  90. int? pointer,
  91. int buttons = kPrimaryButton,
  92. bool warnIfMissed = false,
  93. int milliseconds = 500,
  94. }) async {
  95. await tap(
  96. finder,
  97. buttons: buttons,
  98. warnIfMissed: warnIfMissed,
  99. );
  100. await pumpAndSettle(Duration(milliseconds: milliseconds));
  101. }
  102. Future<void> tapButtonWithName(
  103. String tr, {
  104. int milliseconds = 500,
  105. }) async {
  106. Finder button = find.text(
  107. tr,
  108. findRichText: true,
  109. skipOffstage: false,
  110. );
  111. if (button.evaluate().isEmpty) {
  112. button = find.byWidgetPredicate(
  113. (widget) => widget is FlowyText && widget.text == tr,
  114. );
  115. }
  116. await tapButton(
  117. button,
  118. milliseconds: milliseconds,
  119. );
  120. return;
  121. }
  122. Future<void> tapButtonWithTooltip(
  123. String tr, {
  124. int milliseconds = 500,
  125. }) async {
  126. final button = find.byTooltip(tr);
  127. await tapButton(
  128. button,
  129. milliseconds: milliseconds,
  130. );
  131. return;
  132. }
  133. Future<void> doubleTapAt(
  134. Offset location, {
  135. int? pointer,
  136. int buttons = kPrimaryButton,
  137. int milliseconds = 500,
  138. }) async {
  139. await tapAt(location, pointer: pointer, buttons: buttons);
  140. await pump(kDoubleTapMinTime);
  141. await tapAt(location, pointer: pointer, buttons: buttons);
  142. await pumpAndSettle(Duration(milliseconds: milliseconds));
  143. }
  144. Future<void> doubleTapButton(
  145. Finder finder, {
  146. int? pointer,
  147. int buttons = kPrimaryButton,
  148. bool warnIfMissed = true,
  149. int milliseconds = 500,
  150. }) async {
  151. await tap(
  152. finder,
  153. pointer: pointer,
  154. buttons: buttons,
  155. warnIfMissed: warnIfMissed,
  156. );
  157. await pump(kDoubleTapMinTime);
  158. await tap(
  159. finder,
  160. buttons: buttons,
  161. pointer: pointer,
  162. warnIfMissed: warnIfMissed,
  163. );
  164. await pumpAndSettle(Duration(milliseconds: milliseconds));
  165. }
  166. Future<void> wait(int milliseconds) async {
  167. await pumpAndSettle(Duration(milliseconds: milliseconds));
  168. return;
  169. }
  170. }
  171. extension AppFlowyFinderTestBase on CommonFinders {
  172. Finder findTextInFlowyText(String text) {
  173. return find.byWidgetPredicate(
  174. (widget) => widget is FlowyText && widget.text == text,
  175. );
  176. }
  177. }