Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | <!-- |
michael@0 | 2 | Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | --> |
michael@0 | 5 | <!DOCTYPE HTML> |
michael@0 | 6 | <html> |
michael@0 | 7 | <head> |
michael@0 | 8 | <title>Test for SharedWorker</title> |
michael@0 | 9 | <script src="/tests/SimpleTest/SimpleTest.js"> |
michael@0 | 10 | </script> |
michael@0 | 11 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"> |
michael@0 | 12 | </head> |
michael@0 | 13 | <body> |
michael@0 | 14 | <p id="display"></p> |
michael@0 | 15 | <div id="content" style="display: none"></div> |
michael@0 | 16 | <pre id="test"> |
michael@0 | 17 | <script class="testbody"> |
michael@0 | 18 | "use strict"; |
michael@0 | 19 | |
michael@0 | 20 | const swPref = "dom.workers.sharedWorkers.enabled"; |
michael@0 | 21 | |
michael@0 | 22 | const href = window.location.href; |
michael@0 | 23 | const filename = "sharedWorker_sharedWorker.js"; |
michael@0 | 24 | const sentMessage = "ping"; |
michael@0 | 25 | const errorFilename = href.substring(0, href.lastIndexOf("/") + 1) + |
michael@0 | 26 | filename; |
michael@0 | 27 | const errorLine = 86; |
michael@0 | 28 | const errorColumn = 0; |
michael@0 | 29 | |
michael@0 | 30 | if (!SpecialPowers.getBoolPref(swPref)) { |
michael@0 | 31 | ok(!("SharedWorker" in window), "No SharedWorker without pref set"); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | SpecialPowers.pushPrefEnv({ set: [[swPref, true]] }, function() { |
michael@0 | 35 | var worker = new SharedWorker(filename); |
michael@0 | 36 | |
michael@0 | 37 | ok(worker instanceof SharedWorker, "Got SharedWorker instance"); |
michael@0 | 38 | ok(!("postMessage" in worker), "SharedWorker has no 'postMessage'"); |
michael@0 | 39 | ok(worker.port instanceof MessagePort, |
michael@0 | 40 | "Shared worker has MessagePort"); |
michael@0 | 41 | |
michael@0 | 42 | var receivedMessage; |
michael@0 | 43 | var receivedError; |
michael@0 | 44 | |
michael@0 | 45 | worker.port.onmessage = function(event) { |
michael@0 | 46 | ok(event instanceof MessageEvent, "Got a MessageEvent"); |
michael@0 | 47 | ok(event.target === worker.port, |
michael@0 | 48 | "MessageEvent has correct 'target' property"); |
michael@0 | 49 | is(event.data, sentMessage, "Got correct message"); |
michael@0 | 50 | ok(receivedMessage === undefined, "Haven't gotten message yet"); |
michael@0 | 51 | ok(receivedError === undefined, "Haven't gotten error yet"); |
michael@0 | 52 | receivedMessage = event.data; |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | worker.onerror = function(event) { |
michael@0 | 56 | ok(event instanceof ErrorEvent, "Got an ErrorEvent"); |
michael@0 | 57 | is(event.message, "Error: " + sentMessage, "Got correct error"); |
michael@0 | 58 | is(event.filename, errorFilename, "Got correct filename"); |
michael@0 | 59 | is(event.lineno, errorLine, "Got correct lineno"); |
michael@0 | 60 | is(event.colno, errorColumn, "Got correct column"); |
michael@0 | 61 | ok(receivedMessage !== undefined, "Got message already"); |
michael@0 | 62 | ok(receivedError === undefined, "Haven't gotten error yet"); |
michael@0 | 63 | receivedError = event.message; |
michael@0 | 64 | event.preventDefault(); |
michael@0 | 65 | SimpleTest.finish(); |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | worker.port.postMessage(sentMessage); |
michael@0 | 69 | }); |
michael@0 | 70 | |
michael@0 | 71 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 72 | |
michael@0 | 73 | </script> |
michael@0 | 74 | </pre> |
michael@0 | 75 | </body> |
michael@0 | 76 | </html> |