summaryrefslogtreecommitdiffstats
path: root/docshell/test/navigation/file_scrollRestoration_part3_nobfcache.html
blob: ffc68d6ccc71e3d38cb72412fa1c01327709cdf7 (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
<html>
  <head>
    <script>
      var oldHistoryObject = null;
      var currCaseForIframe = 0;
      var bc = new BroadcastChannel("bug1155730_part3");
      bc.onmessage = (msgEvent) => {
        var msg = msgEvent.data;
        var command = msg.command;
        if (command == "test") {
          var currentCase = msg.currentCase;
          test(currentCase);
        }
      }

      // If onpopstate event takes place, check if we need to call 'test()'
      var callTest = false;
      var nextCase = 0;
      window.onpopstate = (e) => {
        if (callTest) {
          callTest = false;
          setTimeout(() => {
            test(nextCase);
          });
        }
      }

      function test(currentCase) {
        var assertIs = [];
        var assertOk = [];
        var assertIsNot = [];
        switch (currentCase) {
          case 1: {
            history.scrollRestoration = "manual";
            window.location.hash = "hash";
            bc.postMessage({command: "nextCase"});
            requestAnimationFrame(() => {
              test(currentCase + 1);
            });
            break;
          }
          case 2: {
            assertIsNot.push([Math.round(window.scrollY), 0, "Should have scrolled to #hash."]);
            assertIs.push([history.scrollRestoration, "manual", "Should have the same scrollRestoration mode as before fragment navigation."]);
            bc.postMessage({command: "asserts", currentCase, assertIs, assertIsNot});
            window.location.href = "file_scrollRestoration_navigate.html";
            break;
          }
          case 3: {
            assertIs.push([window.scrollY, 0, "Shouldn't have kept the old scroll position."]);
            assertIs.push([history.scrollRestoration, "manual", "Should have the same scrollRestoration mode as before fragment navigation."]);
            bc.postMessage({command: "asserts", currentCase, assertIs});
            history.scrollRestoration = "auto";
            document.getElementById("bottom").scrollIntoView();
            history.pushState({ state: "state1" }, "state1");
            history.pushState({ state: "state2" }, "state2");
            window.scrollTo(0, 0);
            bc.postMessage({command: "nextCase"});
            callTest = true;
            nextCase = currentCase + 1;
            history.back(); // go back to state 1
            break;
          }
          case 4: {
            assertIsNot.push([Math.round(window.scrollY), 0, "Should have scrolled back to the state1's position"]);
            assertIs.push([history.state.state, "state1", "Unexpected state."]);
            bc.postMessage({command: "asserts", currentCase, assertIs, assertIsNot});

            history.scrollRestoration = "manual";
            document.getElementById("bottom").scrollIntoView();
            history.pushState({ state: "state3" }, "state3");
            history.pushState({ state: "state4" }, "state4");
            window.scrollTo(0, 0);
            bc.postMessage({command: "nextCase"});
            callTest = true;
            nextCase = currentCase + 1;
            history.back(); // go back to state 3
            break;
          }
          case 5: {
            assertIs.push([Math.round(window.scrollY), 0, "Shouldn't have scrolled back to the state3's position"]);
            assertIs.push([history.state.state, "state3", "Unexpected state."]);

            history.pushState({ state: "state5" }, "state5");
            history.scrollRestoration = "auto";
            document.getElementById("bottom").scrollIntoView();
            assertIsNot.push([Math.round(window.scrollY), 0, "Should have scrolled to 'bottom'."]);
            bc.postMessage({command: "asserts", currentCase, assertIs, assertIsNot});
            bc.postMessage({command: "nextCase"});
            callTest = true;
            nextCase = currentCase + 1;
            // go back to state 3 (state 4 was removed when state 5 was pushed)
            history.back();
            break;
          }
          case 6: {
            window.scrollTo(0, 0);
            bc.postMessage({command: "nextCase"});
            callTest = true;
            nextCase = currentCase + 1;
            history.forward();
            break;
          }
          case 7: {
            assertIsNot.push([Math.round(window.scrollY), 0, "Should have scrolled back to the state5's position"]);
            bc.postMessage({command: "asserts", currentCase, assertIsNot});

            var ifr = document.createElement("iframe");
            ifr.src = "data:text/html,";
            document.body.appendChild(ifr);
            bc.postMessage({command: "nextCase"});
            currCaseForIframe = currentCase + 1;
            ifr.onload = () => {
              test(currCaseForIframe);
            };
            break;
          }
          case 8: {
            oldHistoryObject = SpecialPowers.wrap(document.getElementsByTagName("iframe")[0]).contentWindow.history;
            bc.postMessage({command: "nextCase"});
            currCaseForIframe++;
            document.getElementsByTagName("iframe")[0].src = "about:blank";
            break;
          }
          case 9: {
            try {
              oldHistoryObject.scrollRestoration;
              assertOk.push([false, "Should have thrown an exception."]);
            } catch (ex) {
              assertOk.push([ex != null, "Did get an exception"]);
            }
            try {
              oldHistoryObject.scrollRestoration = "auto";
              assertOk.push([false, "Should have thrown an exception."]);
            } catch (ex) {
              assertOk.push([ex != null, "Did get an exception"]);
            }
            bc.postMessage({command: "asserts", currentCase, assertOk});
            bc.postMessage({command: "finishing"});
            bc.close();
            window.close();
            break;
          }
        }
      }
      window.onpageshow = (event) => {
        bc.postMessage({command: "pageshow", persisted: event.persisted});
      }
    </script>
  </head>
  <body>
  <div style="border: 1px solid black; height: 5000px;">
  &nbsp;</div>
  <div id="bottom">Hello world</div>
  <a href="#hash" name="hash">hash</a>
  </body>
</html>