test_inspector_getImageDataFromURL.html (3482B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 Tests for InspectorActor.getImageDataFromURL() in following cases: 5 * Normal case, image loads after a small delay. 6 * Image takes too long to load (the method rejects after a timeout). 7 * Image fails to load. 8 9 https://bugzilla.mozilla.org/show_bug.cgi?id=1192536 10 --> 11 <head> 12 <meta charset="utf-8"> 13 <title>Test for Bug 1192536</title> 14 15 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 16 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 17 <script type="application/javascript" src="inspector-helpers.js"></script> 18 <script type="application/javascript"> 19 "use strict"; 20 21 const PATH = "https://example.com/chrome/devtools/server/tests/chrome/"; 22 const BASE_IMAGE = PATH + "inspector-delay-image-response.sjs"; 23 const DELAYED_IMAGE = BASE_IMAGE + "?delay=300"; 24 const TIMEOUT_IMAGE = BASE_IMAGE + "?delay=50000"; 25 const NONEXISTENT_IMAGE = PATH + "this-does-not-exist.png"; 26 27 window.onload = function() { 28 SimpleTest.waitForExplicitFinish(); 29 runNextTest(); 30 }; 31 32 function pushPref(preferenceName, value) { 33 return new Promise(resolve => { 34 const options = {"set": [[preferenceName, value]]}; 35 SpecialPowers.pushPrefEnv(options, resolve); 36 }); 37 } 38 39 let gInspector = null; 40 41 addTest(async function setup() { 42 const url = document.getElementById("inspectorContent").href; 43 const { target } = await attachURL(url); 44 gInspector = await target.getFront("inspector"); 45 runNextTest(); 46 }); 47 48 addTest(async function testTimeout() { 49 info("Testing that the method aborts if the image takes too long to load."); 50 51 // imageToImageData() only times out when flags.testing is not set. 52 await pushPref("devtools.testing", false); 53 54 ensureRejects(gInspector.getImageDataFromURL(TIMEOUT_IMAGE), 55 "Image that loads for too long").then(runNextTest); 56 }); 57 58 addTest(async function testNonExistentImage() { 59 info("Testing that non-existent image causes a rejection."); 60 61 // This test shouldn't hit the timeout. 62 await pushPref("devtools.testing", true); 63 64 ensureRejects(gInspector.getImageDataFromURL(NONEXISTENT_IMAGE), 65 "Non-existent image").then(runNextTest); 66 }); 67 68 addTest(async function testNormalImage() { 69 info("Testing that the method waits for an image to load."); 70 71 // This test shouldn't hit the timeout. 72 await pushPref("devtools.testing", true); 73 74 checkImageData(gInspector.getImageDataFromURL(DELAYED_IMAGE)).then(runNextTest); 75 }); 76 77 addTest(function cleanup() { 78 gInspector = null; 79 runNextTest(); 80 }); 81 82 /** 83 * Asserts that the given promise rejects. 84 */ 85 function ensureRejects(promise, desc) { 86 return promise.then(() => { 87 ok(false, desc + ": promise resolved unexpectedly."); 88 }, () => { 89 ok(true, desc + ": promise rejected as expected."); 90 }); 91 } 92 93 /** 94 * Waits for the call to getImageData() the resolve and checks that the image 95 * size is reported correctly. 96 */ 97 function checkImageData(promise, { width, height } = { width: 1, height: 1 }) { 98 return promise.then(({ size }) => { 99 is(size.naturalWidth, width, "The width is correct."); 100 is(size.naturalHeight, height, "The height is correct."); 101 }); 102 } 103 104 </script> 105 </head> 106 <body> 107 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1192536">Mozilla Bug 1192536</a> 108 <a id="inspectorContent" target="_blank" href="inspector_getImageData.html">Test Document</a> 109 <p id="display"></p> 110 <div id="content" style="display: none"> 111 112 </div> 113 <pre id="test"> 114 </pre> 115 </body> 116 </html>