data.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. const TestWorkspace(this._name);
  12. final String _name;
  13. Future<File> get zip async {
  14. final Directory parent = await TestWorkspace._parent;
  15. final File out = File(p.join(parent.path, '$_name.zip'));
  16. if (await out.exists()) return out;
  17. await out.create();
  18. final ByteData data = await rootBundle.load(_asset);
  19. await out.writeAsBytes(data.buffer.asUint8List());
  20. return out;
  21. }
  22. Future<Directory> get root async {
  23. final Directory parent = await TestWorkspace._parent;
  24. return Directory(p.join(parent.path, _name));
  25. }
  26. static Future<Directory> get _parent async {
  27. final Directory root = await getTemporaryDirectory();
  28. if (await root.exists()) return root;
  29. await root.create();
  30. return root;
  31. }
  32. String get _asset => 'assets/test/workspaces/$_name.zip';
  33. }
  34. class TestWorkspaceService {
  35. const TestWorkspaceService(this.workspace);
  36. final TestWorkspace workspace;
  37. /// Instructs the application to read workspace data from the workspace found under this [TestWorkspace]'s path.
  38. Future<void> setUpAll() async {
  39. SharedPreferences.setMockInitialValues(
  40. {
  41. kSettingsLocationDefaultLocation:
  42. await workspace.root.then((value) => value.path),
  43. },
  44. );
  45. }
  46. /// Workspaces that are checked into source are compressed. [TestWorkspaceService.setUp()] decompresses the file into an ephemeral directory that will be ignored by source control.
  47. Future<void> setUp() async {
  48. final inputStream =
  49. InputFileStream(await workspace.zip.then((value) => value.path));
  50. final archive = ZipDecoder().decodeBuffer(inputStream);
  51. extractArchiveToDisk(
  52. archive, await TestWorkspace._parent.then((value) => value.path));
  53. }
  54. }