<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/speculation-rules/prerender/resources/utils.js"></script>
<script>
function runAlertTest() {
  window.alert('Hello! Preprendering!');
  return 'no block';
}

function runConfirmTest() {
  const result = window.confirm('Are you preprendering page?');
  return 'the return value is ' + (result === true ? 'yes' : 'no');
}

function runPromptTest() {
  const result = window.prompt('Are you preprendering page?',
    'the default value');
  return 'the return value is ' + (result === null ? 'null' : result);
}

const params = new URLSearchParams(location.search);

const uid = params.get('uid');

// The main test page (restriction-message-boxes.html) loads the
// initiator page, then the initiator page will prerender itself with the
// `prerendering` parameter.
const isPrerendering = params.has('prerendering');

if (isPrerendering) {
  // Test web APIs on the pages.
  const bc = new PrerenderChannel('prerender-channel', uid);
  assert_true(document.prerendering);
  if (params.has('alert')) {
    bc.postMessage(runAlertTest());
  } else if (params.has('confirm')) {
    bc.postMessage(runConfirmTest());
  } else if (params.has('prompt')) {
    bc.postMessage(runPromptTest());
  }
  bc.close();
  window.close();
} else {
  // Initiator pages should prerender the prerendering page.
  const url = new URL(document.URL);
  url.searchParams.append('prerendering', '');
  startPrerendering(url);
}
</script>
