base.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'dart:io';
  2. import 'package:app_flowy/main.dart' as app;
  3. import 'package:app_flowy/startup/tasks/prelude.dart';
  4. import 'package:app_flowy/workspace/application/settings/settings_location_cubit.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. kSettingsLocationDefaultLocation: 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(kSettingsLocationDefaultLocation)!;
  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. const MethodChannel('hotkey_manager')
  54. .setMockMethodCallHandler((MethodCall methodCall) async {
  55. if (methodCall.method == 'unregisterAll') {
  56. // do nothing
  57. }
  58. });
  59. await app.main();
  60. await wait(3000);
  61. await pumpAndSettle(const Duration(seconds: 2));
  62. return;
  63. }
  64. Future<void> tapButton(
  65. Finder finder, {
  66. int? pointer,
  67. int buttons = kPrimaryButton,
  68. bool warnIfMissed = true,
  69. int milliseconds = 500,
  70. }) async {
  71. await tap(finder);
  72. await pumpAndSettle(Duration(milliseconds: milliseconds));
  73. return;
  74. }
  75. Future<void> tapButtonWithName(
  76. String tr, {
  77. int milliseconds = 500,
  78. }) async {
  79. final button = find.text(tr);
  80. await tapButton(
  81. button,
  82. milliseconds: milliseconds,
  83. );
  84. return;
  85. }
  86. Future<void> tapButtonWithTooltip(
  87. String tr, {
  88. int milliseconds = 500,
  89. }) async {
  90. final button = find.byTooltip(tr);
  91. await tapButton(
  92. button,
  93. milliseconds: milliseconds,
  94. );
  95. return;
  96. }
  97. Future<void> wait(int milliseconds) async {
  98. await pumpAndSettle(Duration(milliseconds: milliseconds));
  99. return;
  100. }
  101. }