base.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import 'dart:io';
  2. import 'package:appflowy/core/config/kv_keys.dart';
  3. import 'package:appflowy/startup/entry_point.dart';
  4. import 'package:appflowy/startup/startup.dart';
  5. import 'package:appflowy/startup/tasks/prelude.dart';
  6. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  7. import 'package:flutter/gestures.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter/services.dart';
  10. import 'package:flutter_test/flutter_test.dart';
  11. import 'package:path_provider/path_provider.dart';
  12. import 'package:shared_preferences/shared_preferences.dart';
  13. class TestFolder {
  14. /// Location / Path
  15. /// Set a given AppFlowy data storage location under test environment.
  16. ///
  17. /// To pass null means clear the location.
  18. ///
  19. /// The file_picker is a system component and can't be tapped, so using logic instead of tapping.
  20. ///
  21. static Future<void> setTestLocation(String? name) async {
  22. final location = await testLocation(name);
  23. SharedPreferences.setMockInitialValues({
  24. KVKeys.pathLocation: location.path,
  25. });
  26. return;
  27. }
  28. /// Clean the location.
  29. static Future<void> cleanTestLocation(String? name) async {
  30. final dir = await testLocation(name);
  31. await dir.delete(recursive: true);
  32. return;
  33. }
  34. /// Get current using location.
  35. static Future<String> currentLocation() async {
  36. final prefs = await SharedPreferences.getInstance();
  37. return prefs.getString(KVKeys.pathLocation)!;
  38. }
  39. /// Get default location under development environment.
  40. static Future<String> defaultDevelopmentLocation() async {
  41. final dir = await appFlowyDocumentDirectory();
  42. return dir.path;
  43. }
  44. /// Get default location under test environment.
  45. static Future<Directory> testLocation(String? name) async {
  46. final dir = await getApplicationDocumentsDirectory();
  47. var path = '${dir.path}/flowy_test';
  48. if (name != null) {
  49. path += '/$name';
  50. }
  51. return Directory(path).create(recursive: true);
  52. }
  53. }
  54. extension AppFlowyTestBase on WidgetTester {
  55. Future<void> initializeAppFlowy() async {
  56. TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
  57. .setMockMethodCallHandler(const MethodChannel('hotkey_manager'),
  58. (MethodCall methodCall) async {
  59. if (methodCall.method == 'unregisterAll') {
  60. // do nothing
  61. }
  62. return;
  63. });
  64. WidgetsFlutterBinding.ensureInitialized();
  65. await FlowyRunner.run(FlowyApp(), IntegrationMode.integrationTest);
  66. await wait(3000);
  67. await pumpAndSettle(const Duration(seconds: 2));
  68. }
  69. Future<void> tapButton(
  70. Finder finder, {
  71. int? pointer,
  72. int buttons = kPrimaryButton,
  73. bool warnIfMissed = true,
  74. int milliseconds = 500,
  75. }) async {
  76. await tap(
  77. finder,
  78. buttons: buttons,
  79. warnIfMissed: warnIfMissed,
  80. );
  81. await pumpAndSettle(Duration(milliseconds: milliseconds));
  82. return;
  83. }
  84. Future<void> tapButtonWithName(
  85. String tr, {
  86. int milliseconds = 500,
  87. }) async {
  88. Finder button = find.text(
  89. tr,
  90. findRichText: true,
  91. skipOffstage: false,
  92. );
  93. if (button.evaluate().isEmpty) {
  94. button = find.byWidgetPredicate(
  95. (widget) => widget is FlowyText && widget.text == tr,
  96. );
  97. }
  98. await tapButton(
  99. button,
  100. milliseconds: milliseconds,
  101. );
  102. return;
  103. }
  104. Future<void> tapButtonWithTooltip(
  105. String tr, {
  106. int milliseconds = 500,
  107. }) async {
  108. final button = find.byTooltip(tr);
  109. await tapButton(
  110. button,
  111. milliseconds: milliseconds,
  112. );
  113. return;
  114. }
  115. Future<void> wait(int milliseconds) async {
  116. await pumpAndSettle(Duration(milliseconds: milliseconds));
  117. return;
  118. }
  119. }
  120. extension AppFlowyFinderTestBase on CommonFinders {
  121. Finder findTextInFlowyText(String text) {
  122. return find.byWidgetPredicate(
  123. (widget) => widget is FlowyText && widget.text == text,
  124. );
  125. }
  126. }