test_bug669671.html (4646B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=669671 5 --> 6 <head> 7 <title>Test for Bug 669671</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=669671">Mozilla Bug 669671</a> 13 <p id="display"></p> 14 <div id="content" style="display: none"> 15 16 </div> 17 <pre id="test"> 18 <script type="application/javascript"> 19 20 /** 21 * Test for Bug 669671. 22 * 23 * This is a bit complicated. We have a script, file_bug669671.sjs, which counts 24 * how many times it's loaded and returns that count in the body of an HTML 25 * document. For brevity, call this page X. 26 * 27 * X is sent with Cache-Control: max-age=0 and can't be bfcached (it has an 28 * onunload handler). Our test does the following in a popup: 29 * 30 * 1) Load X?pushed, to prime the cache. 31 * 2) Navigate to X. 32 * 3) Call pushState and navigate from X to X?pushed. 33 * 4) Navigate to X?navigated. 34 * 5) Go back (to X?pushed). 35 * 36 * We do all this work so we can check that in step 5, we fetch X?pushed from 37 * the network -- we shouldn't use our cached copy, because of the 38 * cache-control header X sends. 39 * 40 * Then we go back and repeat the whole process but call history.replaceState 41 * instead of pushState. And for good measure, we test once more, this time 42 * modifying only the hash of the URI using replaceState. In this case, we 43 * should* load from the cache. 44 * 45 */ 46 SimpleTest.requestLongerTimeout(2); 47 SimpleTest.waitForExplicitFinish(); 48 49 function onChildLoad() { 50 SimpleTest.executeSoon(function() { gGen.next(); }); 51 } 52 53 var _loadCount = 0; 54 function checkPopupLoadCount() { 55 is(popup.document.body.innerHTML, _loadCount + "", "Load count"); 56 57 // We normally want to increment _loadCount here. But if the test fails 58 // because we didn't do a load we should have, let's not cause a cascade of 59 // failures by incrementing _loadCount. 60 var origCount = _loadCount; 61 if (popup.document.body.innerHTML >= _loadCount + "") 62 _loadCount++; 63 return origCount; 64 } 65 66 function* test() { 67 // Step 0 - Make sure the count is reset to 0 in case of reload 68 popup.location = "file_bug669671.sjs?countreset"; 69 yield; 70 is(popup.document.body.innerHTML, "0", 71 "Load count should be reset to 0"); 72 73 // Step 1 - The popup's body counts how many times we've requested the 74 // resource. This is the first time we've requested it, so it should be '0'. 75 checkPopupLoadCount(); 76 77 // Step 2 - We'll get another onChildLoad when this finishes. 78 popup.location = "file_bug669671.sjs"; 79 yield undefined; 80 81 // Step 3 - Call pushState and change the URI back to ?pushed. 82 checkPopupLoadCount(); 83 popup.history.pushState("", "", "?pushed"); 84 85 // Step 4 - Navigate away. This should trigger another onChildLoad. 86 popup.location = "file_bug669671.sjs?navigated-1"; 87 yield undefined; 88 89 // Step 5 - Go back. This should result in another onload (because the file is 90 // not in bfcache) and should be the fourth time we've requested the sjs file. 91 checkPopupLoadCount(); 92 popup.history.back(); 93 yield undefined; 94 95 // This is the check which was failing before we fixed the bug. 96 checkPopupLoadCount(); 97 98 popup.close(); 99 100 // Do the whole thing again, but with replaceState. 101 popup = window.open("file_bug669671.sjs?replaced"); 102 yield undefined; 103 checkPopupLoadCount(); 104 popup.location = "file_bug669671.sjs"; 105 yield undefined; 106 checkPopupLoadCount(); 107 popup.history.replaceState("", "", "?replaced"); 108 popup.location = "file_bug669671.sjs?navigated-2"; 109 yield undefined; 110 checkPopupLoadCount(); 111 popup.history.back(); 112 yield undefined; 113 checkPopupLoadCount(); 114 popup.close(); 115 116 // Once more, with feeling. Notice that we don't have to prime the cache 117 // with an extra load here, because X and X#hash share the same cache entry. 118 popup = window.open("file_bug669671.sjs?hash-test"); 119 yield undefined; 120 var initialCount = checkPopupLoadCount(); 121 popup.history.replaceState("", "", "#hash"); 122 popup.location = "file_bug669671.sjs?navigated-3"; 123 yield undefined; 124 checkPopupLoadCount(); 125 popup.history.back(); 126 yield undefined; 127 is(popup.document.body.innerHTML, initialCount + "", 128 "Load count (should be cached)"); 129 popup.close(); 130 131 SimpleTest.finish(); 132 } 133 134 var gGen = test(); 135 var popup; 136 137 // Disable RCWN to make cache behavior deterministic. 138 SpecialPowers.pushPrefEnv({set: [["network.http.rcwn.enabled", false]]}, () => { 139 // This will call into onChildLoad once it loads. 140 popup = window.open("file_bug669671.sjs?pushed"); 141 }); 142 </script> 143 </pre> 144 </body> 145 </html>