summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/resources/sandbox-iframe.html
blob: 478dfccb3ab2567344650a57f7f2bc5289351484 (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
<!DOCTYPE html>
<script src="/common/utils.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/speculation-rules/prerender/resources/utils.js"></script>
<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
<body>
<script>

async function main() {
  // Start loading a sandbox iframe, which is treated as cross-origin iframe.
  // The iframe messages us with the value of its document.prerendering,
  // which should be false since load is delayed until after activation.
  const sandboxIframe = document.createElement('iframe');
  sandboxIframe.sandbox = 'allow-scripts';

  const gotMessage = new Promise((resolve, reject) => {
    window.addEventListener('message', (e) => {
     if (e.data === 'document.prerendering: false')
       resolve();
     else
       reject('bad message: ' + e.data);
    });
  });

  sandboxIframe.src = 'post-message-prerendering-completion-notification.html';
  document.body.appendChild(sandboxIframe);

  // To give the test a chance to fail by giving enough time if it loads the
  // cross-origin iframe instead of deferring, wait for a same-origin iframe to
  // load before proceeding with the test.
  await createFrame('empty.html');

  // Start the event collector to trigger activation.
  const prerenderEventCollector = new PrerenderEventCollector();
  prerenderEventCollector.start(gotMessage, 'iframe loaded');
}

// The main test page (prerender/sandbox-iframe.html) loads the initiator
// page, then the initiator page will prerender itself with the
// `prerendering` parameter.
const params = new URLSearchParams(location.search);
if (!params.has('prerendering')) {
  loadInitiatorPage();
} else {
  main();
}
</script>
</body>