1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
6 const RELATIVE_DIR
= "toolkit/components/pdfjs/test/";
7 const TESTROOT
= "https://example.com/browser/" + RELATIVE_DIR
;
9 var MockFilePicker
= SpecialPowers
.MockFilePicker
;
13 async
function promiseDownloadFinished(list
) {
14 return new Promise(resolve
=> {
16 onDownloadChanged(download
) {
17 download
.launchWhenSucceeded
= false;
18 if (download
.succeeded
|| download
.error
) {
19 list
.removeView(this);
27 function createPromiseForFilePicker() {
28 return new Promise(resolve
=> {
29 MockFilePicker
.showCallback
= fp
=> {
30 let destFile
= tempDir
.clone();
31 destFile
.append(fp
.defaultString
);
32 if (destFile
.exists()) {
33 destFile
.remove(false);
35 MockFilePicker
.setFiles([destFile
]);
36 MockFilePicker
.filterIndex
= 0; // kSaveAsType_Complete
42 add_setup(async
function () {
43 tempDir
= createTemporarySaveDirectory();
44 MockFilePicker
.init(window
.browsingContext
);
45 MockFilePicker
.returnValue
= MockFilePicker
.returnOK
;
46 MockFilePicker
.displayDirectory
= tempDir
;
48 registerCleanupFunction(async
function () {
49 MockFilePicker
.cleanup();
50 await
cleanupDownloads();
56 * Check clicking the download button saves the file and doesn't open a new viewer
58 add_task(async
function test_downloading_pdf_nonprivate_window() {
59 const pdfUrl
= TESTROOT
+ "file_pdfjs_test.pdf";
61 await SpecialPowers
.pushPrefEnv({
62 set: [["browser.download.always_ask_before_handling_new_types", false]],
65 await BrowserTestUtils
.withNewTab(
66 { gBrowser
, url
: "about:blank" },
67 async
function (browser
) {
68 await
waitForPdfJS(browser
, pdfUrl
);
70 const tabCount
= gBrowser
.tabs
.length
;
71 info(`${tabCount} tabs are open at the start of the test`);
73 let downloadList
= await Downloads
.getList(Downloads
.PUBLIC
);
75 let filePickerShown
= createPromiseForFilePicker();
77 let downloadFinishedPromise
= promiseDownloadFinished(downloadList
);
79 info("Clicking on the download button...");
80 await SpecialPowers
.spawn(browser
, [], () => {
81 content
.document
.querySelector("#downloadButton").click();
83 info("Waiting for a filename to be picked from the file picker");
84 await filePickerShown
;
86 await downloadFinishedPromise
;
87 ok(true, "A download was added when we clicked download");
89 // See bug 1739348 - don't show panel for downloads that opened dialogs
91 !DownloadsPanel
.isPanelShowing
,
92 "The download panel did not open, since the file picker was shown already"
95 const allDownloads
= await downloadList
.getAll();
96 const dl
= allDownloads
.find(dl
=> dl
.source
.originalUrl
=== pdfUrl
);
97 ok(!!dl
, "The pdf download has the correct url in source.originalUrl");
99 SpecialPowers
.clipboardCopyString("");
100 DownloadsCommon
.copyDownloadLink(dl
);
101 const copiedUrl
= SpecialPowers
.getClipboardData("text/plain");
102 is(copiedUrl
, pdfUrl
, "The copied url must be the original one");
105 gBrowser
.tabs
.length
,
107 "No new tab was opened to view the downloaded PDF"
110 SpecialPowers
.clipboardCopyString("");
111 const downloadsLoaded
= BrowserTestUtils
.waitForEvent(
113 "InitialDownloadsLoaded",
116 await
waitForPdfJSClose(browser
);
118 BrowserTestUtils
.startLoadingURIString(browser
, "about:downloads");
119 await downloadsLoaded
;
121 info("Wait for the clipboard to contain the url of the pdf");
122 await SimpleTest
.promiseClipboardChange(pdfUrl
, () => {
123 goDoCommand("cmd_copy");
128 await SpecialPowers
.popPrefEnv();