session-history-prerender.https.html (5921B)
1 <!DOCTYPE html> 2 <!-- 3 "Activation" suffix in these test names communicates to the test harness that 4 this part of the test is run post-activation. 5 --> 6 <script src="/speculation-rules/prerender/resources/utils.js"></script> 7 <script src="session-history-harness.js"></script> 8 <script src="session-history-test-util.js"></script> 9 <body> 10 <script> 11 function testHistoryPushStateInPrerender() { 12 assert(history.length == 1, "Initial history length"); 13 assert(!history.state, "Initial history state"); 14 15 history.pushState("teststate", null, null); 16 17 assert(history.length == 1, "History length unchanged"); 18 assert(history.state == "teststate", "Update state"); 19 } 20 21 function testHistoryReplaceStateInPrerender() { 22 assert(history.length == 1, "Initial history length"); 23 assert(!history.state, "Initial history state"); 24 25 history.replaceState("teststate", null, null); 26 27 assert(history.length == 1, "History length unchanged"); 28 assert(history.state == "teststate", "Update state"); 29 } 30 31 function testLocationAssignInPrerender() { 32 assert(history.length == 1, "Initial history length"); 33 const initialLocation = location.href; 34 location.assign("#test"); 35 36 assert(history.length == 1, "History length unchanged"); 37 assert(location.href != initialLocation, "Update location"); 38 } 39 40 function testLocationReplaceInPrerender() { 41 assert(history.length == 1, "Initial history length"); 42 const initialLocation = location.href; 43 location.replace("#test"); 44 45 assert(history.length == 1, "History length unchanged"); 46 assert(location.href != initialLocation, "Update location"); 47 } 48 49 function testSetLocationHrefInPrerender() { 50 assert(history.length == 1, "Initial history length"); 51 const initialLocation = location.href; 52 location.href = "#test"; 53 54 assert(history.length == 1, "History length unchanged"); 55 assert(location.href != initialLocation, "Update location"); 56 } 57 58 function testSyntheticAnchorClickInPrerender() { 59 assert(history.length == 1, "Initial history length"); 60 const initialLocation = location.href; 61 62 const anchor = document.createElement("a"); 63 anchor.href = "#test"; 64 document.body.appendChild(anchor); 65 66 anchor.click(); 67 68 assert(history.length == 1, "History length unchanged"); 69 assert(location.href != initialLocation, "Update location"); 70 } 71 72 function testHistoryLengthInPrerender() { 73 assert(history.length == 1, "Initial history length"); 74 } 75 76 function testHistoryLengthInPrerenderActivation() { 77 assert(history.length == 2, "History length after activation"); 78 79 // TODO(http://crbug.com/1220992): Test whether calling history.back() 80 // after activation should go back to the initiator page correctly. 81 // We might need a non-trivial refactoring to test this scenario correctly. 82 } 83 84 // This test runs testSubfrarmeNavigationInPrerenderInSubframe() in a 85 // subframe, and waits for a message from a navigated subframe. 86 async function testSubframeNavigationInPrerender() { 87 assert(window.parent == window, "not the top frame"); 88 const params = new URLSearchParams(window.location.search); 89 const testName = params.get("testName"); 90 const uid = params.get("uid"); 91 const resultPromise = waitChannelMessage( 92 `prerender-channel-${testName}InSubframeAfterNavigation`, uid); 93 94 params.set("testName", testName + "InSubframe"); 95 const frame = document.createElement("iframe"); 96 const url = location.pathname + "?" + params.toString(); 97 frame.src = url; 98 document.body.appendChild(frame); 99 const result = await resultPromise; 100 assert(result == "Passed", result); 101 } 102 103 function testSubframeNavigationInPrerenderInSubframe() { 104 assert(window.parent != window, "not in a subframe"); 105 assert(window.parent == window.top, "the direct parent isn't the top"); 106 assert(history.length == 1, "Initial history length"); 107 108 const params = new URLSearchParams(window.location.search); 109 const testName = params.get("testName"); 110 params.set("testName", testName + "AfterNavigation"); 111 location.href = location.pathname + "?" + params.toString(); 112 } 113 114 function testSubframeNavigationInPrerenderInSubframeAfterNavigation() { 115 assert(window.parent != window, "not in a subframe"); 116 assert(window.parent == window.top, "the direct parent isn't the top"); 117 assert(history.length == 1, "History length after subframe navigation"); 118 } 119 120 // This test runs testSubframeReloadInPrerenderInSubframe() in a 121 // subframe, and waits for a message from a navigated subframe. 122 async function testSubframeReloadInPrerender() { 123 assert(window.parent == window, "not the top frame"); 124 const params = new URLSearchParams(window.location.search); 125 const testName = params.get("testName"); 126 const uid = params.get("uid"); 127 const resultPromise = waitChannelMessage( 128 `prerender-channel-${testName}InSubframe`, uid); 129 130 params.set("testName", testName + "InSubframe"); 131 const frame = document.createElement("iframe"); 132 const url = location.pathname + "?" + params.toString(); 133 frame.src = url; 134 document.body.appendChild(frame); 135 const result = await resultPromise; 136 assert(result == "Passed", result); 137 const second_result = await waitChannelMessage( 138 `prerender-channel-${testName}InSubframe`, uid); 139 assert(second_result == "Passed", second_result); 140 } 141 142 function testSubframeReloadInPrerenderInSubframe() { 143 assert(window.parent != window, "not in a subframe"); 144 assert(window.parent == window.top, "the direct parent isn't the top"); 145 assert(history.length == 1, "Initial history length"); 146 window.location.reload(); 147 } 148 </script> 149 </body>