4 Tests for InspectorActor.getImageData() in following cases:
5 * Image takes too long to load (the method rejects after a timeout).
6 * Image is loading when the method is called and the load finishes before
10 https://bugzilla.mozilla.org/show_bug.cgi?id=1192536
13 <meta charset=
"utf-8">
14 <title>Test for Bug
1192536</title>
16 <script src=
"chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
17 <link rel=
"stylesheet" type=
"text/css" href=
"chrome://mochikit/content/tests/SimpleTest/test.css">
18 <script type=
"application/javascript" src=
"inspector-helpers.js"></script>
19 <script type=
"application/javascript">
22 const PATH =
"https://example.com/chrome/devtools/server/tests/chrome/";
23 const BASE_IMAGE = PATH +
"inspector-delay-image-response.sjs";
24 const DELAYED_IMAGE = BASE_IMAGE +
"?delay=300";
25 const TIMEOUT_IMAGE = BASE_IMAGE +
"?delay=50000";
26 const NONEXISTENT_IMAGE = PATH +
"this-does-not-exist.png";
28 window.onload = function() {
29 SimpleTest.waitForExplicitFinish();
33 function pushPref(preferenceName, value) {
34 return new Promise(resolve =
> {
35 const options = {
"set": [[preferenceName, value]]};
36 SpecialPowers.pushPrefEnv(options, resolve);
41 let gNodeFront = null;
44 addTest(async function setup() {
45 const url = document.getElementById(
"inspectorContent").href;
46 const { target, doc } = await attachURL(url);
47 const inspector = await target.getFront(
"inspector");
48 gWalker = inspector.walker;
49 gNodeFront = await gWalker.querySelector(gWalker.rootNode,
"img.custom");
50 gImg = doc.querySelector(
"img.custom");
51 ok(gNodeFront,
"Got the image NodeFront.");
52 ok(gImg,
"Got the image Node.");
56 addTest(async function testTimeout() {
57 info(
"Testing that the method aborts if the image takes too long to load.");
59 // imageToImageData() only times out when flags.testing is not set.
60 await pushPref(
"devtools.testing", false);
62 gImg.src = TIMEOUT_IMAGE;
64 info(
"Calling getImageData().");
65 ensureRejects(gNodeFront.getImageData(),
"Timeout image").then(runNextTest);
68 addTest(async function testNonExistentImage() {
69 info(
"Testing that non-existent image causes a rejection.");
71 // This test shouldn't hit the timeout.
72 await pushPref(
"devtools.testing", true);
74 gImg.src = NONEXISTENT_IMAGE;
76 info(
"Calling getImageData().");
77 ensureRejects(gNodeFront.getImageData(),
"Non-existent image").then(runNextTest);
80 addTest(async function testDelayedImage() {
81 info(
"Testing that the method waits for an image to load.");
83 // This test shouldn't hit the timeout.
84 await pushPref(
"devtools.testing", true);
86 gImg.src = DELAYED_IMAGE;
88 info(
"Calling getImageData().");
89 checkImageData(gNodeFront.getImageData()).then(runNextTest);
92 addTest(function cleanup() {
100 * Asserts that the given promise rejects.
102 function ensureRejects(promise, desc) {
103 return promise.then(() =
> {
104 ok(false, desc +
": promise resolved unexpectedly.");
106 ok(true, desc +
": promise rejected as expected.");
111 * Waits for the call to getImageData() the resolve and checks that the image
112 * size is reported correctly.
114 function checkImageData(promise, { width, height } = { width:
1, height:
1 }) {
115 return promise.then(({ size }) =
> {
116 is(size.naturalWidth, width,
"The width is correct.");
117 is(size.naturalHeight, height,
"The height is correct.");
124 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=1192536">Mozilla Bug
1192536</a>
125 <a id=
"inspectorContent" target=
"_blank" href=
"inspector_getImageData.html">Test Document
</a>
127 <div id=
"content" style=
"display: none">