Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <!-- |
michael@0 | 4 | https://bugzilla.mozilla.org/show_bug.cgi?id=912456 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <meta charset="utf-8"> |
michael@0 | 8 | <title>Test for Bug 912456 - port cloning</title> |
michael@0 | 9 | <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 10 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 11 | </head> |
michael@0 | 12 | <body> |
michael@0 | 13 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=912456">Mozilla Bug 912456</a> |
michael@0 | 14 | <script type="application/javascript"> |
michael@0 | 15 | |
michael@0 | 16 | function testTransfer() { |
michael@0 | 17 | var a = new MessageChannel(); |
michael@0 | 18 | ok(a, "MessageChannel created"); |
michael@0 | 19 | |
michael@0 | 20 | window.addEventListener('message', receiveMessage, false); |
michael@0 | 21 | function receiveMessage(evt) { |
michael@0 | 22 | ok(evt.data.port, "Port has been received!"); |
michael@0 | 23 | |
michael@0 | 24 | var a = new MessageChannel(); |
michael@0 | 25 | ok(a, "MessageChannel created"); |
michael@0 | 26 | |
michael@0 | 27 | try { |
michael@0 | 28 | evt.data.port.postMessage({port: a.port2}); |
michael@0 | 29 | ok(false, "PostMessage should throw! - no transfered port"); |
michael@0 | 30 | } catch(e) { |
michael@0 | 31 | ok(true, "PostMessage should throw! - no transfered port"); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | try { |
michael@0 | 35 | evt.data.port.postMessage({port: a.port2}, [a.port2, a.port2]); |
michael@0 | 36 | ok(false, "PostMessage should throw - no duplicate!"); |
michael@0 | 37 | } catch(e) { |
michael@0 | 38 | ok(true, "PostMessage should throw - no duplicate!"); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | evt.data.port.postMessage({port: a.port2}, [a.port2]); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | a.port1.onmessage = function(evt) { |
michael@0 | 45 | ok(evt.data.port, "Port has been received!"); |
michael@0 | 46 | window.removeEventListener('message', receiveMessage); |
michael@0 | 47 | runTest(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | try { |
michael@0 | 51 | postMessage({ port: a.port2}, 42, '*'); |
michael@0 | 52 | ok(false, "PostMessage should throw! - no transfered port"); |
michael@0 | 53 | } catch(e) { |
michael@0 | 54 | ok(true, "PostMessage should throw! - no transfered port"); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | try { |
michael@0 | 58 | postMessage({ port: a.port2}, 42, '*', [a.port2, a.port2]); |
michael@0 | 59 | ok(false, "PostMessage should throw - no duplicate!"); |
michael@0 | 60 | } catch(e) { |
michael@0 | 61 | ok(true, "PostMessage should throw - no duplicate!"); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | postMessage({port: a.port2}, '*', [a.port2]); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function testPorts() { |
michael@0 | 68 | var a = new MessageChannel(); |
michael@0 | 69 | ok(a, "MessageChannel created"); |
michael@0 | 70 | |
michael@0 | 71 | window.addEventListener('message', receiveMessage, false); |
michael@0 | 72 | function receiveMessage(evt) { |
michael@0 | 73 | ok(evt.data, "Data is 42"); |
michael@0 | 74 | ok(evt.ports, "Port is received"); |
michael@0 | 75 | is(evt.ports.length, 1, "Ports.length is 1"); |
michael@0 | 76 | |
michael@0 | 77 | var a = new MessageChannel(); |
michael@0 | 78 | ok(a, "MessageChannel created"); |
michael@0 | 79 | |
michael@0 | 80 | evt.ports[0].postMessage(42, [a.port2]); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | a.port1.onmessage = function(evt) { |
michael@0 | 84 | ok(evt.data, "Data is 42"); |
michael@0 | 85 | ok(evt.ports, "Port is received"); |
michael@0 | 86 | is(evt.ports.length, 1, "Ports.length is 1"); |
michael@0 | 87 | window.removeEventListener('message', receiveMessage); |
michael@0 | 88 | runTest(); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | postMessage(42, '*', [a.port2]); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | var tests = [ |
michael@0 | 95 | testTransfer, |
michael@0 | 96 | testPorts |
michael@0 | 97 | ]; |
michael@0 | 98 | |
michael@0 | 99 | function runTest() { |
michael@0 | 100 | if (!tests.length) { |
michael@0 | 101 | SimpleTest.finish(); |
michael@0 | 102 | return; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | var test = tests.shift(); |
michael@0 | 106 | test(); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 110 | SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTest); |
michael@0 | 111 | |
michael@0 | 112 | </script> |
michael@0 | 113 | </body> |
michael@0 | 114 | </html> |
michael@0 | 115 |