<!DOCTYPE html>
<meta name="timeout" content="long">
<script src="/common/get-host-info.sub.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="unload-utils.js"></script>
<body>
<script type="module">
const params = new URLSearchParams(location.search);
const state = params.get('state') || 'start';
const uid = params.get('uid');
// Test steps:
// 1. `start` page starts prerendering `prerendering` page.
// 2. `prerendering` page navigates to `another-page`.
// 3. `prerendering` page asks the `start` page to navigate.
// 4. ``pagehide` and unload` may not be handled as the prerendering is
// cancelled.
// 5. `start` does a fallback navigation as the prerendering is cancelled.
// 6. `prerendering` page loads again with document.prerendering == false.
// 7. `prerendering` page navigates to `another-page` again.
// 8. `pagehide`, `visibilitychange`, and `unload` are handled this time.
// 9. `another-page` finishes the test.
sendChannelMessage(
'load ' + state + (document.prerendering ? ' in prerendering' : ''), uid);
if (state === 'start') {
const url = createTestUrl('prerendering', uid);
startPrerendering(url);
// Wait for a message from `prerendering` to navigate that results in
// fallback network navigation as the prerendering is cancelled.
await waitChannelMessage('request fallback', uid);
// Navigate.
document.location = url;
} else if (state === 'prerendering') {
// unload handler may not be triggered for cancellation.
const eventPromise = addEventListeners('main-frame', uid);
// Trigger a main frame navigation that will cancel the prerendering.
// This navigates successfully if the page isn't prerendered.
document.location = createTestUrl('another-page', uid);
// Ask the `start` page to navigate if on prerendering. Otherwise, finish
// the test.
if (document.prerendering) {
sendChannelMessage('request fallback', uid);
}
// Wait for events fired during activation (e.g., unload).
await eventPromise;
} else if (state === 'another-page') {
// Reach here after the prerendering is cancelled and fallback request loads
// the `prerendering` page with document.prerendering == false. The main
// frame navigation will succeed this time.
}
</script>
</body>