Bug 1192536 - Part 2: Tests for InspectorActor.getImageData() and InspctorActor.getImageDataFromURL(). r=pbro

This commit is contained in:
Sami Jaktholm 2015-08-19 16:05:28 +03:00
Родитель 0e2d46b6bf
Коммит 661a054937
6 изменённых файлов: 315 добавлений и 0 удалений

Просмотреть файл

@ -8,6 +8,7 @@ support-files =
director-helpers.js
hello-actor.js
inspector_getImageData.html
inspector-delay-image-response.sjs
inspector-helpers.js
inspector-styles-data.css
inspector-styles-data.html
@ -57,6 +58,10 @@ skip-if = buildapp == 'mulet'
[test_inspector-dead-nodes.html]
[test_inspector_getImageData.html]
skip-if = buildapp == 'mulet'
[test_inspector_getImageDataFromURL.html]
skip-if = buildapp == 'mulet'
[test_inspector_getImageData-wait-for-load.html]
skip-if = buildapp == 'mulet'
[test_inspector_getNodeFromActor.html]
[test_inspector-hide.html]
[test_inspector-insert.html]

Просмотреть файл

@ -0,0 +1,42 @@
/**
* Adapted from https://dxr.mozilla.org/mozilla-central/rev/
* 4e883591bb5dff021c108d3e30198a99547eed1e/layout/reftests/backgrounds/
* delay-image-response.sjs
*/
"use strict";
// A 1x1 PNG image.
// Source: https://commons.wikimedia.org/wiki/File:1x1.png (Public Domain)
const IMAGE = atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" +
"ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=");
// To avoid GC.
let timer = null;
function handleRequest(request, response) {
let query = {};
request.queryString.split("&").forEach(function(val) {
let [name, value] = val.split("=");
query[name] = unescape(value);
});
response.setStatusLine(request.httpVersion, 200, "OK");
response.setHeader("Content-Type", "image/png", false);
// If there is no delay, we write the image and leave.
if (!("delay" in query)) {
response.write(IMAGE);
return;
}
// If there is a delay, we create a timer which, when it fires, will write
// image and leave.
response.processAsync();
const nsITimer = Components.interfaces.nsITimer;
timer = Components.classes["@mozilla.org/timer;1"].createInstance(nsITimer);
timer.initWithCallback(function() {
response.write(IMAGE);
response.finish();
}, query.delay, nsITimer.TYPE_ONE_SHOT);
}

Просмотреть файл

@ -1,6 +1,7 @@
<html>
<head>
<body>
<img class="custom">
<img class="big-horizontal" src="large-image.jpg" style="width:500px;" />
<canvas class="big-vertical" style="width:500px;"></canvas>
<img class="small" src="small-image.gif"></img>

Просмотреть файл

@ -0,0 +1,136 @@
<!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 type="application/javascript" 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;version=1.8" src="inspector-helpers.js"></script>
<script type="application/javascript;version=1.8">
const DevToolsUtils = require("devtools/toolkit/DevToolsUtils");
const wasTesting = DevToolsUtils.testing;
SimpleTest.registerCleanupFunction(() => DevToolsUtils.testing = wasTesting);
const PATH = "http://mochi.test:8888/chrome/toolkit/devtools/server/tests/mochitest/";
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();
}
var gImg = null;
var gNodeFront = null;
var gWalker = null;
addTest(function setup() {
let url = document.getElementById("inspectorContent").href;
attachURL(url, function(err, client, tab, doc) {
let {InspectorFront} = require("devtools/server/actors/inspector");
let inspector = InspectorFront(client, tab);
promiseDone(inspector.getWalker().then(walker => {
gWalker = walker;
return walker.querySelector(gWalker.rootNode, "img.custom").then(img => {
gNodeFront = img;
gImg = doc.querySelector("img.custom");
ok(gNodeFront, "Got the image NodeFront.");
ok(gImg, "Got the image Node.");
});
}).then(runNextTest));
});
});
addTest(function testTimeout() {
info("Testing that the method aborts if the image takes too long to load.");
// imageToImageData() only times out when DTU.testing is not set.
DevToolsUtils.testing = false;
gImg.src = TIMEOUT_IMAGE;
info("Calling getImageData().");
ensureRejects(gNodeFront.getImageData(), "Timeout image").then(runNextTest);
});
addTest(function testNonExistentImage() {
info("Testing that non-existent image causes a rejection.");
// This test shouldn't hit the timeout.
DevToolsUtils.testing = true;
gImg.src = NONEXISTENT_IMAGE;
info("Calling getImageData().");
ensureRejects(gNodeFront.getImageData(), "Non-existent image").then(runNextTest);
});
addTest(function testDelayedImage() {
info("Testing that the method waits for an image to load.");
// This test shouldn't hit the timeout.
DevToolsUtils.testing = true;
gImg.src = DELAYED_IMAGE;
info("Calling getImageData().");
checkImageData(gNodeFront.getImageData()).then(runNextTest);
});
addTest(function cleanup() {
delete gImg;
delete gNodeFront
delete gWalker;
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>

Просмотреть файл

@ -95,10 +95,27 @@ addTest(function testSmallImage() {
});
});
addTest(function testNonImgOrCanvasElements() {
gWalker.querySelector(gWalker.rootNode, "body").then(body => {
ensureRejects(body.getImageData(), "Invalid element").then(runNextTest);
});
});
addTest(function cleanup() {
delete gWalker;
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.");
});
}
</script>
</head>
<body>

Просмотреть файл

@ -0,0 +1,114 @@
<!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.
https://bugzilla.mozilla.org/show_bug.cgi?id=1192536
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1192536</title>
<script type="application/javascript" 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;version=1.8" src="inspector-helpers.js"></script>
<script type="application/javascript;version=1.8">
const DevToolsUtils = require("devtools/toolkit/DevToolsUtils");
const wasTesting = DevToolsUtils.testing;
SimpleTest.registerCleanupFunction(() => DevToolsUtils.testing = wasTesting);
const PATH = "http://mochi.test:8888/chrome/toolkit/devtools/server/tests/mochitest/";
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();
}
var gInspector = null;
addTest(function setup() {
let url = document.getElementById("inspectorContent").href;
attachURL(url, function(err, client, tab, doc) {
let {InspectorFront} = require("devtools/server/actors/inspector");
gInspector = InspectorFront(client, tab);
runNextTest();
});
});
addTest(function testTimeout() {
info("Testing that the method aborts if the image takes too long to load.");
// imageToImageData() only times out when DTU.testing is not set.
DevToolsUtils.testing = false;
ensureRejects(gInspector.getImageDataFromURL(TIMEOUT_IMAGE),
"Image that loads for too long").then(runNextTest);
});
addTest(function testNonExistentImage() {
info("Testing that non-existent image causes a rejection.");
// This test shouldn't hit the timeout.
DevToolsUtils.testing = true;
ensureRejects(gInspector.getImageDataFromURL(NONEXISTENT_IMAGE),
"Non-existent image").then(runNextTest);
});
addTest(function testNormalImage() {
info("Testing that the method waits for an image to load.");
// This test shouldn't hit the timeout.
DevToolsUtils.testing = true;
checkImageData(gInspector.getImageDataFromURL(DELAYED_IMAGE)).then(runNextTest);
});
addTest(function cleanup() {
delete gInspector;
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>