tor-browser

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

test_force_refresh.html (3443B)


      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  /**
     18   *
     19   */
     20  let iframe;
     21  let registration;
     22 
     23  function start() {
     24    return new Promise(resolve => {
     25      const content = document.getElementById("content");
     26      ok(content, "Parent exists.");
     27 
     28      iframe = document.createElement("iframe");
     29      iframe.setAttribute("src", "sw_clients/refresher_compressed.html");
     30 
     31      /*
     32       * The initial iframe must be the _uncached_ version, which means its
     33       * load must happen before the Service Worker's `activate` event.
     34       * Rather than `waitUntil`-ing the Service Worker's `install` event
     35       * until the load finishes (more concurrency, but involves coordinating
     36       * `postMessage`s), just ensure the load finishes before registering
     37       * the Service Worker (which is simpler).
     38       */
     39      iframe.onload = resolve;
     40 
     41      content.appendChild(iframe);
     42    }).then(async () => {
     43      /*
     44       * There's no need _here_ to explicitly wait for this Service Worker to be
     45       * "activated"; this test will progress when the "READY"/"READY_CACHED"
     46       * messages are received from the iframe, and the iframe will only send
     47       * those messages once the Service Worker is "activated" (by chaining on
     48       * its `navigator.serviceWorker.ready` promise).
     49       */
     50      registration = await navigator.serviceWorker.register(
     51        "force_refresh_worker.js", { scope: "./sw_clients/" });
     52    });
     53  }
     54 
     55  function unregister() {
     56    return registration.unregister().then(function(result) {
     57      ok(result, "Unregister should return true.");
     58    }, function(e) {
     59      dump("Unregistering the SW failed with " + e + "\n");
     60    });
     61  }
     62 
     63  function testForceRefresh(swr) {
     64    return new Promise(function(res, rej) {
     65      var count = 0;
     66      var cachedCount = 0;
     67      window.onmessage = function(e) {
     68        if (e.data === "READY") {
     69          count += 1;
     70          if (count == 2) {
     71            is(cachedCount, 1, "should have received cached message before " +
     72                               "second non-cached message");
     73            res();
     74          }
     75          iframe.contentWindow.postMessage("REFRESH", "*");
     76        } else if (e.data === "READY_CACHED") {
     77          cachedCount += 1;
     78          is(count, 1, "should have received non-cached message before " +
     79                       "cached message");
     80          iframe.contentWindow.postMessage("FORCE_REFRESH", "*");
     81        }
     82      }
     83    }).then(() => document.getElementById("content").removeChild(iframe));
     84  }
     85 
     86  function runTest() {
     87    start()
     88      .then(testForceRefresh)
     89      .then(unregister)
     90      .catch(function(e) {
     91        ok(false, "Some test failed with error " + e);
     92      }).then(SimpleTest.finish);
     93  }
     94 
     95  SimpleTest.waitForExplicitFinish();
     96  SpecialPowers.pushPrefEnv({"set": [
     97    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
     98    ["dom.serviceWorkers.enabled", true],
     99    ["dom.serviceWorkers.testing.enabled", true],
    100  ]}, runTest);
    101 </script>
    102 </pre>
    103 </body>
    104 </html>