tor-browser

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

test_install_event.html (5215B)


      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 94048 - test install event.</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  function simpleRegister() {
     19    var p = navigator.serviceWorker.register("worker.js", { scope: "./install_event" });
     20    return p;
     21  }
     22 
     23  function nextRegister(reg) {
     24    ok(reg instanceof ServiceWorkerRegistration, "reg should be a ServiceWorkerRegistration");
     25    var p = navigator.serviceWorker.register("install_event_worker.js", { scope: "./install_event" });
     26    return p.then(function(swr) {
     27      ok(reg === swr, "register should resolve to the same registration object");
     28      var update_found_promise = new Promise(function(resolve, reject) {
     29        swr.addEventListener('updatefound', function(e) {
     30          ok(true, "Received onupdatefound");
     31          resolve();
     32        });
     33      });
     34 
     35      var worker_activating = new Promise(function(res, reject) {
     36        ok(swr.installing instanceof ServiceWorker, "There should be an installing worker if promise resolves.");
     37        ok(swr.installing.state == "installing", "Installing worker's state should be 'installing'");
     38        swr.installing.onstatechange = function(e) {
     39          if (e.target.state == "activating") {
     40            e.target.onstatechange = null;
     41            res();
     42          }
     43        }
     44      });
     45 
     46      return Promise.all([update_found_promise, worker_activating]);
     47    }, function(e) {
     48      ok(false, "Unexpected Error in nextRegister! " + e);
     49    });
     50  }
     51 
     52  function installError() {
     53    // Silence worker errors so they don't cause the test to fail.
     54    window.onerror = function(e) {}
     55    return navigator.serviceWorker.register("install_event_error_worker.js", { scope: "./install_event" })
     56      .then(function(swr) {
     57        ok(swr.installing instanceof ServiceWorker, "There should be an installing worker if promise resolves.");
     58        ok(swr.installing.state == "installing", "Installing worker's state should be 'installing'");
     59        return new Promise(function(resolve, reject) {
     60          swr.installing.onstatechange = function(e) {
     61            ok(e.target.state == "redundant", "Installation of worker with error should fail.");
     62            resolve();
     63          }
     64        });
     65      }).then(function() {
     66        return navigator.serviceWorker.getRegistration("./install_event").then(function(swr) {
     67          var newest = swr.waiting || swr.active;
     68          ok(newest, "Waiting or active worker should still exist");
     69          ok(newest.scriptURL.match(/install_event_worker.js$/), "Previous worker should remain the newest worker");
     70        });
     71      });
     72  }
     73 
     74  function testActive(worker) {
     75    is(worker.state, "activating", "Should be activating");
     76    return new Promise(function(resolve, reject) {
     77      worker.onstatechange = function(e) {
     78        e.target.onstatechange = null;
     79        is(e.target.state, "activated", "Activation of worker with error in activate event handler should still succeed.");
     80        resolve();
     81      }
     82    });
     83  }
     84 
     85  function activateErrorShouldSucceed() {
     86    // Silence worker errors so they don't cause the test to fail.
     87    window.onerror = function() { }
     88    return navigator.serviceWorker.register("activate_event_error_worker.js", { scope: "./activate_error" })
     89      .then(function(swr) {
     90        var p = new Promise(function(resolve, reject) {
     91          ok(swr.installing.state == "installing", "activateErrorShouldSucceed(): Installing worker's state should be 'installing'");
     92          swr.installing.onstatechange = function(e) {
     93            e.target.onstatechange = null;
     94            if (swr.waiting) {
     95              swr.waiting.onstatechange = function(event) {
     96                event.target.onstatechange = null;
     97                testActive(swr.active).then(resolve, reject);
     98              }
     99            } else {
    100              testActive(swr.active).then(resolve, reject);
    101            }
    102          }
    103        });
    104 
    105        return p.then(function() {
    106          return Promise.resolve(swr);
    107        });
    108      }).then(function(swr) {
    109        return swr.unregister();
    110      });
    111  }
    112 
    113  function unregister() {
    114    return navigator.serviceWorker.getRegistration("./install_event").then(function(reg) {
    115      return reg.unregister();
    116    });
    117  }
    118 
    119  function runTest() {
    120    Promise.resolve()
    121      .then(simpleRegister)
    122      .then(nextRegister)
    123      .then(installError)
    124      .then(activateErrorShouldSucceed)
    125      .then(unregister)
    126      .then(function() {
    127        SimpleTest.finish();
    128      }).catch(function(e) {
    129        ok(false, "Some test failed with error " + e);
    130        SimpleTest.finish();
    131      });
    132  }
    133 
    134  SimpleTest.waitForExplicitFinish();
    135  SpecialPowers.pushPrefEnv({"set": [
    136    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    137    ["dom.serviceWorkers.enabled", true],
    138    ["dom.serviceWorkers.testing.enabled", true]
    139  ]}, runTest);
    140 </script>
    141 </pre>
    142 </body>
    143 </html>