Bug 1171614 - Add a test for data-URI images to test_inspector_getImageData.html, and check that the server is being truthful about resizing. r=jdescottes draft
authorIan Moody <moz-ian@perix.co.uk>
Fri, 01 Jul 2016 18:31:22 +0100
changeset 385023 26e6febbfe2acc47e0273b3d594b2b50e79dc7c9
parent 385022 caf1540023f9fca662bbd7e3e8e9262af32e0212
child 524820 658aa1861d4babc4e764174116aef973daf55dd1
push id22395
push usermoz-ian@perix.co.uk
push dateThu, 07 Jul 2016 13:58:51 +0000
reviewersjdescottes
bugs1171614
milestone50.0a1
Bug 1171614 - Add a test for data-URI images to test_inspector_getImageData.html, and check that the server is being truthful about resizing. r=jdescottes Also tidy up the HTML a bit by removing errant <img> close tags MozReview-Commit-ID: 4cmtW1S2BI8
devtools/server/tests/mochitest/inspector_getImageData.html
devtools/server/tests/mochitest/test_inspector_getImageData.html
--- a/devtools/server/tests/mochitest/inspector_getImageData.html
+++ b/devtools/server/tests/mochitest/inspector_getImageData.html
@@ -1,15 +1,16 @@
 <html>
 <head>
 <body>
   <img class="custom">
-  <img class="big-horizontal" src="large-image.jpg" style="width:500px;" />
+  <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>
+  <img class="small" src="small-image.gif">
+  <img class="data" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAJklEQVRIie3NMREAAAgAoe9fWls4eAzMVM0xoVAoFAqFQqFQ+C9chp4NHvu+4Q4AAAAASUVORK5CYII=">
   <script>
     window.onload = () => {
       var canvas = document.querySelector("canvas"), ctx = canvas.getContext("2d");
       canvas.width = 1000;
       canvas.height = 2000;
       ctx.fillStyle = "red";
       ctx.fillRect(0, 0, 1000, 2000);
 
--- a/devtools/server/tests/mochitest/test_inspector_getImageData.html
+++ b/devtools/server/tests/mochitest/test_inspector_getImageData.html
@@ -42,17 +42,17 @@ addTest(function testLargeImage() {
       ok(imageData.data, "Image data actor was sent back");
       ok(imageData.size, "Image size info was sent back too");
       is(imageData.size.naturalWidth, 5333, "Natural width of the image correct");
       is(imageData.size.naturalHeight, 3000, "Natural width of the image correct");
       ok(imageData.size.resized, "Image was resized");
 
       imageData.data.string().then(str => {
         ok(str, "We have an image data string!");
-        runNextTest();
+        testResizing(imageData, str);
       });
     });
   });
 });
 
 addTest(function testLargeCanvas() {
   // Select the canvas node from the test page
   gWalker.querySelector(gWalker.rootNode, ".big-vertical").then(canvas => {
@@ -63,17 +63,17 @@ addTest(function testLargeCanvas() {
       ok(imageData.data, "Image data actor was sent back");
       ok(imageData.size, "Image size info was sent back too");
       is(imageData.size.naturalWidth, 1000, "Natural width of the image correct");
       is(imageData.size.naturalHeight, 2000, "Natural width of the image correct");
       ok(imageData.size.resized, "Image was resized");
 
       imageData.data.string().then(str => {
         ok(str, "We have an image data string!");
-        runNextTest();
+        testResizing(imageData, str);
       });
     });
   });
 });
 
 addTest(function testSmallImage() {
   // Select the small image node from the test page
   gWalker.querySelector(gWalker.rootNode, ".small").then(img => {
@@ -84,17 +84,38 @@ addTest(function testSmallImage() {
       ok(imageData.data, "Image data actor was sent back");
       ok(imageData.size, "Image size info was sent back too");
       is(imageData.size.naturalWidth, 245, "Natural width of the image correct");
       is(imageData.size.naturalHeight, 240, "Natural width of the image correct");
       ok(!imageData.size.resized, "Image was NOT resized");
 
       imageData.data.string().then(str => {
         ok(str, "We have an image data string!");
-        runNextTest();
+        testResizing(imageData, str);
+      });
+    });
+  });
+});
+
+addTest(function testDataImage() {
+  // Select the data image node from the test page
+  gWalker.querySelector(gWalker.rootNode, ".data").then(img => {
+    ok(img, "Image node found in the test page");
+    ok(img.getImageData, "Image node has the getImageData function");
+
+    img.getImageData(14).then(imageData => {
+      ok(imageData.data, "Image data actor was sent back");
+      ok(imageData.size, "Image size info was sent back too");
+      is(imageData.size.naturalWidth, 28, "Natural width of the image correct");
+      is(imageData.size.naturalHeight, 28, "Natural width of the image correct");
+      ok(imageData.size.resized, "Image was resized");
+
+      imageData.data.string().then(str => {
+        ok(str, "We have an image data string!");
+        testResizing(imageData, str);
       });
     });
   });
 });
 
 addTest(function testNonImgOrCanvasElements() {
   gWalker.querySelector(gWalker.rootNode, "body").then(body => {
     ensureRejects(body.getImageData(), "Invalid element").then(runNextTest);
@@ -102,16 +123,30 @@ addTest(function testNonImgOrCanvasEleme
 });
 
 addTest(function cleanup() {
   delete gWalker;
   runNextTest();
 });
 
 /**
+ * Checks if the server told the truth about resizing the image
+ */
+function testResizing(imageData, str) {
+  let img = document.createElement("img");
+  img.addEventListener("load", () => {
+    let resized = !(img.naturalWidth == imageData.size.naturalWidth &&
+                    img.naturalHeight == imageData.size.naturalHeight);
+    is(imageData.size.resized, resized, "Server told the truth about resizing");
+    runNextTest();
+  }, false);
+  img.src = str;
+}
+
+/**
  * 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.");
   });