Bug 1945643 - Update to mozilla-nimbus-schemas 2025.1.1 r=chumphreys
[gecko.git] / dom / quota / test / mochitest / helpers.js
bloba6f4ad56f70c64d74d2e74f513e873d0a154bb01
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
6 // The path to the top level directory.
7 const depth = "../../../../";
9 var testGenerator;
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);
33 yield undefined;
35 info("Pushing preferences");
37 SpecialPowers.pushPrefEnv(
39 set: [["dom.storageManager.prompt.testing", true]],
41 nextTestHarnessStep
43 yield undefined;
45 info("Clearing old databases");
47 clearAllDatabases(nextTestHarnessStep);
48 yield undefined;
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",
57 });
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);
64 worker.terminate();
65 nextTestHarnessStep();
68 worker.onmessage = function (event) {
69 let message = event.data;
70 switch (message.op) {
71 case "ok":
72 ok(message.condition, message.name + " - " + message.diag);
73 break;
75 case "todo":
76 todo(message.condition, message.name, message.diag);
77 break;
79 case "info":
80 info(message.msg);
81 break;
83 case "ready":
84 worker.postMessage({ op: "load", files: workerScriptPaths });
85 break;
87 case "loaded":
88 worker.postMessage({ op: "start" });
89 break;
91 case "done":
92 ok(true, "Worker finished");
93 nextTestHarnessStep();
94 break;
96 case "clearAllDatabases":
97 clearAllDatabases(function () {
98 worker.postMessage({ op: "clearAllDatabasesDone" });
99 });
100 break;
102 default:
104 false,
105 "Received a bad message from worker: " + JSON.stringify(message)
107 nextTestHarnessStep();
111 URL.revokeObjectURL(workerScriptURL);
113 yield undefined;
115 worker.terminate();
116 worker = null;
118 clearAllDatabases(nextTestHarnessStep);
119 yield undefined;
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());
130 add_task(testSteps);
131 } else {
132 testGenerator = testSteps();
133 testGenerator.next();
135 yield undefined;
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 () {
150 SimpleTest.finish();
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() {
170 "use strict";
172 self.testGenerator = null;
174 self.repr = function (_thing_) {
175 if (typeof _thing_ == "undefined") {
176 return "undefined";
179 let str;
181 try {
182 str = _thing_ + "";
183 } catch (e) {
184 return "[" + typeof _thing_ + "]";
187 if (typeof _thing_ == "function") {
188 str = str.replace(/^\s+/, "");
189 let idx = str.indexOf("{");
190 if (idx != -1) {
191 str = str.substr(0, idx) + "{...}";
195 return str;
198 self.ok = function (_condition_, _name_, _diag_) {
199 self.postMessage({
200 op: "ok",
201 condition: !!_condition_,
202 name: _name_,
203 diag: _diag_,
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_) {
220 self.postMessage({
221 op: "todo",
222 condition: !!_condition_,
223 name: _name_,
224 diag: _diag_,
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) {
236 _fun_();
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_) {
266 false,
267 "Worker: uncaught exception [" +
268 _file_ +
269 ":" +
270 _line_ +
271 "]: '" +
272 _message_ +
275 self.finishTest();
276 self.close();
277 return true;
280 self.onmessage = function (_event_) {
281 let message = _event_.data;
282 switch (message.op) {
283 case "load":
284 info("Worker: loading " + JSON.stringify(message.files));
285 self.importScripts(message.files);
286 self.postMessage({ op: "loaded" });
287 break;
289 case "start":
290 executeSoon(function () {
291 info("Worker: starting tests");
292 testGenerator = testSteps();
293 testGenerator.next();
295 break;
297 case "clearAllDatabasesDone":
298 info("Worker: all databases are cleared");
299 if (self._clearAllDatabasesCallback) {
300 self._clearAllDatabasesCallback();
302 break;
304 default:
305 throw new Error(
306 "Received a bad message from parent: " + JSON.stringify(message)
311 self.postMessage({ op: "ready" });