2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
6 // The path to the top level directory.
7 const depth
= "../../../../";
10 var testHarnessGenerator
;
11 var workerScriptPaths
= [];
13 loadScript("dom/quota/test/common/mochitest.js");
15 function loadScript(path
) {
16 const url
= new URL(depth
+ path
, window
.location
.href
);
17 SpecialPowers
.Services
.scriptloader
.loadSubScript(url
.href
, this);
20 function loadWorkerScript(path
) {
21 const url
= new URL(depth
+ path
, window
.location
.href
);
22 workerScriptPaths
.push(url
.href
);
25 function* testHarnessSteps() {
26 function nextTestHarnessStep(val
) {
27 testHarnessGenerator
.next(val
);
30 info("Enabling storage testing");
32 enableStorageTesting().then(nextTestHarnessStep
);
35 info("Pushing preferences");
37 SpecialPowers
.pushPrefEnv(
39 set: [["dom.storageManager.prompt.testing", true]],
45 info("Clearing old databases");
47 clearAllDatabases(nextTestHarnessStep
);
50 info("Running test in given scopes");
52 if (workerScriptPaths
.length
) {
53 info("Running test in a worker");
55 let workerScriptBlob
= new Blob(["(" + workerScript
.toString() + ")();"], {
56 type
: "text/javascript",
58 let workerScriptURL
= URL
.createObjectURL(workerScriptBlob
);
60 let worker
= new Worker(workerScriptURL
);
62 worker
.onerror = function (event
) {
63 ok(false, "Worker had an error: " + event
.message
);
65 nextTestHarnessStep();
68 worker
.onmessage = function (event
) {
69 let message
= event
.data
;
72 ok(message
.condition
, message
.name
+ " - " + message
.diag
);
76 todo(message
.condition
, message
.name
, message
.diag
);
84 worker
.postMessage({ op
: "load", files
: workerScriptPaths
});
88 worker
.postMessage({ op
: "start" });
92 ok(true, "Worker finished");
93 nextTestHarnessStep();
96 case "clearAllDatabases":
97 clearAllDatabases(function () {
98 worker
.postMessage({ op
: "clearAllDatabasesDone" });
105 "Received a bad message from worker: " + JSON
.stringify(message
)
107 nextTestHarnessStep();
111 URL
.revokeObjectURL(workerScriptURL
);
118 clearAllDatabases(nextTestHarnessStep
);
122 info("Running test in main thread");
124 // Now run the test script in the main thread.
125 if (testSteps
.constructor.name
=== "AsyncFunction") {
126 SimpleTest
.registerCleanupFunction(async
function () {
127 await
requestFinished(clearAllDatabases());
132 testGenerator
= testSteps();
133 testGenerator
.next();
139 if (!window
.runTest
) {
140 window
.runTest = function () {
141 SimpleTest
.waitForExplicitFinish();
142 testHarnessGenerator
= testHarnessSteps();
143 testHarnessGenerator
.next();
147 function finishTest() {
148 SimpleTest
.executeSoon(function () {
149 clearAllDatabases(function () {
155 function grabArgAndContinueHandler(arg
) {
156 testGenerator
.next(arg
);
159 function continueToNextStep() {
160 SimpleTest
.executeSoon(function () {
161 testGenerator
.next();
165 function continueToNextStepSync() {
166 testGenerator
.next();
169 function workerScript() {
172 self
.testGenerator
= null;
174 self
.repr = function (_thing_
) {
175 if (typeof _thing_
== "undefined") {
184 return "[" + typeof _thing_
+ "]";
187 if (typeof _thing_
== "function") {
188 str
= str
.replace(/^\s+/, "");
189 let idx
= str
.indexOf("{");
191 str
= str
.substr(0, idx
) + "{...}";
198 self
.ok = function (_condition_
, _name_
, _diag_
) {
201 condition
: !!_condition_
,
207 self
.is = function (_a_
, _b_
, _name_
) {
208 let pass
= _a_
== _b_
;
209 let diag
= pass
? "" : "got " + repr(_a_
) + ", expected " + repr(_b_
);
210 ok(pass
, _name_
, diag
);
213 self
.isnot = function (_a_
, _b_
, _name_
) {
214 let pass
= _a_
!= _b_
;
215 let diag
= pass
? "" : "didn't expect " + repr(_a_
) + ", but got it";
216 ok(pass
, _name_
, diag
);
219 self
.todo = function (_condition_
, _name_
, _diag_
) {
222 condition
: !!_condition_
,
228 self
.info = function (_msg_
) {
229 self
.postMessage({ op
: "info", msg
: _msg_
});
232 self
.executeSoon = function (_fun_
) {
233 var channel
= new MessageChannel();
234 channel
.port1
.postMessage("");
235 channel
.port2
.onmessage = function (event
) {
240 self
.finishTest = function () {
241 self
.postMessage({ op
: "done" });
244 self
.grabArgAndContinueHandler = function (_arg_
) {
245 testGenerator
.next(_arg_
);
248 self
.continueToNextStep = function () {
249 executeSoon(function () {
250 testGenerator
.next();
254 self
.continueToNextStepSync = function () {
255 testGenerator
.next();
258 self
._clearAllDatabasesCallback
= undefined;
259 self
.clearAllDatabases = function (_callback_
) {
260 self
._clearAllDatabasesCallback
= _callback_
;
261 self
.postMessage({ op
: "clearAllDatabases" });
264 self
.onerror = function (_message_
, _file_
, _line_
) {
267 "Worker: uncaught exception [" +
280 self
.onmessage = function (_event_
) {
281 let message
= _event_
.data
;
282 switch (message
.op
) {
284 info("Worker: loading " + JSON
.stringify(message
.files
));
285 self
.importScripts(message
.files
);
286 self
.postMessage({ op
: "loaded" });
290 executeSoon(function () {
291 info("Worker: starting tests");
292 testGenerator
= testSteps();
293 testGenerator
.next();
297 case "clearAllDatabasesDone":
298 info("Worker: all databases are cleared");
299 if (self
._clearAllDatabasesCallback
) {
300 self
._clearAllDatabasesCallback();
306 "Received a bad message from parent: " + JSON
.stringify(message
)
311 self
.postMessage({ op
: "ready" });