summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/chrome/test_inspector_getImageData-wait-for-load.html
blob: 129116b91333ea21fceed3420ca835f8ee8eb6f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE HTML>
<html>
<!--
Tests for InspectorActor.getImageData() in following cases:
 * Image takes too long to load (the method rejects after a timeout).
 * Image is loading when the method is called and the load finishes before
   timeout.
 * Image fails to load.

https://bugzilla.mozilla.org/show_bug.cgi?id=1192536
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1192536</title>

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script type="application/javascript" src="inspector-helpers.js"></script>
  <script type="application/javascript">
"use strict";

const PATH = "https://example.com/chrome/devtools/server/tests/chrome/";
const BASE_IMAGE = PATH + "inspector-delay-image-response.sjs";
const DELAYED_IMAGE = BASE_IMAGE + "?delay=300";
const TIMEOUT_IMAGE = BASE_IMAGE + "?delay=50000";
const NONEXISTENT_IMAGE = PATH + "this-does-not-exist.png";

window.onload = function() {
  SimpleTest.waitForExplicitFinish();
  runNextTest();
};

function pushPref(preferenceName, value) {
  return new Promise(resolve => {
    const options = {"set": [[preferenceName, value]]};
    SpecialPowers.pushPrefEnv(options, resolve);
  });
}

let gImg = null;
let gNodeFront = null;
let gWalker = null;

addTest(async function setup() {
  const url = document.getElementById("inspectorContent").href;
  const { target, doc } = await attachURL(url);
  const inspector = await target.getFront("inspector");
  gWalker = inspector.walker;
  gNodeFront = await gWalker.querySelector(gWalker.rootNode, "img.custom");
  gImg = doc.querySelector("img.custom");
  ok(gNodeFront, "Got the image NodeFront.");
  ok(gImg, "Got the image Node.");
  runNextTest();
});

addTest(async function testTimeout() {
  info("Testing that the method aborts if the image takes too long to load.");

  // imageToImageData() only times out when flags.testing is not set.
  await pushPref("devtools.testing", false);

  gImg.src = TIMEOUT_IMAGE;

  info("Calling getImageData().");
  ensureRejects(gNodeFront.getImageData(), "Timeout image").then(runNextTest);
});

addTest(async function testNonExistentImage() {
  info("Testing that non-existent image causes a rejection.");

  // This test shouldn't hit the timeout.
  await pushPref("devtools.testing", true);

  gImg.src = NONEXISTENT_IMAGE;

  info("Calling getImageData().");
  ensureRejects(gNodeFront.getImageData(), "Non-existent image").then(runNextTest);
});

addTest(async function testDelayedImage() {
  info("Testing that the method waits for an image to load.");

  // This test shouldn't hit the timeout.
  await pushPref("devtools.testing", true);

  gImg.src = DELAYED_IMAGE;

  info("Calling getImageData().");
  checkImageData(gNodeFront.getImageData()).then(runNextTest);
});

addTest(function cleanup() {
  gImg = null;
  gNodeFront = null;
  gWalker = null;
  runNextTest();
});

/**
 * Asserts that the given promise rejects.
 */
function ensureRejects(promise, desc) {
  return promise.then(() => {
    ok(false, desc + ": promise resolved unexpectedly.");
  }, () => {
    ok(true, desc + ": promise rejected as expected.");
  });
}

/**
 * Waits for the call to getImageData() the resolve and checks that the image
 * size is reported correctly.
 */
function checkImageData(promise, { width, height } = { width: 1, height: 1 }) {
  return promise.then(({ size }) => {
    is(size.naturalWidth, width, "The width is correct.");
    is(size.naturalHeight, height, "The height is correct.");
  });
}

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1192536">Mozilla Bug 1192536</a>
<a id="inspectorContent" target="_blank" href="inspector_getImageData.html">Test Document</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>