|
| 1 | +<!doctype html> |
| 2 | +<meta charset=utf-8> |
| 3 | +<title>img.complete shouldn't change on viewport changes that don't change the selected source</title> |
| 4 | +<link rel=help href="https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete"> |
| 5 | +<link rel=help href="https://html.spec.whatwg.org/multipage/images.html#reacting-to-environment-changes"> |
| 6 | +<script src="/resources/testharness.js"></script> |
| 7 | +<script src="/resources/testharnessreport.js"></script> |
| 8 | +<body> |
| 9 | +<script> |
| 10 | + // The <source> and the <img> point at the same URL, so no matter which one is |
| 11 | + // selected reacting to environment changes never starts a new load. |
| 12 | + const IFRAME_SRCDOC = ` |
| 13 | + <style>html, body { margin: 0 }</style> |
| 14 | + <picture> |
| 15 | + <source media='(max-width: 150px)' srcset='/images/green-2x2.png'> |
| 16 | + <img src='/images/green-2x2.png'> |
| 17 | + </picture> |
| 18 | +`; |
| 19 | + |
| 20 | + promise_test(async t => { |
| 21 | + const iframe = document.createElement("iframe"); |
| 22 | + iframe.style.width = "300px"; |
| 23 | + iframe.style.height = "100px"; |
| 24 | + iframe.srcdoc = IFRAME_SRCDOC; |
| 25 | + document.body.appendChild(iframe); |
| 26 | + t.add_cleanup(() => iframe.remove()); |
| 27 | + await new Promise(resolve => {iframe.onload = resolve;}); |
| 28 | + |
| 29 | + const win = iframe.contentWindow; |
| 30 | + const img = win.document.querySelector("img"); |
| 31 | + const mql = win.matchMedia("(max-width: 150px)"); |
| 32 | + assert_true(img.complete, "Image should be complete once loaded"); |
| 33 | + assert_false(mql.matches, "Media query shouldn't match initially"); |
| 34 | + const currentSrc = img.currentSrc; |
| 35 | + |
| 36 | + img.addEventListener("load", t.unreached_func("Unexpected load event")); |
| 37 | + img.addEventListener("error", t.unreached_func("Unexpected error event")); |
| 38 | + |
| 39 | + for (const width of ["100px", "300px"]) { |
| 40 | + const shouldMatch = width == "100px"; |
| 41 | + const completeOnChange = new Promise(resolve => { |
| 42 | + mql.addEventListener("change", () => resolve(img.complete), {once: true}); |
| 43 | + }); |
| 44 | + |
| 45 | + iframe.style.width = width; |
| 46 | + if (mql.matches == shouldMatch) { |
| 47 | + assert_true(img.complete, |
| 48 | + `complete right after the media feature values changed (${width})`); |
| 49 | + } |
| 50 | + assert_true(await completeOnChange, |
| 51 | + `complete during the media query change event (${width})`); |
| 52 | + assert_true(img.complete, `complete after the environment change (${width})`); |
| 53 | + assert_equals(mql.matches, shouldMatch, `Media query state (${width})`); |
| 54 | + assert_equals(img.currentSrc, currentSrc, `currentSrc unchanged (${width})`); |
| 55 | + await new Promise(r => { |
| 56 | + // Give a chance for any unexpected load / error event to fire. |
| 57 | + win.requestAnimationFrame(() => win.requestAnimationFrame(r)) |
| 58 | + }); |
| 59 | + } |
| 60 | + }, "Resizing the viewport with a <picture> element shouldn't change img.complete"); |
| 61 | +</script> |
0 commit comments