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).
9 https://bugzilla.mozilla.org/show_bug.cgi?id=1192536
12 <meta charset=
"utf-8">
13 <title>Test for Bug
1192536</title>
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">
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";
27 window.onload = function() {
28 SimpleTest.waitForExplicitFinish();
32 function pushPref(preferenceName, value) {
33 return new Promise(resolve =
> {
34 const options = {
"set": [[preferenceName, value]]};
35 SpecialPowers.pushPrefEnv(options, resolve);
39 let gInspector = null;
41 addTest(async function setup() {
42 const url = document.getElementById(
"inspectorContent").href;
43 const { target } = await attachURL(url);
44 gInspector = await target.getFront(
"inspector");
48 addTest(async function testTimeout() {
49 info(
"Testing that the method aborts if the image takes too long to load.");
51 // imageToImageData() only times out when flags.testing is not set.
52 await pushPref(
"devtools.testing", false);
54 ensureRejects(gInspector.getImageDataFromURL(TIMEOUT_IMAGE),
55 "Image that loads for too long").then(runNextTest);
58 addTest(async function testNonExistentImage() {
59 info(
"Testing that non-existent image causes a rejection.");
61 // This test shouldn't hit the timeout.
62 await pushPref(
"devtools.testing", true);
64 ensureRejects(gInspector.getImageDataFromURL(NONEXISTENT_IMAGE),
65 "Non-existent image").then(runNextTest);
68 addTest(async function testNormalImage() {
69 info(
"Testing that the method waits for an image to load.");
71 // This test shouldn't hit the timeout.
72 await pushPref(
"devtools.testing", true);
74 checkImageData(gInspector.getImageDataFromURL(DELAYED_IMAGE)).then(runNextTest);
77 addTest(function cleanup() {
83 * Asserts that the given promise rejects.
85 function ensureRejects(promise, desc) {
86 return promise.then(() =
> {
87 ok(false, desc +
": promise resolved unexpectedly.");
89 ok(true, desc +
": promise rejected as expected.");
94 * Waits for the call to getImageData() the resolve and checks that the image
95 * size is reported correctly.
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.");
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>
110 <div id=
"content" style=
"display: none">