Bug 1734711 - Fix the mixed-content tests to pass when download improvements are enabled. r=Gijs

* Run the tests with both true and false values for the browser.download.improvements_to_download_panel pref
* Handle the case when the download improvements pref will cause the downloads to save by default and not prompt for how to handle it

Differential Revision: https://phabricator.services.mozilla.com/D127994
This commit is contained in:
Sam Foster 2021-10-11 23:17:44 +00:00
Родитель 37de8c9ba9
Коммит 77da6039ad
1 изменённых файлов: 166 добавлений и 113 удалений

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

@ -41,15 +41,40 @@ async function task_openPanel() {
await promise;
}
const downloadMonitoringView = {
_listeners: [],
onDownloadAdded(download) {
for (let listener of this._listeners) {
listener(download);
}
this._listeners = [];
},
waitForDownload(listener) {
this._listeners.push(listener);
},
};
/**
* Waits until the download Prompt is shown, selects
* Waits until a download is triggered.
* When the improvements_to_download_panel prefs is true, the download
* will simply be saved, so resolve when the view is notified of the new download.
* When false, it waits until a prompt is shown, selects
* the choosen <action>, then accepts the dialog
* @param [action] Which action to select, either:
* "handleInternally", "save" or "open".
* @returns {Promise} Resolved once done.
*/
function shouldPromptDownload(action = "save") {
function shouldTriggerDownload(action = "save") {
if (
Services.prefs.getBoolPref(
"browser.download.improvements_to_download_panel"
)
) {
return new Promise(res => {
downloadMonitoringView.waitForDownload(res);
});
}
return new Promise((resolve, reject) => {
Services.wm.addListener({
onOpenWindow(xulWin) {
@ -102,36 +127,31 @@ async function resetDownloads() {
}
}
async function shouldNotifyDownloadUI() {
// Waits until a Blocked download was added to the Download List
// -> returns the blocked Download
let list = await Downloads.getList(Downloads.ALL);
return new Promise((res, err) => {
const view = {
onDownloadAdded: async aDownload => {
let { error } = aDownload;
if (
error.becauseBlockedByReputationCheck &&
error.reputationCheckVerdict == Downloads.Error.BLOCK_VERDICT_INSECURE
) {
// It's an insecure Download, now Check that it has been cleaned up properly
if ((await IOUtils.stat(aDownload.target.path)).size != 0) {
throw new Error(`Download target is not empty!`);
}
if ((await IOUtils.stat(aDownload.target.path)).size != 0) {
throw new Error(`Download partFile was not cleaned up properly`);
}
// Assert that the Referrer is presnt
if (!aDownload.source.referrerInfo) {
throw new Error("The Blocked download is missing the ReferrerInfo");
}
res(aDownload);
list.removeView(view);
function shouldNotifyDownloadUI() {
return new Promise(res => {
downloadMonitoringView.waitForDownload(async aDownload => {
let { error } = aDownload;
if (
error.becauseBlockedByReputationCheck &&
error.reputationCheckVerdict == Downloads.Error.BLOCK_VERDICT_INSECURE
) {
// It's an insecure Download, now Check that it has been cleaned up properly
if ((await IOUtils.stat(aDownload.target.path)).size != 0) {
throw new Error(`Download target is not empty!`);
}
},
};
list.addView(view);
if ((await IOUtils.stat(aDownload.target.path)).size != 0) {
throw new Error(`Download partFile was not cleaned up properly`);
}
// Assert that the Referrer is presnt
if (!aDownload.source.referrerInfo) {
throw new Error("The Blocked download is missing the ReferrerInfo");
}
res(aDownload);
} else {
ok(false, "No error for download that was expected to error!");
}
});
});
}
@ -163,100 +183,133 @@ async function runTest(url, link, checkFunction, description) {
await SpecialPowers.popPrefEnv();
}
add_task(async function setup() {
let list = await Downloads.getList(Downloads.ALL);
list.addView(downloadMonitoringView);
registerCleanupFunction(() => list.removeView(downloadMonitoringView));
});
// Test Blocking
add_task(async function() {
await runTest(
INSECURE_BASE_URL,
"insecure",
shouldPromptDownload,
"Insecure -> Insecure should download"
);
await runTest(
INSECURE_BASE_URL,
"secure",
shouldPromptDownload,
"Insecure -> Secure should download"
);
await runTest(
SECURE_BASE_URL,
"insecure",
() =>
Promise.all([
shouldPromptDownload(),
shouldNotifyDownloadUI(),
shouldConsoleError(),
]),
"Secure -> Insecure should Error"
);
await runTest(
SECURE_BASE_URL,
"secure",
shouldPromptDownload,
"Secure -> Secure should Download"
);
for (let prefVal of [true, false]) {
await SpecialPowers.pushPrefEnv({
set: [["browser.download.improvements_to_download_panel", prefVal]],
});
await runTest(
INSECURE_BASE_URL,
"insecure",
shouldTriggerDownload,
"Insecure -> Insecure should download"
);
await runTest(
INSECURE_BASE_URL,
"secure",
shouldTriggerDownload,
"Insecure -> Secure should download"
);
await runTest(
SECURE_BASE_URL,
"insecure",
() =>
Promise.all([
shouldTriggerDownload(),
shouldNotifyDownloadUI(),
shouldConsoleError(),
]),
"Secure -> Insecure should Error"
);
await runTest(
SECURE_BASE_URL,
"secure",
shouldTriggerDownload,
"Secure -> Secure should Download"
);
await SpecialPowers.popPrefEnv();
}
});
// Test Manual Unblocking
add_task(async function() {
await runTest(
SECURE_BASE_URL,
"insecure",
async () => {
await shouldPromptDownload();
let download = await shouldNotifyDownloadUI();
await download.unblock();
ok(download.error == null, "There should be no error after unblocking");
},
"A Blocked Download Should succeeded to Download after a Manual unblock"
);
for (let prefVal of [true, false]) {
await SpecialPowers.pushPrefEnv({
set: [["browser.download.improvements_to_download_panel", prefVal]],
});
await runTest(
SECURE_BASE_URL,
"insecure",
async () => {
let [, download] = await Promise.all([
shouldTriggerDownload(),
shouldNotifyDownloadUI(),
]);
await download.unblock();
ok(download.error == null, "There should be no error after unblocking");
},
"A Blocked Download Should succeeded to Download after a Manual unblock"
);
await SpecialPowers.popPrefEnv();
}
});
// Test Unblock Download Visible
add_task(async function() {
// Focus, open and close the panel once
// to make sure the panel is loaded and ready
await promiseFocus();
await runTest(
SECURE_BASE_URL,
"insecure",
async () => {
let panelHasOpened = promisePanelOpened();
info("awaiting that the Download Prompt is shown");
await shouldPromptDownload();
info("awaiting that the Download list adds the new download");
await shouldNotifyDownloadUI();
info("awaiting that the Download list shows itself");
await panelHasOpened;
DownloadsPanel.hidePanel();
ok(true, "The Download Panel should have opened on blocked download");
},
"A Blocked Download Should open the Download Panel"
);
for (let prefVal of [true, false]) {
await SpecialPowers.pushPrefEnv({
set: [["browser.download.improvements_to_download_panel", prefVal]],
});
// Focus, open and close the panel once
// to make sure the panel is loaded and ready
await promiseFocus();
await runTest(
SECURE_BASE_URL,
"insecure",
async () => {
let panelHasOpened = promisePanelOpened();
info("awaiting that the a download is triggered and added to the list");
await Promise.all([shouldTriggerDownload(), shouldNotifyDownloadUI()]);
info("awaiting that the Download list shows itself");
await panelHasOpened;
DownloadsPanel.hidePanel();
ok(true, "The Download Panel should have opened on blocked download");
},
"A Blocked Download Should open the Download Panel"
);
await SpecialPowers.popPrefEnv();
}
});
// Test Download an insecure pdf and choose "Open with Firefox"
add_task(async function download_open_insecure_pdf() {
await promiseFocus();
await runTest(
SECURE_BASE_URL,
"insecurePDF",
async () => {
info("awaiting that the Download Prompt is shown");
await shouldPromptDownload("handleInternally");
let download = await shouldNotifyDownloadUI();
let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
await download.unblock();
ok(download.error == null, "There should be no error after unblocking");
let tab = await newTabPromise;
for (let prefVal of [true, false]) {
await SpecialPowers.pushPrefEnv({
set: [["browser.download.improvements_to_download_panel", prefVal]],
});
await promiseFocus();
await runTest(
SECURE_BASE_URL,
"insecurePDF",
async () => {
info("awaiting that the a download is triggered and added to the list");
let [_, download] = await Promise.all([
shouldTriggerDownload("handleInternally"),
shouldNotifyDownloadUI(),
]);
// Fix (possible) windows file delemiters
let usablePath = download.target.path.replace("\\", "/");
ok(
tab.linkedBrowser._documentURI.filePath.includes(".pdf"),
"The download target was opened"
);
BrowserTestUtils.removeTab(tab);
ok(true, "The Content was opened in a new tab");
},
"A Blocked PDF can be opened internally"
);
let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
await download.unblock();
ok(download.error == null, "There should be no error after unblocking");
let tab = await newTabPromise;
ok(
tab.linkedBrowser._documentURI.filePath.includes(".pdf"),
"The download target was opened"
);
BrowserTestUtils.removeTab(tab);
ok(true, "The Content was opened in a new tab");
await SpecialPowers.popPrefEnv();
},
"A Blocked PDF can be opened internally"
);
}
});