summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/test_install_event.html
blob: 87f89725dcf4f67e23e75ca6e6bd790576592278 (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
<!--
  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 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: "./install_event" });
    return p;
  }

  function nextRegister(reg) {
    ok(reg instanceof ServiceWorkerRegistration, "reg should be a ServiceWorkerRegistration");
    var p = navigator.serviceWorker.register("install_event_worker.js", { scope: "./install_event" });
    return p.then(function(swr) {
      ok(reg === swr, "register should resolve to the same registration object");
      var update_found_promise = new Promise(function(resolve, reject) {
        swr.addEventListener('updatefound', function(e) {
          ok(true, "Received onupdatefound");
          resolve();
        });
      });

      var worker_activating = new Promise(function(res, reject) {
        ok(swr.installing instanceof ServiceWorker, "There should be an installing worker if promise resolves.");
        ok(swr.installing.state == "installing", "Installing worker's state should be 'installing'");
        swr.installing.onstatechange = function(e) {
          if (e.target.state == "activating") {
            e.target.onstatechange = null;
            res();
          }
        }
      });

      return Promise.all([update_found_promise, worker_activating]);
    }, function(e) {
      ok(false, "Unexpected Error in nextRegister! " + e);
    });
  }

  function installError() {
    // Silence worker errors so they don't cause the test to fail.
    window.onerror = function(e) {}
    return navigator.serviceWorker.register("install_event_error_worker.js", { scope: "./install_event" })
      .then(function(swr) {
        ok(swr.installing instanceof ServiceWorker, "There should be an installing worker if promise resolves.");
        ok(swr.installing.state == "installing", "Installing worker's state should be 'installing'");
        return new Promise(function(resolve, reject) {
          swr.installing.onstatechange = function(e) {
            ok(e.target.state == "redundant", "Installation of worker with error should fail.");
            resolve();
          }
        });
      }).then(function() {
        return navigator.serviceWorker.getRegistration("./install_event").then(function(swr) {
          var newest = swr.waiting || swr.active;
          ok(newest, "Waiting or active worker should still exist");
          ok(newest.scriptURL.match(/install_event_worker.js$/), "Previous worker should remain the newest worker");
        });
      });
  }

  function testActive(worker) {
    is(worker.state, "activating", "Should be activating");
    return new Promise(function(resolve, reject) {
      worker.onstatechange = function(e) {
        e.target.onstatechange = null;
        is(e.target.state, "activated", "Activation of worker with error in activate event handler should still succeed.");
        resolve();
      }
    });
  }

  function activateErrorShouldSucceed() {
    // Silence worker errors so they don't cause the test to fail.
    window.onerror = function() { }
    return navigator.serviceWorker.register("activate_event_error_worker.js", { scope: "./activate_error" })
      .then(function(swr) {
        var p = new Promise(function(resolve, reject) {
          ok(swr.installing.state == "installing", "activateErrorShouldSucceed(): Installing worker's state should be 'installing'");
          swr.installing.onstatechange = function(e) {
            e.target.onstatechange = null;
            if (swr.waiting) {
              swr.waiting.onstatechange = function(event) {
                event.target.onstatechange = null;
                testActive(swr.active).then(resolve, reject);
              }
            } else {
              testActive(swr.active).then(resolve, reject);
            }
          }
        });

        return p.then(function() {
          return Promise.resolve(swr);
        });
      }).then(function(swr) {
        return swr.unregister();
      });
  }

  function unregister() {
    return navigator.serviceWorker.getRegistration("./install_event").then(function(reg) {
      return reg.unregister();
    });
  }

  function runTest() {
    Promise.resolve()
      .then(simpleRegister)
      .then(nextRegister)
      .then(installError)
      .then(activateErrorShouldSucceed)
      .then(unregister)
      .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]
  ]}, runTest);
</script>
</pre>
</body>
</html>