summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/test_serviceworkermanager.xul
blob: ead935a3c4f427db93e1f64e641a04f04ff244e6 (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
<?xml version="1.0"?>
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<window title="Test for ServiceWorkerManager"
        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 + "serviceworkermanager_iframe.html";

    function test() {
      SimpleTest.waitForExplicitFinish();

      SpecialPowers.pushPrefEnv({'set': [
        ["dom.serviceWorkers.enabled", true],
        ["dom.serviceWorkers.testing.enabled", true],
      ]}, function () {
        Task.spawn(function* () {
          let registrations = swm.getAllRegistrations();
          is(registrations.length, 0);

          let iframe = $("iframe");
          let promise = waitForIframeLoad(iframe);
          iframe.src = IFRAME_URL;
          yield promise;

          info("Check that the service worker manager notifies its listeners " +
               "when a service worker is registered.");
          promise = waitForRegister(EXAMPLE_URL);
          iframe.contentWindow.postMessage("register", "*");
          let registration = yield promise;

          registrations = swm.getAllRegistrations();
          is(registrations.length, 1);
          is(registrations.queryElementAt(0, Ci.nsIServiceWorkerRegistrationInfo),
             registration);

          info("Check that the service worker manager does not notify its " +
               "listeners when a service worker is registered with the same " +
               "scope as an existing registration.");
          let listener = {
            onRegister: function () {
              ok(false, "Listener should not have been notified.");
            }
          };
          swm.addListener(listener);
          iframe.contentWindow.postMessage("register", "*");

          info("Check that the service worker manager notifies its listeners " +
               "when a service worker is unregistered.");
          promise = waitForUnregister(EXAMPLE_URL);
          iframe.contentWindow.postMessage("unregister", "*");
          registration = yield promise;
          swm.removeListener(listener);

          registrations = swm.getAllRegistrations();
          is(registrations.length, 0);

          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>