1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
function makeWorkerUrl(runner) {
return "data:application/javascript;charset=utf-8," + encodeURI("let run=" + runner.toSource()) + ";run();"
}
var getFrameWorkerHandle;
function test() {
waitForExplicitFinish();
let scope = {};
Cu.import("resource://gre/modules/FrameWorker.jsm", scope);
getFrameWorkerHandle = scope.getFrameWorkerHandle;
runTests(tests);
}
let tests = {
testSimple: function(cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
port.onmessage = function(e) {
if (e.data.topic == "ping") {
port.postMessage({topic: "pong"});
}
}
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSimple");
isnot(worker._worker.frame.contentWindow.toString(), "[object ChromeWindow]", "worker window isn't a chrome window");
worker.port.onmessage = function(e) {
if (e.data.topic == "pong") {
worker.terminate();
cbnext();
}
}
worker.port.postMessage({topic: "ping"})
},
// when the client closes early but the worker tries to send anyway...
testEarlyClose: function(cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
port.postMessage({topic: "oh hai"});
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testEarlyClose");
worker.port.close();
worker.terminate();
cbnext();
},
// Check we do get a social.port-closing message as the port is closed.
testPortClosingMessage: function(cbnext) {
// We use 2 ports - we close the first and report success via the second.
let run = function() {
let firstPort, secondPort;
onconnect = function(e) {
let port = e.ports[0];
if (firstPort === undefined) {
firstPort = port;
port.onmessage = function(e) {
if (e.data.topic == "social.port-closing") {
secondPort.postMessage({topic: "got-closing"});
}
}
} else {
secondPort = port;
// now both ports are connected we can trigger the client side
// closing the first.
secondPort.postMessage({topic: "connected"});
}
}
}
let workerurl = makeWorkerUrl(run);
let worker1 = getFrameWorkerHandle(workerurl, undefined, "testPortClosingMessage worker1");
let worker2 = getFrameWorkerHandle(workerurl, undefined, "testPortClosingMessage worker2");
worker2.port.onmessage = function(e) {
if (e.data.topic == "connected") {
// both ports connected, so close the first.
worker1.port.close();
} else if (e.data.topic == "got-closing") {
worker2.terminate();
cbnext();
}
}
},
// Tests that prototypes added to core objects work with data sent over
// the message ports.
testPrototypes: function(cbnext) {
let run = function() {
// Modify the Array prototype...
Array.prototype.customfunction = function() {};
onconnect = function(e) {
let port = e.ports[0];
port.onmessage = function(e) {
// Check the data we get via the port has the prototype modification
if (e.data.topic == "hello" && e.data.data.customfunction) {
port.postMessage({topic: "hello", data: [1,2,3]});
}
}
}
}
// hrmph - this kinda sucks as it is really just testing the actual
// implementation rather than the end result, but is OK for now.
// Really we are just testing that JSON.parse in the client window
// is called.
let fakeWindow = {
JSON: {
parse: function(s) {
let data = JSON.parse(s);
data.data.somextrafunction = function() {};
return data;
}
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), fakeWindow, "testPrototypes");
worker.port.onmessage = function(e) {
if (e.data.topic == "hello" && e.data.data.somextrafunction) {
worker.terminate();
cbnext();
}
}
worker.port.postMessage({topic: "hello", data: [1,2,3]});
},
testArrayUsingBuffer: function(cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
port.onmessage = function(e) {
if (e.data.topic == "go") {
let buffer = new ArrayBuffer(10);
// this one has always worked in the past, but worth checking anyway...
if (new Uint8Array(buffer).length != 10) {
port.postMessage({topic: "result", reason: "first length was not 10"});
return;
}
let reader = new FileReader();
reader.onload = function(event) {
if (new Uint8Array(buffer).length != 10) {
port.postMessage({topic: "result", reason: "length in onload handler was not 10"});
return;
}
// all seems good!
port.postMessage({topic: "result", reason: "ok"});
}
let blob = new Blob([buffer], {type: "binary"});
reader.readAsArrayBuffer(blob);
}
}
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testArray");
worker.port.onmessage = function(e) {
if (e.data.topic == "result") {
is(e.data.reason, "ok", "check the array worked");
worker.terminate();
cbnext();
}
}
worker.port.postMessage({topic: "go"});
},
testArrayUsingReader: function(cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
port.onmessage = function(e) {
if (e.data.topic == "go") {
let buffer = new ArrayBuffer(10);
let reader = new FileReader();
reader.onload = function(event) {
try {
if (new Uint8Array(reader.result).length != 10) {
port.postMessage({topic: "result", reason: "length in onload handler was not 10"});
return;
}
// all seems good!
port.postMessage({topic: "result", reason: "ok"});
} catch (ex) {
port.postMessage({topic: "result", reason: ex.toString()});
}
}
let blob = new Blob([buffer], {type: "binary"});
reader.readAsArrayBuffer(blob);
}
}
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testArray");
worker.port.onmessage = function(e) {
if (e.data.topic == "result") {
is(e.data.reason, "ok", "check the array worked");
worker.terminate();
cbnext();
}
}
worker.port.postMessage({topic: "go"});
},
testXHR: function(cbnext) {
// NOTE: this url MUST be in the same origin as worker_xhr.js fetches from!
let url = "https://example.com/browser/toolkit/components/social/test/browser/worker_xhr.js";
let worker = getFrameWorkerHandle(url, undefined, "testXHR");
worker.port.onmessage = function(e) {
if (e.data.topic == "done") {
is(e.data.result, "ok", "check the xhr test worked");
worker.terminate();
cbnext();
}
}
},
testLocalStorage: function(cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
try {
localStorage.setItem("foo", "1");
} catch(e) {
port.postMessage({topic: "done", result: "FAILED to set localStorage, " + e.toString() });
return;
}
var ok;
try {
ok = localStorage["foo"] == 1;
} catch (e) {
port.postMessage({topic: "done", result: "FAILED to read localStorage, " + e.toString() });
return;
}
port.postMessage({topic: "done", result: "ok"});
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testLocalStorage");
worker.port.onmessage = function(e) {
if (e.data.topic == "done") {
is(e.data.result, "ok", "check the localStorage test worked");
worker.terminate();
cbnext();
}
}
},
testBase64: function (cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
var ok = false;
try {
ok = btoa("1234") == "MTIzNA==";
} catch(e) {
port.postMessage({topic: "done", result: "FAILED to call btoa, " + e.toString() });
return;
}
if (!ok) {
port.postMessage({topic: "done", result: "FAILED calling btoa"});
return;
}
try {
ok = atob("NDMyMQ==") == "4321";
} catch (e) {
port.postMessage({topic: "done", result: "FAILED to call atob, " + e.toString() });
return;
}
if (!ok) {
port.postMessage({topic: "done", result: "FAILED calling atob"});
return;
}
port.postMessage({topic: "done", result: "ok"});
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testBase64");
worker.port.onmessage = function(e) {
if (e.data.topic == "done") {
is(e.data.result, "ok", "check the atob/btoa test worked");
worker.terminate();
cbnext();
}
}
},
testTimeouts: function (cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
var timeout;
try {
timeout = setTimeout(function () {
port.postMessage({topic: "done", result: "FAILED cancelled timeout was called"});
}, 100);
} catch (ex) {
port.postMessage({topic: "done", result: "FAILED calling setTimeout: " + ex});
return;
}
try {
clearTimeout(timeout);
} catch (ex) {
port.postMessage({topic: "done", result: "FAILED calling clearTimeout: " + ex});
return;
}
var counter = 0;
try {
timeout = setInterval(function () {
if (++counter == 2) {
clearInterval(timeout);
setTimeout(function () {
port.postMessage({topic: "done", result: "ok"});
return;
}, 0);
}
}, 100);
} catch (ex) {
port.postMessage({topic: "done", result: "FAILED calling setInterval: " + ex});
return;
}
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testTimeouts");
worker.port.onmessage = function(e) {
if (e.data.topic == "done") {
is(e.data.result, "ok", "check that timeouts worked");
worker.terminate();
cbnext();
}
}
},
testWebSocket: function (cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
try {
var exampleSocket = new WebSocket("ws://mochi.test:8888/socketserver");
} catch (e) {
port.postMessage({topic: "done", result: "FAILED calling WebSocket constructor: " + e});
return;
}
port.postMessage({topic: "done", result: "ok"});
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testWebSocket");
worker.port.onmessage = function(e) {
if (e.data.topic == "done") {
is(e.data.result, "ok", "check that websockets worked");
worker.terminate();
cbnext();
}
}
},
testSameOriginImport: function(cbnext) {
let run = function() {
onconnect = function(e) {
let port = e.ports[0];
port.onmessage = function(e) {
if (e.data.topic == "ping") {
try {
importScripts("http://foo.bar/error");
} catch(ex) {
port.postMessage({topic: "pong", data: ex});
return;
}
port.postMessage({topic: "pong", data: null});
}
}
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSameOriginImport");
worker.port.onmessage = function(e) {
if (e.data.topic == "pong") {
isnot(e.data.data, null, "check same-origin applied to importScripts");
worker.terminate();
cbnext();
}
}
worker.port.postMessage({topic: "ping"})
},
testRelativeImport: function(cbnext) {
let url = "https://example.com/browser/toolkit/components/social/test/browser/worker_relative.js";
let worker = getFrameWorkerHandle(url, undefined, "testSameOriginImport");
worker.port.onmessage = function(e) {
if (e.data.topic == "done") {
is(e.data.result, "ok", "check relative url in importScripts");
worker.terminate();
cbnext();
}
}
},
testNavigator: function(cbnext) {
let run = function() {
let port;
ononline = function() {
port.postMessage({topic: "ononline", data: navigator.onLine});
}
onoffline = function() {
port.postMessage({topic: "onoffline", data: navigator.onLine});
}
onconnect = function(e) {
port = e.ports[0];
port.postMessage({topic: "ready",
data: {
appName: navigator.appName,
appVersion: navigator.appVersion,
platform: navigator.platform,
userAgent: navigator.userAgent,
}
});
}
}
let ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService2);
let oldManage = ioService.manageOfflineStatus;
let oldOffline = ioService.offline;
ioService.manageOfflineStatus = false;
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testNavigator");
let expected_topic = "onoffline";
let expected_data = false;
worker.port.onmessage = function(e) {
is(e.data.topic, "ready");
for each (let attr in ['appName', 'appVersion', 'platform', 'userAgent']) {
// each attribute must be a string with length > 0.
is(typeof e.data.data[attr], "string");
ok(e.data.data[attr].length > 0);
}
worker.port.onmessage = function(e) {
// a handler specifically for the offline notification.
is(e.data.topic, "onoffline");
is(e.data.data, false);
// add another handler specifically for the 'online' case.
worker.port.onmessage = function(e) {
is(e.data.topic, "ononline");
is(e.data.data, true);
// all good!
ioService.manageOfflineStatus = oldManage;
ioService.offline = oldOffline;
worker.terminate();
cbnext();
}
ioService.offline = false;
}
ioService.offline = true;
}
},
testMissingWorker: function(cbnext) {
// don't ever create this file! We want a 404.
let url = "https://example.com/browser/toolkit/components/social/test/browser/worker_is_missing.js";
let worker = getFrameWorkerHandle(url, undefined, "testMissingWorker");
Services.obs.addObserver(function handleError(subj, topic, data) {
Services.obs.removeObserver(handleError, "social:frameworker-error");
is(data, worker._worker.origin, "social:frameworker-error was handled");
worker.terminate();
cbnext();
}, 'social:frameworker-error', false);
worker.port.onmessage = function(e) {
ok(false, "social:frameworker-error was handled");
cbnext();
}
},
testNoConnectWorker: function(cbnext) {
let worker = getFrameWorkerHandle(makeWorkerUrl(function () {}),
undefined, "testNoConnectWorker");
Services.obs.addObserver(function handleError(subj, topic, data) {
Services.obs.removeObserver(handleError, "social:frameworker-error");
is(data, worker._worker.origin, "social:frameworker-error was handled");
worker.terminate();
cbnext();
}, 'social:frameworker-error', false);
worker.port.onmessage = function(e) {
ok(false, "social:frameworker-error was handled");
cbnext();
}
},
testEmptyWorker: function(cbnext) {
let worker = getFrameWorkerHandle("data:application/javascript;charset=utf-8,",
undefined, "testEmptyWorker");
Services.obs.addObserver(function handleError(subj, topic, data) {
Services.obs.removeObserver(handleError, "social:frameworker-error");
is(data, worker._worker.origin, "social:frameworker-error was handled");
worker.terminate();
cbnext();
}, 'social:frameworker-error', false);
worker.port.onmessage = function(e) {
ok(false, "social:frameworker-error was handled");
cbnext();
}
},
testWorkerConnectError: function(cbnext) {
let run = function () {
onconnect = function(e) {
throw new Error("worker failure");
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run),
undefined, "testWorkerConnectError");
Services.obs.addObserver(function handleError(subj, topic, data) {
Services.obs.removeObserver(handleError, "social:frameworker-error");
is(data, worker._worker.origin, "social:frameworker-error was handled");
worker.terminate();
cbnext();
}, 'social:frameworker-error', false);
worker.port.onmessage = function(e) {
ok(false, "social:frameworker-error was handled");
cbnext();
}
},
testReloadAndNewPort: function(cbnext) {
let run = function () {
onconnect = function(e) {
e.ports[0].postMessage({topic: "ready"});
}
}
let doneReload = false;
let worker = getFrameWorkerHandle(makeWorkerUrl(run),
undefined, "testReloadAndNewPort");
worker.port.onmessage = function(e) {
if (e.data.topic == "ready" && !doneReload) {
// do the "reload"
doneReload = true;
worker._worker.reload();
let worker2 = getFrameWorkerHandle(makeWorkerUrl(run),
undefined, "testReloadAndNewPort");
worker2.port.onmessage = function(e) {
if (e.data.topic == "ready") {
// "worker" and "worker2" are handles to the same worker
worker2.terminate();
cbnext();
}
}
}
}
},
// This will create the worker, then send a message to the port, then close
// the port - all before the worker has actually initialized.
testCloseFirstSend: function(cbnext) {
let run = function() {
let numPings = 0, numCloses = 0;
onconnect = function(e) {
let port = e.ports[0];
port.onmessage = function(e) {
if (e.data.topic == "ping") {
numPings += 1;
} else if (e.data.topic == "social.port-closing") {
numCloses += 1;
} else if (e.data.topic == "get-counts") {
port.postMessage({topic: "result",
result: {ping: numPings, close: numCloses}});
}
}
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSendAndClose");
worker.port.postMessage({topic: "ping"});
worker.port.close();
let newPort = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSendAndClose").port;
newPort.onmessage = function(e) {
if (e.data.topic == "result") {
is(e.data.result.ping, 1, "the worker got the ping");
is(e.data.result.close, 1, "the worker got 1 close message");
worker.terminate();
cbnext();
}
}
newPort.postMessage({topic: "get-counts"});
},
// Like testCloseFirstSend, although in this test the worker has already
// initialized (so the "connect pending ports" part of the worker isn't
// what needs to handle this case.)
testCloseAfterInit: function(cbnext) {
let run = function() {
let numPings = 0, numCloses = 0;
onconnect = function(e) {
let port = e.ports[0];
port.onmessage = function(e) {
if (e.data.topic == "ping") {
numPings += 1;
} else if (e.data.topic == "social.port-closing") {
numCloses += 1;
} else if (e.data.topic == "get-counts") {
port.postMessage({topic: "result",
result: {ping: numPings, close: numCloses}});
} else if (e.data.topic == "get-ready") {
port.postMessage({topic: "ready"});
}
}
}
}
let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSendAndClose");
worker.port.onmessage = function(e) {
if (e.data.topic == "ready") {
let newPort = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSendAndClose").port;
newPort.postMessage({topic: "ping"});
newPort.close();
worker.port.postMessage({topic: "get-counts"});
} else if (e.data.topic == "result") {
is(e.data.result.ping, 1, "the worker got the ping");
is(e.data.result.close, 1, "the worker got 1 close message");
worker.terminate();
cbnext();
}
}
worker.port.postMessage({topic: "get-ready"});
},
}
|