main-frame-navigation.html (2886B)
1 <!DOCTYPE html> 2 <script src="/common/utils.js"></script> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/speculation-rules/prerender/resources/utils.js"></script> 6 <script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script> 7 <script> 8 9 // The main test page loads the initiator page, then the initiator page will 10 // prerender itself with the `prerendering` parameter. The prerendered page will 11 // trigger a main frame navigation with the `navigation` parameter. 12 const params = new URLSearchParams(location.search); 13 const uid = params.get('uid'); 14 15 const isPrerendering = params.has('prerendering'); 16 const isNavigation = params.has('navigation'); 17 18 if (isPrerendering && isNavigation) { 19 assert_true(document.prerendering); 20 21 const result = { 22 // Check the value of document.prerendering now and after activation. 23 prerenderingValueBeforeActivation: document.prerendering, 24 prerenderingValueAfterActivation: null, 25 26 // True if the prerenderingchange event is fired. 27 onprerenderingchangeCalled: false, 28 }; 29 30 window.addEventListener('load', () => { 31 const prerenderChannel = new PrerenderChannel('prerender-channel', uid); 32 prerenderChannel.postMessage('readyToActivate'); 33 prerenderChannel.close(); 34 }); 35 36 document.addEventListener('prerenderingchange', (e) => { 37 assert_false(document.prerendering); 38 39 const entry = performance.getEntriesByType('navigation')[0]; 40 assert_greater_than_equal(entry.activationStart, 0, 41 'activationStart must be greater than 0') 42 43 result.onprerenderingchangeCalled = true; 44 result.prerenderingValueAfterActivation = document.prerendering; 45 46 const resultChannel = new PrerenderChannel('result', uid); 47 resultChannel.postMessage(result); 48 resultChannel.close(); 49 window.close(); 50 }); 51 } else if (isPrerendering) { 52 assert_true(document.prerendering); 53 54 location.href = location.href + '&navigation'; 55 } else { 56 assert_false(document.prerendering); 57 58 const prerenderingUrl = location.href + '&prerendering'; 59 60 const prerenderChannel = new PrerenderChannel('prerender-channel', uid); 61 const readyToActivate = new Promise((resolve, reject) => { 62 prerenderChannel.addEventListener('message', e => { 63 if (e.data === 'readyToActivate') { 64 resolve(); 65 } else { 66 reject(`The initiator page receives an unsupported message: ${e.data}`); 67 } 68 }); 69 }); 70 71 // Activate the page when prerendering is ready. 72 readyToActivate.then(() => { 73 window.location = prerenderingUrl.toString(); 74 }).catch(e => { 75 const resultChannel = new PrerenderChannel('result', uid); 76 resultChannel.postMessage( 77 `Failed to navigate the prerendered page: ${e.toString()}`); 78 resultChannel.close(); 79 window.close(); 80 }); 81 82 startPrerendering(prerenderingUrl); 83 } 84 </script>