data.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import 'dart:io';
  2. import 'package:appflowy/core/config/kv_keys.dart';
  3. import 'package:archive/archive_io.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:path/path.dart' as p;
  6. import 'package:path_provider/path_provider.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. enum TestWorkspace {
  9. board("board"),
  10. emptyDocument("empty_document"),
  11. aiWorkSpace("ai_workspace"),
  12. coverImage("cover_image");
  13. const TestWorkspace(this._name);
  14. final String _name;
  15. Future<File> get zip async {
  16. final Directory parent = await TestWorkspace._parent;
  17. final File out = File(p.join(parent.path, '$_name.zip'));
  18. if (await out.exists()) return out;
  19. await out.create();
  20. final ByteData data = await rootBundle.load(_asset);
  21. await out.writeAsBytes(data.buffer.asUint8List());
  22. return out;
  23. }
  24. Future<Directory> get root async {
  25. final Directory parent = await TestWorkspace._parent;
  26. return Directory(p.join(parent.path, _name));
  27. }
  28. static Future<Directory> get _parent async {
  29. final Directory root = await getTemporaryDirectory();
  30. if (await root.exists()) return root;
  31. await root.create();
  32. return root;
  33. }
  34. String get _asset => 'assets/test/workspaces/$_name.zip';
  35. }
  36. class TestWorkspaceService {
  37. const TestWorkspaceService(this.workspace);
  38. final TestWorkspace workspace;
  39. /// Instructs the application to read workspace data from the workspace found under this [TestWorkspace]'s path.
  40. Future<void> setUpAll() async {
  41. final root = await workspace.root;
  42. final path = root.path;
  43. SharedPreferences.setMockInitialValues(
  44. {
  45. KVKeys.pathLocation: path,
  46. },
  47. );
  48. }
  49. /// Workspaces that are checked into source are compressed. [TestWorkspaceService.setUp()] decompresses the file into an ephemeral directory that will be ignored by source control.
  50. Future<void> setUp() async {
  51. final inputStream =
  52. InputFileStream(await workspace.zip.then((value) => value.path));
  53. final archive = ZipDecoder().decodeBuffer(inputStream);
  54. extractArchiveToDisk(
  55. archive,
  56. await TestWorkspace._parent.then((value) => value.path),
  57. );
  58. }
  59. }