summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/mochitest/test_inspector_getImageData.html
blob: be3c2419487aa62a24e1a36b18d65987b591bf5d (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=932937
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 932937</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 inspector = require("devtools/shared/fronts/inspector");

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

var gWalker = null;

addTest(function setup() {
  let url = document.getElementById("inspectorContent").href;
  attachURL(url, function(err, client, tab, doc) {
    let {InspectorFront} = require("devtools/shared/fronts/inspector");
    let inspector = InspectorFront(client, tab);

    promiseDone(inspector.getWalker().then(walker => {
      gWalker = walker;
    }).then(runNextTest));
  });
});

addTest(function testLargeImage() {
  // Select the image node from the test page
  gWalker.querySelector(gWalker.rootNode, ".big-horizontal").then(img => {
    ok(img, "Image node found in the test page");
    ok(img.getImageData, "Image node has the getImageData function");

    img.getImageData(100).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, 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!");
        testResizing(imageData, str);
      });
    });
  });
});

addTest(function testLargeCanvas() {
  // Select the canvas node from the test page
  gWalker.querySelector(gWalker.rootNode, ".big-vertical").then(canvas => {
    ok(canvas, "Image node found in the test page");
    ok(canvas.getImageData, "Image node has the getImageData function");

    canvas.getImageData(350).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, 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!");
        testResizing(imageData, str);
      });
    });
  });
});

addTest(function testSmallImage() {
  // Select the small image node from the test page
  gWalker.querySelector(gWalker.rootNode, ".small").then(img => {
    ok(img, "Image node found in the test page");
    ok(img.getImageData, "Image node has the getImageData function");

    img.getImageData().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, 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!");
        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);
  });
});

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.");
  });
}
  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=932937">Mozilla Bug 932937</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>