[tbb-commits] [tor-browser/tor-browser-60.4.0esr-8.5-1] Bug 1504159 - add test to verify we can save a mixed content image from the context menu, r=jkt
gk at torproject.org
gk at torproject.org
Wed Jan 23 08:04:19 UTC 2019
commit 77b26b69926f7bb93181320084ca8cb9958b3a95
Author: Gijs Kruitbosch <gijskruitbosch at gmail.com>
Date: Fri Nov 9 10:59:52 2018 +0000
Bug 1504159 - add test to verify we can save a mixed content image from the context menu, r=jkt
Depends on D11411
Differential Revision: https://phabricator.services.mozilla.com/D11412
--HG--
rename : browser/themes/windows/preferences/saveFile.png => dom/tests/browser/dummy.png
extra : moz-landing-system : lando
---
dom/tests/browser/browser.ini | 4 +
.../browser/browser_persist_mixed_content_image.js | 102 +++++++++++++++++++++
dom/tests/browser/dummy.png | Bin 0 -> 703 bytes
dom/tests/browser/test_mixed_content_image.html | 1 +
4 files changed, 107 insertions(+)
diff --git a/dom/tests/browser/browser.ini b/dom/tests/browser/browser.ini
index 11cf9ebf2346..24d4d5f4fc23 100644
--- a/dom/tests/browser/browser.ini
+++ b/dom/tests/browser/browser.ini
@@ -59,6 +59,10 @@ skip-if = !e10s # This is a test of e10s functionality.
support-files =
set-samesite-cookies-and-redirect.sjs
mimeme.sjs
+[browser_persist_mixed_content_image.js]
+support-files =
+ test_mixed_content_image.html
+ dummy.png
[browser_test_focus_after_modal_state.js]
support-files =
focus_after_prompt.html
diff --git a/dom/tests/browser/browser_persist_mixed_content_image.js b/dom/tests/browser/browser_persist_mixed_content_image.js
new file mode 100644
index 000000000000..f7af5f1790cb
--- /dev/null
+++ b/dom/tests/browser/browser_persist_mixed_content_image.js
@@ -0,0 +1,102 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const TEST_PATH = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "https://example.org");
+
+var MockFilePicker = SpecialPowers.MockFilePicker;
+MockFilePicker.init(window);
+
+registerCleanupFunction(async function() {
+ info("Running the cleanup code");
+ MockFilePicker.cleanup();
+ if (gTestDir && gTestDir.exists()) {
+ // On Windows, sometimes nsIFile.remove() throws, probably because we're
+ // still writing to the directory we're trying to remove, despite
+ // waiting for the download to complete. Just retry a bit later...
+ let succeeded = false;
+ while (!succeeded) {
+ try {
+ gTestDir.remove(true);
+ succeeded = true;
+ } catch (ex) {
+ await new Promise(requestAnimationFrame);
+ }
+ }
+ }
+});
+
+let gTestDir = null;
+
+function createTemporarySaveDirectory() {
+ var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
+ saveDir.append("testsavedir");
+ if (!saveDir.exists()) {
+ info("create testsavedir!");
+ saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
+ }
+ info("return from createTempSaveDir: " + saveDir.path);
+ return saveDir;
+}
+
+
+add_task(async function test_image_download() {
+ await BrowserTestUtils.withNewTab(TEST_PATH + "test_mixed_content_image.html", async (browser) => {
+ // Add the image, and wait for it to load.
+ await ContentTask.spawn(browser, null, function() {
+ let loc = content.document.location.href;
+ let httpRoot = loc.replace("https", "http");
+ let imgloc = new content.URL("dummy.png", httpRoot);
+ let img = content.document.createElement("img");
+ img.src = imgloc;
+ return new Promise(resolve => {
+ img.onload = resolve;
+ content.document.body.appendChild(img);
+ });
+ });
+ gTestDir = createTemporarySaveDirectory();
+
+ let destFile = gTestDir.clone();
+
+ MockFilePicker.displayDirectory = gTestDir;
+ let fileName;
+ MockFilePicker.showCallback = function(fp) {
+ info("showCallback");
+ fileName = fp.defaultString;
+ info("fileName: " + fileName);
+ destFile.append(fileName);
+ info("path: " + destFile.path);
+ MockFilePicker.setFiles([destFile]);
+ MockFilePicker.filterIndex = 0; // just save the file
+ info("done showCallback");
+ };
+ let downloadFinishedPromise = new Promise(async (resolve) => {
+ let dls = await Downloads.getList(Downloads.PUBLIC);
+ dls.addView({
+ onDownloadChanged(download) {
+ info("Download changed!");
+ if (download.succeeded || download.error) {
+ info("Download succeeded or errored");
+ dls.removeView(this);
+ dls.removeFinished();
+ resolve(download);
+ }
+ }
+ });
+ });
+ // open the context menu.
+ let popup = document.getElementById("contentAreaContextMenu");
+ let popupShown = BrowserTestUtils.waitForEvent(popup, "popupshown");
+ BrowserTestUtils.synthesizeMouseAtCenter("img", {type: "contextmenu", button: 2}, browser);
+ await popupShown;
+ let popupHidden = BrowserTestUtils.waitForEvent(popup, "popuphidden");
+ popup.querySelector("#context-saveimage").click();
+ popup.hidePopup();
+ await popupHidden;
+ info("Context menu hidden, waiting for download to finish");
+ let imageDownload = await downloadFinishedPromise;
+ ok(imageDownload.succeeded, "Image should have downloaded successfully");
+ });
+
+});
diff --git a/dom/tests/browser/dummy.png b/dom/tests/browser/dummy.png
new file mode 100644
index 000000000000..a1089af09b9c
Binary files /dev/null and b/dom/tests/browser/dummy.png differ
diff --git a/dom/tests/browser/test_mixed_content_image.html b/dom/tests/browser/test_mixed_content_image.html
new file mode 100644
index 000000000000..c8b7661f4221
--- /dev/null
+++ b/dom/tests/browser/test_mixed_content_image.html
@@ -0,0 +1 @@
+<body></body>
More information about the tbb-commits
mailing list