summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/controller/index.html
blob: 740e24f15cfe3f8c85a600d46437a107e08dc671 (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 94048 - test install event.</title>
  <script type="text/javascript" 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">

  // Make sure to use good, unique messages, since the actual expression will not show up in test results.
  function my_ok(result, msg) {
    parent.postMessage({status: "ok", result: result, message: msg}, "*");
  }

  function finish() {
    parent.postMessage({status: "done"}, "*");
  }

  navigator.serviceWorker.ready.then(function(swr) {
    my_ok(swr.scope.match(/serviceworkers\/control$/),
          "This page should be controlled by upper level registration");
    my_ok(swr.installing == undefined,
          "Upper level registration should not have a installing worker.");
    if (navigator.serviceWorker.controller) {
      // We are controlled.
      // Register a new worker for this sub-scope. After that, controller should still be for upper level, but active should change to be this scope's.
      navigator.serviceWorker.register("../worker2.js", { scope: "./" }).then(function(e) {
        my_ok("installing" in e, "ServiceWorkerRegistration.installing exists.");
        my_ok(e.installing instanceof ServiceWorker, "ServiceWorkerRegistration.installing is a ServiceWorker.");

        my_ok("waiting" in e, "ServiceWorkerRegistration.waiting exists.");
        my_ok("active" in e, "ServiceWorkerRegistration.active exists.");

        my_ok(e.installing &&
              e.installing.scriptURL.match(/worker2.js$/),
              "Installing is serviceworker/controller");

        my_ok("scope" in e, "ServiceWorkerRegistration.scope exists.");
        my_ok(e.scope.match(/serviceworkers\/controller\/$/), "Scope is serviceworker/controller " + e.scope);

        my_ok("unregister" in e, "ServiceWorkerRegistration.unregister exists.");

        my_ok(navigator.serviceWorker.controller.scriptURL.match(/worker\.js$/),
              "Controller is still worker.js");

        e.unregister().then(function(result) {
          my_ok(result, "Unregistering the SW should succeed");
          finish();
        }, function(e) {
          dump("Error unregistering the SW: " + e + "\n");
        });
      });
    } else {
      my_ok(false, "Should've been controlled!");
      finish();
    }
  }).catch(function(e) {
    my_ok(false, "Some test threw an error " + e);
    finish();
  });
</script>
</pre>
</body>
</html>