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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/* global helpers, btoa, whenDelayedStartupFinished, OpenBrowserWindow */
// Test that screenshot command works properly
"use strict";
const TEST_URI = "http://example.com/browser/devtools/client/commandline/" +
"test/browser_cmd_screenshot.html";
var FileUtils = (Cu.import("resource://gre/modules/FileUtils.jsm", {})).FileUtils;
function test() {
// This test gets bombarded by a cascade of GCs and often takes 50s so lets be
// safe and give the test 90s to run.
requestLongerTimeout(3);
return Task.spawn(spawnTest).then(finish, helpers.handleError);
}
function* spawnTest() {
waitForExplicitFinish();
info("RUN TEST: non-private window");
let normWin = yield addWindow({ private: false });
yield addTabWithToolbarRunTests(normWin);
normWin.close();
info("RUN TEST: private window");
let pbWin = yield addWindow({ private: true });
yield addTabWithToolbarRunTests(pbWin);
pbWin.close();
}
function* addTabWithToolbarRunTests(win) {
let options = yield helpers.openTab(TEST_URI, { chromeWindow: win });
let browser = options.browser;
yield helpers.openToolbar(options);
// Test input status
yield helpers.audit(options, [
{
setup: "screenshot",
check: {
input: "screenshot",
markup: "VVVVVVVVVV",
status: "VALID",
args: {
}
},
},
{
setup: "screenshot abc.png",
check: {
input: "screenshot abc.png",
markup: "VVVVVVVVVVVVVVVVVV",
status: "VALID",
args: {
filename: { value: "abc.png"},
}
},
},
{
setup: "screenshot --fullpage",
check: {
input: "screenshot --fullpage",
markup: "VVVVVVVVVVVVVVVVVVVVV",
status: "VALID",
args: {
fullpage: { value: true},
}
},
},
{
setup: "screenshot abc --delay 5",
check: {
input: "screenshot abc --delay 5",
markup: "VVVVVVVVVVVVVVVVVVVVVVVV",
status: "VALID",
args: {
filename: { value: "abc"},
delay: { value: 5 },
}
},
},
{
setup: "screenshot --selector img#testImage",
check: {
input: "screenshot --selector img#testImage",
markup: "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV",
status: "VALID",
},
},
]);
// Test capture to file
let file = FileUtils.getFile("TmpD", [ "TestScreenshotFile.png" ]);
yield helpers.audit(options, [
{
setup: "screenshot " + file.path,
check: {
args: {
filename: { value: "" + file.path },
fullpage: { value: false },
clipboard: { value: false },
},
},
exec: {
output: new RegExp("^Saved to "),
},
post: function () {
// Bug 849168: screenshot command tests fail in try but not locally
// ok(file.exists(), "Screenshot file exists");
if (file.exists()) {
file.remove(false);
}
}
},
]);
// Test capture to clipboard
yield helpers.audit(options, [
{
setup: "screenshot --clipboard",
check: {
args: {
clipboard: { value: true },
},
},
exec: {
output: new RegExp("^Copied to clipboard.$"),
},
post: Task.async(function* () {
let imgSize1 = yield getImageSizeFromClipboard();
yield ContentTask.spawn(browser, imgSize1, function* (imgSize) {
Assert.equal(imgSize.width, content.innerWidth,
"Image width matches window size");
Assert.equal(imgSize.height, content.innerHeight,
"Image height matches window size");
});
})
},
{
setup: "screenshot --fullpage --clipboard",
check: {
args: {
fullpage: { value: true },
clipboard: { value: true },
},
},
exec: {
output: new RegExp("^Copied to clipboard.$"),
},
post: Task.async(function* () {
let imgSize1 = yield getImageSizeFromClipboard();
yield ContentTask.spawn(browser, imgSize1, function* (imgSize) {
Assert.equal(imgSize.width,
content.innerWidth + content.scrollMaxX - content.scrollMinX,
"Image width matches page size");
Assert.equal(imgSize.height,
content.innerHeight + content.scrollMaxY - content.scrollMinY,
"Image height matches page size");
});
})
},
{
setup: "screenshot --selector img#testImage --clipboard",
check: {
args: {
clipboard: { value: true },
},
},
exec: {
output: new RegExp("^Copied to clipboard.$"),
},
post: Task.async(function* () {
let imgSize1 = yield getImageSizeFromClipboard();
yield ContentTask.spawn(browser, imgSize1, function* (imgSize) {
let img = content.document.querySelector("img#testImage");
Assert.equal(imgSize.width, img.clientWidth,
"Image width matches element size");
Assert.equal(imgSize.height, img.clientHeight,
"Image height matches element size");
});
})
},
]);
// Trigger scrollbars by forcing document to overflow
// This only affects results on OSes with scrollbars that reduce document size
// (non-floating scrollbars). With default OS settings, this means Windows
// and Linux are affected, but Mac is not. For Mac to exhibit this behavior,
// change System Preferences -> General -> Show scroll bars to Always.
yield ContentTask.spawn(browser, {}, function* () {
content.document.body.classList.add("overflow");
});
let scrollbarSize = yield ContentTask.spawn(browser, {}, function* () {
const winUtils = content.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
let scrollbarHeight = {};
let scrollbarWidth = {};
winUtils.getScrollbarSize(true, scrollbarWidth, scrollbarHeight);
return {
width: scrollbarWidth.value,
height: scrollbarHeight.value,
};
});
info(`Scrollbar size: ${scrollbarSize.width}x${scrollbarSize.height}`);
// Test capture to clipboard in presence of scrollbars
yield helpers.audit(options, [
{
setup: "screenshot --clipboard",
check: {
args: {
clipboard: { value: true },
},
},
exec: {
output: new RegExp("^Copied to clipboard.$"),
},
post: Task.async(function* () {
let imgSize1 = yield getImageSizeFromClipboard();
imgSize1.scrollbarWidth = scrollbarSize.width;
imgSize1.scrollbarHeight = scrollbarSize.height;
yield ContentTask.spawn(browser, imgSize1, function* (imgSize) {
Assert.equal(imgSize.width, content.innerWidth - imgSize.scrollbarWidth,
"Image width matches window size minus scrollbar size");
Assert.equal(imgSize.height, content.innerHeight - imgSize.scrollbarHeight,
"Image height matches window size minus scrollbar size");
});
})
},
{
setup: "screenshot --fullpage --clipboard",
check: {
args: {
fullpage: { value: true },
clipboard: { value: true },
},
},
exec: {
output: new RegExp("^Copied to clipboard.$"),
},
post: Task.async(function* () {
let imgSize1 = yield getImageSizeFromClipboard();
imgSize1.scrollbarWidth = scrollbarSize.width;
imgSize1.scrollbarHeight = scrollbarSize.height;
yield ContentTask.spawn(browser, imgSize1, function* (imgSize) {
Assert.equal(imgSize.width,
(content.innerWidth + content.scrollMaxX -
content.scrollMinX) - imgSize.scrollbarWidth,
"Image width matches page size minus scrollbar size");
Assert.equal(imgSize.height,
(content.innerHeight + content.scrollMaxY -
content.scrollMinY) - imgSize.scrollbarHeight,
"Image height matches page size minus scrollbar size");
});
})
},
{
setup: "screenshot --selector img#testImage --clipboard",
check: {
args: {
clipboard: { value: true },
},
},
exec: {
output: new RegExp("^Copied to clipboard.$"),
},
post: Task.async(function* () {
let imgSize1 = yield getImageSizeFromClipboard();
yield ContentTask.spawn(browser, imgSize1, function* (imgSize) {
let img = content.document.querySelector("img#testImage");
Assert.equal(imgSize.width, img.clientWidth,
"Image width matches element size");
Assert.equal(imgSize.height, img.clientHeight,
"Image height matches element size");
});
})
},
]);
yield helpers.closeToolbar(options);
yield helpers.closeTab(options);
}
function addWindow(windowOptions) {
return new Promise(resolve => {
let win = OpenBrowserWindow(windowOptions);
// This feels hacky, we should refactor it
whenDelayedStartupFinished(win, () => {
// Would like to get rid of this executeSoon, but without it the url
// (TEST_URI) provided in addTabWithToolbarRunTests hasn't loaded
executeSoon(() => {
resolve(win);
});
});
});
}
let getImageSizeFromClipboard = Task.async(function* () {
let clipid = Ci.nsIClipboard;
let clip = Cc["@mozilla.org/widget/clipboard;1"].getService(clipid);
let trans = Cc["@mozilla.org/widget/transferable;1"]
.createInstance(Ci.nsITransferable);
let flavor = "image/png";
trans.init(null);
trans.addDataFlavor(flavor);
clip.getData(trans, clipid.kGlobalClipboard);
let data = new Object();
let dataLength = new Object();
trans.getTransferData(flavor, data, dataLength);
ok(data.value, "screenshot exists");
ok(dataLength.value > 0, "screenshot has length");
let image = data.value;
let dataURI = `data:${flavor};base64,`;
// Due to the differences in how images could be stored in the clipboard the
// checks below are needed. The clipboard could already provide the image as
// byte streams, but also as pointer, or as image container. If it's not
// possible obtain a byte stream, the function returns `null`.
if (image instanceof Ci.nsISupportsInterfacePointer) {
image = image.data;
}
if (image instanceof Ci.imgIContainer) {
image = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.encodeImage(image, flavor);
}
if (image instanceof Ci.nsIInputStream) {
let binaryStream = Cc["@mozilla.org/binaryinputstream;1"]
.createInstance(Ci.nsIBinaryInputStream);
binaryStream.setInputStream(image);
let rawData = binaryStream.readBytes(binaryStream.available());
let charCodes = Array.from(rawData, c => c.charCodeAt(0) & 0xff);
let encodedData = String.fromCharCode(...charCodes);
encodedData = btoa(encodedData);
dataURI = dataURI + encodedData;
} else {
throw new Error("Unable to read image data");
}
let img = document.createElementNS("http://www.w3.org/1999/xhtml", "img");
let loaded = new Promise(resolve => {
img.addEventListener("load", function onLoad() {
img.removeEventListener("load", onLoad);
resolve();
});
});
img.src = dataURI;
document.documentElement.appendChild(img);
yield loaded;
img.remove();
return {
width: img.width,
height: img.height,
};
});
|