summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/test_serviceworkerinfo.xhtml
blob: 07b6a3034528eceb09a2dde5dd9eb59a8133f389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?xml version="1.0"?>
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<window title="Test for ServiceWorkerInfo"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="test();">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="application/javascript" src="chrome_helpers.js"/>
  <script type="application/javascript">
  <![CDATA[

    let IFRAME_URL = EXAMPLE_URL + "serviceworkerinfo_iframe.html";

    function wait_for_active_worker(registration) {
      ok(registration, "Registration is valid.");
      return new Promise(function(res, rej) {
        if (registration.activeWorker) {
          res(registration);
          return;
        }
        let listener = {
          onChange() {
            if (registration.activeWorker) {
              registration.removeListener(listener);
              res(registration);
            }
          }
        }
        registration.addListener(listener);
      });
    }

    function test() {
      SimpleTest.waitForExplicitFinish();

      SpecialPowers.pushPrefEnv({'set': [
        ["dom.serviceWorkers.enabled", true],
        ["dom.serviceWorkers.idle_extended_timeout", 1000000],
        ["dom.serviceWorkers.idle_timeout", 0],
        ["dom.serviceWorkers.testing.enabled", true],
      ]}, function () {
        (async function() {
          let iframe = $("iframe");
          let promise = new Promise(function (resolve) {
            iframe.onload = function () {
              resolve();
            };
          });
          iframe.src = IFRAME_URL;
          await promise;

          info("Check that a service worker eventually shuts down.");
          promise = Promise.all([
            waitForRegister(EXAMPLE_URL),
            waitForServiceWorkerShutdown()
          ]);
          iframe.contentWindow.postMessage("register", "*");
          let [registration] = await promise;

          // Make sure the worker is active.
          registration = await wait_for_active_worker(registration);

          let activeWorker = registration.activeWorker;
          ok(activeWorker !== null, "Worker is not active!");
          ok(activeWorker.debugger === null);

          info("Attach a debugger to the service worker, and check that the " +
               "service worker is restarted.");
          activeWorker.attachDebugger();
          let workerDebugger = activeWorker.debugger;
          ok(workerDebugger !== null);

          // Verify debugger properties
          ok(workerDebugger.principal instanceof Ci.nsIPrincipal);
          is(workerDebugger.url, EXAMPLE_URL + "worker.js");

          info("Verify that getRegistrationByPrincipal return the same " +
               "nsIServiceWorkerRegistrationInfo");
          let reg = swm.getRegistrationByPrincipal(workerDebugger.principal,
                                                   workerDebugger.url);
          is(reg, registration);

          info("Check that getWorkerByID returns the same nsIWorkerDebugger");
          is(activeWorker, reg.getWorkerByID(workerDebugger.serviceWorkerID));

          info("Detach the debugger from the service worker, and check that " +
               "the service worker eventually shuts down again.");
          promise = waitForServiceWorkerShutdown();
          activeWorker.detachDebugger();
          await promise;
          ok(activeWorker.debugger === null);

          promise = waitForUnregister(EXAMPLE_URL);
          iframe.contentWindow.postMessage("unregister", "*");
          registration = await promise;

          SimpleTest.finish();
        })();
      });
    }

  ]]>
  </script>

  <body xmlns="http://www.w3.org/1999/xhtml">
    <p id="display"></p>
    <div id="content" style="display:none;"></div>
    <pre id="test"></pre>
    <iframe id="iframe"></iframe>
  </body>
  <label id="test-result"/>
</window>