summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/resources/main-frame-navigation.html
blob: 8781edef6670b48bf17cf644b5623f08bfca0fdb (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>
<script src="/common/utils.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/speculation-rules/prerender/resources/utils.js"></script>
<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
<script>

// The main test page loads the initiator page, then the initiator page will
// prerender itself with the `prerendering` parameter. The prerendered page will
// trigger a main frame navigation with the `navigation` parameter.
const params = new URLSearchParams(location.search);
const uid = params.get('uid');

const isPrerendering = params.has('prerendering');
const isNavigation = params.has('navigation');

if (isPrerendering && isNavigation) {
  assert_true(document.prerendering);

  const result = {
    // Check the value of document.prerendering now and after activation.
    prerenderingValueBeforeActivation: document.prerendering,
    prerenderingValueAfterActivation: null,

    // True if the prerenderingchange event is fired.
    onprerenderingchangeCalled: false,
  };

  window.addEventListener('load', () => {
    const prerenderChannel = new PrerenderChannel('prerender-channel', uid);
    prerenderChannel.postMessage('readyToActivate');
    prerenderChannel.close();
  });

  document.addEventListener('prerenderingchange', (e) => {
    assert_false(document.prerendering);

    const entry = performance.getEntriesByType('navigation')[0];
    assert_greater_than_equal(entry.activationStart, 0,
      'activationStart must be greater than 0')

    result.onprerenderingchangeCalled = true;
    result.prerenderingValueAfterActivation = document.prerendering;

    const resultChannel = new PrerenderChannel('result', uid);
    resultChannel.postMessage(result);
    resultChannel.close();
    window.close();
  });
} else if (isPrerendering) {
  assert_true(document.prerendering);

  location.href = location.href + '&navigation';
} else {
  assert_false(document.prerendering);

  const prerenderingUrl = location.href + '&prerendering';

  const prerenderChannel = new PrerenderChannel('prerender-channel', uid);
  const readyToActivate = new Promise((resolve, reject) => {
    prerenderChannel.addEventListener('message', e => {
      if (e.data === 'readyToActivate') {
        resolve();
      } else {
        reject(`The initiator page receives an unsupported message: ${e.data}`);
      }
    });
  });

  // Activate the page when prerendering is ready.
  readyToActivate.then(() => {
    window.location = prerenderingUrl.toString();
  }).catch(e => {
    const resultChannel = new PrerenderChannel('result', uid);
    resultChannel.postMessage(
        `Failed to navigate the prerendered page: ${e.toString()}`);
    resultChannel.close();
    window.close();
  });

  startPrerendering(prerenderingUrl);
}
</script>