mock_file_picker.dart 1.9 KB

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