Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs captureVisibleTab Test</title>
5
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
6
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
7
<script type="text/javascript" src="head.js"></script>
8
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
9
</head>
10
<body>
11
12
<script type="text/javascript">
13
"use strict";
14
15
function* runTest(options) {
16
options.neutral = [0xaa, 0xaa, 0xaa];
17
18
let html = `
19
<!DOCTYPE html>
20
<html lang="en">
21
<head><meta charset="UTF-8"></head>
22
<body style="background-color: rgb(${options.color})">
23
<!-- Fill most of the image with a neutral color to test edge-to-edge scaling. -->
24
<div style="position: absolute;
25
left: 2px;
26
right: 2px;
27
top: 2px;
28
bottom: 2px;
29
background: rgb(${options.neutral});"></div>
30
</body>
31
</html>
32
`;
33
34
let url = `data:text/html,${encodeURIComponent(html)}`;
35
let win = window.open(url);
36
37
async function background(options) {
38
browser.test.log(`Test color ${options.color} at fullZoom=${options.fullZoom}`);
39
40
try {
41
let [tab] = await browser.tabs.query({currentWindow: true, active: true});
42
43
let [jpeg, png, ...pngs] = await Promise.all([
44
browser.tabs.captureVisibleTab(tab.windowId, {format: "jpeg", quality: 95}),
45
browser.tabs.captureVisibleTab(tab.windowId, {format: "png", quality: 95}),
46
browser.tabs.captureVisibleTab(tab.windowId, {quality: 95}),
47
browser.tabs.captureVisibleTab(tab.windowId),
48
]);
49
50
browser.test.assertTrue(pngs.every(url => url == png), "All PNGs are identical");
51
52
browser.test.assertTrue(jpeg.startsWith("data:image/jpeg;base64,"), "jpeg is JPEG");
53
browser.test.assertTrue(png.startsWith("data:image/png;base64,"), "png is PNG");
54
55
let promises = [jpeg, png].map(url => new Promise(resolve => {
56
let img = new Image();
57
img.src = url;
58
img.onload = () => resolve(img);
59
}));
60
61
[jpeg, png] = await Promise.all(promises);
62
let tabDims = `${tab.width}\u00d7${tab.height}`;
63
64
let images = {jpeg, png};
65
for (let format of Object.keys(images)) {
66
let img = images[format];
67
68
let dims = `${img.width}\u00d7${img.height}`;
69
browser.test.assertEq(tabDims, dims, `${format} dimensions are correct`);
70
71
let canvas = document.createElement("canvas");
72
canvas.width = img.width;
73
canvas.height = img.height;
74
canvas.mozOpaque = true;
75
76
let ctx = canvas.getContext("2d");
77
ctx.drawImage(img, 0, 0);
78
79
// Check the colors of the first and last pixels of the image, to make
80
// sure we capture the entire frame, and scale it correctly.
81
let coords = [
82
{x: 0, y: 0,
83
color: options.color},
84
{x: img.width - 1,
85
y: img.height - 1,
86
color: options.color},
87
{x: img.width / 2 | 0,
88
y: img.height / 2 | 0,
89
color: options.neutral},
90
];
91
92
for (let {x, y, color} of coords) {
93
let imageData = ctx.getImageData(x, y, 1, 1).data;
94
95
if (format == "png") {
96
browser.test.assertEq(`rgba(${color},255)`, `rgba(${[...imageData]})`, `${format} image color is correct at (${x}, ${y})`);
97
} else {
98
// Allow for some deviation in JPEG version due to lossy compression.
99
const SLOP = 3;
100
101
browser.test.log(`Testing ${format} image color at (${x}, ${y}), have rgba(${[...imageData]}), expecting approx. rgba(${color},255)`);
102
103
browser.test.assertTrue(Math.abs(color[0] - imageData[0]) <= SLOP, `${format} image color.red is correct at (${x}, ${y})`);
104
browser.test.assertTrue(Math.abs(color[1] - imageData[1]) <= SLOP, `${format} image color.green is correct at (${x}, ${y})`);
105
browser.test.assertTrue(Math.abs(color[2] - imageData[2]) <= SLOP, `${format} image color.blue is correct at (${x}, ${y})`);
106
browser.test.assertEq(255, imageData[3], `${format} image color.alpha is correct at (${x}, ${y})`);
107
}
108
}
109
}
110
111
browser.test.notifyPass("captureVisibleTab");
112
} catch (e) {
113
browser.test.fail(`Error: ${e} :: ${e.stack}`);
114
browser.test.notifyFail("captureVisibleTab");
115
}
116
}
117
118
let extension = ExtensionTestUtils.loadExtension({
119
manifest: {
120
"permissions": ["<all_urls>"],
121
},
122
123
background: `(${background})(${JSON.stringify(options)})`,
124
});
125
126
yield extension.startup();
127
128
yield extension.awaitFinish("captureVisibleTab");
129
130
yield extension.unload();
131
132
win.close();
133
}
134
135
add_task(async function testCaptureVisibleTab() {
136
await SpecialPowers.pushPrefEnv({
137
"set": [["security.data_uri.block_toplevel_data_uri_navigations", false]],
138
});
139
140
await runTest({color: [0, 0, 0]});
141
142
await runTest({color: [255, 255, 255]});
143
});
144
145
add_task(async function testCaptureVisibleTabPermissions() {
146
let extension = ExtensionTestUtils.loadExtension({
147
manifest: {
148
"permissions": ["tabs"],
149
},
150
151
background: function() {
152
browser.test.assertEq(undefined, browser.tabs.captureVisibleTab,
153
'Extension without "<all_urls>" permission should not have access to captureVisibleTab');
154
browser.test.notifyPass("captureVisibleTabPermissions");
155
},
156
});
157
158
await extension.startup();
159
160
await extension.awaitFinish("captureVisibleTabPermissions");
161
162
await extension.unload();
163
});
164
</script>
165
166
</body>
167
</html>