1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
6 const TESTROOT = getRootDirectory(gTestPath).replace(
7 "chrome://mochitests/content/",
8 "http://mochi.test:8888/"
11 // Get a ref to the pdf we want to open.
12 const PDF_URL = TESTROOT + "file_pdfjs_object_stream.pdf";
14 var gMIMEService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
17 * Check that if we open a PDF with octet-stream mimetype, it can load
20 add_task(async function test_octet_stream_opens_pdfjs() {
21 await SpecialPowers.pushPrefEnv({ set: [["pdfjs.handleOctetStream", true]] });
22 let handlerInfo = gMIMEService.getFromTypeAndExtension(
27 // Make sure pdf.js is the default handler.
29 handlerInfo.alwaysAskBeforeHandling,
31 "pdf handler defaults to always-ask is false"
34 handlerInfo.preferredAction,
35 Ci.nsIHandlerInfo.handleInternally,
36 "pdf handler defaults to internal"
39 await BrowserTestUtils.withNewTab(
40 { gBrowser, url: "about:blank" },
41 async function (newTabBrowser) {
42 await waitForPdfJS(newTabBrowser, PDF_URL);
43 is(newTabBrowser.currentURI.spec, PDF_URL, "Should load pdfjs");
44 await waitForPdfJSClose(newTabBrowser);
50 * Check that if the octet-stream thing is in a frame, we don't load it inside PDF.js
52 add_task(async function test_octet_stream_in_frame() {
53 await SpecialPowers.pushPrefEnv({
54 set: [["pdfjs.handleOctetStream", true]],
57 let handlerInfo = gMIMEService.getFromTypeAndExtension(
62 // Make sure pdf.js is the default handler.
64 handlerInfo.alwaysAskBeforeHandling,
66 "pdf handler defaults to always-ask is false"
69 handlerInfo.preferredAction,
70 Ci.nsIHandlerInfo.handleInternally,
71 "pdf handler defaults to internal"
74 let downloadsPanelPromise = BrowserTestUtils.waitForEvent(
79 // Once downloaded, the PDF will be opened as a file:// URI in a new tab
80 let previewTabPromise = BrowserTestUtils.waitForNewTab(
83 let uri = NetUtil.newURI(url);
84 return uri.scheme == "file" && uri.spec.endsWith(".pdf");
86 false, // dont wait for load
87 true // any tab, not just the next one
90 await BrowserTestUtils.withNewTab(
91 { gBrowser, url: `data:text/html,<iframe src='${PDF_URL}'>` },
93 // wait until downloadsPanel opens before continuing with test
94 info("Waiting for download panel to open");
95 await downloadsPanelPromise;
97 DownloadsPanel.panel.state,
99 "Check the download panel state is 'open'"
101 let downloadList = await Downloads.getList(Downloads.PUBLIC);
102 let [download] = downloadList._downloads;
104 // Verify the downloaded PDF opened in a new tab,
105 // with its download file URI
106 info("Waiting for preview tab");
107 let previewTab = await previewTabPromise;
108 await waitForSelector(
109 previewTab.linkedBrowser,
110 ".textLayer .endOfContent",
111 "Wait for text layer."
114 ok(previewTab, "PDF opened in a new tab");
116 is(DownloadsPanel.isPanelShowing, true, "DownloadsPanel should be open.");
118 downloadList._downloads.length,
120 "File should be successfully downloaded."
123 await waitForPdfJSClose(previewTab.linkedBrowser, /* closeTab */ true);
125 info("cleaning up downloads");
127 if (Services.appinfo.OS === "WINNT") {
128 // We need to make the file writable to delete it on Windows.
129 await IOUtils.setPermissions(download.target.path, 0o600);
131 await IOUtils.remove(download.target.path);
133 info("The file " + download.target.path + " is not removed, " + error);
135 await downloadList.remove(download);
136 await download.finalize();
138 if (DownloadsPanel.panel.state !== "closed") {
139 let hiddenPromise = BrowserTestUtils.waitForEvent(
140 DownloadsPanel.panel,
143 DownloadsPanel.hidePanel();
147 DownloadsPanel.panel.state,
149 "Check that the download panel is closed"
154 await SpecialPowers.popPrefEnv();