tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_post_message.html (2372B)


      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 src="utils.js"></script>
     17 <script class="testbody" type="text/javascript">
     18  var magic_value = "MAGIC_VALUE_123";
     19  var registration;
     20  function start() {
     21    return navigator.serviceWorker.register("message_posting_worker.js",
     22                                            { scope: "./sw_clients/" })
     23      .then(swr => waitForState(swr.installing, 'activated', swr))
     24      .then((swr) => registration = swr);
     25  }
     26 
     27  function unregister() {
     28    return registration.unregister().then(function(result) {
     29      ok(result, "Unregister should return true.");
     30    }, function(e) {
     31      dump("Unregistering the SW failed with " + e + "\n");
     32    });
     33  }
     34 
     35 
     36  function testPostMessage(swr) {
     37    var p = new Promise(function(res, rej) {
     38      window.onmessage = function(e) {
     39        if (e.data === "READY") {
     40          swr.active.postMessage(magic_value);
     41        } else if (e.data === magic_value) {
     42          ok(true, "Worker posted the correct value.");
     43          res();
     44        } else {
     45          ok(false, "Wrong value. Expected: " + magic_value +
     46                    ", got: " + e.data);
     47          res();
     48        }
     49      }
     50    });
     51 
     52    var content = document.getElementById("content");
     53    ok(content, "Parent exists.");
     54 
     55    iframe = document.createElement("iframe");
     56    iframe.setAttribute('src', "sw_clients/service_worker_controlled.html");
     57    content.appendChild(iframe);
     58 
     59    return p.then(() => content.removeChild(iframe));
     60  }
     61 
     62  function runTest() {
     63    start()
     64      .then(testPostMessage)
     65      .then(unregister)
     66      .catch(function(e) {
     67        ok(false, "Some test failed with error " + e);
     68      }).then(SimpleTest.finish);
     69  }
     70 
     71  SimpleTest.waitForExplicitFinish();
     72  SpecialPowers.pushPrefEnv({"set": [
     73    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
     74    ["dom.serviceWorkers.enabled", true],
     75    ["dom.serviceWorkers.testing.enabled", true]
     76  ]}, runTest);
     77 </script>
     78 </pre>
     79 </body>
     80 </html>