summaryrefslogtreecommitdiffstats
path: root/docshell/test/navigation/test_scrollRestoration.html
blob: d31598f391fb4c198159d4aa0a9f4f0801b3ff69 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=
-->
<head>
  <title>Test for Bug 1155730</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="runTest()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1155730">Mozilla Bug 1155730</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
  <script type="application/javascript">
    SimpleTest.waitForExplicitFinish();
    SimpleTest.requestFlakyTimeout("untriaged");

    function assertCheck(data) {
      if (data.assertIs) {
        for (const args of data.assertIs) {
          is(args[0], args[1], args[2]);
        }
      }
      if (data.assertOk) {
        for (const args of data.assertOk) {
          ok(args[0], args[1]);
        }
      }
      if (data.assertIsNot) {
        for (const args of data.assertIsNot) {
          isnot(args[0], args[1], args[2]);
        }
      }
    }

    var bc1, currentCase = 0;
    function test1() {
      bc1 = new BroadcastChannel("bug1155730_part1");
      bc1.onmessage = (msgEvent) => {
        var msg = msgEvent.data;
        var command = msg.command;
        if (command == "pageshow") {
          currentCase++;
          var persisted = msg.persisted;
          is(persisted, false, "Shouldn't have persisted session history entry.");
          bc1.postMessage({command: "test", currentCase});
        } else if (command == "asserts") {
          is(msg.currentCase, currentCase, "correct case");
          info(`Checking asserts for case ${msg.currentCase}`);
          assertCheck(msg);
          if (currentCase == 3) {
            // move on to the next test
            bc1.close();
            test2();
          }
        }
      }
      window.open("file_scrollRestoration_part1_nobfcache.html", "", "width=360,height=480,noopener");
    }

    var bc2, bc2navigate;
    function test2() {
      currentCase = 0;
      bc2 = new BroadcastChannel("bug1155730_part2");
      bc2.onmessage = (msgEvent) => {
        var msg = msgEvent.data;
        var command = msg.command;
        if (command == "pageshow") {
          currentCase++;
          var persisted = msg.persisted;
          switch (currentCase) {
            case 1:
              is(persisted, false, "Shouldn't have persisted session history entry.");
              break;
            case 2:
              is(persisted, true, "Should have persisted session history entry.");
          }
          bc2.postMessage({command: "test", currentCase});
        } else if (command == "asserts") {
          is(msg.currentCase, currentCase, "correct case");
          info(`Checking asserts for case ${msg.currentCase}`);
          assertCheck(msg);
          if (currentCase == 3) {
            // move on to the next test
            bc2.close();
            test3();
          }
        } else if (command == "nextCase") {
          currentCase++;
        }
      }

      bc2navigate = new BroadcastChannel("navigate");
      bc2navigate.onmessage = (event) => {
        if (event.data.command == "loaded") {
          bc2navigate.postMessage({command: "back"})
          bc2navigate.close();
        }
      }
      window.open("file_scrollRestoration_part2_bfcache.html", "", "width=360,height=480,noopener");
    }

    var bc3, bc3navigate;
    function test3() {
      currentCase = 0;
      bc3 = new BroadcastChannel("bug1155730_part3");
      bc3.onmessage = (msgEvent) => {
        var msg = msgEvent.data;
        var command = msg.command;
        if (command == "pageshow") {
          currentCase++;
          if (currentCase == 3) {
            var persisted = msg.persisted;
            is(persisted, false, "Shouldn't have persisted session history entry.");
          }

          bc3.postMessage({command: "test", currentCase});
        } else if (command == "asserts") {
          is(msg.currentCase, currentCase, "correct case");
          info(`Checking asserts for case ${msg.currentCase}`);
          assertCheck(msg);
        } else if (command == "nextCase") {
          currentCase++;
        } else if (command == "finishing") {
          bc3.close();
          test4();
        }
      }

      bc3navigate = new BroadcastChannel("navigate");
      bc3navigate.onmessage = (event) => {
        if (event.data.command == "loaded") {
          is(event.data.scrollRestoration, 'auto', "correct scroll restoration");
          bc3navigate.postMessage({command: "back"})
          bc3navigate.close();
        }
      }
      window.open("file_scrollRestoration_part3_nobfcache.html", "", "width=360,height=480,noopener");
    }

    // test4 opens a new page which can enter bfcache. That page then loads
    // another page which can't enter bfcache. That second page then scrolls
    // down. History API is then used to navigate back and forward. When the
    // second page loads again, it should scroll down automatically.
    var bc4a, bc4b;
    var scrollYCounter = 0;
    function test4() {
      currentCase = 0;
      bc4a = new BroadcastChannel("bfcached");
      bc4a.onmessage = (msgEvent) => {
        var msg = msgEvent.data;
        var command = msg.command;
        if (command == "pageshow") {
          ++currentCase;
          if (currentCase == 1) {
            ok(!msg.persisted, "The first page should not be persisted initially.");
            bc4a.postMessage("loadNext");
          } else if (currentCase == 3) {
            ok(msg.persisted, "The first page should be persisted.");
            bc4a.postMessage("forward");
            bc4a.close();
          }
        }
      }

      bc4b = new BroadcastChannel("notbfcached");
      bc4b.onmessage = (event) => {
        var msg = event.data;
        var command = msg.command;
        if (command == "pageshow") {
          ++currentCase;
          if (currentCase == 2) {
            ok(!msg.persisted, "The second page should not be persisted.");
            bc4b.postMessage("getScrollY");
            bc4b.postMessage("scroll");
            bc4b.postMessage("getScrollY");
            bc4b.postMessage("back");
          } else if (currentCase == 4) {
            ok(!msg.persisted, "The second page should not be persisted.");
            bc4b.postMessage("getScrollY");
          }
        } else if (msg == "closed") {
          bc4b.close();
          SimpleTest.finish();
        } else if ("scrollY" in msg) {
          ++scrollYCounter;
          if (scrollYCounter == 1) {
            is(msg.scrollY, 0, "The page should be initially scrolled to top.");
          } else if (scrollYCounter == 2) {
            isnot(msg.scrollY, 0, "The page should be then scrolled down.");
          } else if (scrollYCounter == 3) {
            isnot(msg.scrollY, 0, "The page should be scrolled down after being restored from the session history.");
            bc4b.postMessage("close");
          }
        }
      }
      window.open("file_scrollRestoration_bfcache_and_nobfcache.html", "", "width=360,height=480,noopener");
    }

    function runTest() {
      // If Fission is disabled, the pref is no-op.
      SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => {
        test1();
      });
    }

  </script>
</pre>
</body>
</html>