fetch-destination-no-load-event.https.html (4683B)
1 <!DOCTYPE html> 2 <title>Fetch destination tests for resources with no load event</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/common/get-host-info.sub.js"></script> 6 <script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script> 7 <script> 8 let frame; 9 10 // Set up the service worker and the frame. 11 promise_test(t => { 12 const kScope = 'resources/'; 13 const kFrame = 'resources/empty.https.html'; 14 const kScript = 'resources/fetch-destination-worker-no-load-event.js'; 15 return service_worker_unregister_and_register(t, kScript, kScope) 16 .then(registration => { 17 add_completion_callback(() => { 18 registration.unregister(); 19 }); 20 21 return wait_for_state(t, registration.installing, 'activated'); 22 }) 23 .then(() => { 24 return with_iframe(kFrame); 25 }) 26 .then(f => { 27 frame = f; 28 add_completion_callback(() => { f.remove(); }); 29 }); 30 }, 'Initialize global state'); 31 32 var waitOnMessageFromSW = async t => { 33 await new Promise((resolve, reject) => { 34 frame.contentWindow.navigator.serviceWorker.onmessage = t.step_func(event => { 35 if (event.data == "PASS") { 36 resolve(); 37 } else { 38 reject(); 39 } 40 }); 41 }).catch(() => {; 42 assert_unreached("Wrong destination."); 43 }); 44 t.add_cleanup(() => { frame.contentWindow.navigator.serviceWorker.onmessage = null; }); 45 } 46 // Actual tests 47 48 // Image destination 49 //////////////////// 50 51 // CSS background image - image destination 52 promise_test(async t => { 53 let node = frame.contentWindow.document.createElement("div"); 54 node.style = "background-image: url(dummy.png?t=bg2&dest=image)"; 55 frame.contentWindow.document.body.appendChild(node); 56 57 await waitOnMessageFromSW(t); 58 }, 'Background image fetches with an "image" Request.destination'); 59 60 // SVG use element - image destination 61 // See for discussion https://github.com/whatwg/fetch/issues/1012. 62 promise_test(async t => { 63 const doc = frame.contentWindow.document; 64 65 let svg = doc.createElementNS("http://www.w3.org/2000/svg", "svg"); 66 let use = doc.createElementNS("http://www.w3.org/2000/svg", "use"); 67 use.setAttribute("href", "dummy.svg?t=use&dest=image#ref"); 68 svg.appendChild(use); 69 doc.body.appendChild(svg); 70 71 await waitOnMessageFromSW(t); 72 }, 'SVG use element fetches with an "image" Request.destination'); 73 74 // Font destination 75 /////////////////// 76 77 // Font loading API - font destination 78 promise_test(async t => { 79 let font = new frame.contentWindow.FontFace("foo", "url(dummy.ttf?t=api&dest=font)"); 80 font.load(); 81 82 await waitOnMessageFromSW(t); 83 }, 'Font loading API fetches with an "font" Request.destination'); 84 85 // CSS font - font destination 86 promise_test(async t => { 87 let style = frame.contentWindow.document.createElement("style"); 88 style.innerHTML = "@font-face { font-family: foo; src: url(dummy.ttf?t=css&dest=font); }"; 89 style.innerHTML += "div {font-family: foo; }"; 90 let div = frame.contentWindow.document.createElement("div"); 91 div.innerHTML = "bar"; 92 frame.contentWindow.document.body.appendChild(style); 93 frame.contentWindow.document.body.appendChild(div); 94 95 await waitOnMessageFromSW(t); 96 }, 'CSS font fetches with an "font" Request.destination'); 97 98 // Empty string destination 99 /////////////////////////// 100 101 // sendBeacon() - empty string destination 102 promise_test(async t => { 103 frame.contentWindow.navigator.sendBeacon("dummy?t=beacon&dest=", "foobar"); 104 105 await waitOnMessageFromSW(t); 106 }, 'sendBeacon() fetches with an empty string Request.destination'); 107 108 // Cache.add() - empty string destination 109 promise_test(async t => { 110 frame.contentWindow.caches.open("foo").then(cache => { 111 cache.add("dummy?t=cache&dest="); 112 }); 113 114 await waitOnMessageFromSW(t); 115 }, 'Cache.add() fetches with an empty string Request.destination'); 116 117 // script destination 118 ///////////////////// 119 120 // importScripts() - script destination 121 promise_test(async t => { 122 let worker = new frame.contentWindow.Worker("importer.js"); 123 124 await waitOnMessageFromSW(t); 125 }, 'importScripts() fetches with a "script" Request.destination'); 126 127 // style destination 128 ///////////////////// 129 // @import - style destination 130 promise_test(async t => { 131 let node = frame.contentWindow.document.createElement("style"); 132 node.innerHTML = '@import url("dummy?t=import&dest=style")'; 133 frame.contentWindow.document.body.appendChild(node); 134 135 await waitOnMessageFromSW(t); 136 }, '@import fetches with a "style" Request.destination'); 137 138 </script>