summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/lifecycle/resources/window.html
blob: 58181f32da7337773a0c420ff929631d2ba9ad1d (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
<!doctype html>
<html>
<head><title>Frozen Window</title></head>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/common/utils.js"></script>
<body>
<h1>This window will be frozen</h1>
<iframe id="child_frame" src="child.html"></iframe>
<script>

const freezingStepName = 'testOnFreeze';

function testFetch(keepalive) {
  var request_token = token();
  var name = 'testfetch' + (keepalive ? 'with' : 'without') + 'keepalive';
  window.opener.add_step(name);

  function handler() {
    window.opener.step_fail(name);
  }

  fetch('beacon.py?token=' + request_token, {
    keepalive: keepalive
  }).then(() => handler()).catch(() => handler());

  window.opener.poll_for_result(request_token, name, keepalive);
}

function testXHR(async) {
  var request_token = token();
  var name = 'test' + (async ? 'Async' : 'Sync') + 'XHR';
  window.opener.add_step(name);
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = () => {
    if (xhr.readyState === 4) {
      if (xhr.status === 0)
        window.opener.step_success(name);
      else
        window.opener.step_fail(name);
    }
  }
  xhr.open('GET', 'beacon.py?token=' + request_token, async);
  try {
    xhr.send(null);
    if (async) {
      window.opener.poll_for_result(request_token, name, false);
    }
  } catch {
    window.opener.step_success(name);
  };
}

function testSendBeacon() {
  var request_token = token();
  var name = 'testSendBeacon';
  window.opener.add_step(name);
  if (navigator.sendBeacon("beacon.py?token=" + request_token, "")) {
    window.opener.poll_for_result(request_token, name, true);
  } else {
    window.opener.step_fail(name);
  }
}

window.document.addEventListener("freeze", () => {
  // Testing fetch, only fetch keepalive should succeed.
  testFetch(true /* keepalive */);
  testFetch(false /* keepalive */);
  // Testing XHR, both sync and async should fail.
  testXHR(true /* async */);
  testXHR(false /* sync */);
  // Testing navigator.sendBeacon, which should be allowed.
  testSendBeacon();
  window.opener.step_success(freezingStepName);
});

onload = function() {
  window.opener.add_step(freezingStepName);
  test_driver.freeze();
};

</script>
</body>
</html>