Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / toolkit / components / pdfjs / test / browser_pdfjs_download_button.js
blob99d475ce2b077280e902cbdc5c16ca284bcd59bc
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
7 const TESTROOT = "https://example.com/browser/" + RELATIVE_DIR;
9 var MockFilePicker = SpecialPowers.MockFilePicker;
11 var tempDir;
13 async function promiseDownloadFinished(list) {
14 return new Promise(resolve => {
15 list.addView({
16 onDownloadChanged(download) {
17 download.launchWhenSucceeded = false;
18 if (download.succeeded || download.error) {
19 list.removeView(this);
20 resolve(download);
23 });
24 });
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
37 resolve();
39 });
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();
51 tempDir.remove(true);
52 });
53 });
55 /**
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]],
63 });
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();
82 });
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
90 ok(
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,
106 tabCount,
107 "No new tab was opened to view the downloaded PDF"
110 SpecialPowers.clipboardCopyString("");
111 const downloadsLoaded = BrowserTestUtils.waitForEvent(
112 browser,
113 "InitialDownloadsLoaded",
114 true
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();