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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/toolkit/components/printing/tests/head.js",
this
);
const PSSVC = Cc["@mozilla.org/gfx/printsettings-service;1"].getService(
Ci.nsIPrintSettingsService
);
let mockCA = makeMockContentAnalysis();
add_setup(async function test_setup() {
mockCA = mockContentAnalysisService(mockCA);
});
const TEST_PAGE_URL = PrintHelper.getTestPageUrlHTTPS(
"changing_page_for_print.html"
);
function addUniqueSuffix(prefix) {
return `${prefix}-${Services.uuid
.generateUUID()
.toString()
.slice(1, -1)}.pdf`;
}
async function printToDestination(aBrowser, aDestination) {
let tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
let fileName = addUniqueSuffix(`printDestinationTest-${aDestination}`);
let filePath = PathUtils.join(tmpDir.path, fileName);
info(`Printing to ${filePath}`);
let settings = PSSVC.createNewPrintSettings();
settings.outputFormat = Ci.nsIPrintSettings.kOutputFormatPDF;
settings.outputDestination = aDestination;
settings.headerStrCenter = "";
settings.headerStrLeft = "";
settings.headerStrRight = "";
settings.footerStrCenter = "";
settings.footerStrLeft = "";
settings.footerStrRight = "";
settings.unwriteableMarginTop = 1; /* Just to ensure settings are respected on both */
let outStream = null;
if (aDestination == Ci.nsIPrintSettings.kOutputDestinationFile) {
settings.toFileName = PathUtils.join(tmpDir.path, fileName);
} else {
is(aDestination, Ci.nsIPrintSettings.kOutputDestinationStream);
outStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
Ci.nsIFileOutputStream
);
let tmpFile = tmpDir.clone();
tmpFile.append(fileName);
outStream.init(tmpFile, -1, 0o666, 0);
settings.outputStream = outStream;
}
await aBrowser.browsingContext.print(settings);
return filePath;
}
function assertContentAnalysisRequest(request) {
is(request.url.spec, TEST_PAGE_URL, "request has correct URL");
is(
request.analysisType,
Ci.nsIContentAnalysisRequest.ePrint,
"request has print analysisType"
);
is(
request.operationTypeForDisplay,
Ci.nsIContentAnalysisRequest.eOperationPrint,
"request has print operationTypeForDisplay"
);
is(request.textContent, "", "request textContent should be empty");
is(request.filePath, "", "request filePath should be empty");
isnot(request.printDataHandle, 0, "request printDataHandle should not be 0");
isnot(request.printDataSize, 0, "request printDataSize should not be 0");
ok(!!request.requestToken.length, "request requestToken should not be empty");
}
add_task(
async function testPrintToStreamWithContentAnalysisActiveAndAllowing() {
await PrintHelper.withTestPage(
async helper => {
mockCA.setupForTest(true);
let filePath = await printToDestination(
helper.sourceBrowser,
Ci.nsIPrintSettings.kOutputDestinationFile
);
is(
mockCA.calls.length,
1,
"Correct number of calls to Content Analysis"
);
assertContentAnalysisRequest(mockCA.calls[0]);
// This effectively tests that the PDF content sent to Content Analysis
// and the content that is actually printed matches. This is necessary
// because a previous iteration of the Content Analysis code didn't use
// a static Document clone for this and so the content would differ. (since
// the .html file in question adds content to the page when print events
// happen)
await waitForFileToAlmostMatchSize(
filePath,
mockCA.calls[0].printDataSize
);
await IOUtils.remove(filePath);
},
TEST_PAGE_URL,
true
);
}
);
add_task(
async function testPrintToStreamWithContentAnalysisActiveAndBlocking() {
await PrintHelper.withTestPage(
async helper => {
mockCA.setupForTest(false);
try {
await printToDestination(
helper.sourceBrowser,
Ci.nsIPrintSettings.kOutputDestinationFile
);
ok(false, "Content analysis should make this fail to print");
} catch (e) {
ok(
/NS_ERROR_CONTENT_BLOCKED/.test(e.toString()),
"Got content blocked error"
);
}
is(
mockCA.calls.length,
1,
"Correct number of calls to Content Analysis"
);
assertContentAnalysisRequest(mockCA.calls[0]);
},
TEST_PAGE_URL,
true
);
}
);
add_task(async function testPrintToStreamWithContentAnalysisReturningError() {
await PrintHelper.withTestPage(
async helper => {
expectUncaughtException();
mockCA.setupForTestWithError(Cr.NS_ERROR_NOT_AVAILABLE);
try {
await printToDestination(
helper.sourceBrowser,
Ci.nsIPrintSettings.kOutputDestinationFile
);
ok(false, "Content analysis should make this fail to print");
} catch (e) {
ok(
/NS_ERROR_NOT_AVAILABLE/.test(e.toString()),
"Error in mock CA was propagated out"
);
}
is(mockCA.calls.length, 1, "Correct number of calls to Content Analysis");
assertContentAnalysisRequest(mockCA.calls[0]);
},
TEST_PAGE_URL,
true
);
});
add_task(async function testPrintThroughDialogWithContentAnalysisActive() {
await PrintHelper.withTestPage(
async helper => {
mockCA.setupForTest(true);
let fileName = addUniqueSuffix(`printDialogTest`);
let file = helper.mockFilePicker(fileName);
info(`Printing to ${file.path}`);
await helper.startPrint();
await helper.assertPrintToFile(file, () => {
EventUtils.sendKey("return", helper.win);
});
is(mockCA.calls.length, 1, "Correct number of calls to Content Analysis");
assertContentAnalysisRequest(mockCA.calls[0]);
await waitForFileToAlmostMatchSize(
file.path,
mockCA.calls[0].printDataSize
);
},
TEST_PAGE_URL,
true
);
});
add_task(
async function testPrintThroughDialogWithContentAnalysisActiveAndBlocking() {
await PrintHelper.withTestPage(
async helper => {
mockCA.setupForTest(false);
await helper.startPrint();
let fileName = addUniqueSuffix(`printDialogTest`);
let file = helper.mockFilePicker(fileName);
info(`Printing to ${file.path}`);
try {
await helper.assertPrintToFile(file, () => {
EventUtils.sendKey("return", helper.win);
});
} catch (e) {
ok(
/Wait for target file to get created/.test(e.toString()),
"Target file should not get created"
);
}
ok(!file.exists(), "File should not exist");
is(
mockCA.calls.length,
1,
"Correct number of calls to Content Analysis"
);
assertContentAnalysisRequest(mockCA.calls[0]);
},
TEST_PAGE_URL,
true
);
}
);
add_task(
async function testPrintThroughDialogWithContentAnalysisReturningError() {
await PrintHelper.withTestPage(
async helper => {
expectUncaughtException();
mockCA.setupForTestWithError(Cr.NS_ERROR_NOT_AVAILABLE);
await helper.startPrint();
let fileName = addUniqueSuffix(`printDialogTest`);
let file = helper.mockFilePicker(fileName);
info(`Printing to ${file.path}`);
try {
await helper.assertPrintToFile(file, () => {
EventUtils.sendKey("return", helper.win);
});
} catch (e) {
ok(
/Wait for target file to get created/.test(e.toString()),
"Target file should not get created"
);
}
ok(!file.exists(), "File should not exist");
is(
mockCA.calls.length,
1,
"Correct number of calls to Content Analysis"
);
assertContentAnalysisRequest(mockCA.calls[0]);
},
TEST_PAGE_URL,
true
);
}
);
|