Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!DOCTYPE HTML>
<html>
<!--
Tests for InspectorActor.getImageDataFromURL() in following cases:
* Normal case, image loads after a small delay.
* Image takes too long to load (the method rejects after a timeout).
* Image fails to load.
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1192536</title>
<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 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 gInspector = null;
addTest(async function setup() {
const url = document.getElementById("inspectorContent").href;
const { target } = await attachURL(url);
gInspector = await target.getFront("inspector");
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);
ensureRejects(gInspector.getImageDataFromURL(TIMEOUT_IMAGE),
"Image that loads for too long").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);
ensureRejects(gInspector.getImageDataFromURL(NONEXISTENT_IMAGE),
"Non-existent image").then(runNextTest);
});
addTest(async function testNormalImage() {
info("Testing that the method waits for an image to load.");
// This test shouldn't hit the timeout.
await pushPref("devtools.testing", true);
checkImageData(gInspector.getImageDataFromURL(DELAYED_IMAGE)).then(runNextTest);
});
addTest(function cleanup() {
gInspector = 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 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>