data.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Future<void> clean() async {
  35. final Directory workspaceRoot = await root;
  36. if (await workspaceRoot.exists()) {
  37. workspaceRoot.delete(recursive: true);
  38. }
  39. }
  40. String get _asset => 'assets/test/workspaces/$_name.zip';
  41. }
  42. class TestWorkspaceService {
  43. const TestWorkspaceService(this.workspace);
  44. final TestWorkspace workspace;
  45. /// Instructs the application to read workspace data from the workspace found under this [TestWorkspace]'s path.
  46. Future<void> setUpAll() async {
  47. SharedPreferences.setMockInitialValues(
  48. {
  49. kSettingsLocationDefaultLocation:
  50. await workspace.root.then((value) => value.path),
  51. },
  52. );
  53. }
  54. /// Workspaces that are checked into source are compressed. [TestWorkspaceService.setUp()] decompresses the file into an ephemeral directory that will be ignored by source control.
  55. Future<void> setUp() async {
  56. final inputStream =
  57. InputFileStream(await workspace.zip.then((value) => value.path));
  58. final archive = ZipDecoder().decodeBuffer(inputStream);
  59. extractArchiveToDisk(
  60. archive,
  61. await TestWorkspace._parent.then((value) => value.path),
  62. );
  63. }
  64. Future<void> tearDown() async {
  65. await workspace.clean();
  66. }
  67. Future<void> tearDownAll() async {
  68. await SharedPreferences.getInstance().then((value) => value.remove(kSettingsLocationDefaultLocation));
  69. }
  70. }