summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/page-visibility/iframe-session-history.html
blob: e0232153167e680b66e6d5eb1ca637f31ea7c934 (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
<!DOCTYPE HTML>
<title>Test the correct sequence of pagevisibility-related events in conjunction with history navigation</title>
<link rel="author" title="Noam Rosenthal"  href="mailto:noam@webkit.org">
<link rel="help" href="https://www.w3.org/TR/page-visibility-2/">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>

const iframePath1 = 'resources/iframe-post-load.html?v=1'
const iframePath2 = 'resources/iframe-post-load.html?v=2'

function waitForLoad(iframe) {
  return new Promise(resolve => {
    const callback = e => {
      if (e.data === 'load') {
        window.removeEventListener('message', callback)
        resolve()
      }
    }

    window.addEventListener('message', callback)
  })
}

function assert_sequence(sequence) {
  assert_equals(sequence.length, 2)
  assert_equals(sequence[0].type, 'pagehide')
  assert_equals(sequence[0].target, '#document')
  assert_equals(sequence[0].visibilityState, 'visible')
  assert_equals(sequence[0].cancelable, true)
  assert_equals(sequence[1].type, 'visibilitychange')
  assert_equals(sequence[1].target, '#document')
  assert_equals(sequence[1].visibilityState, 'hidden')
  assert_equals(sequence[1].cancelable, false)
}
promise_test(async t => {
    const iframe = document.createElement('iframe');
    iframe.src = iframePath1;
    document.body.appendChild(iframe);
    let sequence = [];
    t.add_cleanup(() => iframe.remove());
    iframe.src = iframePath1;
    await waitForLoad(iframe);
    const handler = t.step_func(event => sequence.push({
      type: event.type,
      target: event.target.nodeName,
      cancelable: event.cancelable,
      visibilityState: iframe.contentWindow.document.visibilityState
    }));
    iframe.contentWindow.document.addEventListener('visibilitychange', handler);
    iframe.contentWindow.addEventListener('pagehide', handler);
    iframe.contentWindow.location.href = iframePath2;
    await waitForLoad(iframe);
    assert_sequence(sequence);
    sequence = [];
    iframe.contentWindow.addEventListener('pagehide', handler);
    iframe.contentWindow.document.addEventListener('visibilitychange', handler);
    iframe.contentWindow.history.back();
    await waitForLoad(iframe);
    assert_sequence(sequence);
}, "pagehide should be called before visibilitychange, and visibilityState should be set to hidden at the right time");
</script>
</body>