Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / pdfjs / test / browser_pdfjs_nonpdf_filename.js
blobcc976444f20f1e693d4d03325bcee08f1e94109e
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 const TESTROOT = getRootDirectory(gTestPath).replace(
7   "chrome://mochitests/content/",
8   "https://example.com/"
9 );
11 // The page we use to open the PDF.
12 const LINK_PAGE_URL = TESTROOT + "file_pdf_download_link.html";
14 add_task(async function test_filename_nonpdf_extension() {
15   var MockFilePicker = SpecialPowers.MockFilePicker;
16   MockFilePicker.init(window.browsingContext);
17   let filepickerNamePromise = new Promise(resolve => {
18     MockFilePicker.showCallback = function (fp) {
19       resolve(fp.defaultString);
20       return MockFilePicker.returnCancel;
21     };
22   });
23   registerCleanupFunction(() => MockFilePicker.cleanup());
25   await SpecialPowers.pushPrefEnv({
26     set: [["browser.download.open_pdf_attachments_inline", true]],
27   });
29   await BrowserTestUtils.withNewTab(
30     { gBrowser, url: LINK_PAGE_URL },
31     async function (browser) {
32       await SpecialPowers.spawn(browser, [], async () => {
33         let link = content.document.getElementById("custom_filename");
34         let response = await content.fetch(link.href, {
35           method: "GET",
36           headers: {
37             "Content-Type": "application/pdf",
38           },
39         });
40         let blob = await response.blob();
41         const url = content.URL.createObjectURL(blob);
42         link.href = url;
43         link.download = "Fido-2022-04-28";
44         link.rel = "noopener";
45       });
47       let pdfLoaded = BrowserTestUtils.waitForNewTab(
48         gBrowser,
49         url => url.startsWith("blob:"),
50         true
51       );
52       await BrowserTestUtils.synthesizeMouseAtCenter(
53         "#custom_filename",
54         {},
55         browser
56       );
57       let newTab = await pdfLoaded;
59       info("Clicking on the download button...");
60       await SpecialPowers.spawn(newTab.linkedBrowser, [], () => {
61         content.document.getElementById("downloadButton").click();
62       });
63       info("Waiting for a filename to be picked from the file picker");
64       let defaultName = await filepickerNamePromise;
65       is(
66         defaultName,
67         "Fido-2022-04-28.pdf",
68         "Should have gotten the provided filename with pdf suffixed."
69       );
70       await waitForPdfJSClose(newTab.linkedBrowser, /* closeTab = */ true);
71     }
72   );
74   await SpecialPowers.popPrefEnv();
75 });