data.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. SharedPreferences.setMockInitialValues(
  42. {
  43. KVKeys.pathLocation: await workspace.root.then((value) => value.path),
  44. },
  45. );
  46. }
  47. /// Workspaces that are checked into source are compressed. [TestWorkspaceService.setUp()] decompresses the file into an ephemeral directory that will be ignored by source control.
  48. Future<void> setUp() async {
  49. final inputStream =
  50. InputFileStream(await workspace.zip.then((value) => value.path));
  51. final archive = ZipDecoder().decodeBuffer(inputStream);
  52. extractArchiveToDisk(
  53. archive,
  54. await TestWorkspace._parent.then((value) => value.path),
  55. );
  56. }
  57. }