tor-browser

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

test_post_message_advanced.html (3318B)


      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 advanced </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 registration;
     19  var base = ["string", true, 42];
     20  var blob = new Blob(["blob_content"]);
     21  var file = new File(["file_content"], "file");
     22  var obj = { body : "object_content" };
     23 
     24  function readBlob(blobToRead) {
     25    return new Promise(function(resolve, reject) {
     26      var reader = new FileReader();
     27      reader.onloadend = () => resolve(reader.result);
     28      reader.readAsText(blobToRead);
     29    });
     30  }
     31 
     32  function equals(v1, v2) {
     33    return Promise.all([v1, v2]).then(function(val) {
     34      ok(val[0] === val[1], "Values should match.");
     35    });
     36  }
     37 
     38  function blob_equals(b1, b2) {
     39    return equals(readBlob(b1), readBlob(b2));
     40  }
     41 
     42  function file_equals(f1, f2) {
     43    return equals(f1.name, f2.name).then(blob_equals(f1, f2));
     44  }
     45 
     46  function obj_equals(o1, o2) {
     47    return equals(o1.body, o2.body);
     48  }
     49 
     50  function start() {
     51    return navigator.serviceWorker.register("message_posting_worker.js",
     52                                            { scope: "./sw_clients/" })
     53      .then(swr => waitForState(swr.installing, 'activated', swr))
     54      .then((swr) => registration = swr);
     55  }
     56 
     57  function unregister() {
     58    return registration.unregister().then(function(result) {
     59      ok(result, "Unregister should return true.");
     60    }, function(e) {
     61      dump("Unregistering the SW failed with " + e + "\n");
     62    });
     63  }
     64 
     65  function testPostMessageObject(object, test) {
     66    var p = new Promise(function(res, rej) {
     67      window.onmessage = function(e) {
     68        if (e.data === "READY") {
     69          registration.active.postMessage(object)
     70        } else {
     71          test(object, e.data).then(res);
     72        }
     73      }
     74    });
     75 
     76    var content = document.getElementById("content");
     77    ok(content, "Parent exists.");
     78 
     79    iframe = document.createElement("iframe");
     80    iframe.setAttribute('src', "sw_clients/service_worker_controlled.html");
     81    content.appendChild(iframe);
     82 
     83    return p.then(() => content.removeChild(iframe));
     84  }
     85 
     86  function runTest() {
     87    start()
     88      .then(testPostMessageObject.bind(this, base[0], equals))
     89      .then(testPostMessageObject.bind(this, base[1], equals))
     90      .then(testPostMessageObject.bind(this, base[2], equals))
     91      .then(testPostMessageObject.bind(this, blob, blob_equals))
     92      .then(testPostMessageObject.bind(this, file, file_equals))
     93      .then(testPostMessageObject.bind(this, obj, obj_equals))
     94      .then(unregister)
     95      .catch(function(e) {
     96        ok(false, "Some test failed with error " + e);
     97      }).then(SimpleTest.finish);
     98  }
     99 
    100  SimpleTest.waitForExplicitFinish();
    101  SpecialPowers.pushPrefEnv({"set": [
    102    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    103    ["dom.serviceWorkers.enabled", true],
    104    ["dom.serviceWorkers.testing.enabled", true]
    105  ]}, runTest);
    106 </script>
    107 </pre>
    108 </body>
    109 </html>