summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/modules/system/worker
diff options
context:
space:
mode:
Diffstat (limited to 'dom/quota/test/modules/system/worker')
-rw-r--r--dom/quota/test/modules/system/worker/.eslintrc.js21
-rw-r--r--dom/quota/test/modules/system/worker/Assert.js22
-rw-r--r--dom/quota/test/modules/system/worker/ModuleLoader.js52
-rw-r--r--dom/quota/test/modules/system/worker/Utils.js55
-rw-r--r--dom/quota/test/modules/system/worker/UtilsChild.js56
-rw-r--r--dom/quota/test/modules/system/worker/head.js56
6 files changed, 262 insertions, 0 deletions
diff --git a/dom/quota/test/modules/system/worker/.eslintrc.js b/dom/quota/test/modules/system/worker/.eslintrc.js
new file mode 100644
index 0000000000..505e079c57
--- /dev/null
+++ b/dom/quota/test/modules/system/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: ["head.js"],
+ env: {
+ worker: true,
+ },
+ },
+ ],
+};
diff --git a/dom/quota/test/modules/system/worker/Assert.js b/dom/quota/test/modules/system/worker/Assert.js
new file mode 100644
index 0000000000..7c7e2683ea
--- /dev/null
+++ b/dom/quota/test/modules/system/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/system/worker/ModuleLoader.js b/dom/quota/test/modules/system/worker/ModuleLoader.js
new file mode 100644
index 0000000000..b79f3ff5b9
--- /dev/null
+++ b/dom/quota/test/modules/system/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, proto) {
+ 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/system/worker/Utils.js b/dom/quota/test/modules/system/worker/Utils.js
new file mode 100644
index 0000000000..ca9b54a92a
--- /dev/null
+++ b/dom/quota/test/modules/system/worker/Utils.js
@@ -0,0 +1,55 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+let UtilsChild;
+
+async function ensureUtilsChild() {
+ if (UtilsChild) {
+ return;
+ }
+
+ try {
+ const { UtilsChild: importedUtilsChild } = await import(
+ "/dom/quota/test/modules/worker/UtilsChild.js"
+ );
+
+ UtilsChild = importedUtilsChild;
+
+ throw Error("Please switch to dynamic module import");
+ } catch (e) {
+ if (e.message == "Please switch to dynamic module import") {
+ throw e;
+ }
+
+ importScripts("/dom/quota/test/modules/worker/UtilsChild.js");
+
+ const { UtilsChild: importedUtilsChild } = globalThis.importUtilsChild();
+
+ UtilsChild = importedUtilsChild;
+ }
+}
+
+const Utils = {
+ async getCachedOriginUsage() {
+ await ensureUtilsChild();
+
+ const result = await UtilsChild.getCachedOriginUsage();
+ return result;
+ },
+
+ async shrinkStorageSize(size) {
+ await ensureUtilsChild();
+
+ const result = await UtilsChild.shrinkStorageSize(size);
+ return result;
+ },
+
+ async restoreStorageSize() {
+ await ensureUtilsChild();
+
+ const result = await UtilsChild.restoreStorageSize();
+ return result;
+ },
+};
diff --git a/dom/quota/test/modules/system/worker/UtilsChild.js b/dom/quota/test/modules/system/worker/UtilsChild.js
new file mode 100644
index 0000000000..4addd6b948
--- /dev/null
+++ b/dom/quota/test/modules/system/worker/UtilsChild.js
@@ -0,0 +1,56 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+function _sendMessage(messageBody) {
+ const messageHeader = {
+ moduleName: "UtilsParent",
+ objectName: "UtilsParent",
+ };
+
+ const message = { ...messageHeader, ...messageBody };
+
+ postMessage(message);
+}
+
+function _recvMessage() {
+ return new Promise(function (resolve) {
+ addEventListener("message", async function onMessage(event) {
+ removeEventListener("message", onMessage);
+ const data = event.data;
+ resolve(data);
+ });
+ });
+}
+
+const _UtilsChild = {
+ async getCachedOriginUsage() {
+ _sendMessage({
+ op: "getCachedOriginUsage",
+ });
+
+ return _recvMessage();
+ },
+
+ async shrinkStorageSize(size) {
+ _sendMessage({
+ op: "shrinkStorageSize",
+ size,
+ });
+
+ return _recvMessage();
+ },
+
+ async restoreStorageSize() {
+ _sendMessage({
+ op: "restoreStorageSize",
+ });
+
+ return _recvMessage();
+ },
+};
+
+function importUtilsChild() {
+ return { UtilsChild: _UtilsChild };
+}
diff --git a/dom/quota/test/modules/system/worker/head.js b/dom/quota/test/modules/system/worker/head.js
new file mode 100644
index 0000000000..c415e0c56b
--- /dev/null
+++ b/dom/quota/test/modules/system/worker/head.js
@@ -0,0 +1,56 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+// eslint-disable-next-line mozilla/no-define-cc-etc
+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);
+});