base.dart 3.3 KB

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