Bug 1815696 - Introduce Utils in tests with a method for shrinking storage size; r=dom-storage-reviewers,jari

The new method is needed for testing quota management and it works in xpcshell
tests including workers in xpcshell tests.

Differential Revision: https://phabricator.services.mozilla.com/D165072
This commit is contained in:
Jan Varga 2023-02-09 10:27:34 +00:00
parent 3c14a0bff0
commit 23e21aa708
8 changed files with 183 additions and 0 deletions

View File

@ -13,6 +13,8 @@ async function require_module(id) {
importScripts("/dom/quota/test/modules/worker/Assert.js");
importScripts("/dom/quota/test/modules/worker/Utils.js");
require_module.moduleLoader = new globalThis.ModuleLoader(base, depth);
}

View File

@ -63,3 +63,19 @@ export async function clearStoragesForOrigin(principal) {
return request.result;
}
export async function resetStorage() {
const request = Services.qms.reset();
await new Promise(function(resolve) {
request.callback = function() {
resolve();
};
});
if (request.resultCode != Cr.NS_OK) {
throw new RequestError(request.resultCode, request.resultName);
}
return request.result;
}

View File

@ -0,0 +1,27 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
import { resetStorage } from "resource://testing-common/dom/quota/test/modules/StorageUtils.sys.mjs";
export const Utils = {
async shrinkStorageSize(size) {
Services.prefs.setIntPref(
"dom.quotaManager.temporaryStorage.fixedLimit",
size
);
const result = await resetStorage();
return result;
},
async restoreStorageSize() {
Services.prefs.clearUserPref(
"dom.quotaManager.temporaryStorage.fixedLimit"
);
const result = await resetStorage();
return result;
},
};

View File

@ -0,0 +1,27 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
import { Utils } from "resource://testing-common/dom/quota/test/modules/Utils.sys.mjs";
export const UtilsParent = {
async OnMessageReceived(worker, msg) {
switch (msg.op) {
case "shrinkStorageSize": {
const result = await Utils.shrinkStorageSize(msg.size);
worker.postMessage(result);
break;
}
case "restoreStorageSize": {
const result = await Utils.restoreStorageSize();
worker.postMessage(result);
break;
}
default:
throw new Error(`Unknown op ${msg.op}`);
}
},
};

View File

@ -9,10 +9,26 @@ export async function runTestInWorker(script, base, listener) {
"resource://testing-common/dom/quota/test/modules/worker/head.js"
);
let modules = {};
const worker = new Worker(globalHeadUrl.href);
worker.onmessage = function(event) {
const data = event.data;
const moduleName = data.moduleName;
const objectName = data.objectName;
if (moduleName && objectName) {
if (!modules[moduleName]) {
modules[moduleName] = ChromeUtils.importESModule(
"resource://testing-common/dom/quota/test/modules/" +
moduleName +
".sys.mjs"
);
}
modules[moduleName][objectName].OnMessageReceived(worker, data);
return;
}
switch (data.op) {
case "ok":

View File

@ -0,0 +1,48 @@
/**
* 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 shrinkStorageSize(size) {
await ensureUtilsChild();
const result = await UtilsChild.shrinkStorageSize(size);
return result;
},
async restoreStorageSize() {
await ensureUtilsChild();
const result = await UtilsChild.restoreStorageSize();
return result;
},
};

View File

@ -0,0 +1,43 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
const _UtilsChild = {
async shrinkStorageSize(size) {
postMessage({
moduleName: "UtilsParent",
objectName: "UtilsParent",
op: "shrinkStorageSize",
size,
});
return new Promise(function(resolve) {
addEventListener("message", async function onMessage(event) {
removeEventListener("message", onMessage);
const data = event.data;
resolve(data);
});
});
},
async restoreStorageSize() {
postMessage({
moduleName: "UtilsParent",
objectName: "UtilsParent",
op: "restoreStorageSize",
});
return new Promise(function(resolve) {
addEventListener("message", async function onMessage(event) {
removeEventListener("message", onMessage);
const data = event.data;
resolve(data);
});
});
},
};
function importUtilsChild() {
return { UtilsChild: _UtilsChild };
}

View File

@ -66,6 +66,8 @@ TEST_HARNESS_FILES.xpcshell.dom.quota.test.xpcshell.common += [
TESTING_JS_MODULES.dom.quota.test.modules += [
"modules/system/ModuleLoader.sys.mjs",
"modules/system/StorageUtils.sys.mjs",
"modules/system/Utils.sys.mjs",
"modules/system/UtilsParent.sys.mjs",
"modules/system/WorkerDriver.sys.mjs",
]
@ -73,4 +75,6 @@ TESTING_JS_MODULES.dom.quota.test.modules.worker += [
"modules/system/worker/Assert.js",
"modules/system/worker/head.js",
"modules/system/worker/ModuleLoader.js",
"modules/system/worker/Utils.js",
"modules/system/worker/UtilsChild.js",
]