navigation-id.helper.js (4900B)
1 // The test functions called in the navigation-counter test. They rely on 2 // artifacts defined in 3 // '/html/browsers/browsing-the-web/back-forward-cache/resources/helper.sub.js' 4 // which should be included before this file to use these functions. 5 6 // This function is to obtain navigation ids of all performance entries to 7 // verify. 8 let testInitial = () => { 9 return window.performance.getEntries().map(e => e.navigationId); 10 } 11 12 let testMarkMeasure = (markId, markName, MeasureName) => { 13 const markName1 = 'test-mark'; 14 const markName2 = 'test-mark' + markId; 15 const measureName = 'test-measure' + markId; 16 17 window.performance.mark(markName1); 18 window.performance.mark(markName2); 19 window.performance.measure(measureName, markName1, markName2); 20 return window.performance.getEntriesByName(markName2).concat( 21 window.performance.getEntriesByName(measureName)).map(e => e.navigationId); 22 } 23 24 let testResourceTiming = async (resourceTimingEntryId) => { 25 let navigationId; 26 27 let p = new Promise(resolve => { 28 new PerformanceObserver((list) => { 29 const entry = list.getEntries().find( 30 e => e.name.includes('json_resource' + resourceTimingEntryId)); 31 if (entry) { 32 navigationId = entry.navigationId; 33 resolve(); 34 } 35 }).observe({ type: 'resource' }); 36 }); 37 38 const resp = await fetch( 39 '/performance-timeline/resources/json_resource' + resourceTimingEntryId + '.json'); 40 await p; 41 return [navigationId]; 42 } 43 44 let testElementTiming = async (elementTimingEntryId) => { 45 let navigationId; 46 let p = new Promise(resolve => { 47 new PerformanceObserver((list) => { 48 const entry = list.getEntries().find( 49 e => e.entryType === 'element' && e.identifier === 'test-element-timing' + elementTimingEntryId); 50 if (entry) { 51 navigationId = entry.navigationId; 52 resolve(); 53 } 54 }).observe({ type: 'element' }); 55 }); 56 57 let el = document.createElement('p'); 58 el.setAttribute('elementtiming', 'test-element-timing' + elementTimingEntryId); 59 el.textContent = 'test element timing text'; 60 document.body.appendChild(el); 61 await p; 62 return [navigationId]; 63 } 64 65 let testLongTask = async () => { 66 let navigationIds = []; 67 68 let p = new Promise(resolve => { 69 new PerformanceObserver((list) => { 70 const entry = list.getEntries().find(e => e.entryType === 'longtask') 71 if (entry) { 72 navigationIds.push(entry.navigationId); 73 navigationIds = navigationIds.concat( 74 entry.attribution.map(a => a.navigationId)); 75 resolve(); 76 } 77 }).observe({ type: 'longtask' }); 78 }); 79 80 const script = document.createElement('script'); 81 script.src = '/performance-timeline/resources/make_long_task.js'; 82 document.body.appendChild(script); 83 await p; 84 document.body.removeChild(script); 85 return navigationIds; 86 } 87 88 const testFunctionMap = { 89 'mark_measure': testMarkMeasure, 90 'resource_timing': testResourceTiming, 91 'element_timing': testElementTiming, 92 'long_task_task_attribution': testLongTask, 93 }; 94 95 function runNavigationIdTest(params, description) { 96 const defaultParams = { 97 openFunc: url => window.open(url, '_blank', 'noopener'), 98 scripts: [], 99 funcBeforeNavigation: () => { }, 100 targetOrigin: originCrossSite, 101 navigationTimes: 4, 102 funcAfterAssertion: () => { }, 103 } // Apply defaults. 104 params = { ...defaultParams, ...params }; 105 106 promise_test(async t => { 107 const pageA = new RemoteContext(token()); 108 const pageB = new RemoteContext(token()); 109 110 const urlA = executorPath + pageA.context_id; 111 const urlB = params.targetOrigin + executorPath + pageB.context_id; 112 // Open url A. 113 params.openFunc(urlA); 114 await pageA.execute_script(waitForPageShow); 115 116 // Assert navigation ids of all performance entries are the same. 117 let navigationIds = await pageA.execute_script(testInitial); 118 assert_true( 119 navigationIds.every(t => t === navigationIds[0]), 120 'Navigation Ids should be the same as the initial load.'); 121 122 for (i = 1; i <= params.navigationTimes; i++) { 123 // Navigate away to url B and back. 124 await navigateAndThenBack(pageA, pageB, urlB); 125 126 // Assert new navigation ids are generated when the document is load from bfcache. 127 let nextNavigationIds = await pageA.execute_script( 128 testFunctionMap[params.testName], [i + 1]); 129 130 // Assert navigation ids of all performance entries are the same. 131 assert_true( 132 nextNavigationIds.every(t => t === nextNavigationIds[0]), 133 'All Navigation Ids should be same after bfcache navigation.'); 134 135 // Assert navigation ids after bfcache navigation are different from those before. 136 assert_true( 137 navigationIds[0] !== nextNavigationIds[0], 138 params.testName + 139 ' Navigation Ids should be re-generated and different from the previous ones.'); 140 141 navigationIds = nextNavigationIds; 142 } 143 }, description); 144 }