summaryrefslogtreecommitdiffstats
path: root/dom/filesystem/tests/script_fileList.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/filesystem/tests/script_fileList.js
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/filesystem/tests/script_fileList.js')
-rw-r--r--dom/filesystem/tests/script_fileList.js126
1 files changed, 126 insertions, 0 deletions
diff --git a/dom/filesystem/tests/script_fileList.js b/dom/filesystem/tests/script_fileList.js
new file mode 100644
index 0000000000..c1e8bb42a8
--- /dev/null
+++ b/dom/filesystem/tests/script_fileList.js
@@ -0,0 +1,126 @@
+/* eslint-env mozilla/frame-script */
+// eslint-disable-next-line mozilla/reject-importGlobalProperties
+Cu.importGlobalProperties(["File"]);
+const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+function createProfDFile() {
+ return Services.dirsvc
+ .QueryInterface(Ci.nsIProperties)
+ .get("ProfD", Ci.nsIFile);
+}
+
+// Creates a parametric arity directory hierarchy as a function of depth.
+// Each directory contains one leaf file, and subdirectories of depth [1, depth).
+// e.g. for depth 3:
+//
+// subdir3
+// - file.txt
+// - subdir2
+// - file.txt
+// - subdir1
+// - file.txt
+// - subdir1
+// - file.txt
+//
+// Returns the parent directory of the subtree.
+function createTreeFile(depth, parent) {
+ if (!parent) {
+ parent = Services.dirsvc
+ .QueryInterface(Ci.nsIProperties)
+ .get("TmpD", Ci.nsIFile);
+ parent.append("dir-tree-test");
+ parent.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o700);
+ }
+
+ var nextFile = parent.clone();
+ if (depth == 0) {
+ nextFile.append("file.txt");
+ nextFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
+ } else {
+ nextFile.append("subdir" + depth);
+ nextFile.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o700);
+ // Decrement the maximal depth by one for each level of nesting.
+ for (var i = 0; i < depth; i++) {
+ createTreeFile(i, nextFile);
+ }
+ }
+
+ return parent;
+}
+
+function createRootFile() {
+ var testFile = createProfDFile();
+
+ // Let's go back to the root of the FileSystem
+ while (true) {
+ var parent = testFile.parent;
+ if (!parent) {
+ break;
+ }
+
+ testFile = parent;
+ }
+
+ return testFile;
+}
+
+function createTestFile() {
+ var tmpFile = Services.dirsvc
+ .QueryInterface(Ci.nsIProperties)
+ .get("TmpD", Ci.nsIFile);
+ tmpFile.append("dir-test");
+ tmpFile.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o700);
+
+ var file1 = tmpFile.clone();
+ file1.append("foo.txt");
+ file1.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
+
+ var dir = tmpFile.clone();
+ dir.append("subdir");
+ dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o700);
+
+ var file2 = dir.clone();
+ file2.append("bar.txt");
+ file2.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
+
+ return tmpFile;
+}
+
+addMessageListener("dir.open", function(e) {
+ var testFile;
+
+ switch (e.path) {
+ case "ProfD":
+ // Note that files in the profile directory are not guaranteed to persist-
+ // see bug 1284742.
+ testFile = createProfDFile();
+ break;
+
+ case "root":
+ testFile = createRootFile();
+ break;
+
+ case "test":
+ testFile = createTestFile();
+ break;
+
+ case "tree":
+ testFile = createTreeFile(3);
+ break;
+ }
+
+ sendAsyncMessage("dir.opened", {
+ dir: testFile.path,
+ name: testFile.leafName,
+ });
+});
+
+addMessageListener("file.open", function(e) {
+ var testFile = Services.dirsvc
+ .QueryInterface(Ci.nsIProperties)
+ .get("ProfD", Ci.nsIFile);
+ testFile.append("prefs.js");
+
+ File.createFromNsIFile(testFile).then(function(file) {
+ sendAsyncMessage("file.opened", { file });
+ });
+});