Bug 1759231 - Downloads panel should not open on extension created downloads created while not handling user input. r=mak,robwu

Differential Revision: https://phabricator.services.mozilla.com/D143268
This commit is contained in:
Luca Greco 2022-04-27 15:51:00 +00:00
Родитель f7b18962b0
Коммит 2613268e81
2 изменённых файлов: 114 добавлений и 0 удалений

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

@ -167,6 +167,115 @@ add_task(async function test_downloads_openDownloadsListOnStart_param() {
await SpecialPowers.popPrefEnv();
});
/**
* Make sure the downloads panel _does not_ open automatically when an
* extension calls the browser.downloads.download API method while it is
* not handling user input, but that we do open it automatically when
* the same WebExtensions API is called while handling user input
* (See Bug 1759231)
*/
add_task(async function test_downloads_panel_on_webext_download_api() {
await SpecialPowers.pushPrefEnv({
set: [
["browser.download.improvements_to_download_panel", true],
["browser.download.always_ask_before_handling_new_types", false],
["browser.download.alwaysOpenPanel", true],
],
});
registerCleanupFunction(async () => {
await SpecialPowers.popPrefEnv();
});
const extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["downloads"],
},
background() {
async function startDownload(downloadOptions) {
/* globals browser */
const downloadId = await browser.downloads.download(downloadOptions);
const downloadDone = new Promise(resolve => {
browser.downloads.onChanged.addListener(function listener(delta) {
browser.test.log(`downloads.onChanged = ${JSON.stringify(delta)}`);
if (
delta.id == downloadId &&
delta.state?.current !== "in_progress"
) {
browser.downloads.onChanged.removeListener(listener);
resolve();
}
});
});
browser.test.sendMessage("start-download:done");
await downloadDone;
await browser.downloads.removeFile(downloadId);
browser.test.sendMessage("removed-download-file");
}
browser.test.onMessage.addListener(
(msg, { withHandlingUserInput, downloadOptions }) => {
if (msg !== "start-download") {
browser.test.fail(`Got unexpected test message: ${msg}`);
return;
}
if (withHandlingUserInput) {
browser.test.withHandlingUserInput(() =>
startDownload(downloadOptions)
);
} else {
startDownload(downloadOptions);
}
}
);
},
});
await extension.startup();
startServer();
async function testExtensionDownloadCall({ withHandlingUserInput }) {
mustInterruptResponses();
let rnd = Math.random();
let url = httpUrl(`interruptible.txt?q=${rnd}`);
extension.sendMessage("start-download", {
withHandlingUserInput,
downloadOptions: { url },
});
await extension.awaitMessage("start-download:done");
let publicList = await Downloads.getList(Downloads.PUBLIC);
let downloads = await publicList.getAll();
let download = downloads.find(d => d.source.url === url);
is(download.source.url, url, "download has the expected url");
is(
download.openDownloadsListOnStart,
withHandlingUserInput,
`download panel should ${withHandlingUserInput ? "open" : "stay closed"}`
);
continueResponses();
await extension.awaitMessage("removed-download-file");
}
info(
"Test extension downloads.download API method call without handling user input"
);
await testExtensionDownloadCall({ withHandlingUserInput: true });
info(
"Test extension downloads.download API method call while handling user input"
);
await testExtensionDownloadCall({ withHandlingUserInput: false });
await extension.unload();
await SpecialPowers.popPrefEnv();
});
/**
* Make sure the downloads panel opens automatically with new download, only if
* no other downloads are in progress.

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

@ -666,6 +666,8 @@ this.downloads = class extends ExtensionAPIPersistent {
return {
downloads: {
download(options) {
const isHandlingUserInput =
context.callContextData?.isHandlingUserInput;
let { filename } = options;
if (filename && AppConstants.platform === "win") {
// cross platform javascript code uses "/"
@ -1014,6 +1016,9 @@ this.downloads = class extends ExtensionAPIPersistent {
}
return Downloads.createDownload({
// Only open the download panel if the method has been called
// while handling user input (See Bug 1759231).
openDownloadsListOnStart: isHandlingUserInput,
source,
target: {
path: target,