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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const TESTROOT = getRootDirectory(gTestPath).replace(
"chrome://mochitests/content/",
"http://mochi.test:8888/"
);
// Get a ref to the pdf we want to open.
const PDF_URL = TESTROOT + "file_pdfjs_object_stream.pdf";
var gMIMEService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
/**
* Check that if we open a PDF with octet-stream mimetype, it can load
* PDF.js .
*/
add_task(async function test_octet_stream_opens_pdfjs() {
await SpecialPowers.pushPrefEnv({ set: [["pdfjs.handleOctetStream", true]] });
let handlerInfo = gMIMEService.getFromTypeAndExtension(
"application/pdf",
"pdf"
);
// Make sure pdf.js is the default handler.
is(
handlerInfo.alwaysAskBeforeHandling,
false,
"pdf handler defaults to always-ask is false"
);
is(
handlerInfo.preferredAction,
Ci.nsIHandlerInfo.handleInternally,
"pdf handler defaults to internal"
);
await BrowserTestUtils.withNewTab(
{ gBrowser, url: "about:blank" },
async function(newTabBrowser) {
await waitForPdfJS(newTabBrowser, PDF_URL);
is(newTabBrowser.currentURI.spec, PDF_URL, "Should load pdfjs");
}
);
});
/**
* Check that when the improvements_to_download_panel pref is false and
* if the octet-stream thing is in a frame, we don't load it inside PDF.js
*/
add_task(async function test_octet_stream_in_frame_downloads() {
await SpecialPowers.pushPrefEnv({
set: [
["pdfjs.handleOctetStream", true],
["browser.download.improvements_to_download_panel", false],
],
});
let handlerInfo = gMIMEService.getFromTypeAndExtension(
"application/pdf",
"pdf"
);
// Make sure pdf.js is the default handler.
is(
handlerInfo.alwaysAskBeforeHandling,
false,
"pdf handler defaults to always-ask is false"
);
is(
handlerInfo.preferredAction,
Ci.nsIHandlerInfo.handleInternally,
"pdf handler defaults to internal"
);
let dialogPromise = BrowserTestUtils.domWindowOpenedAndLoaded();
await BrowserTestUtils.withNewTab(
{ gBrowser, url: `data:text/html,<iframe src='${PDF_URL}'>` },
async function(newTabBrowser) {
let dialogWin = await dialogPromise;
ok(dialogWin, "Should have a dialog asking what to do.");
dialogWin.close(); // This is going to cancel the dialog.
}
);
});
/**
* Check that when the improvements_to_download_panel pref is true and
* if the octet-stream thing is in a frame, we don't load it inside PDF.js
*/
add_task(
async function test_octet_stream_in_frame_downloads_improvements_to_download_panel() {
await SpecialPowers.pushPrefEnv({
set: [
["pdfjs.handleOctetStream", true],
["browser.download.improvements_to_download_panel", true],
],
});
let handlerInfo = gMIMEService.getFromTypeAndExtension(
"application/pdf",
"pdf"
);
// Make sure pdf.js is the default handler.
is(
handlerInfo.alwaysAskBeforeHandling,
false,
"pdf handler defaults to always-ask is false"
);
is(
handlerInfo.preferredAction,
Ci.nsIHandlerInfo.handleInternally,
"pdf handler defaults to internal"
);
let downloadsPanelPromise = BrowserTestUtils.waitForEvent(
DownloadsPanel.panel,
"popupshown"
);
// Once downloaded, the PDF will be opened as a file:// URI in a new tab
let previewTabPromise = BrowserTestUtils.waitForNewTab(
gBrowser,
url => {
let uri = NetUtil.newURI(url);
return uri.scheme == "file" && uri.spec.endsWith(".pdf");
},
false, // dont wait for load
true // any tab, not just the next one
);
await BrowserTestUtils.withNewTab(
{ gBrowser, url: `data:text/html,<iframe src='${PDF_URL}'>` },
async function(newTabBrowser) {
// wait until downloadsPanel opens before continuing with test
info("Waiting for download panel to open");
await downloadsPanelPromise;
is(
DownloadsPanel.panel.state,
"open",
"Check the download panel state is 'open'"
);
let downloadList = await Downloads.getList(Downloads.PUBLIC);
let [download] = downloadList._downloads;
// Verify the downloaded PDF opened in a new tab,
// with its download file URI
info("Waiting for preview tab");
let previewTab = await previewTabPromise;
ok(previewTab, "PDF opened in a new tab");
is(
DownloadsPanel.isPanelShowing,
true,
"DownloadsPanel should be open."
);
is(
downloadList._downloads.length,
1,
"File should be successfully downloaded."
);
await BrowserTestUtils.removeTab(previewTab);
info("cleaning up downloads");
try {
if (Services.appinfo.OS === "WINNT") {
// We need to make the file writable to delete it on Windows.
await IOUtils.setPermissions(download.target.path, 0o600);
}
await IOUtils.remove(download.target.path);
} catch (error) {
info(
"The file " + download.target.path + " is not removed, " + error
);
}
await downloadList.remove(download);
await download.finalize();
if (DownloadsPanel.panel.state !== "closed") {
let hiddenPromise = BrowserTestUtils.waitForEvent(
DownloadsPanel.panel,
"popuphidden"
);
DownloadsPanel.hidePanel();
await hiddenPromise;
}
is(
DownloadsPanel.panel.state,
"closed",
"Check that the download panel is closed"
);
}
);
}
);
|