summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/test_serviceworkerregistrationinfo.xul
blob: c879dc01beb6b53c5c1bb9d3adf95ea9e96c2a20 (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
115
<?xml version="1.0"?>
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<window title="Test for ServiceWorkerRegistrationInfo"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="test();">
  <script type="application/javascript"
          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 + "serviceworkerregistrationinfo_iframe.html";

    function test() {
      SimpleTest.waitForExplicitFinish();

      SpecialPowers.pushPrefEnv({'set': [
        ["dom.serviceWorkers.enabled", true],
        ["dom.serviceWorkers.testing.enabled", true],
      ]}, function () {
        Task.spawn(function* () {
          let iframe = $("iframe");
          let promise = waitForIframeLoad(iframe);
          iframe.src = IFRAME_URL;
          yield promise;

          // The change handler is not guaranteed to be called within the same
          // tick of the event loop as the one in which the change happened.
          // Because of this, the exact state of the service worker registration
          // is only known until the handler returns.
          //
          // Because then-handlers are resolved asynchronously, the following
          // checks are done using callbacks, which are called synchronously
          // when then handler is called. These callbacks can return a promise,
          // which is used to resolve the promise returned by the function.

          info("Check that a service worker registration notifies its " +
               "listeners when its state changes.");
          promise = waitForRegister(EXAMPLE_URL, function (registration) {
            is(registration.scriptSpec, "");
            ok(registration.installingWorker === null);
            ok(registration.waitingWorker === null);
            ok(registration.activeWorker === null);

            return waitForServiceWorkerRegistrationChange(registration, function  () {
              is(registration.scriptSpec, EXAMPLE_URL + "worker.js");
              ok(registration.installingWorker !== null);
              is(registration.installingWorker.scriptSpec, EXAMPLE_URL + "worker.js");
              ok(registration.waitingWorker === null);
              ok(registration.activeWorker === null);

              return waitForServiceWorkerRegistrationChange(registration, function () {
                ok(registration.installingWorker === null);
                ok(registration.waitingWorker !== null);
                ok(registration.activeWorker === null);

                return waitForServiceWorkerRegistrationChange(registration, function () {
                  ok(registration.installingWorker === null);
                  ok(registration.waitingWorker === null);
                  ok(registration.activeWorker !== null);

                  return registration;
                });
              });
            });
          });
          iframe.contentWindow.postMessage("register", "*");
          let registration = yield promise;

          promise = waitForServiceWorkerRegistrationChange(registration, function () {
            is(registration.scriptSpec, EXAMPLE_URL + "worker2.js");
            ok(registration.installingWorker !== null);
            is(registration.installingWorker.scriptSpec, EXAMPLE_URL + "worker2.js");
            ok(registration.waitingWorker === null);
            ok(registration.activeWorker !== null);

            return waitForServiceWorkerRegistrationChange(registration, function () {
              ok(registration.installingWorker === null);
              ok(registration.waitingWorker !== null);
              ok(registration.activeWorker !== null);

              return waitForServiceWorkerRegistrationChange(registration, function () {
                ok(registration.installingWorker === null);
                ok(registration.waitingWorker === null);
                ok(registration.activeWorker !== null);

                return registration;
              });
            });
          });
          iframe.contentWindow.postMessage("register", "*");
          yield promise;

          iframe.contentWindow.postMessage("unregister", "*");
          yield waitForUnregister(EXAMPLE_URL);

          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>