<!DOCTYPE HTML>
<html>
<head>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>

<form id="form" action="file_reload_large_postdata.sjs" target="_blank" rel="opener" method="POST">
  <input id="input" name="payload" type="hidden" value=""/>
</form>

<pre id="test">
<script>
// This is derived from `kTooLargeStream` in `IPCStreamUtils.cpp`.
const kTooLargeStream = 1024 * 1024;

function waitForPopup(expected) {
  return new Promise(resolve => {
    addEventListener("message", evt => {
      info("got message!");
      is(evt.source.opener, window, "the event source's opener should be this window");
      is(evt.data, expected, "got the expected data from the popup");
      resolve(evt.source);
    }, { once: true });
  });
}

add_task(async function() {
  await SpecialPowers.pushPrefEnv({"set": [["dom.confirm_repost.testing.always_accept", true]]});
  let form = document.getElementById("form");
  let input = document.getElementById("input");

  // Create a very large value to include in the post payload. This should
  // ensure that the value isn't sent directly over IPC, and is instead sent as
  // an async inputstream.
  let payloadSize = kTooLargeStream;

  let popupReady = waitForPopup(payloadSize);
  input.value = "A".repeat(payloadSize);
  form.submit();

  let popup = await popupReady;
  try {
    let popupReady2 = waitForPopup(payloadSize);
    info("reloading popup");
    popup.location.reload();
    let popup2 = await popupReady2;
    is(popup, popup2);
  } finally {
    popup.close();
  }
});

// The .sjs server can time out processing the 1mb payload in debug builds.
SimpleTest.requestLongerTimeout(2);
</script>
</pre>
</body>
</html>
