test_img_picture_preload.html (2782B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1067345 5 --> 6 <head> 7 <title>Test for Bug 1067345</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" href="/tests/SimpleTest/test.css"> 10 </head> 11 <body> 12 <script> 13 // Ensure srcset/picture are enabled, re-run the test at different DPI 14 // levels to ensure preload step does the right responsive image selection 15 16 SimpleTest.waitForExplicitFinish(); 17 18 var testDPIs = ["1.0", "0.5", "2.0", "1.5"]; 19 var iframe = document.createElement("iframe"); 20 21 // These accessed by child frame 22 var currentDPI; 23 24 document.body.appendChild(iframe); 25 26 // Reset the sjs helper so repeat runs work 27 resetRequests(); 28 setTimeout(nextTest, 0); 29 30 function resetRequests() { 31 // Ask the SJS to reset requests 32 var request = new XMLHttpRequest(); 33 request.open("GET", "./file_img_picture_preload.sjs?reset", false); 34 request.send(null); 35 is(request.status, 200, "Sending reset to helper should succeed"); 36 // Script responds with pre-reset request count 37 var previousRequests = +request.responseText; 38 39 return previousRequests; 40 } 41 42 // Called when iframe is finished 43 function childTestFinished(requestsMade) { 44 setTimeout(function() { 45 // Reset sjs, ensure no new requests appeared after test finished 46 var requestsCleared = resetRequests(); 47 is(requestsCleared, requestsMade, 48 "Should not have recorded new requests after test iteration completed"); 49 50 setTimeout(nextTest, 0); 51 }, 0); 52 } 53 54 function nextTest() { 55 // Re-run test for each DPI level 56 if (!testDPIs.length) { 57 SimpleTest.finish(); 58 return; 59 } 60 currentDPI = testDPIs.pop(); 61 info("Starting test for DPI: " + currentDPI); 62 // To avoid spurious image loads being reported when the resolution changes, 63 // load an intermediate iframe. 64 iframe.src = "about:blank"; 65 iframe.addEventListener("load", async function() { 66 await SpecialPowers.pushPrefEnv({"set": [ [ "layout.css.devPixelsPerPx", currentDPI ]] }); 67 // Make sure we trigger a layout flush so that the frame is sized 68 // appropriately after the DPI changes. 69 iframe.getBoundingClientRect(); 70 // Clear image cache for next run (we don't try to validate cached items 71 // in preload). 72 SpecialPowers.Cc["@mozilla.org/image/tools;1"] 73 .getService(SpecialPowers.Ci.imgITools) 74 .getImgCacheForDocument(iframe.contentDocument) 75 .clearCache(false); 76 iframe.src = "./file_img_picture_preload.html?" + currentDPI; 77 }, {once: true}); 78 } 79 </script> 80 </body> 81 </html>