summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/browser_cached_force_refresh.html
blob: faf2ee7a839dfa18743f1c707bfe7baa57e976e7 (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/javascript">
function ok(exp, msg) {
  if (!exp) {
    throw(msg);
  }
}

function is(actual, expected, msg) {
  if (actual !== expected) {
    throw('got "' + actual + '", but expected "' + expected + '" - ' + msg);
  }
}

function fail(err) {
  window.dispatchEvent(new Event("cached-failure", { bubbles: true, detail: err }));
}

function getUncontrolledClients(sw) {
  return new Promise(function(resolve, reject) {
    navigator.serviceWorker.addEventListener('message', function onMsg(evt) {
      if (evt.data.type === 'CLIENTS') {
        navigator.serviceWorker.removeEventListener('message', onMsg);
        resolve(evt.data.detail);
      }
    });
    sw.postMessage({ type: 'GET_UNCONTROLLED_CLIENTS' })
  });
}

addEventListener('load', function(event) {
  if (!navigator.serviceWorker.controller) {
    return fail(window.location.href + ' is not controlled!');
  }

  getUncontrolledClients(navigator.serviceWorker.controller)
    .then(function(clientList) {
      is(clientList.length, 1, 'should only have one client');
      is(clientList[0].url, window.location.href,
         'client url should match current window');
      is(clientList[0].frameType, 'top-level',
         'client should be a top-level window');
      window.dispatchEvent(new Event('cached-load', { bubbles: true }));
    })
    .catch(function(err) {
      fail(err);
    });
});
</script>
</body>
</html>