| <body> |
| <p>Test simple shared worker sharing cases. Should print several PASS lines followed by DONE.</p> |
| <div id=result></div> |
| <script> |
| function log(message) |
| { |
| document.getElementById("result").innerHTML += message + "<br>"; |
| } |
| |
| if (window.testRunner) { |
| testRunner.dumpAsText(); |
| testRunner.waitUntilDone(); |
| } |
| |
| // Load two workers simultaneously, to ensure that simultaneous loads also yield the same instance. |
| // Loading a worker named "name" tests that workers shutdown when the parent document exits, because other tests also create workers with that same name but with different URLs. |
| var worker = new SharedWorker('resources/shared-worker-common.js', 'name'); |
| var worker2 = new SharedWorker('resources/shared-worker-common.js', 'name'); |
| |
| try { |
| new SharedWorker('resources/some-other-url.js', 'name'); |
| log("FAIL: Creating SharedWorker with different URLs but the same name should fail"); |
| } catch (ex) { |
| log("PASS: Exception thrown when creating SharedWorker with different URLs but same name: " + ex); |
| } |
| |
| |
| // Set something in global context in one worker, read value back on other worker, to make sure they are truly shared. |
| worker.port.postMessage("eval self.foo"); |
| worker.port.onmessage = function(event) |
| { |
| log((event.data == "self.foo: undefined" ? "PASS: " : "FAIL: ") + "Accessing new instance of shared worker: " + event.data); |
| worker.port.postMessage("eval self.foo = 1234"); |
| worker.port.onmessage = function(event) |
| { |
| log((event.data == "self.foo = 1234: 1234" ? "PASS: " : "FAIL: ") + "Setting global variable in shared worker: " + event.data); |
| worker2.port.postMessage("eval self.foo"); |
| worker2.port.onmessage = function(event) |
| { |
| log((event.data == "self.foo: 1234" ? "PASS: " : "FAIL: ") + "Accessing simultaneously-loaded instance of shared worker: " + event.data); |
| testNewWorker(); |
| } |
| } |
| } |
| |
| function testNewWorker() |
| { |
| // New name, so should be a distinct worker from the previous one. |
| var worker3 = new SharedWorker('resources/shared-worker-common.js', 'name2'); |
| worker3.port.postMessage("eval self.foo"); |
| worker3.port.onmessage = function(event) |
| { |
| log((event.data == "self.foo: undefined" ? "PASS: " : "FAIL: ") + "Accessing new instance of shared worker: " + event.data); |
| testAlreadyLoaded(); |
| }; |
| } |
| |
| function testAlreadyLoaded() |
| { |
| // Make sure that referencing a worker that is already loaded yields the same instance. |
| var worker4 = new SharedWorker('resources/shared-worker-common.js', 'name'); |
| worker4.port.postMessage("eval self.foo"); |
| worker4.port.onmessage = function(event) |
| { |
| log((event.data == "self.foo: 1234" ? "PASS: " : "FAIL: ") + "Accessing already-loaded instance of shared worker: " + event.data); |
| done(); |
| }; |
| } |
| |
| function done() |
| { |
| log("DONE"); |
| if (window.testRunner) |
| testRunner.notifyDone(); |
| } |
| </script> |
| </body> |