Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / toolkit / components / pdfjs / test / browser_pdfjs_saveas.js
blob34e10fbdc4b2a71447eacad7b09562c3413780ee
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 = "http://example.com/browser/" + RELATIVE_DIR;
9 var MockFilePicker = SpecialPowers.MockFilePicker;
11 Services.scriptloader.loadSubScript(
12 "chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js",
13 this
16 function createPromiseForTransferComplete(expectedFileName, destFile) {
17 return new Promise(resolve => {
18 MockFilePicker.showCallback = fp => {
19 info("Filepicker shown, checking filename");
20 is(fp.defaultString, expectedFileName, "Filename should be correct.");
21 let fileName = fp.defaultString;
22 destFile.append(fileName);
24 MockFilePicker.setFiles([destFile]);
25 MockFilePicker.filterIndex = 0; // kSaveAsType_Complete
27 MockFilePicker.showCallback = null;
28 mockTransferCallback = function (downloadSuccess) {
29 ok(downloadSuccess, "File should have been downloaded successfully");
30 mockTransferCallback = () => {};
31 resolve();
34 });
37 let tempDir = createTemporarySaveDirectory();
39 add_setup(async function () {
40 MockFilePicker.init(window.browsingContext);
41 mockTransferRegisterer.register();
42 registerCleanupFunction(function () {
43 mockTransferRegisterer.unregister();
44 MockFilePicker.cleanup();
45 tempDir.remove(true);
46 });
47 });
49 /**
50 * Check triggering "Save Page As" on a non-forms PDF opens the "Save As" dialog
51 * and successfully saves the file.
53 add_task(async function test_pdf_saveas() {
54 await BrowserTestUtils.withNewTab(
55 { gBrowser, url: "about:blank" },
56 async function (browser) {
57 await waitForPdfJS(browser, TESTROOT + "file_pdfjs_test.pdf");
58 let destFile = tempDir.clone();
59 MockFilePicker.displayDirectory = tempDir;
60 let fileSavedPromise = createPromiseForTransferComplete(
61 "file_pdfjs_test.pdf",
62 destFile
64 saveBrowser(browser);
65 await fileSavedPromise;
66 await waitForPdfJSClose(browser);
69 });
71 /**
72 * Check triggering "Save Page As" on a PDF with forms that has been modified
73 * does the following:
74 * 1) opens the "Save As" dialog
75 * 2) successfully saves the file
76 * 3) the new file contains the new form data
78 add_task(async function test_pdf_saveas_forms() {
79 let destFile = tempDir.clone();
80 await BrowserTestUtils.withNewTab(
81 { gBrowser, url: "about:blank" },
82 async function (browser) {
83 await waitForPdfJSAnnotationLayer(
84 browser,
85 TESTROOT + "file_pdfjs_form.pdf"
87 // Fill in the form input field.
88 await SpecialPowers.spawn(browser, [], async function () {
89 let formInput = content.document.querySelector(
90 "#viewerContainer input"
92 ok(formInput, "PDF contains text field.");
93 is(formInput.value, "", "Text field is empty to start.");
94 formInput.value = "test";
95 formInput.dispatchEvent(new content.window.Event("input"));
96 });
98 MockFilePicker.displayDirectory = tempDir;
99 let fileSavedPromise = createPromiseForTransferComplete(
100 "file_pdfjs_form.pdf",
101 destFile
103 saveBrowser(browser);
104 await fileSavedPromise;
105 await waitForPdfJSClose(browser);
109 // Now that the file has been modified and saved, load it to verify the form
110 // data persisted.
111 await BrowserTestUtils.withNewTab(
112 { gBrowser, url: "about:blank" },
113 async function (browser) {
114 await waitForPdfJSAnnotationLayer(browser, NetUtil.newURI(destFile).spec);
115 await SpecialPowers.spawn(browser, [], async function () {
116 let formInput = content.document.querySelector(
117 "#viewerContainer input"
119 ok(formInput, "PDF contains text field.");
120 is(formInput.value, "test", "Text field is filled in.");
122 await waitForPdfJSClose(browser);
128 * Check triggering "Save Page As" on a PDF which was loaded with a custom
129 * content disposition filename defaults to using the provided filename.
131 add_task(async function test_pdf_saveas_customname() {
132 await SpecialPowers.pushPrefEnv({
133 set: [
134 ["browser.helperApps.showOpenOptionForPdfJS", true],
135 ["browser.helperApps.showOpenOptionForViewableInternally", true],
136 ["browser.download.always_ask_before_handling_new_types", false],
137 ["browser.download.open_pdf_attachments_inline", true],
140 await BrowserTestUtils.withNewTab(
141 { gBrowser, url: TESTROOT + "file_pdf_download_link.html" },
142 async function (browser) {
143 // Click on the download link in the loaded file. This will create a new
144 // tab with the PDF loaded in it.
145 BrowserTestUtils.synthesizeMouseAtCenter("#custom_filename", {}, browser);
146 let tab = await BrowserTestUtils.waitForNewTab(gBrowser);
147 info("tab created");
149 // Wait for the PDF's metadata to be fully loaded before downloading, as
150 // otherwise it won't be aware of the content disposition filfename yet.
151 await BrowserTestUtils.waitForContentEvent(
152 tab.linkedBrowser,
153 "metadataloaded",
154 false,
155 null,
156 true
158 info("metadata loaded");
160 let destFile = tempDir.clone();
161 MockFilePicker.displayDirectory = tempDir;
162 let fileSavedPromise = createPromiseForTransferComplete(
163 "custom_filename.pdf",
164 destFile
166 saveBrowser(tab.linkedBrowser);
167 await fileSavedPromise;
168 await waitForPdfJSClose(tab.linkedBrowser, /* closeTab = */ true);
171 await SpecialPowers.popPrefEnv();
175 * Check if the directory where the pdfs are saved is based on the original
176 * domain (see bug 1768383).
178 add_task(async function () {
179 await BrowserTestUtils.withNewTab(
180 { gBrowser, url: "about:blank" },
181 async function (browser) {
182 const downloadLastDir = new DownloadLastDir(null);
183 const destDirs = [];
184 for (let i = 1; i <= 2; i++) {
185 const destDir = createTemporarySaveDirectory(i);
186 destDirs.push(destDir);
187 const url = `http://test${i}.example.com/browser/${RELATIVE_DIR}file_pdfjs_test.pdf`;
188 downloadLastDir.setFile(url, destDir);
189 await TestUtils.waitForTick();
192 const url = `http://test1.example.com/browser/${RELATIVE_DIR}file_pdfjs_hcm.pdf`;
193 await waitForPdfJS(browser, url);
195 const fileSavedPromise = new Promise(resolve => {
196 MockFilePicker.showCallback = fp => {
197 MockFilePicker.setFiles([]);
198 MockFilePicker.showCallback = null;
199 resolve(fp.displayDirectory.path);
202 registerCleanupFunction(() => {
203 for (const destDir of destDirs) {
204 destDir.remove(true);
207 saveBrowser(browser);
208 const dirPath = await fileSavedPromise;
210 dirPath,
211 destDirs[0].path,
212 "Proposed directory must be based on the domain"
214 await waitForPdfJSClose(browser);