<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test form restoration for no-store pages</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    function waitForMessage(aBroadcastChannel) {
      return new Promise(resolve => {
        aBroadcastChannel.addEventListener("message", ({ data }) => {
          resolve(data);
        }, { once: true });
      });
    }

    function postMessageAndWait(aBroadcastChannel, aMsg) {
      let promise = waitForMessage(aBroadcastChannel);
      aBroadcastChannel.postMessage(aMsg);
      return promise;
    }

    async function startTest(aTestFun) {
      let bc = new BroadcastChannel("form_restoration");

      let promise = waitForMessage(bc);
      window.open("file_form_restoration_no_store.html", "", "noopener");
      await promise;

      // test steps
      await aTestFun(bc);

      // close broadcast channel and window
      bc.postMessage("close");
      bc.close();
    }

    /* Test for bug1740517 */
    add_task(async function history_back() {
      await startTest(async (aBroadcastChannel) => {
        // update form data
        aBroadcastChannel.postMessage("enter_data");

        // navigate
        await postMessageAndWait(aBroadcastChannel, "navigate");

        // history back
        let { persisted, formData } = await postMessageAndWait(aBroadcastChannel, "back");

        // check form data
        ok(!persisted, "Page with a no-store header shouldn't be bfcached.");
        is(formData, "initial", "We shouldn't restore form data when going back to a page with a no-store header.");
      });
    });

    /* Test for bug1752250 */
    add_task(async function location_reload() {
      await startTest(async (aBroadcastChannel) => {
        // update form data
        aBroadcastChannel.postMessage("enter_data");

        // reload
        let { persisted, formData } = await postMessageAndWait(aBroadcastChannel, "reload");

        // check form data
        ok(!persisted, "Page with a no-store header shouldn't be bfcached.");
        is(formData, "initial", "We shouldn't restore form data when reload a page with a no-store header.");
      });
    });
  </script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>
