summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/test_form_restoration.html
blob: b9292367701ef707556b275e03b1b99a8e463f52 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!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>