test_online_offline_bfcache.html (3477B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Online/Offline with BFCache</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" href="/tests/SimpleTest/test.css"/> 8 <script> 9 10 /* 11 * The test is designed to work with and without bfcache. 12 * (1) First the test opens a window which then loads another page which 13 * goes back to the original page to detect if bfcache is enabled. If 14 * bfcache isn't enabled, close message is sent to the opened window and it 15 * closes itself and sends a message back and the test finishes. 16 * (2) The browser is set to offline mode. The opened page sends message 17 * that it has received offline event. This controller page then asks the 18 * page to go forward. The page which comes out from the bfcache gets 19 * offline event and sends message about that to this controller. 20 * (3) Browser is set to online mode. Similar cycle as with offline happens. 21 * (4) Controller page sends close message to the opened window and it 22 * closes itself and sends a message back and the test finishes. 23 */ 24 25 function offlineOnline(online) { 26 function offlineFn() { 27 /* eslint-env mozilla/chrome-script */ 28 Services.io.offline = true; 29 } 30 function onlineFn() { 31 /* eslint-env mozilla/chrome-script */ 32 Services.io.offline = false; 33 } 34 SpecialPowers.loadChromeScript(online ? onlineFn : offlineFn); 35 } 36 37 var bc = SpecialPowers.wrap(BroadcastChannel).unpartitionedTestingChannel("online_offline_bfcache"); 38 var pageshowCount = 0; 39 var offlineCount = 0; 40 var onlineCount = 0; 41 42 bc.onmessage = function(event) { 43 if (event.data.event == "pageshow") { 44 ++pageshowCount; 45 info("pageshow " + pageshowCount); 46 if (pageshowCount == 1) { 47 ok(!event.data.persisted); 48 bc.postMessage("nextpage"); 49 } else if (pageshowCount == 2) { 50 ok(!event.data.persisted); 51 bc.postMessage("back"); 52 } else if (pageshowCount == 3) { 53 if (!event.data.persisted) { 54 info("BFCache is not enabled, return early"); 55 bc.postMessage("close"); 56 } else { 57 offlineOnline(false); 58 } 59 } 60 } else if (event.data == "offline") { 61 ++offlineCount; 62 info("offline " + offlineCount); 63 if (offlineCount == 1) { 64 bc.postMessage("forward"); 65 } else if (offlineCount == 2) { 66 offlineOnline(true); 67 } else { 68 ok(false, "unexpected offline event"); 69 } 70 } else if (event.data == "online") { 71 ++onlineCount; 72 info("online " + onlineCount); 73 if (onlineCount == 1) { 74 bc.postMessage("back"); 75 } else if (onlineCount == 2) { 76 bc.postMessage("close"); 77 } else { 78 ok(false, "unexpected online event"); 79 } 80 } else if ("closed") { 81 ok(true, "Did pass the test"); 82 bc.close(); 83 SimpleTest.finish(); 84 } 85 }; 86 87 function runTest() { 88 SpecialPowers.pushPrefEnv({"set": [["network.manage-offline-status", false]]}, function() { 89 window.open("file_online_offline_bfcache.html", "", "noopener"); 90 }); 91 } 92 93 SimpleTest.waitForExplicitFinish(); 94 </script> 95 </head> 96 <body onload="runTest()"> 97 <p id="display"></p> 98 <div id="content" style="display: none"></div> 99 <pre id="test"></pre> 100 </body> 101 </html>