window-open-in-prerenderingchange.html (2064B)
1 <!DOCTYPE html> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <script src="utils.js"></script> 5 <script> 6 7 // This file is loaded twice. First this is loaded as a page to trigger 8 // prerendering and then loaded as a prerendering page. The trigger page 9 // activates the prerendering page. 10 11 // Runs as the trigger page. This page starts prerendering and waits for signal 12 // from the prerendering page. After the signal, this page starts activation. 13 function runAsTriggerPage() { 14 assert_false(document.prerendering); 15 16 // Start prerendering. 17 const prerendering_url = location.href + '&prerendering=true'; 18 startPrerendering(prerendering_url); 19 20 // Activate the prerendering page once it gets ready. 21 const bc = new PrerenderChannel('activation-ready'); 22 bc.onmessage = () => { window.location = prerendering_url }; 23 } 24 25 // Runs as prerendeirng page. First this page waits for the load event and 26 // signals to the trigger page for starting activation. Then, this page fires 27 // the prerenderingchange event and tests window.open() in the event. 28 function runAsPrerenderingPage() { 29 assert_true(document.prerendering); 30 31 window.onload = () => { 32 assert_true(document.prerendering); 33 34 // Notify the trigger page of activation ready. 35 const bc = new PrerenderChannel('activation-ready'); 36 bc.postMessage('ready for activation'); 37 } 38 39 new PrerenderChannel('close').addEventListener('message', () => { 40 window.close(); 41 }); 42 document.onprerenderingchange = () => { 43 assert_false(document.prerendering); 44 45 // Attempt to open a window in the prerenderingchange event. 46 const win = window.open('empty.html', '_blank'); 47 48 // Send the result to the test runner page. 49 const bc = new PrerenderChannel('result'); 50 if (win) { 51 bc.postMessage('opened'); 52 win.close(); 53 } else { 54 bc.postMessage('failed to open'); 55 } 56 }; 57 } 58 59 if (new URLSearchParams(location.search).has('prerendering')) { 60 runAsPrerenderingPage(); 61 } else { 62 runAsTriggerPage(); 63 } 64 65 </script>