diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /dom/quota/test/modules/content | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/quota/test/modules/content')
-rw-r--r-- | dom/quota/test/modules/content/.eslintrc.js | 22 | ||||
-rw-r--r-- | dom/quota/test/modules/content/Assert.js | 10 | ||||
-rw-r--r-- | dom/quota/test/modules/content/ModuleLoader.js | 61 | ||||
-rw-r--r-- | dom/quota/test/modules/content/StorageUtils.js | 48 | ||||
-rw-r--r-- | dom/quota/test/modules/content/WorkerDriver.js | 53 | ||||
-rw-r--r-- | dom/quota/test/modules/content/worker/.eslintrc.js | 21 | ||||
-rw-r--r-- | dom/quota/test/modules/content/worker/Assert.js | 22 | ||||
-rw-r--r-- | dom/quota/test/modules/content/worker/ModuleLoader.js | 52 | ||||
-rw-r--r-- | dom/quota/test/modules/content/worker/head.js | 55 |
9 files changed, 344 insertions, 0 deletions
diff --git a/dom/quota/test/modules/content/.eslintrc.js b/dom/quota/test/modules/content/.eslintrc.js new file mode 100644 index 0000000000..03499166b5 --- /dev/null +++ b/dom/quota/test/modules/content/.eslintrc.js @@ -0,0 +1,22 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +"use strict"; + +module.exports = { + overrides: [ + { + files: [ + "Assert.js", + "ModuleLoader.js", + "StorageUtils.js", + "WorkerDriver.js", + ], + parserOptions: { + sourceType: "module", + }, + }, + ], +}; diff --git a/dom/quota/test/modules/content/Assert.js b/dom/quota/test/modules/content/Assert.js new file mode 100644 index 0000000000..e2c8df19c8 --- /dev/null +++ b/dom/quota/test/modules/content/Assert.js @@ -0,0 +1,10 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +// Just a wrapper around SimpleTest related functions for now. +export const Assert = { + ok, + equal: is, +}; diff --git a/dom/quota/test/modules/content/ModuleLoader.js b/dom/quota/test/modules/content/ModuleLoader.js new file mode 100644 index 0000000000..a47061f751 --- /dev/null +++ b/dom/quota/test/modules/content/ModuleLoader.js @@ -0,0 +1,61 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +export function ModuleLoader(base, depth, proto) { + const modules = {}; + + const principal = SpecialPowers.wrap(document).nodePrincipal; + + const sharedGlobalSandbox = SpecialPowers.Cu.Sandbox(principal, { + invisibleToDebugger: true, + sandboxName: "FS Module Loader", + sandboxPrototype: proto, + wantComponents: false, + wantGlobalProperties: [], + wantXrays: false, + }); + + const require = async function(id) { + if (modules[id]) { + return modules[id].exported_symbols; + } + + const url = new URL(depth + id, base); + + const module = Object.create(null, { + exported_symbols: { + configurable: false, + enumerable: true, + value: Object.create(null), + writable: true, + }, + }); + + modules[id] = module; + + const properties = { + require_module: require, + exported_symbols: module.exported_symbols, + }; + + // Create a new object in this sandbox, that will be used as the scope + // object for this particular module. + const sandbox = sharedGlobalSandbox.Object(); + Object.assign(sandbox, properties); + + SpecialPowers.Services.scriptloader.loadSubScript(url.href, sandbox); + + return module.exported_symbols; + }; + + const returnObj = { + require: { + enumerable: true, + value: require, + }, + }; + + return Object.create(null, returnObj); +} diff --git a/dom/quota/test/modules/content/StorageUtils.js b/dom/quota/test/modules/content/StorageUtils.js new file mode 100644 index 0000000000..b29f7d9c26 --- /dev/null +++ b/dom/quota/test/modules/content/StorageUtils.js @@ -0,0 +1,48 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +class RequestError extends Error { + constructor(resultCode, resultName) { + super(`Request failed (code: ${resultCode}, name: ${resultName})`); + this.name = "RequestError"; + this.resultCode = resultCode; + this.resultName = resultName; + } +} + +export async function setStoragePrefs(optionalPrefsToSet) { + const prefsToSet = [ + // Not needed right now, but might be needed in future. + // ["dom.quotaManager.testing", true], + ]; + + if (SpecialPowers.Services.appinfo.OS === "WINNT") { + prefsToSet.push(["dom.quotaManager.useDOSDevicePathSyntax", true]); + } + + if (optionalPrefsToSet) { + prefsToSet.push(...optionalPrefsToSet); + } + + await SpecialPowers.pushPrefEnv({ set: prefsToSet }); +} + +export async function clearStoragesForOrigin(principal) { + const request = SpecialPowers.Services.qms.clearStoragesForPrincipal( + principal + ); + + await new Promise(function(resolve) { + request.callback = SpecialPowers.wrapCallback(function() { + resolve(); + }); + }); + + if (request.resultCode != SpecialPowers.Cr.NS_OK) { + throw new RequestError(request.resultCode, request.resultName); + } + + return request.result; +} diff --git a/dom/quota/test/modules/content/WorkerDriver.js b/dom/quota/test/modules/content/WorkerDriver.js new file mode 100644 index 0000000000..365d00f363 --- /dev/null +++ b/dom/quota/test/modules/content/WorkerDriver.js @@ -0,0 +1,53 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +export async function runTestInWorker(script, base, listener) { + return new Promise(function(resolve) { + const globalHeadUrl = new URL( + "/tests/dom/quota/test/modules/worker/head.js", + base + ); + + const worker = new Worker(globalHeadUrl.href); + + worker.onmessage = function(event) { + const data = event.data; + + switch (data.op) { + case "ok": + listener.onOk(data.value, data.message); + break; + + case "is": + listener.onIs(data.a, data.b, data.message); + break; + + case "info": + listener.onInfo(data.message); + break; + + case "finish": + resolve(); + break; + + case "failure": + listener.onOk(false, "Worker had a failure: " + data.message); + resolve(); + break; + } + }; + + worker.onerror = function(event) { + listener.onOk(false, "Worker had an error: " + event.data); + resolve(); + }; + + const scriptUrl = new URL(script, base); + + const localHeadUrl = new URL("head.js", scriptUrl); + + worker.postMessage([localHeadUrl.href, scriptUrl.href]); + }); +} diff --git a/dom/quota/test/modules/content/worker/.eslintrc.js b/dom/quota/test/modules/content/worker/.eslintrc.js new file mode 100644 index 0000000000..470c23b2fa --- /dev/null +++ b/dom/quota/test/modules/content/worker/.eslintrc.js @@ -0,0 +1,21 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +"use strict"; + +module.exports = { + env: { + worker: true, + }, + + overrides: [ + { + files: ["Assert.js", "ModuleLoader.js"], + parserOptions: { + sourceType: "script", + }, + }, + ], +}; diff --git a/dom/quota/test/modules/content/worker/Assert.js b/dom/quota/test/modules/content/worker/Assert.js new file mode 100644 index 0000000000..7c7e2683ea --- /dev/null +++ b/dom/quota/test/modules/content/worker/Assert.js @@ -0,0 +1,22 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const Assert = { + ok(value, message) { + postMessage({ + op: "ok", + value: !!value, + message, + }); + }, + equal(a, b, message) { + postMessage({ + op: "is", + a, + b, + message, + }); + }, +}; diff --git a/dom/quota/test/modules/content/worker/ModuleLoader.js b/dom/quota/test/modules/content/worker/ModuleLoader.js new file mode 100644 index 0000000000..23ee7c06df --- /dev/null +++ b/dom/quota/test/modules/content/worker/ModuleLoader.js @@ -0,0 +1,52 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +function ModuleLoader(base, depth) { + const modules = {}; + + const require = async function(id) { + if (modules[id]) { + return modules[id].exported_symbols; + } + + const url = new URL(depth + id, base); + + const module = Object.create(null, { + exported_symbols: { + configurable: false, + enumerable: true, + value: Object.create(null), + writable: true, + }, + }); + + modules[id] = module; + + const xhr = new XMLHttpRequest(); + xhr.open("GET", url.href, false); + xhr.responseType = "text"; + xhr.send(); + + let source = xhr.responseText; + + let code = new Function( + "require_module", + "exported_symbols", + `eval(arguments[2] + "\\n//# sourceURL=" + arguments[3] + "\\n")` + ); + code(require, module.exported_symbols, source, url.href); + + return module.exported_symbols; + }; + + const returnObj = { + require: { + enumerable: true, + value: require, + }, + }; + + return Object.create(null, returnObj); +} diff --git a/dom/quota/test/modules/content/worker/head.js b/dom/quota/test/modules/content/worker/head.js new file mode 100644 index 0000000000..58d4591e47 --- /dev/null +++ b/dom/quota/test/modules/content/worker/head.js @@ -0,0 +1,55 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const Cr = { + NS_ERROR_NOT_IMPLEMENTED: 2147500033, +}; + +function add_task(func) { + if (!add_task.tasks) { + add_task.tasks = []; + add_task.index = 0; + } + + add_task.tasks.push(func); +} + +addEventListener("message", async function onMessage(event) { + function info(message) { + postMessage({ op: "info", message }); + } + + function executeSoon(callback) { + const channel = new MessageChannel(); + channel.port1.postMessage(""); + channel.port2.onmessage = function() { + callback(); + }; + } + + function runNextTest() { + if (add_task.index < add_task.tasks.length) { + const task = add_task.tasks[add_task.index++]; + info("add_task | Entering test " + task.name); + task() + .then(function() { + executeSoon(runNextTest); + info("add_task | Leaving test " + task.name); + }) + .catch(function(ex) { + postMessage({ op: "failure", message: "" + ex }); + }); + } else { + postMessage({ op: "finish" }); + } + } + + removeEventListener("message", onMessage); + + const data = event.data; + importScripts(...data); + + executeSoon(runNextTest); +}); |