test_opaque_intercept.html (2992B)
1 <!-- 2 Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ 4 --> 5 <!DOCTYPE HTML> 6 <html> 7 <head> 8 <title>Bug 982726 - Test service worker post message </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 <p id="display"></p> 14 <div id="content" style="display: none"></div> 15 <pre id="test"></pre> 16 <script class="testbody" type="text/javascript"> 17 var registration; 18 function start() { 19 return navigator.serviceWorker.register("opaque_intercept_worker.js", 20 { scope: "./sw_clients/" }) 21 .then((swr) => registration = swr); 22 } 23 24 function unregister() { 25 return registration.unregister().then(function(result) { 26 ok(result, "Unregister should return true."); 27 }, function(e) { 28 dump("Unregistering the SW failed with " + e + "\n"); 29 }); 30 } 31 32 33 function testOpaqueIntercept(swr) { 34 var p = new Promise(function(res, rej) { 35 var ready = false; 36 var scriptLoaded = false; 37 window.onmessage = function(e) { 38 if (e.data === "READY") { 39 ok(!ready, "ready message should only be received once"); 40 ok(!scriptLoaded, "ready message should be received before script loaded"); 41 if (ready) { 42 res(); 43 return; 44 } 45 ready = true; 46 iframe.contentWindow.postMessage("REFRESH", "*"); 47 } else if (e.data === "SCRIPT_LOADED") { 48 ok(ready, "script loaded should be received after ready"); 49 ok(!scriptLoaded, "script loaded message should be received only once"); 50 scriptLoaded = true; 51 res(); 52 } 53 } 54 }); 55 56 var content = document.getElementById("content"); 57 ok(content, "Parent exists."); 58 59 var iframe = document.createElement("iframe"); 60 iframe.setAttribute('src', "sw_clients/refresher.html"); 61 content.appendChild(iframe); 62 63 // Our service worker waits for us to finish installing. If it didn't do 64 // this, then loading our frame would race with it becoming active, 65 // possibly intercepting the first load of the iframe. This guarantees 66 // that our iframe will load first directly from the network. Note that 67 // refresher.html explicitly waits for the service worker to transition to 68 // active. 69 registration.installing.postMessage("ready"); 70 71 return p.then(() => content.removeChild(iframe)); 72 } 73 74 function runTest() { 75 start() 76 .then(testOpaqueIntercept) 77 .then(unregister) 78 .catch(function(e) { 79 ok(false, "Some test failed with error " + e); 80 }).then(SimpleTest.finish); 81 } 82 83 SimpleTest.waitForExplicitFinish(); 84 SpecialPowers.pushPrefEnv({"set": [ 85 ["dom.serviceWorkers.exemptFromPerDomainMax", true], 86 ["dom.serviceWorkers.enabled", true], 87 ["dom.serviceWorkers.testing.enabled", true], 88 ]}, runTest); 89 </script> 90 </pre> 91 </body> 92 </html>