summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/test_installation_simple.html
blob: 69c9518ea07d12a9746f5cc9fa122664747bf439 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 930348 - test stub Navigator ServiceWorker utilities.</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">

  function simpleRegister() {
    var p = navigator.serviceWorker.register("worker.js", { scope: "simpleregister/" });
    ok(p instanceof Promise, "register() should return a Promise");
    return Promise.resolve();
  }

  function sameOriginWorker() {
    p = navigator.serviceWorker.register("http://some-other-origin/worker.js");
    return p.then(function(w) {
      ok(false, "Worker from different origin should fail");
    }, function(e) {
      ok(e.name === "SecurityError", "Should fail with a SecurityError");
    });
  }

  function sameOriginScope() {
    p = navigator.serviceWorker.register("worker.js", { scope: "http://www.example.com/" });
    return p.then(function(w) {
      ok(false, "Worker controlling scope for different origin should fail");
    }, function(e) {
      ok(e.name === "SecurityError", "Should fail with a SecurityError");
    });
  }

  function httpsOnly() {
    return SpecialPowers.pushPrefEnv({'set': [["dom.serviceWorkers.testing.enabled", false]] })
    .then(function() {
      return navigator.serviceWorker.register("/worker.js");
    }).then(function(w) {
      ok(false, "non-HTTPS pages cannot register ServiceWorkers");
    }, function(e) {
      ok(e.name === "TypeError", "navigator.serviceWorker should be undefined");
    }).then(function() {
      return SpecialPowers.popPrefEnv();
    });
  }

  function realWorker() {
    var p = navigator.serviceWorker.register("worker.js", { scope: "realworker" });
    return p.then(function(wr) {
      ok(wr instanceof ServiceWorkerRegistration, "Register a ServiceWorker");

      info(wr.scope);
      ok(wr.scope == (new URL("realworker", document.baseURI)).href, "Scope should match");
      // active, waiting, installing should return valid worker instances
      // because the registration is for the realworker scope, so the workers
      // should be obtained for that scope and not for
      // test_installation_simple.html
      var worker = wr.installing;
      ok(worker && wr.scope.match(/realworker$/) &&
         worker.scriptURL.match(/worker.js$/), "Valid worker instance should be available.");
      return wr.unregister().then(function(success) {
        ok(success, "The worker should be unregistered successfully");
      }, function(e) {
        dump("Error unregistering the worker: " + e + "\n");
      });
    }, function(e) {
      info("Error: " + e.name);
      ok(false, "realWorker Registration should have succeeded!");
    });
  }

  function networkError404() {
    return navigator.serviceWorker.register("404.js", { scope: "network_error/"}).then(function(w) {
        ok(false, "404 response should fail with TypeError");
      }, function(e) {
        ok(e.name === "TypeError", "404 response should fail with TypeError");
      });
  }

  function redirectError() {
    return navigator.serviceWorker.register("redirect_serviceworker.sjs", { scope: "redirect_error/" }).then(function(swr) {
        ok(false, "redirection should fail");
      }, function (e) {
        ok(e.name === "SecurityError", "redirection should fail with SecurityError");
      });
  }

  function parseError() {
    var p = navigator.serviceWorker.register("parse_error_worker.js", { scope: "parse_error/" });
    return p.then(function(wr) {
      ok(false, "Registration should fail with parse error");
      return navigator.serviceWorker.getRegistration("parse_error/").then(function(swr) {
        // See https://github.com/slightlyoff/ServiceWorker/issues/547
        is(swr, undefined, "A failed registration for a scope with no prior controllers should clear itself");
      });
    }, function(e) {
      ok(e instanceof Error, "Registration should fail with parse error");
    });
  }

  // FIXME(nsm): test for parse error when Update step doesn't happen (directly from register).

  function updatefound() {
    var frame = document.createElement("iframe");
    frame.setAttribute("id", "simpleregister-frame");
    frame.setAttribute("src", new URL("simpleregister/index.html", document.baseURI).href);
    document.body.appendChild(frame);
    var resolve, reject;
    var p = new Promise(function(res, rej) {
      resolve = res;
      reject = rej;
    });

    var regPromise;
    function continueTest() {
      regPromise = navigator.serviceWorker.register(
        "worker2.js", { scope: "simpleregister/" });
    }

    window.onmessage = function(e) {
      if (e.data.type == "ready") {
        continueTest();
      } else if (e.data.type == "finish") {
        window.onmessage = null;
        // We have to make frame navigate away, otherwise it will call
        // MaybeStopControlling() when this document is unloaded. At that point
        // the pref has been disabled, so the ServiceWorkerManager is not available.
        frame.setAttribute("src", new URL("about:blank").href);
        regPromise.then(function(reg) {
          reg.unregister().then(function(success) {
            ok(success, "The worker should be unregistered successfully");
            resolve();
          }, function(error) {
            dump("Error unregistering the worker: " + error + "\n");
          });
        });
      } else if (e.data.type == "check") {
        ok(e.data.status, e.data.msg);
      }
    }
    return p;
  }

  var readyPromiseResolved = false;

  function readyPromise() {
    var frame = document.createElement("iframe");
    frame.setAttribute("id", "simpleregister-frame-ready");
    frame.setAttribute("src", new URL("simpleregister/ready.html", document.baseURI).href);
    document.body.appendChild(frame);

    var channel = new MessageChannel();
    frame.addEventListener('load', function() {
      frame.contentWindow.postMessage('your port!', '*', [channel.port2]);
    });

    channel.port1.onmessage = function() {
      readyPromiseResolved = true;
    }

    return Promise.resolve();
  }

  function checkReadyPromise() {
    ok(readyPromiseResolved, "The ready promise has been resolved!");
    return Promise.resolve();
  }

  function runTest() {
    simpleRegister()
      .then(sameOriginWorker)
      .then(sameOriginScope)
      .then(httpsOnly)
      .then(readyPromise)
      .then(realWorker)
      .then(networkError404)
      .then(redirectError)
      .then(parseError)
      .then(updatefound)
      .then(checkReadyPromise)
      // put more tests here.
      .then(function() {
        SimpleTest.finish();
      }).catch(function(e) {
        ok(false, "Some test failed with error " + e);
        SimpleTest.finish();
      });
  }

  SimpleTest.waitForExplicitFinish();
  SpecialPowers.pushPrefEnv({"set": [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["dom.caches.testing.enabled", true],
  ]}, runTest);
</script>
</pre>
</body>
</html>