test_bug1166138.html (4743B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1166138 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 1166138</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 </head> 12 <body> 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1166138">Mozilla Bug 1166138</a> 14 <p id="display"></p> 15 <div id="content" style="display: none"> 16 </div> 17 18 <script type="application/javascript"> 19 var img1x = `${location.origin}/tests/dom/html/test/file_bug1166138_1x.png`; 20 var img2x = `${location.origin}/tests/dom/html/test/file_bug1166138_2x.png`; 21 var imgdef = `${location.origin}/tests/dom/html/test/file_bug1166138_def.png`; 22 var onLoadCallback = null; 23 var done = false; 24 25 var startPromise = new Promise((a) => { 26 onLoadCallback = () => { 27 var image = document.querySelector('img'); 28 // If we aren't starting at 2x scale, resize to 2x scale, and wait for a load 29 if (image.currentSrc != img2x) { 30 onLoadCallback = a; 31 SpecialPowers.pushPrefEnv({'set': [['layout.css.devPixelsPerPx', 2]]}); 32 } else { 33 a(); 34 } 35 }; 36 }); 37 38 // if aLoad is true, waits for a load event. Otherwise, spins the event loop twice to 39 // ensure that no events were queued to be fired. 40 function spin(aLoad) { 41 if (aLoad) { 42 return new Promise((a) => { 43 ok(!onLoadCallback, "Shouldn't be an existing callback"); 44 onLoadCallback = a; 45 }); 46 } else { 47 return new Promise((a) => SimpleTest.executeSoon(() => SimpleTest.executeSoon(a))); 48 } 49 } 50 51 function onLoad() { 52 if (done) return; 53 ok(onLoadCallback, "Expected a load event"); 54 if (onLoadCallback) { 55 var cb = onLoadCallback; 56 onLoadCallback = null; 57 cb(); 58 } 59 } 60 61 add_task(async function() { 62 await startPromise; 63 var image = document.querySelector('img'); 64 is(image.currentSrc, img2x, "initial scale must be 2x"); 65 66 SpecialPowers.pushPrefEnv({'set': [['layout.css.devPixelsPerPx', 1]]}); 67 await spin(true); 68 is(image.currentSrc, img1x, "pre-existing img tag to 1x"); 69 70 SpecialPowers.pushPrefEnv({'set': [['layout.css.devPixelsPerPx', 2]]}); 71 await spin(true); 72 is(image.currentSrc, img2x, "pre-existing img tag to 2x"); 73 74 // Try removing & re-adding the image 75 document.body.removeChild(image); 76 77 SpecialPowers.pushPrefEnv({'set': [['layout.css.devPixelsPerPx', 1]]}); 78 await spin(true); 79 is(image.currentSrc, img1x, "changing to 1x while unbound should update"); 80 81 document.body.appendChild(image); 82 await spin(false); 83 is(image.currentSrc, img1x, "remove and re-add tag after changing to 1x"); 84 85 document.body.removeChild(image); 86 SpecialPowers.pushPrefEnv({'set': [['layout.css.devPixelsPerPx', 2]]}); 87 await spin(true); 88 is(image.currentSrc, img2x, "changing to 2x while unbound should update"); 89 90 document.body.appendChild(image); 91 await spin(false); 92 is(image.currentSrc, img2x, "remove and re-add tag after changing to 2x"); 93 94 // get rid of the srcset attribute! It should become the default 95 image.removeAttribute('srcset'); 96 await spin(true); 97 is(image.currentSrc, imgdef, "remove srcset attribute"); 98 99 // Setting srcset again should return it to the correct value 100 image.setAttribute('srcset', "file_bug1166138_1x.png 1x, file_bug1166138_2x.png 2x"); 101 await spin(true); 102 is(image.currentSrc, img2x, "restore srcset attribute"); 103 104 // Create a new image 105 var newImage = document.createElement('img'); 106 // Switch load listening over to newImage 107 newImage.addEventListener('load', onLoad); 108 image.removeEventListener('load', onLoad); 109 110 document.body.appendChild(newImage); 111 await spin(false); // no load event should fire - as the image has no attributes 112 is(newImage.currentSrc, "", "New element with no attributes"); 113 newImage.setAttribute('srcset', "file_bug1166138_1x.png 1x, file_bug1166138_2x.png 2x"); 114 await spin(true); 115 is(newImage.currentSrc, img2x, "Adding srcset attribute"); 116 117 SpecialPowers.pushPrefEnv({'set': [['layout.css.devPixelsPerPx', 1]]}); 118 await spin(true); 119 is(newImage.currentSrc, img1x, "new image after switching to 1x"); 120 is(image.currentSrc, img1x, "old image after switching to 1x"); 121 122 // Clear the listener 123 done = true; 124 }); 125 </script> 126 127 <img srcset="file_bug1166138_1x.png 1x, file_bug1166138_2x.png 2x" 128 src="file_bug1166138_def.png" 129 onload="onLoad()"> 130 131 </body> 132 </html>