mock_file_picker.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import 'package:appflowy/startup/startup.dart';
  2. import 'package:appflowy/util/file_picker/file_picker_service.dart';
  3. import 'package:file_picker/file_picker.dart' as fp;
  4. class MockFilePicker implements FilePickerService {
  5. MockFilePicker({
  6. this.mockPath = '',
  7. this.mockPaths = const [],
  8. });
  9. final String mockPath;
  10. final List<String> mockPaths;
  11. @override
  12. Future<String?> getDirectoryPath({String? title}) {
  13. return Future.value(mockPath);
  14. }
  15. @override
  16. Future<String?> saveFile({
  17. String? dialogTitle,
  18. String? fileName,
  19. String? initialDirectory,
  20. fp.FileType type = fp.FileType.any,
  21. List<String>? allowedExtensions,
  22. bool lockParentWindow = false,
  23. }) {
  24. return Future.value(mockPath);
  25. }
  26. @override
  27. Future<FilePickerResult?> pickFiles({
  28. String? dialogTitle,
  29. String? initialDirectory,
  30. fp.FileType type = fp.FileType.any,
  31. List<String>? allowedExtensions,
  32. Function(fp.FilePickerStatus p1)? onFileLoading,
  33. bool allowCompression = true,
  34. bool allowMultiple = false,
  35. bool withData = false,
  36. bool withReadStream = false,
  37. bool lockParentWindow = false,
  38. }) {
  39. final platformFiles = mockPaths
  40. .map((e) => fp.PlatformFile(path: e, name: '', size: 0))
  41. .toList();
  42. return Future.value(
  43. FilePickerResult(
  44. platformFiles,
  45. ),
  46. );
  47. }
  48. }
  49. Future<void> mockGetDirectoryPath(
  50. String path,
  51. ) async {
  52. getIt.unregister<FilePickerService>();
  53. getIt.registerFactory<FilePickerService>(
  54. () => MockFilePicker(
  55. mockPath: path,
  56. ),
  57. );
  58. return;
  59. }
  60. Future<String> mockSaveFilePath(
  61. String path,
  62. ) async {
  63. getIt.unregister<FilePickerService>();
  64. getIt.registerFactory<FilePickerService>(
  65. () => MockFilePicker(
  66. mockPath: path,
  67. ),
  68. );
  69. return path;
  70. }
  71. Future<List<String>> mockPickFilePaths({
  72. required List<String> paths,
  73. }) async {
  74. // late final Directory dir;
  75. // if (customPath != null) {
  76. // dir = Directory(customPath);
  77. // } else {
  78. // dir = await TestFolder.testLocation(applicationDataPath, name);
  79. // }
  80. // final paths = fileNames.map((e) => p.join(dir.path, e)).toList();
  81. getIt.unregister<FilePickerService>();
  82. getIt.registerFactory<FilePickerService>(
  83. () => MockFilePicker(
  84. mockPaths: paths,
  85. ),
  86. );
  87. return paths;
  88. }