file_scrollRestoration_part3_nobfcache.html (6218B)
1 <html> 2 <head> 3 <script> 4 var oldHistoryObject = null; 5 var currCaseForIframe = 0; 6 var bc = new BroadcastChannel("bug1155730_part3"); 7 bc.onmessage = (msgEvent) => { 8 var msg = msgEvent.data; 9 var command = msg.command; 10 if (command == "test") { 11 var currentCase = msg.currentCase; 12 test(currentCase); 13 } 14 } 15 16 // If onpopstate event takes place, check if we need to call 'test()' 17 var callTest = false; 18 var nextCase = 0; 19 window.onpopstate = () => { 20 if (callTest) { 21 callTest = false; 22 setTimeout(() => { 23 test(nextCase); 24 }); 25 } 26 } 27 28 function test(currentCase) { 29 var assertIs = []; 30 var assertOk = []; 31 var assertIsNot = []; 32 switch (currentCase) { 33 case 1: { 34 history.scrollRestoration = "manual"; 35 window.location.hash = "hash"; 36 bc.postMessage({command: "nextCase"}); 37 requestAnimationFrame(() => { 38 test(currentCase + 1); 39 }); 40 break; 41 } 42 case 2: { 43 assertIsNot.push([Math.round(window.scrollY), 0, "Should have scrolled to #hash."]); 44 assertIs.push([history.scrollRestoration, "manual", "Should have the same scrollRestoration mode as before fragment navigation."]); 45 bc.postMessage({command: "asserts", currentCase, assertIs, assertIsNot}); 46 window.location.href = "file_scrollRestoration_navigate.html"; 47 break; 48 } 49 case 3: { 50 assertIs.push([window.scrollY, 0, "Shouldn't have kept the old scroll position."]); 51 assertIs.push([history.scrollRestoration, "manual", "Should have the same scrollRestoration mode as before fragment navigation."]); 52 bc.postMessage({command: "asserts", currentCase, assertIs}); 53 history.scrollRestoration = "auto"; 54 document.getElementById("bottom").scrollIntoView(); 55 history.pushState({ state: "state1" }, "state1"); 56 history.pushState({ state: "state2" }, "state2"); 57 window.scrollTo(0, 0); 58 bc.postMessage({command: "nextCase"}); 59 callTest = true; 60 nextCase = currentCase + 1; 61 history.back(); // go back to state 1 62 break; 63 } 64 case 4: { 65 assertIsNot.push([Math.round(window.scrollY), 0, "Should have scrolled back to the state1's position"]); 66 assertIs.push([history.state.state, "state1", "Unexpected state."]); 67 bc.postMessage({command: "asserts", currentCase, assertIs, assertIsNot}); 68 69 history.scrollRestoration = "manual"; 70 document.getElementById("bottom").scrollIntoView(); 71 history.pushState({ state: "state3" }, "state3"); 72 history.pushState({ state: "state4" }, "state4"); 73 window.scrollTo(0, 0); 74 bc.postMessage({command: "nextCase"}); 75 callTest = true; 76 nextCase = currentCase + 1; 77 history.back(); // go back to state 3 78 break; 79 } 80 case 5: { 81 assertIs.push([Math.round(window.scrollY), 0, "Shouldn't have scrolled back to the state3's position"]); 82 assertIs.push([history.state.state, "state3", "Unexpected state."]); 83 84 history.pushState({ state: "state5" }, "state5"); 85 history.scrollRestoration = "auto"; 86 document.getElementById("bottom").scrollIntoView(); 87 assertIsNot.push([Math.round(window.scrollY), 0, "Should have scrolled to 'bottom'."]); 88 bc.postMessage({command: "asserts", currentCase, assertIs, assertIsNot}); 89 bc.postMessage({command: "nextCase"}); 90 callTest = true; 91 nextCase = currentCase + 1; 92 // go back to state 3 (state 4 was removed when state 5 was pushed) 93 history.back(); 94 break; 95 } 96 case 6: { 97 window.scrollTo(0, 0); 98 bc.postMessage({command: "nextCase"}); 99 callTest = true; 100 nextCase = currentCase + 1; 101 history.forward(); 102 break; 103 } 104 case 7: { 105 assertIsNot.push([Math.round(window.scrollY), 0, "Should have scrolled back to the state5's position"]); 106 bc.postMessage({command: "asserts", currentCase, assertIsNot}); 107 108 var ifr = document.createElement("iframe"); 109 ifr.src = "data:text/html,"; 110 document.body.appendChild(ifr); 111 bc.postMessage({command: "nextCase"}); 112 currCaseForIframe = currentCase + 1; 113 ifr.onload = () => { 114 test(currCaseForIframe); 115 }; 116 break; 117 } 118 case 8: { 119 oldHistoryObject = SpecialPowers.wrap(document.getElementsByTagName("iframe")[0]).contentWindow.history; 120 bc.postMessage({command: "nextCase"}); 121 currCaseForIframe++; 122 document.getElementsByTagName("iframe")[0].src = "about:blank"; 123 break; 124 } 125 case 9: { 126 try { 127 oldHistoryObject.scrollRestoration; 128 assertOk.push([false, "Should have thrown an exception."]); 129 } catch (ex) { 130 assertOk.push([ex != null, "Did get an exception"]); 131 } 132 try { 133 oldHistoryObject.scrollRestoration = "auto"; 134 assertOk.push([false, "Should have thrown an exception."]); 135 } catch (ex) { 136 assertOk.push([ex != null, "Did get an exception"]); 137 } 138 bc.postMessage({command: "asserts", currentCase, assertOk}); 139 bc.postMessage({command: "finishing"}); 140 bc.close(); 141 window.close(); 142 break; 143 } 144 } 145 } 146 window.onpageshow = (event) => { 147 bc.postMessage({command: "pageshow", persisted: event.persisted}); 148 } 149 </script> 150 </head> 151 <body> 152 <div style="border: 1px solid black; height: 5000px;"> 153 </div> 154 <div id="bottom">Hello world</div> 155 <a href="#hash" name="hash">hash</a> 156 </body> 157 </html>