data.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. coverImage("cover_image");
  12. const TestWorkspace(this._name);
  13. final String _name;
  14. Future<File> get zip async {
  15. final Directory parent = await TestWorkspace._parent;
  16. final File out = File(p.join(parent.path, '$_name.zip'));
  17. if (await out.exists()) return out;
  18. await out.create();
  19. final ByteData data = await rootBundle.load(_asset);
  20. await out.writeAsBytes(data.buffer.asUint8List());
  21. return out;
  22. }
  23. Future<Directory> get root async {
  24. final Directory parent = await TestWorkspace._parent;
  25. return Directory(p.join(parent.path, _name));
  26. }
  27. static Future<Directory> get _parent async {
  28. final Directory root = await getTemporaryDirectory();
  29. if (await root.exists()) return root;
  30. await root.create();
  31. return root;
  32. }
  33. String get _asset => 'assets/test/workspaces/$_name.zip';
  34. }
  35. class TestWorkspaceService {
  36. const TestWorkspaceService(this.workspace);
  37. final TestWorkspace workspace;
  38. /// Instructs the application to read workspace data from the workspace found under this [TestWorkspace]'s path.
  39. Future<void> setUpAll() async {
  40. SharedPreferences.setMockInitialValues(
  41. {
  42. kSettingsLocationDefaultLocation:
  43. 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. }