fetchpriority-urgency-destination.https.h2.html (9176B)
1 <!DOCTYPE html> 2 <title>Fetch destination tests</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/common/utils.js"></script> 6 <script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script> 7 <script src="support/priority-dependent-content.js"></script> 8 <script> 9 let frame; 10 11 // Set up the service worker and the frame. 12 promise_test(t => { 13 const kScope = 'support/resources/empty.https.h2.html'; 14 const kScript = 'support/fetch-destination-worker.js'; 15 return service_worker_unregister_and_register(t, kScript, kScope) 16 .then(registration => { 17 add_completion_callback(() => { 18 registration.unregister(); 19 }); 20 return wait_for_state(t, registration.installing, 'activated'); 21 }) 22 .then(() => { 23 return with_iframe(kScope); 24 }) 25 .then(f => { 26 frame = f; 27 add_completion_callback(() => { f.remove(); }); 28 }); 29 }, 'Initialize global state'); 30 31 // Actual tests 32 33 function fetchPriorityParam(fetchpriority) { 34 return fetchpriority ? `fetchpriority=${fetchpriority}` : ""; 35 } 36 37 // TODO: should also test what's happening when a fetchpriority 38 // attribute is set on the img, script or link nodes below. 39 40 // TODO: Add tests for more destination. See /fetch/api/request/destination 41 // and bug 1855388. 42 43 // HTMLImageElement - image destination 44 promise_test(async t => { 45 async function getUrgencyForImage(fetchpriority) { 46 let rect = await new Promise((resolve, reject) => { 47 let node = frame.contentWindow.document.createElement("img"); 48 frame.contentWindow.document.body.appendChild(node); 49 node.onload = () => { 50 resolve(node.getBoundingClientRect()); 51 }; 52 node.onerror = reject; 53 node.src = `priority-dependent-content.py?as-type=image&resource-id=image_${fetchpriority}&destination=image&${fetchPriorityParam(fetchpriority)}`; 54 }).catch((e) => { 55 assert_unreached("Fetch errored."); 56 }); 57 return convertRectToUrgency(rect); 58 } 59 60 // See network.fetchpriority.adjustments.images and 61 // nsHttpHandler::UrgencyFromCoSFlags. 62 let normalUrgency = 4; 63 assert_equals(await getUrgencyForImage("low"), normalUrgency + 2); 64 assert_equals(await getUrgencyForImage("high"), normalUrgency - 1); 65 assert_equals(await getUrgencyForImage("auto"), normalUrgency + 1); 66 assert_equals(await getUrgencyForImage(), normalUrgency + 1); 67 }, 'Adjustment of urgency parameter when fetching with an "image" Request.destination'); 68 69 // HTMLScriptElement - script destination 70 promise_test(async t => { 71 async function getUrgencyForScript(fetchpriority) { 72 return await new Promise((resolve, reject) => { 73 const resource_id = `script_${fetchpriority}`; 74 let node = frame.contentWindow.document.createElement("script"); 75 node.onload = () => { 76 resolve(frame.contentWindow.scriptUrgency[resource_id]); 77 }; 78 node.onerror = reject; 79 node.src = `priority-dependent-content.py?as-type=script&resource-id=${resource_id}&destination=script&${fetchPriorityParam(fetchpriority)}`; 80 frame.contentWindow.document.body.appendChild(node); 81 }).catch((e) => { 82 assert_unreached("Fetch errored."); 83 }); 84 } 85 86 // See network.fetchpriority.adjustments.link-preload-script and 87 // nsHttpHandler::UrgencyFromCoSFlags. 88 let normalUrgency = 4; 89 assert_equals(await getUrgencyForScript("low"), normalUrgency + 2); 90 assert_equals(await getUrgencyForScript("high"), normalUrgency); 91 assert_equals(await getUrgencyForScript("auto"), normalUrgency); 92 assert_equals(await getUrgencyForScript(), normalUrgency); 93 }, 'Adjustment of urgency parameter when fetching with a "script" Request.destination'); 94 95 // HTMLLinkElement with rel=stylesheet - style destination 96 promise_test(async t => { 97 async function getUrgencyForStyle(fetchpriority) { 98 let rect = await new Promise((resolve, reject) => { 99 const resource_id = `style_${fetchpriority}`; 100 let node = frame.contentWindow.document.createElement("link"); 101 let div = frame.contentWindow.document.createElement("div"); 102 div.id = `urgency_square_${resource_id}`; 103 node.rel = "stylesheet"; 104 node.onload = () => { 105 resolve(div.getBoundingClientRect()); 106 }; 107 node.onerror = reject; 108 node.href = `priority-dependent-content.py?as-type=style&resource-id=${resource_id}&destination=style&${fetchPriorityParam(fetchpriority)}`; 109 frame.contentWindow.document.body.appendChild(node); 110 frame.contentWindow.document.body.appendChild(div); 111 }).catch(() => { 112 assert_unreached("Fetch errored."); 113 }); 114 return convertRectToUrgency(rect); 115 } 116 117 // See network.fetchpriority.adjustments.link-preload-style and 118 // nsHttpHandler::UrgencyFromCoSFlags. 119 let normalUrgency = 4; 120 assert_equals(await getUrgencyForStyle("low"), normalUrgency + 1); 121 assert_equals(await getUrgencyForStyle("high"), normalUrgency); 122 assert_equals(await getUrgencyForStyle("auto"), normalUrgency); 123 assert_equals(await getUrgencyForStyle(), normalUrgency); 124 }, 'Adjustment of urgency parameter when fetching with a "style" Request.destination'); 125 126 // HTMLAudioElement/HTMLVideoElement - audio/video destination 127 ["audio", "video"].forEach(media => { 128 promise_test(async t => { 129 async function getUrgencyForMedia(fetchpriority) { 130 let duration = await new Promise((resolve, reject) => { 131 let node = frame.contentWindow.document.createElement(media); 132 node.onloadeddata = () => { resolve(node.duration); }; 133 node.onerror = reject; 134 node.src = `priority-dependent-content.py?as-type=${media}&resource-id=${media}_${fetchpriority}&destination=${media}&${fetchPriorityParam(fetchpriority)}`; 135 frame.contentWindow.document.body.appendChild(node); 136 }).catch((e) => { 137 assert_unreached("Fetch errored."); 138 }); 139 return convertDurationToUrgency(duration); 140 } 141 142 // See network.fetchpriority.adjustments.media and 143 // nsHttpHandler::UrgencyFromCoSFlags. 144 let normalUrgency = 4; 145 assert_equals(await getUrgencyForMedia("low"), normalUrgency + 1); 146 assert_equals(await getUrgencyForMedia("high"), normalUrgency); 147 assert_equals(await getUrgencyForMedia("auto"), normalUrgency); 148 assert_equals(await getUrgencyForMedia(), normalUrgency); 149 }, `Adjustment of urgency parameter when fetching with a "${media}" Request.destination`); 150 }); 151 152 // HTMLLinkElement with rel=preload and as=font - font destination 153 promise_test(async t => { 154 async function getUrgencyForFont(fetchpriority) { 155 let fontFamily = `custom_font_priority_${fetchpriority ? fetchpriority : "default"}`; 156 let span = frame.contentWindow.document.createElement("span"); 157 span.style = `font-family: ${fontFamily}; font-size: 10px` 158 span.textContent = 'M'; 159 frame.contentWindow.document.body.appendChild(span); 160 let link = frame.contentWindow.document.createElement("link"); 161 link.rel = "preload"; 162 link.as = "font"; 163 link.crossOrigin = ""; 164 let size = await new Promise((resolve, reject) => { 165 link.onload = async () => { 166 let response = await fetch(link.href, {cache: "force-cache"}); 167 let font_face = new FontFace(fontFamily, 168 await response.arrayBuffer()); 169 frame.contentWindow.document.fonts.add(font_face); 170 resolve(span.getBoundingClientRect().width); 171 }; 172 link.onerror = reject; 173 link.href = `priority-dependent-content.py?as-type=font&resource-id=font_${fetchpriority}_${token()}&destination=font&${fetchPriorityParam(fetchpriority)}&use-cache=true`; 174 frame.contentWindow.document.body.appendChild(link); 175 }).catch(() => { 176 assert_unreached("Fetch errored."); 177 }); 178 return convertSizeToUrgency(size); 179 } 180 181 // See network.fetchpriority.adjustments.link_preload_font and 182 // nsHttpHandler::UrgencyFromCoSFlags. 183 let normalUrgency = 3; 184 assert_equals(await getUrgencyForFont("low"), normalUrgency + 2); 185 assert_equals(await getUrgencyForFont("high"), normalUrgency - 1); 186 assert_equals(await getUrgencyForFont("auto"), normalUrgency); 187 assert_equals(await getUrgencyForFont(), normalUrgency); 188 }, 'Adjustment of urgency parameter when fetching with a "font" Request.destination'); 189 </script>