Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / tests / browser / browser_persist_image_accept.js
blob24675a4469b70df74a1c8ac5ab5dba67108f71e3
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 const TEST_PATH = getRootDirectory(gTestPath).replace(
7 "chrome://mochitests/content",
8 "https://example.org"
9 );
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;
22 while (!succeeded) {
23 try {
24 gTestDir.remove(true);
25 succeeded = true;
26 } catch (ex) {
27 await new Promise(requestAnimationFrame);
31 });
33 let gTestDir = null;
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);
43 return saveDir;
46 function expectedImageAcceptHeader() {
47 if (Services.prefs.prefHasUserValue("image.http.accept")) {
48 return Services.prefs.getCharPref("image.http.accept");
51 return (
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");
65 img.src = imgloc;
66 await new Promise(resolve => {
67 img.onload = resolve;
68 content.document.body.appendChild(img);
69 });
70 });
71 gTestDir = createTemporarySaveDirectory();
73 let destFile = gTestDir.clone();
75 MockFilePicker.displayDirectory = gTestDir;
76 let fileName;
77 MockFilePicker.showCallback = function (fp) {
78 info("showCallback");
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();
96 resolve(download);
99 });
101 let httpOnModifyPromise = TestUtils.topicObserved(
102 "http-on-modify-request",
103 s => {
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);
108 return false;
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"
117 return true;
120 // open the context menu.
121 let popup = document.getElementById("contentAreaContextMenu");
122 let popupShown = BrowserTestUtils.waitForEvent(popup, "popupshown");
123 BrowserTestUtils.synthesizeMouseAtCenter(
124 "img",
125 { type: "contextmenu", button: 2 },
126 browser
128 await popupShown;
129 let popupHidden = BrowserTestUtils.waitForEvent(popup, "popuphidden");
130 popup.activateItem(popup.querySelector("#context-saveimage"));
131 await popupHidden;
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;