data.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import 'dart:io';
  2. import 'package:appflowy/workspace/application/settings/settings_location_cubit.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. kSettingsLocationDefaultLocation:
  44. await workspace.root.then((value) => value.path),
  45. },
  46. );
  47. }
  48. /// Workspaces that are checked into source are compressed. [TestWorkspaceService.setUp()] decompresses the file into an ephemeral directory that will be ignored by source control.
  49. Future<void> setUp() async {
  50. final inputStream =
  51. InputFileStream(await workspace.zip.then((value) => value.path));
  52. final archive = ZipDecoder().decodeBuffer(inputStream);
  53. extractArchiveToDisk(
  54. archive,
  55. await TestWorkspace._parent.then((value) => value.path),
  56. );
  57. }
  58. }