|
|
|
|
|
|
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 |
// The test js is shared between xpcshell (which has no SpecialPowers object) |
| 9 |
// and content mochitests (where the |Components| object is accessible only as |
| 10 |
// SpecialPowers.Components). Expose Components if necessary here to make things |
| 11 |
// work everywhere. |
| 12 |
// |
| 13 |
// Even if the real |Components| doesn't exist, we might shim in a simple JS |
| 14 |
// placebo for compat. An easy way to differentiate this from the real thing |
| 15 |
// is whether the property is read-only or not. |
| 16 |
var c = Object.getOwnPropertyDescriptor(this, 'Components'); |
| 17 |
if ((!c.value || c.writable) && typeof SpecialPowers === 'object') |
| 18 |
Components = SpecialPowers.Components; |
| 19 |
|
| 20 |
function executeSoon(aFun) |
| 21 |
{ |
| 22 |
let comp = SpecialPowers.wrap(Components); |
| 23 |
|
| 24 |
let thread = comp.classes["@mozilla.org/thread-manager;1"] |
| 25 |
.getService(comp.interfaces.nsIThreadManager) |
| 26 |
.mainThread; |
| 27 |
|
| 28 |
thread.dispatch({ |
| 29 |
run: function() { |
| 30 |
aFun(); |
| 31 |
} |
| 32 |
}, Components.interfaces.nsIThread.DISPATCH_NORMAL); |
| 33 |
} |
| 34 |
|
| 35 |
function clearAllDatabases(callback) { |
| 36 |
let qms = SpecialPowers.Services.qms; |
| 37 |
let principal = SpecialPowers.wrap(document).nodePrincipal; |
| 38 |
let request = qms.clearStoragesForPrincipal(principal); |
| 39 |
let cb = SpecialPowers.wrapCallback(callback); |
| 40 |
request.callback = cb; |
| 41 |
} |
| 42 |
|
| 43 |
var testHarnessGenerator = testHarnessSteps(); |
| 44 |
testHarnessGenerator.next(); |
| 45 |
|
| 46 |
function testHarnessSteps() { |
| 47 |
function nextTestHarnessStep(val) { |
| 48 |
testHarnessGenerator.next(val); |
| 49 |
} |
| 50 |
|
| 51 |
let testScriptPath; |
| 52 |
let testScriptFilename; |
| 53 |
|
| 54 |
let scripts = document.getElementsByTagName("script"); |
| 55 |
for (let i = 0; i < scripts.length; i++) { |
| 56 |
let src = scripts[i].src; |
| 57 |
let match = src.match(/quota\/test\/(test_[^\/]+\.js)$/); |
| 58 |
if (match && match.length == 2) { |
| 59 |
testScriptPath = src; |
| 60 |
testScriptFilename = match[1]; |
| 61 |
break; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
yield undefined; |
| 66 |
|
| 67 |
info("Clearing old databases"); |
| 68 |
|
| 69 |
clearAllDatabases(nextTestHarnessStep); |
| 70 |
yield undefined; |
| 71 |
|
| 72 |
info("Running" + |
| 73 |
(testScriptFilename ? " '" + testScriptFilename + "'" : "")); |
| 74 |
|
| 75 |
if (testScriptFilename && !window.disableWorkerTest) { |
| 76 |
info("Running test in a worker"); |
| 77 |
|
| 78 |
let workerScriptBlob = |
| 79 |
new Blob([ "(" + workerScript.toString() + ")();" ], |
| 80 |
{ type: "text/javascript;version=1.7" }); |
| 81 |
let workerScriptURL = URL.createObjectURL(workerScriptBlob); |
| 82 |
|
| 83 |
let worker = new Worker(workerScriptURL); |
| 84 |
|
| 85 |
worker._expectingUncaughtException = false; |
| 86 |
worker.onerror = function(event) { |
| 87 |
if (worker._expectingUncaughtException) { |
| 88 |
ok(true, "Worker had an expected error: " + event.message); |
| 89 |
worker._expectingUncaughtException = false; |
| 90 |
event.preventDefault(); |
| 91 |
return; |
| 92 |
} |
| 93 |
ok(false, "Worker had an error: " + event.message); |
| 94 |
worker.terminate(); |
| 95 |
nextTestHarnessStep(); |
| 96 |
}; |
| 97 |
|
| 98 |
worker.onmessage = function(event) { |
| 99 |
let message = event.data; |
| 100 |
switch (message.op) { |
| 101 |
case "ok": |
| 102 |
ok(message.condition, message.name, message.diag); |
| 103 |
break; |
| 104 |
|
| 105 |
case "todo": |
| 106 |
todo(message.condition, message.name, message.diag); |
| 107 |
break; |
| 108 |
|
| 109 |
case "info": |
| 110 |
info(message.msg); |
| 111 |
break; |
| 112 |
|
| 113 |
case "ready": |
| 114 |
worker.postMessage({ op: "load", files: [ testScriptPath ] }); |
| 115 |
break; |
| 116 |
|
| 117 |
case "loaded": |
| 118 |
worker.postMessage({ op: "start" }); |
| 119 |
break; |
| 120 |
|
| 121 |
case "done": |
| 122 |
ok(true, "Worker finished"); |
| 123 |
nextTestHarnessStep(); |
| 124 |
break; |
| 125 |
|
| 126 |
case "expectUncaughtException": |
| 127 |
worker._expectingUncaughtException = message.expecting; |
| 128 |
break; |
| 129 |
|
| 130 |
case "clearAllDatabases": |
| 131 |
clearAllDatabases(function(){ |
| 132 |
worker.postMessage({ op: "clearAllDatabasesDone" }); |
| 133 |
}); |
| 134 |
break; |
| 135 |
|
| 136 |
default: |
| 137 |
ok(false, |
| 138 |
"Received a bad message from worker: " + JSON.stringify(message)); |
| 139 |
nextTestHarnessStep(); |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
URL.revokeObjectURL(workerScriptURL); |
| 144 |
|
| 145 |
yield undefined; |
| 146 |
|
| 147 |
if (worker._expectingUncaughtException) { |
| 148 |
ok(false, "expectUncaughtException was called but no uncaught " + |
| 149 |
"exception was detected!"); |
| 150 |
} |
| 151 |
|
| 152 |
worker.terminate(); |
| 153 |
worker = null; |
| 154 |
|
| 155 |
clearAllDatabases(nextTestHarnessStep); |
| 156 |
yield undefined; |
| 157 |
} else if (testScriptFilename) { |
| 158 |
todo(false, |
| 159 |
"Skipping test in a worker because it is explicitly disabled: " + |
| 160 |
disableWorkerTest); |
| 161 |
} else { |
| 162 |
todo(false, |
| 163 |
"Skipping test in a worker because it's not structured properly"); |
| 164 |
} |
| 165 |
|
| 166 |
info("Running test in main thread"); |
| 167 |
|
| 168 |
// Now run the test script in the main thread. |
| 169 |
testGenerator.next(); |
| 170 |
|
| 171 |
yield undefined; |
| 172 |
} |
| 173 |
|
| 174 |
if (!window.runTest) { |
| 175 |
window.runTest = function() |
| 176 |
{ |
| 177 |
SimpleTest.waitForExplicitFinish(); |
| 178 |
testHarnessGenerator.next(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
function finishTest() |
| 183 |
{ |
| 184 |
SpecialPowers.notifyObserversInParentProcess(null, |
| 185 |
"disk-space-watcher", |
| 186 |
"free"); |
| 187 |
|
| 188 |
SimpleTest.executeSoon(function() { |
| 189 |
clearAllDatabases(function() { SimpleTest.finish(); }); |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
function continueToNextStep() |
| 194 |
{ |
| 195 |
SimpleTest.executeSoon(function() { |
| 196 |
testGenerator.next(); |
| 197 |
}); |
| 198 |
} |
| 199 |
|
| 200 |
function continueToNextStepSync() |
| 201 |
{ |
| 202 |
testGenerator.next(); |
| 203 |
} |
| 204 |
|
| 205 |
function errorHandler(event) |
| 206 |
{ |
| 207 |
ok(false, "error, '" + event.target.error.name + "'"); |
| 208 |
finishTest(); |
| 209 |
} |
| 210 |
|
| 211 |
function expectUncaughtException(expecting) |
| 212 |
{ |
| 213 |
SimpleTest.expectUncaughtException(expecting); |
| 214 |
} |
| 215 |
|
| 216 |
function unexpectedSuccessHandler() |
| 217 |
{ |
| 218 |
ok(false, "Got success, but did not expect it!"); |
| 219 |
finishTest(); |
| 220 |
} |
| 221 |
|
| 222 |
function expectedErrorHandler(name) |
| 223 |
{ |
| 224 |
return function(event) { |
| 225 |
is(event.type, "error", "Got an error event"); |
| 226 |
is(event.target.error.name, name, "Expected error was thrown."); |
| 227 |
event.preventDefault(); |
| 228 |
grabEventAndContinueHandler(event); |
| 229 |
}; |
| 230 |
} |
| 231 |
|
| 232 |
function ExpectError(name, preventDefault) |
| 233 |
{ |
| 234 |
this._name = name; |
| 235 |
this._preventDefault = preventDefault; |
| 236 |
} |
| 237 |
ExpectError.prototype = { |
| 238 |
handleEvent: function(event) |
| 239 |
{ |
| 240 |
is(event.type, "error", "Got an error event"); |
| 241 |
is(event.target.error.name, this._name, "Expected error was thrown."); |
| 242 |
if (this._preventDefault) { |
| 243 |
event.preventDefault(); |
| 244 |
event.stopPropagation(); |
| 245 |
} |
| 246 |
grabEventAndContinueHandler(event); |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
function workerScript() { |
| 251 |
"use strict"; |
| 252 |
|
| 253 |
self.repr = function(_thing_) { |
| 254 |
if (typeof(_thing_) == "undefined") { |
| 255 |
return "undefined"; |
| 256 |
} |
| 257 |
|
| 258 |
let str; |
| 259 |
|
| 260 |
try { |
| 261 |
str = _thing_ + ""; |
| 262 |
} catch (e) { |
| 263 |
return "[" + typeof(_thing_) + "]"; |
| 264 |
} |
| 265 |
|
| 266 |
if (typeof(_thing_) == "function") { |
| 267 |
str = str.replace(/^\s+/, ""); |
| 268 |
let idx = str.indexOf("{"); |
| 269 |
if (idx != -1) { |
| 270 |
str = str.substr(0, idx) + "{...}"; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
return str; |
| 275 |
}; |
| 276 |
|
| 277 |
self.ok = function(_condition_, _name_, _diag_) { |
| 278 |
self.postMessage({ op: "ok", |
| 279 |
condition: !!_condition_, |
| 280 |
name: _name_, |
| 281 |
diag: _diag_ }); |
| 282 |
}; |
| 283 |
|
| 284 |
self.is = function(_a_, _b_, _name_) { |
| 285 |
let pass = (_a_ == _b_); |
| 286 |
let diag = pass ? "" : "got " + repr(_a_) + ", expected " + repr(_b_); |
| 287 |
ok(pass, _name_, diag); |
| 288 |
}; |
| 289 |
|
| 290 |
self.isnot = function(_a_, _b_, _name_) { |
| 291 |
let pass = (_a_ != _b_); |
| 292 |
let diag = pass ? "" : "didn't expect " + repr(_a_) + ", but got it"; |
| 293 |
ok(pass, _name_, diag); |
| 294 |
}; |
| 295 |
|
| 296 |
self.todo = function(_condition_, _name_, _diag_) { |
| 297 |
self.postMessage({ op: "todo", |
| 298 |
condition: !!_condition_, |
| 299 |
name: _name_, |
| 300 |
diag: _diag_ }); |
| 301 |
}; |
| 302 |
|
| 303 |
self.info = function(_msg_) { |
| 304 |
self.postMessage({ op: "info", msg: _msg_ }); |
| 305 |
}; |
| 306 |
|
| 307 |
self.executeSoon = function(_fun_) { |
| 308 |
var channel = new MessageChannel(); |
| 309 |
channel.port1.postMessage(""); |
| 310 |
channel.port2.onmessage = function(event) { _fun_(); }; |
| 311 |
}; |
| 312 |
|
| 313 |
self.finishTest = function() { |
| 314 |
if (self._expectingUncaughtException) { |
| 315 |
self.ok(false, "expectUncaughtException was called but no uncaught " |
| 316 |
+ "exception was detected!"); |
| 317 |
} |
| 318 |
self.postMessage({ op: "done" }); |
| 319 |
}; |
| 320 |
|
| 321 |
self.grabEventAndContinueHandler = function(_event_) { |
| 322 |
testGenerator.next(_event_); |
| 323 |
}; |
| 324 |
|
| 325 |
self.continueToNextStep = function() { |
| 326 |
executeSoon(function() { |
| 327 |
testGenerator.next(); |
| 328 |
}); |
| 329 |
}; |
| 330 |
|
| 331 |
self.continueToNextStepSync = function() { |
| 332 |
testGenerator.next(); |
| 333 |
}; |
| 334 |
|
| 335 |
self.errorHandler = function(_event_) { |
| 336 |
ok(false, "error, '" + _event_.target.error.name + "'"); |
| 337 |
finishTest(); |
| 338 |
}; |
| 339 |
|
| 340 |
self.unexpectedSuccessHandler = function() |
| 341 |
{ |
| 342 |
ok(false, "Got success, but did not expect it!"); |
| 343 |
finishTest(); |
| 344 |
}; |
| 345 |
|
| 346 |
self.expectedErrorHandler = function(_name_) |
| 347 |
{ |
| 348 |
return function(_event_) { |
| 349 |
is(_event_.type, "error", "Got an error event"); |
| 350 |
is(_event_.target.error.name, _name_, "Expected error was thrown."); |
| 351 |
_event_.preventDefault(); |
| 352 |
grabEventAndContinueHandler(_event_); |
| 353 |
}; |
| 354 |
}; |
| 355 |
|
| 356 |
self.ExpectError = function(_name_, _preventDefault_) |
| 357 |
{ |
| 358 |
this._name = _name_; |
| 359 |
this._preventDefault = _preventDefault_; |
| 360 |
} |
| 361 |
self.ExpectError.prototype = { |
| 362 |
handleEvent: function(_event_) |
| 363 |
{ |
| 364 |
is(_event_.type, "error", "Got an error event"); |
| 365 |
is(_event_.target.error.name, this._name, "Expected error was thrown."); |
| 366 |
if (this._preventDefault) { |
| 367 |
_event_.preventDefault(); |
| 368 |
_event_.stopPropagation(); |
| 369 |
} |
| 370 |
grabEventAndContinueHandler(_event_); |
| 371 |
} |
| 372 |
}; |
| 373 |
|
| 374 |
self._expectingUncaughtException = false; |
| 375 |
self.expectUncaughtException = function(_expecting_) { |
| 376 |
self._expectingUncaughtException = !!_expecting_; |
| 377 |
self.postMessage({ op: "expectUncaughtException", expecting: !!_expecting_ }); |
| 378 |
}; |
| 379 |
|
| 380 |
self._clearAllDatabasesCallback = undefined; |
| 381 |
self.clearAllDatabases = function(_callback_) { |
| 382 |
self._clearAllDatabasesCallback = _callback_; |
| 383 |
self.postMessage({ op: "clearAllDatabases" }); |
| 384 |
} |
| 385 |
|
| 386 |
self.onerror = function(_message_, _file_, _line_) { |
| 387 |
if (self._expectingUncaughtException) { |
| 388 |
self._expectingUncaughtException = false; |
| 389 |
ok(true, "Worker: expected exception [" + _file_ + ":" + _line_ + "]: '" + |
| 390 |
_message_ + "'"); |
| 391 |
return; |
| 392 |
} |
| 393 |
ok(false, |
| 394 |
"Worker: uncaught exception [" + _file_ + ":" + _line_ + "]: '" + |
| 395 |
_message_ + "'"); |
| 396 |
self.finishTest(); |
| 397 |
self.close(); |
| 398 |
return true; |
| 399 |
}; |
| 400 |
|
| 401 |
self.onmessage = function(_event_) { |
| 402 |
let message = _event_.data; |
| 403 |
switch (message.op) { |
| 404 |
case "load": |
| 405 |
info("Worker: loading " + JSON.stringify(message.files)); |
| 406 |
self.importScripts(message.files); |
| 407 |
self.postMessage({ op: "loaded" }); |
| 408 |
break; |
| 409 |
|
| 410 |
case "start": |
| 411 |
executeSoon(function() { |
| 412 |
info("Worker: starting tests"); |
| 413 |
testGenerator.next(); |
| 414 |
}); |
| 415 |
break; |
| 416 |
|
| 417 |
case "clearAllDatabasesDone": |
| 418 |
info("Worker: all databases are cleared"); |
| 419 |
if (self._clearAllDatabasesCallback) { |
| 420 |
self._clearAllDatabasesCallback(); |
| 421 |
} |
| 422 |
break; |
| 423 |
|
| 424 |
default: |
| 425 |
throw new Error("Received a bad message from parent: " + |
| 426 |
JSON.stringify(message)); |
| 427 |
} |
| 428 |
}; |
| 429 |
|
| 430 |
self.postMessage({ op: "ready" }); |
| 431 |
} |