mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-27 04:38:02 +00:00
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:
parent
3c14a0bff0
commit
23e21aa708
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
27
dom/quota/test/modules/system/Utils.sys.mjs
Normal file
27
dom/quota/test/modules/system/Utils.sys.mjs
Normal 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;
|
||||
},
|
||||
};
|
27
dom/quota/test/modules/system/UtilsParent.sys.mjs
Normal file
27
dom/quota/test/modules/system/UtilsParent.sys.mjs
Normal 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}`);
|
||||
}
|
||||
},
|
||||
};
|
@ -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":
|
||||
|
48
dom/quota/test/modules/system/worker/Utils.js
Normal file
48
dom/quota/test/modules/system/worker/Utils.js
Normal 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;
|
||||
},
|
||||
};
|
43
dom/quota/test/modules/system/worker/UtilsChild.js
Normal file
43
dom/quota/test/modules/system/worker/UtilsChild.js
Normal 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 };
|
||||
}
|
@ -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",
|
||||
]
|
||||
|
Loading…
x
Reference in New Issue
Block a user