test_messageChannel_bug1224825.html (2086B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1224825 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 1224825</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 </head> 12 <body> 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1224825">Mozilla Bug 1224825</a> 14 <div id="content"></div> 15 <pre id="test"> 16 </pre> 17 <script type="application/javascript"> 18 19 var MAX = 100; 20 21 function test_fullDeliveredMessages() { 22 var worker = new Worker('data:javascript,onmessage = function(e) { e.ports[0].onmessage = function(evt) { postMessage(evt.data);}}'); 23 24 var count = 0; 25 worker.onmessage = function(e) { 26 is(e.data, count, "Correct value expected!"); 27 ok(count < MAX,"No count > MAX messages!"); 28 if (++count == MAX) { 29 30 SimpleTest.requestFlakyTimeout("Testing an event not happening"); 31 setTimeout(function() { 32 runTests(); 33 }, 200); 34 35 info("All the messages correctly received"); 36 } 37 } 38 39 var mc = new MessageChannel(); 40 worker.postMessage(42, [mc.port2]); 41 42 for (let i = 0; i < MAX; ++i) { 43 mc.port1.postMessage(i); 44 } 45 46 mc.port1.close(); 47 48 for (let i = 0; i < MAX * 2; ++i) { 49 mc.port1.postMessage(i); 50 } 51 } 52 53 function test_closeInBetween() { 54 var mc = new MessageChannel(); 55 56 for (var i = 0; i < MAX; ++i) { 57 mc.port1.postMessage(i); 58 } 59 60 mc.port1.onmessage = function(e) { 61 ok (e.data < MAX/2, "Correct message received from port1:" + e.data); 62 } 63 64 mc.port2.onmessage = function(e) { 65 ok (e.data < MAX, "Correct message received from port2:" + e.data); 66 if (e.data == MAX/2) { 67 mc.port2.close(); 68 } 69 70 mc.port2.postMessage(e.data); 71 72 if (e.data == MAX - 1) { 73 runTests(); 74 } 75 } 76 } 77 78 var tests = [ test_fullDeliveredMessages, test_closeInBetween ]; 79 80 function runTests() { 81 if (!tests.length) { 82 SimpleTest.finish(); 83 return; 84 } 85 86 var test = tests.shift(); 87 test(); 88 } 89 90 SimpleTest.waitForExplicitFinish(); 91 runTests(); 92 </script> 93 </body> 94 </html>