|
|
|
|
|
|
1 |
/** |
| 2 |
* Any copyright is dedicated to the Public Domain. |
| 3 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
| 4 |
*/ |
| 5 |
|
| 6 |
var testGenerator = testSteps(); |
| 7 |
|
| 8 |
function clearAllDatabases(callback) |
| 9 |
{ |
| 10 |
let qms = SpecialPowers.Services.qms; |
| 11 |
let principal = SpecialPowers.wrap(document).nodePrincipal; |
| 12 |
let request = qms.clearStoragesForPrincipal(principal); |
| 13 |
let cb = SpecialPowers.wrapCallback(callback); |
| 14 |
request.callback = cb; |
| 15 |
} |
| 16 |
|
| 17 |
var testHarnessGenerator = testHarnessSteps(); |
| 18 |
testHarnessGenerator.next(); |
| 19 |
|
| 20 |
function testHarnessSteps() |
| 21 |
{ |
| 22 |
function nextTestHarnessStep(val) |
| 23 |
{ |
| 24 |
testHarnessGenerator.next(val); |
| 25 |
} |
| 26 |
|
| 27 |
let testScriptPath; |
| 28 |
let testScriptFilename; |
| 29 |
|
| 30 |
let scripts = document.getElementsByTagName("script"); |
| 31 |
for (let i = 0; i < scripts.length; i++) { |
| 32 |
let src = scripts[i].src; |
| 33 |
let match = src.match(/quota\/test\/unit\/(test_[^\/]+\.js)$/); |
| 34 |
if (match && match.length == 2) { |
| 35 |
testScriptPath = src; |
| 36 |
testScriptFilename = match[1]; |
| 37 |
break; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
yield undefined; |
| 42 |
|
| 43 |
info("Clearing old databases"); |
| 44 |
|
| 45 |
clearAllDatabases(nextTestHarnessStep); |
| 46 |
yield undefined; |
| 47 |
|
| 48 |
info("Running" + |
| 49 |
(testScriptFilename ? " '" + testScriptFilename + "'" : "")); |
| 50 |
|
| 51 |
if (testScriptFilename && !window.disableWorkerTest) { |
| 52 |
info("Running test in a worker"); |
| 53 |
|
| 54 |
let workerScriptBlob = |
| 55 |
new Blob([ "(" + workerScript.toString() + ")();" ], |
| 56 |
{ type: "text/javascript;version=1.7" }); |
| 57 |
let workerScriptURL = URL.createObjectURL(workerScriptBlob); |
| 58 |
|
| 59 |
let worker = new Worker(workerScriptURL); |
| 60 |
|
| 61 |
worker.onerror = function(event) { |
| 62 |
ok(false, "Worker had an error: " + event.message); |
| 63 |
worker.terminate(); |
| 64 |
nextTestHarnessStep(); |
| 65 |
}; |
| 66 |
|
| 67 |
worker.onmessage = function(event) { |
| 68 |
let message = event.data; |
| 69 |
switch (message.op) { |
| 70 |
case "ok": |
| 71 |
ok(message.condition, message.name, message.diag); |
| 72 |
break; |
| 73 |
|
| 74 |
case "todo": |
| 75 |
todo(message.condition, message.name, message.diag); |
| 76 |
break; |
| 77 |
|
| 78 |
case "info": |
| 79 |
info(message.msg); |
| 80 |
break; |
| 81 |
|
| 82 |
case "ready": |
| 83 |
worker.postMessage({ op: "load", files: [ testScriptPath ] }); |
| 84 |
break; |
| 85 |
|
| 86 |
case "loaded": |
| 87 |
worker.postMessage({ op: "start" }); |
| 88 |
break; |
| 89 |
|
| 90 |
case "done": |
| 91 |
ok(true, "Worker finished"); |
| 92 |
nextTestHarnessStep(); |
| 93 |
break; |
| 94 |
|
| 95 |
case "clearAllDatabases": |
| 96 |
clearAllDatabases(function(){ |
| 97 |
worker.postMessage({ op: "clearAllDatabasesDone" }); |
| 98 |
}); |
| 99 |
break; |
| 100 |
|
| 101 |
default: |
| 102 |
ok(false, |
| 103 |
"Received a bad message from worker: " + JSON.stringify(message)); |
| 104 |
nextTestHarnessStep(); |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
URL.revokeObjectURL(workerScriptURL); |
| 109 |
|
| 110 |
yield undefined; |
| 111 |
|
| 112 |
worker.terminate(); |
| 113 |
worker = null; |
| 114 |
|
| 115 |
clearAllDatabases(nextTestHarnessStep); |
| 116 |
yield undefined; |
| 117 |
} else if (testScriptFilename) { |
| 118 |
todo(false, |
| 119 |
"Skipping test in a worker because it is explicitly disabled: " + |
| 120 |
disableWorkerTest); |
| 121 |
} else { |
| 122 |
todo(false, |
| 123 |
"Skipping test in a worker because it's not structured properly"); |
| 124 |
} |
| 125 |
|
| 126 |
info("Running test in main thread"); |
| 127 |
|
| 128 |
// Now run the test script in the main thread. |
| 129 |
testGenerator.next(); |
| 130 |
|
| 131 |
yield undefined; |
| 132 |
} |
| 133 |
|
| 134 |
if (!window.runTest) { |
| 135 |
window.runTest = function() |
| 136 |
{ |
| 137 |
SimpleTest.waitForExplicitFinish(); |
| 138 |
testHarnessGenerator.next(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
function finishTest() |
| 143 |
{ |
| 144 |
SimpleTest.executeSoon(function() { |
| 145 |
clearAllDatabases(function() { SimpleTest.finish(); }); |
| 146 |
}); |
| 147 |
} |
| 148 |
|
| 149 |
function grabArgAndContinueHandler(arg) |
| 150 |
{ |
| 151 |
testGenerator.next(arg); |
| 152 |
} |
| 153 |
|
| 154 |
function continueToNextStep() |
| 155 |
{ |
| 156 |
SimpleTest.executeSoon(function() { |
| 157 |
testGenerator.next(); |
| 158 |
}); |
| 159 |
} |
| 160 |
|
| 161 |
function continueToNextStepSync() |
| 162 |
{ |
| 163 |
testGenerator.next(); |
| 164 |
} |
| 165 |
|
| 166 |
function workerScript() |
| 167 |
{ |
| 168 |
"use strict"; |
| 169 |
|
| 170 |
self.repr = function(_thing_) { |
| 171 |
if (typeof(_thing_) == "undefined") { |
| 172 |
return "undefined"; |
| 173 |
} |
| 174 |
|
| 175 |
let str; |
| 176 |
|
| 177 |
try { |
| 178 |
str = _thing_ + ""; |
| 179 |
} catch (e) { |
| 180 |
return "[" + typeof(_thing_) + "]"; |
| 181 |
} |
| 182 |
|
| 183 |
if (typeof(_thing_) == "function") { |
| 184 |
str = str.replace(/^\s+/, ""); |
| 185 |
let idx = str.indexOf("{"); |
| 186 |
if (idx != -1) { |
| 187 |
str = str.substr(0, idx) + "{...}"; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return str; |
| 192 |
}; |
| 193 |
|
| 194 |
self.ok = function(_condition_, _name_, _diag_) { |
| 195 |
self.postMessage({ op: "ok", |
| 196 |
condition: !!_condition_, |
| 197 |
name: _name_, |
| 198 |
diag: _diag_ }); |
| 199 |
}; |
| 200 |
|
| 201 |
self.is = function(_a_, _b_, _name_) { |
| 202 |
let pass = (_a_ == _b_); |
| 203 |
let diag = pass ? "" : "got " + repr(_a_) + ", expected " + repr(_b_); |
| 204 |
ok(pass, _name_, diag); |
| 205 |
}; |
| 206 |
|
| 207 |
self.isnot = function(_a_, _b_, _name_) { |
| 208 |
let pass = (_a_ != _b_); |
| 209 |
let diag = pass ? "" : "didn't expect " + repr(_a_) + ", but got it"; |
| 210 |
ok(pass, _name_, diag); |
| 211 |
}; |
| 212 |
|
| 213 |
self.todo = function(_condition_, _name_, _diag_) { |
| 214 |
self.postMessage({ op: "todo", |
| 215 |
condition: !!_condition_, |
| 216 |
name: _name_, |
| 217 |
diag: _diag_ }); |
| 218 |
}; |
| 219 |
|
| 220 |
self.info = function(_msg_) { |
| 221 |
self.postMessage({ op: "info", msg: _msg_ }); |
| 222 |
}; |
| 223 |
|
| 224 |
self.executeSoon = function(_fun_) { |
| 225 |
var channel = new MessageChannel(); |
| 226 |
channel.port1.postMessage(""); |
| 227 |
channel.port2.onmessage = function(event) { _fun_(); }; |
| 228 |
}; |
| 229 |
|
| 230 |
self.finishTest = function() { |
| 231 |
self.postMessage({ op: "done" }); |
| 232 |
}; |
| 233 |
|
| 234 |
self.continueToNextStep = function() { |
| 235 |
executeSoon(function() { |
| 236 |
testGenerator.next(); |
| 237 |
}); |
| 238 |
}; |
| 239 |
|
| 240 |
self.continueToNextStepSync = function() { |
| 241 |
testGenerator.next(); |
| 242 |
}; |
| 243 |
|
| 244 |
self._clearAllDatabasesCallback = undefined; |
| 245 |
self.clearAllDatabases = function(_callback_) { |
| 246 |
self._clearAllDatabasesCallback = _callback_; |
| 247 |
self.postMessage({ op: "clearAllDatabases" }); |
| 248 |
} |
| 249 |
|
| 250 |
self.onerror = function(_message_, _file_, _line_) { |
| 251 |
ok(false, |
| 252 |
"Worker: uncaught exception [" + _file_ + ":" + _line_ + "]: '" + |
| 253 |
_message_ + "'"); |
| 254 |
self.finishTest(); |
| 255 |
self.close(); |
| 256 |
return true; |
| 257 |
}; |
| 258 |
|
| 259 |
self.onmessage = function(_event_) { |
| 260 |
let message = _event_.data; |
| 261 |
switch (message.op) { |
| 262 |
case "load": |
| 263 |
info("Worker: loading " + JSON.stringify(message.files)); |
| 264 |
self.importScripts(message.files); |
| 265 |
self.postMessage({ op: "loaded" }); |
| 266 |
break; |
| 267 |
|
| 268 |
case "start": |
| 269 |
executeSoon(function() { |
| 270 |
info("Worker: starting tests"); |
| 271 |
testGenerator.next(); |
| 272 |
}); |
| 273 |
break; |
| 274 |
|
| 275 |
case "clearAllDatabasesDone": |
| 276 |
info("Worker: all databases are cleared"); |
| 277 |
if (self._clearAllDatabasesCallback) { |
| 278 |
self._clearAllDatabasesCallback(); |
| 279 |
} |
| 280 |
break; |
| 281 |
|
| 282 |
default: |
| 283 |
throw new Error("Received a bad message from parent: " + |
| 284 |
JSON.stringify(message)); |
| 285 |
} |
| 286 |
}; |
| 287 |
|
| 288 |
self.postMessage({ op: "ready" }); |
| 289 |
} |