list-of-available-images-matching.https.html (2198B)
1 <!doctype html> 2 <html> 3 <title>List of available images tuple-matching logic</title> 4 <link rel="author" title="Dom Farolino" href="mailto:dom@chromium.org"> 5 <link rel="help" href="https://html.spec.whatwg.org/multipage/#the-list-of-available-images"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script> 9 <script src="/common/get-host-info.sub.js"></script> 10 11 <script> 12 const path = location.origin + '/html/semantics/embedded-content/the-img-element/'; 13 const image_url = path + 'image-1.jpg'; 14 15 function syncWait(ms) { 16 const start = Date.now(); 17 while (Date.now() - start < ms); 18 } 19 20 let sawNoCorsRequest = false; 21 22 navigator.serviceWorker.onmessage = ({data}) => { 23 if (data.url === image_url && data.mode === 'no-cors') { 24 sawNoCorsRequest = true; 25 } 26 }; 27 28 promise_test(t => { 29 return service_worker_unregister_and_register(t, 'resources/sw.js', path) 30 .then(r => { 31 return wait_for_state(t, r.installing, 'activated'); 32 }); 33 }, 'registering service worker'); 34 35 promise_test(async t => { 36 const img = new Image(); 37 38 function load_img_promise() { 39 return new Promise((resolve, reject) => { 40 img.onload = resolve; 41 img.onerror = e => { reject("The img should not fail to load") }; 42 43 img.src = image_url; 44 // If there is not a matching image in the list of available images, the 45 // actual fetch operation is queued as a microtask, so we will see a 46 // request with mode 'cors' due to setting the crossorigin attribute 47 // below. 48 syncWait(500); 49 img.crossOrigin = 'anonymous'; 50 }); 51 }; 52 53 await load_img_promise(); 54 assert_false(sawNoCorsRequest, "The image is not fetched with mode: no-cors"); 55 await new Promise(resolve => { 56 img.onload = img.onerror = resolve; 57 img.src = ''; 58 img.crossOrigin = null; 59 }); 60 await load_img_promise(); 61 assert_false(sawNoCorsRequest, "The image is not fetched with mode: no-cors"); 62 63 }, 'list of available images tuple-matching logic'); 64 65 promise_test(t => { 66 return service_worker_unregister(t, path); 67 }, 'unregistering service worker'); 68 </script>