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 function expectedImageAcceptHeader() {
47 if (Services
.prefs
.prefHasUserValue("image.http.accept")) {
48 return Services
.prefs
.getCharPref("image.http.accept");
52 (Services
.prefs
.getBoolPref("image.avif.enabled") ? "image/avif," : "") +
53 (Services
.prefs
.getBoolPref("image.jxl.enabled") ? "image/jxl," : "") +
54 "image/webp,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"
58 add_task(async
function test_image_download() {
59 await BrowserTestUtils
.withNewTab(TEST_PATH
+ "dummy.html", async browser
=> {
60 // Add the image, and wait for it to load.
61 await SpecialPowers
.spawn(browser
, [], async
function () {
62 let loc
= content
.document
.location
.href
;
63 let imgloc
= new content
.URL("dummy.png", loc
);
64 let img
= content
.document
.createElement("img");
66 await
new Promise(resolve
=> {
68 content
.document
.body
.appendChild(img
);
71 gTestDir
= createTemporarySaveDirectory();
73 let destFile
= gTestDir
.clone();
75 MockFilePicker
.displayDirectory
= gTestDir
;
77 MockFilePicker
.showCallback = function (fp
) {
79 fileName
= fp
.defaultString
;
80 info("fileName: " + fileName
);
81 destFile
.append(fileName
);
82 info("path: " + destFile
.path
);
83 MockFilePicker
.setFiles([destFile
]);
84 MockFilePicker
.filterIndex
= 0; // just save the file
85 info("done showCallback");
87 let publicDownloads
= await Downloads
.getList(Downloads
.PUBLIC
);
88 let downloadFinishedPromise
= new Promise(resolve
=> {
89 publicDownloads
.addView({
90 onDownloadChanged(download
) {
91 info("Download changed!");
92 if (download
.succeeded
|| download
.error
) {
93 info("Download succeeded or errored");
94 publicDownloads
.removeView(this);
95 publicDownloads
.removeFinished();
101 let httpOnModifyPromise
= TestUtils
.topicObserved(
102 "http-on-modify-request",
104 let channel
= s
.QueryInterface(Ci
.nsIChannel
);
105 let uri
= channel
.URI
&& channel
.URI
.spec
;
106 if (!uri
.endsWith("dummy.png")) {
107 info("Ignoring request for " + uri
);
110 ok(channel
instanceof Ci
.nsIHttpChannel
, "Should be HTTP channel");
111 channel
.QueryInterface(Ci
.nsIHttpChannel
);
113 channel
.getRequestHeader("Accept"),
114 expectedImageAcceptHeader(),
115 "Header should be image header"
120 // open the context menu.
121 let popup
= document
.getElementById("contentAreaContextMenu");
122 let popupShown
= BrowserTestUtils
.waitForEvent(popup
, "popupshown");
123 BrowserTestUtils
.synthesizeMouseAtCenter(
125 { type
: "contextmenu", button
: 2 },
129 let popupHidden
= BrowserTestUtils
.waitForEvent(popup
, "popuphidden");
130 popup
.activateItem(popup
.querySelector("#context-saveimage"));
132 info("Context menu hidden, waiting for download to finish");
133 let imageDownload
= await downloadFinishedPromise
;
134 ok(imageDownload
.succeeded
, "Image should have downloaded successfully");
135 info("Waiting for http request to complete.");
136 // Ensure we got the http request:
137 await httpOnModifyPromise
;