test_bfcache.html (3438B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test for bfcache and BroadcastChannel</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 8 </head> 9 <body> 10 <script type="application/javascript"> 11 12 /* 13 * The test opens a new window. Then a message 'load' is sent there to load 14 * another page. If expectedPersisted is false, a dummy message is sent 15 * through a BroadcastChannel which the first has registered to use. 16 * That should evict the page from bfcache. 17 * 'back' message is sent to call history.back(). 18 * The page which is loaded from session history should be persisted if 19 * expectedPersisted is true. 20 */ 21 22 SimpleTest.waitForExplicitFinish(); 23 var testUrl1 = "testUrl1_bfcache.html"; 24 25 26 function executeTest() { 27 var bc1 = new BroadcastChannel("testUrl1_bfcache"); 28 var bc2 = new BroadcastChannel("testUrl2_bfcache"); 29 bc1.onmessage = function(event) { 30 if (event.data == "closed") { 31 info("Closed"); 32 runTest(); 33 return; 34 } 35 page1Shown(event.data); 36 }; 37 bc2.onmessage = function(event) { page2Shown(event.data); }; 38 39 var counter = 0; 40 var expectedPersisted = false; 41 var bc = new BroadcastChannel("a"); 42 43 function page1Shown(e) { 44 if (counter == 0) { 45 ok(!e.persisted, "test page should have been persisted initially"); 46 bc1.postMessage("load"); 47 } else { 48 is(e.persisted, expectedPersisted, "test page should have been persisted in pageshow"); 49 bc1.postMessage("close"); 50 } 51 52 counter++; 53 } 54 55 function page2Shown() { 56 if (!expectedPersisted) { 57 SimpleTest.executeSoon(function() { 58 info("Posting a message."); 59 bc.postMessage(42); 60 }); 61 } 62 63 SimpleTest.executeSoon(function() { 64 info("Going back"); 65 bc2.postMessage("back"); 66 }); 67 } 68 69 var tests = [ 70 { expectedPersisted: true }, 71 { expectedPersisted: false }, 72 ]; 73 74 function runTest() { 75 if (!tests.length) { 76 bc.close(); 77 bc1.close(); 78 bc2.close(); 79 SimpleTest.finish(); 80 return; 81 } 82 83 var test = tests.shift(); 84 85 counter = 0; 86 expectedPersisted = test.expectedPersisted; 87 window.open(testUrl1, "", "noopener"); 88 } 89 90 91 // If Fission is disabled, the pref is no-op. 92 SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => { 93 runTest(); 94 }); 95 96 } 97 98 if (isXOrigin) { 99 // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5) 100 // Acquire storage access permission here so that the BroadcastChannel used to 101 // communicate with the opened windows works in xorigin tests. Otherwise, 102 // the iframe containing this page is isolated from first-party storage access, 103 // which isolates BroadcastChannel communication. 104 SpecialPowers.wrap(document).notifyUserGestureActivation(); 105 SpecialPowers.addPermission("storageAccessAPI", true, window.location.href).then(() => { 106 SpecialPowers.wrap(document).requestStorageAccess().then(() => { 107 SpecialPowers.pushPrefEnv({ 108 set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]] 109 }).then(() => { 110 executeTest(); 111 }); 112 }); 113 }); 114 } else { 115 executeTest(); 116 } 117 118 </script> 119 </body> 120 </html>