Bug 1740135 - Treat download button like save action to avoid opening on completion. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D131945
This commit is contained in:
Sam Foster 2021-11-29 20:07:57 +00:00
Родитель 024b1ac41c
Коммит 2816a6e799
5 изменённых файлов: 157 добавлений и 11 удалений

Просмотреть файл

@ -356,8 +356,16 @@ class ChromeActions {
var blobUri = NetUtil.newURI(blobUrl); var blobUri = NetUtil.newURI(blobUrl);
// If the download was triggered from the ctrl/cmd+s or "Save Page As" // If the download was triggered from the ctrl/cmd+s or "Save Page As"
// launch the "Save As" dialog. // or the download button, launch the "Save As" dialog.
if (data.sourceEventType == "save") { const saveOnDownload = getBoolPref(
"browser.download.improvements_to_download_panel",
false
);
if (
data.sourceEventType == "save" ||
(saveOnDownload && data.sourceEventType == "download")
) {
let actor = getActor(this.domWindow); let actor = getActor(this.domWindow);
actor.sendAsyncMessage("PDFJS:Parent:saveURL", { actor.sendAsyncMessage("PDFJS:Parent:saveURL", {
blobUrl, blobUrl,

Просмотреть файл

@ -3,6 +3,7 @@ support-files =
file_pdfjs_test.pdf file_pdfjs_test.pdf
head.js head.js
[browser_pdfjs_download_button.js]
[browser_pdfjs_fill_login.js] [browser_pdfjs_fill_login.js]
support-files = support-files =
file_pdfjs_form.pdf file_pdfjs_form.pdf

Просмотреть файл

@ -0,0 +1,117 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);
MockFilePicker.returnValue = MockFilePicker.returnOK;
var tempDir;
function createPromiseForFilePicker() {
return new Promise(resolve => {
MockFilePicker.showCallback = fp => {
let destFile = tempDir.clone();
destFile.append(fp.defaultString);
if (destFile.exists()) {
destFile.remove(false);
}
MockFilePicker.setFiles([destFile]);
MockFilePicker.filterIndex = 0; // kSaveAsType_Complete
resolve();
};
});
}
add_task(async function setup() {
tempDir = createTemporarySaveDirectory();
MockFilePicker.displayDirectory = tempDir;
registerCleanupFunction(async function() {
MockFilePicker.cleanup();
await cleanupDownloads();
tempDir.remove(true);
});
});
async function closeDownloadsPanel() {
if (DownloadsPanel.panel.state !== "closed") {
let hiddenPromise = BrowserTestUtils.waitForEvent(
DownloadsPanel.panel,
"popuphidden"
);
DownloadsPanel.hidePanel();
await hiddenPromise;
}
is(
DownloadsPanel.panel.state,
"closed",
"Check that the download panel is closed"
);
}
/**
* Check clicking the download button saves the file and doesn't open a new viewer
*/
add_task(async function test_downloading_pdf_nonprivate_window() {
const pdfUrl = TESTROOT + "file_pdfjs_test.pdf";
await SpecialPowers.pushPrefEnv({
set: [["browser.download.improvements_to_download_panel", true]],
});
await BrowserTestUtils.withNewTab(
{ gBrowser, url: "about:blank" },
async function(browser) {
await waitForPdfJS(browser, pdfUrl);
const tabCount = gBrowser.tabs.length;
info(`${tabCount} tabs are open at the start of the test`);
let downloadList = await Downloads.getList(Downloads.PUBLIC);
const initialDownloadCount = (await downloadList.getAll()).length;
let filePickerShown = createPromiseForFilePicker();
let downloadsPanelPromise = BrowserTestUtils.waitForEvent(
DownloadsPanel.panel,
"popupshown"
);
info("Clicking on the download button...");
await SpecialPowers.spawn(browser, [], () => {
content.document.querySelector("#download").click();
});
info("Waiting for a filename to be picked from the file picker");
await filePickerShown;
// check that resulted in a download being added to the list
// and the dl panel opened
info("Waiting for download panel to open when the download is complete");
await downloadsPanelPromise;
is(
DownloadsPanel.panel.state,
"open",
"Check the download panel state is 'open'"
);
downloadList = await Downloads.getList(Downloads.PUBLIC);
let currentDownloadCount = (await downloadList.getAll()).length;
is(
currentDownloadCount,
initialDownloadCount + 1,
"A download was added when we clicked download"
);
is(
gBrowser.tabs.length,
tabCount,
"No new tab was opened to view the downloaded PDF"
);
await closeDownloadsPanel();
}
);
});

Просмотреть файл

@ -15,15 +15,6 @@ Services.scriptloader.loadSubScript(
this this
); );
function createTemporarySaveDirectory() {
var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
saveDir.append("testsavedir");
if (!saveDir.exists()) {
saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
}
return saveDir;
}
function createPromiseForTransferComplete(expectedFileName, destFile) { function createPromiseForTransferComplete(expectedFileName, destFile) {
return new Promise(resolve => { return new Promise(resolve => {
MockFilePicker.showCallback = fp => { MockFilePicker.showCallback = fp => {

Просмотреть файл

@ -74,3 +74,32 @@ function changeMimeHandler(preferredAction, alwaysAskBeforeHandling) {
return oldAction; return oldAction;
} }
function createTemporarySaveDirectory() {
var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
saveDir.append("testsavedir");
if (!saveDir.exists()) {
saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
}
return saveDir;
}
async function cleanupDownloads(listId = Downloads.PUBLIC) {
info("cleaning up downloads");
let downloadList = await Downloads.getList(listId);
for (let download of await downloadList.getAll()) {
await download.finalize(true);
try {
if (Services.appinfo.OS === "WINNT") {
// We need to make the file writable to delete it on Windows.
await IOUtils.setPermissions(download.target.path, 0o600);
}
await IOUtils.remove(download.target.path);
} catch (error) {
info("The file " + download.target.path + " is not removed, " + error);
}
await downloadList.remove(download);
await download.finalize();
}
}