summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/bug1151916_driver.html
blob: e540ad2396454f25f48451c790341fff6f5167d0 (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
<html>
  <body>
    <script language="javascript">
      function fail(msg) {
        window.parent.postMessage({ status: "failed", message: msg }, "*");
      }

      function success(msg) {
        window.parent.postMessage({ status: "success", message: msg }, "*");
      }

      if (!window.parent) {
        dump("This file must be embedded in an iframe!");
      }

      navigator.serviceWorker.getRegistration()
      .then(function(reg) {
        if (!reg) {
          navigator.serviceWorker.ready.then(function(reg) {
            if (reg.active.state == "activating") {
              reg.active.onstatechange = function(e) {
                reg.active.onstatechange = null;
                if (reg.active.state == "activated") {
                  success("Registered and activated");
                }
              }
            } else {
              success("Registered and activated");
            }
          });
          navigator.serviceWorker.register("bug1151916_worker.js",
                                           { scope: "." });
        } else {
          // Simply force the sw to load a resource and touch self.caches.
          if (!reg.active) {
            fail("no-active-worker");
            return;
          }

          fetch("madeup.txt").then(function(res) {
            res.text().then(function(v) {
              if (v == "Hi there") {
                success("Loaded from cache");
              } else {
                fail("Response text did not match");
              }
            }, fail);
          }, fail);
        }
      }, fail);
    </script>
  </body>
</html>