1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
6 const TEST_PATH
= getRootDirectory(gTestPath
).replace(
7 "chrome://mochitests/content",
11 var MockFilePicker
= SpecialPowers
.MockFilePicker
;
12 MockFilePicker
.init(window
.browsingContext
);
14 registerCleanupFunction(async
function () {
15 info("Running the cleanup code");
16 MockFilePicker
.cleanup();
17 if (gTestDir
&& gTestDir
.exists()) {
18 // On Windows, sometimes nsIFile.remove() throws, probably because we're
19 // still writing to the directory we're trying to remove, despite
20 // waiting for the download to complete. Just retry a bit later...
21 let succeeded
= false;
24 gTestDir
.remove(true);
27 await
new Promise(requestAnimationFrame
);
35 function createTemporarySaveDirectory() {
36 var saveDir
= Services
.dirsvc
.get("TmpD", Ci
.nsIFile
);
37 saveDir
.append("testsavedir");
38 if (!saveDir
.exists()) {
39 info("create testsavedir!");
40 saveDir
.create(Ci
.nsIFile
.DIRECTORY_TYPE
, 0o755);
42 info("return from createTempSaveDir: " + saveDir
.path
);
46 add_task(async
function test_image_download() {
47 await BrowserTestUtils
.withNewTab(
48 TEST_PATH
+ "test_mixed_content_image.html",
50 // Add the image, and wait for it to load.
51 await SpecialPowers
.spawn(browser
, [], async
function () {
52 let loc
= content
.document
.location
.href
;
53 let httpRoot
= loc
.replace("https", "http");
54 let imgloc
= new content
.URL("dummy.png", httpRoot
);
55 let img
= content
.document
.createElement("img");
57 await
new Promise(resolve
=> {
59 content
.document
.body
.appendChild(img
);
62 gTestDir
= createTemporarySaveDirectory();
64 let destFile
= gTestDir
.clone();
66 MockFilePicker
.displayDirectory
= gTestDir
;
68 MockFilePicker
.showCallback = function (fp
) {
70 fileName
= fp
.defaultString
;
71 info("fileName: " + fileName
);
72 destFile
.append(fileName
);
73 info("path: " + destFile
.path
);
74 MockFilePicker
.setFiles([destFile
]);
75 MockFilePicker
.filterIndex
= 0; // just save the file
76 info("done showCallback");
78 let publicDownloads
= await Downloads
.getList(Downloads
.PUBLIC
);
79 let downloadFinishedPromise
= new Promise(resolve
=> {
80 publicDownloads
.addView({
81 onDownloadChanged(download
) {
82 info("Download changed!");
83 if (download
.succeeded
|| download
.error
) {
84 info("Download succeeded or errored");
85 publicDownloads
.removeView(this);
86 publicDownloads
.removeFinished();
92 // open the context menu.
93 let popup
= document
.getElementById("contentAreaContextMenu");
94 let popupShown
= BrowserTestUtils
.waitForEvent(popup
, "popupshown");
95 BrowserTestUtils
.synthesizeMouseAtCenter(
97 { type
: "contextmenu", button
: 2 },
101 let popupHidden
= BrowserTestUtils
.waitForEvent(popup
, "popuphidden");
102 popup
.activateItem(popup
.querySelector("#context-saveimage"));
104 info("Context menu hidden, waiting for download to finish");
105 let imageDownload
= await downloadFinishedPromise
;
106 ok(imageDownload
.succeeded
, "Image should have downloaded successfully");