tor-browser

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

test_serviceworkerinfo.xhtml (3958B)


      1 <?xml version="1.0"?>
      2 <!--
      3  Any copyright is dedicated to the Public Domain.
      4  http://creativecommons.org/publicdomain/zero/1.0/
      5 -->
      6 <window title="Test for ServiceWorkerInfo"
      7        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
      8        onload="test();">
      9  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
     10  <script type="application/javascript" src="chrome_helpers.js"/>
     11  <script type="application/javascript">
     12  <![CDATA[
     13 
     14    let IFRAME_URL = EXAMPLE_URL + "serviceworkerinfo_iframe.html";
     15 
     16    function wait_for_active_worker(registration) {
     17      ok(registration, "Registration is valid.");
     18      return new Promise(function(res, rej) {
     19        if (registration.activeWorker) {
     20          res(registration);
     21          return;
     22        }
     23        let listener = {
     24          onChange() {
     25            if (registration.activeWorker) {
     26              registration.removeListener(listener);
     27              res(registration);
     28            }
     29          }
     30        }
     31        registration.addListener(listener);
     32      });
     33    }
     34 
     35    function test() {
     36      SimpleTest.waitForExplicitFinish();
     37 
     38      SpecialPowers.pushPrefEnv({'set': [
     39        ["dom.serviceWorkers.enabled", true],
     40        ["dom.serviceWorkers.idle_extended_timeout", 1000000],
     41        ["dom.serviceWorkers.idle_timeout", 0],
     42        ["dom.serviceWorkers.testing.enabled", true],
     43      ]}, function () {
     44        (async function() {
     45          let iframe = $("iframe");
     46          let promise = new Promise(function (resolve) {
     47            iframe.onload = function () {
     48              resolve();
     49            };
     50          });
     51          iframe.src = IFRAME_URL;
     52          await promise;
     53 
     54          info("Check that a service worker eventually shuts down.");
     55          promise = Promise.all([
     56            waitForRegister(EXAMPLE_URL),
     57            waitForServiceWorkerShutdown()
     58          ]);
     59          iframe.contentWindow.postMessage("register", "*");
     60          let [registration] = await promise;
     61 
     62          // Make sure the worker is active.
     63          registration = await wait_for_active_worker(registration);
     64 
     65          let activeWorker = registration.activeWorker;
     66          ok(activeWorker !== null, "Worker is not active!");
     67          ok(activeWorker.debugger === null);
     68 
     69          info("Attach a debugger to the service worker, and check that the " +
     70               "service worker is restarted.");
     71          activeWorker.attachDebugger();
     72          let workerDebugger = activeWorker.debugger;
     73          ok(workerDebugger !== null);
     74 
     75          // Verify debugger properties
     76          ok(workerDebugger.principal instanceof Ci.nsIPrincipal);
     77          is(workerDebugger.url, EXAMPLE_URL + "worker.js");
     78 
     79          info("Verify that getRegistrationByPrincipal return the same " +
     80               "nsIServiceWorkerRegistrationInfo");
     81          let reg = swm.getRegistrationByPrincipal(workerDebugger.principal,
     82                                                   workerDebugger.url);
     83          is(reg, registration);
     84 
     85          info("Check that getWorkerByID returns the same nsIWorkerDebugger");
     86          is(activeWorker, reg.getWorkerByID(workerDebugger.serviceWorkerID));
     87 
     88          info("Detach the debugger from the service worker, and check that " +
     89               "the service worker eventually shuts down again.");
     90          promise = waitForServiceWorkerShutdown();
     91          activeWorker.detachDebugger();
     92          await promise;
     93          ok(activeWorker.debugger === null);
     94 
     95          promise = waitForUnregister(EXAMPLE_URL);
     96          iframe.contentWindow.postMessage("unregister", "*");
     97          registration = await promise;
     98 
     99          SimpleTest.finish();
    100        })();
    101      });
    102    }
    103 
    104  ]]>
    105  </script>
    106 
    107  <body xmlns="http://www.w3.org/1999/xhtml">
    108    <p id="display"></p>
    109    <div id="content" style="display:none;"></div>
    110    <pre id="test"></pre>
    111    <iframe id="iframe"></iframe>
    112  </body>
    113  <label id="test-result"/>
    114 </window>