From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- dom/filesystem/tests/filesystem_commons.js | 180 ++++++++++++++ dom/filesystem/tests/mochitest.toml | 16 ++ dom/filesystem/tests/moz.build | 7 + dom/filesystem/tests/script_fileList.js | 176 ++++++++++++++ dom/filesystem/tests/script_promptHandler.js | 18 ++ dom/filesystem/tests/test_basic.html | 118 ++++++++++ dom/filesystem/tests/test_bug1319088.html | 65 ++++++ dom/filesystem/tests/test_webkitdirectory.html | 309 +++++++++++++++++++++++++ dom/filesystem/tests/test_worker_basic.html | 73 ++++++ dom/filesystem/tests/worker_basic.js | 50 ++++ 10 files changed, 1012 insertions(+) create mode 100644 dom/filesystem/tests/filesystem_commons.js create mode 100644 dom/filesystem/tests/mochitest.toml create mode 100644 dom/filesystem/tests/moz.build create mode 100644 dom/filesystem/tests/script_fileList.js create mode 100644 dom/filesystem/tests/script_promptHandler.js create mode 100644 dom/filesystem/tests/test_basic.html create mode 100644 dom/filesystem/tests/test_bug1319088.html create mode 100644 dom/filesystem/tests/test_webkitdirectory.html create mode 100644 dom/filesystem/tests/test_worker_basic.html create mode 100644 dom/filesystem/tests/worker_basic.js (limited to 'dom/filesystem/tests') diff --git a/dom/filesystem/tests/filesystem_commons.js b/dom/filesystem/tests/filesystem_commons.js new file mode 100644 index 0000000000..af0ce36339 --- /dev/null +++ b/dom/filesystem/tests/filesystem_commons.js @@ -0,0 +1,180 @@ +function createPath(parentDir, dirOrFile) { + return parentDir.path + (parentDir.path == "/" ? "" : "/") + dirOrFile.name; +} + +function createRelativePath(parentDir, dirOrFile) { + let path = createPath(parentDir, dirOrFile); + is(path[0], "/", "The full path should start with '/'"); + return path.substring(1); +} + +function setup_tests(aNext) { + SimpleTest.requestLongerTimeout(2); + SpecialPowers.pushPrefEnv( + { + set: [ + ["dom.filesystem.pathcheck.disabled", true], + ["dom.webkitBlink.dirPicker.enabled", true], + ], + }, + aNext + ); +} + +function test_basic(aDirectory, aNext) { + ok(aDirectory, "Directory exists."); + ok(aDirectory instanceof Directory, "We have a directory."); + is(aDirectory.path, "/" + aDirectory.name, "directory.path must be '/'+name"); + aNext(); +} + +function test_getFilesAndDirectories(aDirectory, aRecursive, aNext) { + function checkSubDir(dir) { + return dir.getFilesAndDirectories().then(function (data) { + for (var i = 0; i < data.length; ++i) { + ok( + data[i] instanceof File || data[i] instanceof Directory, + "Just Files or Directories" + ); + if (data[i] instanceof Directory) { + isnot( + data[i].name, + "/", + "Subdirectory should be called with the leafname" + ); + isnot( + data[i].path, + "/", + "Subdirectory path should be called with the leafname" + ); + isnot( + data[i].path, + dir.path, + "Subdirectory path should contain the parent path." + ); + is( + data[i].path, + createPath(dir, data[i]), + "Subdirectory path should be called parentdir.path + '/' + leafname: " + + data[i].path + ); + } + + if (data[i] instanceof File) { + is( + data[i].webkitRelativePath, + createRelativePath(dir, data[i]), + "File.webkitRelativePath should be called: parentdir.path + '/' + file.name: " + + data[i].webkitRelativePath + ); + ok( + !data[i].webkitRelativePath.endsWith("symlink.txt"), + "We should never see a path ending with symlink.txt, our symlink sentinel." + ); + } + } + }); + } + + aDirectory + .getFilesAndDirectories() + .then( + function (data) { + ok(data.length, "We should have some data."); + var promises = []; + for (var i = 0; i < data.length; ++i) { + ok( + data[i] instanceof File || data[i] instanceof Directory, + "Just Files or Directories: " + data[i].name + ); + if (data[i] instanceof Directory) { + isnot( + data[i].name, + "/", + "Subdirectory should be called with the leafname" + ); + is( + data[i].path, + createPath(aDirectory, data[i]), + "Subdirectory path should be called parentdir.path + '/' + leafname: " + + data[i].path + ); + if (aRecursive) { + promises.push(checkSubDir(data[i])); + } + } + + if (data[i] instanceof File) { + is( + data[i].webkitRelativePath, + createRelativePath(aDirectory, data[i]), + "File.webkitRelativePath should be called file.name: " + + data[i].webkitRelativePath + ); + } + } + + return Promise.all(promises); + }, + function () { + ok(false, "Something when wrong"); + } + ) + .then(aNext); +} + +function test_getFiles(aDirectory, aRecursive, aNext) { + aDirectory + .getFiles(aRecursive) + .then( + function (data) { + for (var i = 0; i < data.length; ++i) { + ok(data[i] instanceof File, "File: " + data[i].name); + is(aDirectory.path[0], "/", "Directory path must start with '/'"); + ok( + data[i].webkitRelativePath.indexOf(aDirectory.path.substring(1)) == + 0 && + data[i].webkitRelativePath.indexOf("/" + data[i].name) + + ("/" + data[i].name).length == + data[i].webkitRelativePath.length, + "File.webkitRelativePath should be called dir.path + '/' + file.name: " + + data[i].webkitRelativePath + ); + } + }, + function () { + ok(false, "Something when wrong"); + } + ) + .then(aNext); +} + +function test_getFiles_recursiveComparison(aDirectory, aNext) { + aDirectory + .getFiles(true) + .then(function (data) { + is(data.length, 2, "Only 2 files for this test."); + ok( + data[0].name == "foo.txt" || data[0].name == "bar.txt", + "First filename matches" + ); + ok( + data[1].name == "foo.txt" || data[1].name == "bar.txt", + "Second filename matches" + ); + }) + .then(function () { + return aDirectory.getFiles(false); + }) + .then(function (data) { + is(data.length, 1, "Only 1 file for this test."); + ok( + data[0].name == "foo.txt" || data[0].name == "bar.txt", + "First filename matches" + ); + }) + .catch(function () { + ok(false, "Something when wrong"); + }) + .then(aNext); +} diff --git a/dom/filesystem/tests/mochitest.toml b/dom/filesystem/tests/mochitest.toml new file mode 100644 index 0000000000..869fe2d37f --- /dev/null +++ b/dom/filesystem/tests/mochitest.toml @@ -0,0 +1,16 @@ +[DEFAULT] +support-files = [ + "filesystem_commons.js", + "script_fileList.js", + "worker_basic.js", +] + +["test_basic.html"] + +["test_bug1319088.html"] + +["test_webkitdirectory.html"] +skip-if = ["os == 'android'"] # Bug 1674428 +support-files = ["script_promptHandler.js"] + +["test_worker_basic.html"] diff --git a/dom/filesystem/tests/moz.build b/dom/filesystem/tests/moz.build new file mode 100644 index 0000000000..2f41008128 --- /dev/null +++ b/dom/filesystem/tests/moz.build @@ -0,0 +1,7 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +MOCHITEST_MANIFESTS += ["mochitest.toml"] diff --git a/dom/filesystem/tests/script_fileList.js b/dom/filesystem/tests/script_fileList.js new file mode 100644 index 0000000000..47438aa7b3 --- /dev/null +++ b/dom/filesystem/tests/script_fileList.js @@ -0,0 +1,176 @@ +/* eslint-env mozilla/chrome-script */ +// eslint-disable-next-line mozilla/reject-importGlobalProperties +Cu.importGlobalProperties(["File"]); +function createProfDFile() { + return Services.dirsvc + .QueryInterface(Ci.nsIProperties) + .get("ProfD", Ci.nsIFile); +} + +const { AppConstants } = ChromeUtils.importESModule( + "resource://gre/modules/AppConstants.sys.mjs" +); + +// 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); + + // It's not possible to create symlinks on windows by default or on our + // Android platforms, so we can't create the symlink file there. Our + // callers that care are aware of this and also check AppConstants. + if ( + AppConstants.platform !== "win" && + AppConstants.platform !== "android" + ) { + var linkFile = parent.clone(); + linkFile.append("symlink.txt"); + createSymLink(nextFile.path, linkFile.path); + } + } 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; +} + +var process; +function createSymLink(target, linkName) { + if (!process) { + var ln = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); + ln.initWithPath("/bin/ln"); + + process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess); + process.init(ln); + } + + const args = ["-s", target, linkName]; + process.run(true, args, args.length); + Assert.equal(process.exitValue, 0); +} + +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); + + // It's not possible to create symlinks on windows by default or on our + // Android platforms, so we can't create the symlink file there. Our + // callers that care are aware of this and also check AppConstants. + if (AppConstants.platform !== "win" && AppConstants.platform !== "android") { + var linkFile = dir.clone(); + linkFile.append("symlink.txt"); + createSymLink(file1.path, linkFile.path); + } + + 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 }); + }); +}); + +addMessageListener("symlink.open", function (e) { + let testDir = createTestFile(); + let testFile = testDir.clone(); + testFile.append("subdir"); + testFile.append("symlink.txt"); + + File.createFromNsIFile(testFile).then(function (file) { + sendAsyncMessage("symlink.opened", { dir: testDir.path, file }); + }); +}); diff --git a/dom/filesystem/tests/script_promptHandler.js b/dom/filesystem/tests/script_promptHandler.js new file mode 100644 index 0000000000..ed3bda7404 --- /dev/null +++ b/dom/filesystem/tests/script_promptHandler.js @@ -0,0 +1,18 @@ +/* eslint-env mozilla/chrome-script */ + +let dialogObserverTopic = "common-dialog-loaded"; + +function dialogObserver(subj, topic, data) { + subj.document.querySelector("dialog").acceptDialog(); + sendAsyncMessage("promptAccepted"); +} + +addMessageListener("init", message => { + Services.obs.addObserver(dialogObserver, dialogObserverTopic); + sendAsyncMessage("initDone"); +}); + +addMessageListener("cleanup", message => { + Services.obs.removeObserver(dialogObserver, dialogObserverTopic); + sendAsyncMessage("cleanupDone"); +}); diff --git a/dom/filesystem/tests/test_basic.html b/dom/filesystem/tests/test_basic.html new file mode 100644 index 0000000000..b4daea79c9 --- /dev/null +++ b/dom/filesystem/tests/test_basic.html @@ -0,0 +1,118 @@ + + + + Test for Directory API + + + + + + + + + diff --git a/dom/filesystem/tests/test_bug1319088.html b/dom/filesystem/tests/test_bug1319088.html new file mode 100644 index 0000000000..98e0ad46f0 --- /dev/null +++ b/dom/filesystem/tests/test_bug1319088.html @@ -0,0 +1,65 @@ + + + + Test for bug 1319088 + + + + + + + + + + diff --git a/dom/filesystem/tests/test_webkitdirectory.html b/dom/filesystem/tests/test_webkitdirectory.html new file mode 100644 index 0000000000..50080ad7ba --- /dev/null +++ b/dom/filesystem/tests/test_webkitdirectory.html @@ -0,0 +1,309 @@ + + + + Test for webkitdirectory and webkitRelativePath + + + + + + + + + + + + diff --git a/dom/filesystem/tests/test_worker_basic.html b/dom/filesystem/tests/test_worker_basic.html new file mode 100644 index 0000000000..920f32719b --- /dev/null +++ b/dom/filesystem/tests/test_worker_basic.html @@ -0,0 +1,73 @@ + + + + Test for Directory API in workers + + + + + + + + + diff --git a/dom/filesystem/tests/worker_basic.js b/dom/filesystem/tests/worker_basic.js new file mode 100644 index 0000000000..2771c778fd --- /dev/null +++ b/dom/filesystem/tests/worker_basic.js @@ -0,0 +1,50 @@ +/* eslint-env worker */ +importScripts("filesystem_commons.js"); + +function finish() { + postMessage({ type: "finish" }); +} + +function ok(a, msg) { + postMessage({ type: "test", test: !!a, message: msg }); +} + +function is(a, b, msg) { + ok(a === b, msg); +} + +function isnot(a, b, msg) { + ok(a != b, msg); +} + +var tests = [ + function () { + test_basic(directory, next); + }, + function () { + test_getFilesAndDirectories(directory, true, next); + }, + function () { + test_getFiles(directory, false, next); + }, + function () { + test_getFiles(directory, true, next); + }, +]; + +function next() { + if (!tests.length) { + finish(); + return; + } + + var test = tests.shift(); + test(); +} + +var directory; + +onmessage = function (e) { + directory = e.data; + next(); +}; -- cgit v1.2.3