base.dart 3.1 KB

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