test_ship_beforeunload_fired.html (2536B)
1 <html> 2 <head> 3 <title> 4 Test that ensures beforeunload is fired when session-history-in-parent is enabled 5 </title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" href="/tests/SimpleTest/test.css"/> 8 </head> 9 <script> 10 SimpleTest.waitForExplicitFinish(); 11 12 /* 13 * This test ensures beforeunload is fired on the current page 14 * when it is entering BFCache and the next page is coming out 15 * from BFCache 16 * 17 * (1) The controller page opens a new window, and page A is loaded there. 18 * (2) Page A then navigates to page B, and a beforeunload event 19 * listener is registered on page B. 20 * (3) Page B then navigates back to page A, and the beforeunload handler 21 * should send a message to the controller page. 22 * (4) Page A then navigates back to page B to check if page B has 23 * been successfully added to BFCache. 24 */ 25 26 var bc = SpecialPowers.wrap(BroadcastChannel).unpartitionedTestingChannel("ship_beforeunload"); 27 var pageshowCount = 0; 28 var beforeUnloadFired = false; 29 bc.onmessage = function(event) { 30 if (event.data.type == "pageshow") { 31 ++pageshowCount; 32 if (pageshowCount == 1) { 33 bc.postMessage({action: "navigate_to_page_b"}); 34 } else if (pageshowCount == 2) { 35 ok(!event.data.persisted, "?pageb shouldn't in BFCache because it's the first navigation"); 36 bc.postMessage({action: "register_beforeunload", loadNextPageFromSessionHistory: true}); 37 } else if (pageshowCount == 3) { 38 ok(event.data.persisted, "navigated back to page A that was in BFCacache from page B"); 39 ok(beforeUnloadFired, "beforeunload has fired on page B"); 40 bc.postMessage({action: "back_to_page_b", forwardNavigateToPageB: true}); 41 } else if (pageshowCount == 4) { 42 ok(event.data.persisted, "page B has beforeunload fired and also entered BFCache"); 43 bc.postMessage({action: "close"}); 44 SimpleTest.finish(); 45 } 46 } else if (event.data == "beforeunload_fired") { 47 beforeUnloadFired = true; 48 } 49 } 50 51 function runTest() { 52 SpecialPowers.pushPrefEnv({"set": [ 53 ["fission.bfcacheInParent", true], 54 ["docshell.shistory.bfcache.ship_allow_beforeunload_listeners", true] 55 ]}, 56 function() { 57 window.open("file_ship_beforeunload_fired.html", "", "noopener"); 58 } 59 ); 60 } 61 </script> 62 <body onload="runTest()"></body> 63 </html>