diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /browser/base/content/test/general/browser_save_private_link_perwindowpb.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | browser/base/content/test/general/browser_save_private_link_perwindowpb.js | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/browser/base/content/test/general/browser_save_private_link_perwindowpb.js b/browser/base/content/test/general/browser_save_private_link_perwindowpb.js new file mode 100644 index 0000000000..8ede97e640 --- /dev/null +++ b/browser/base/content/test/general/browser_save_private_link_perwindowpb.js @@ -0,0 +1,127 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +function createTemporarySaveDirectory() { + var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + saveDir.append("testsavedir"); + if (!saveDir.exists()) { + saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755); + } + return saveDir; +} + +function promiseNoCacheEntry(filename) { + return new Promise((resolve, reject) => { + Visitor.prototype = { + onCacheStorageInfo(num, consumption) { + info("disk storage contains " + num + " entries"); + }, + onCacheEntryInfo(uri) { + let urispec = uri.asciiSpec; + info(urispec); + is( + urispec.includes(filename), + false, + "web content present in disk cache" + ); + }, + onCacheEntryVisitCompleted() { + resolve(); + }, + }; + function Visitor() {} + + let storage = Services.cache2.diskCacheStorage( + Services.loadContextInfo.default + ); + storage.asyncVisitStorage(new Visitor(), true /* Do walk entries */); + }); +} + +function promiseImageDownloaded() { + return new Promise((resolve, reject) => { + let fileName; + let MockFilePicker = SpecialPowers.MockFilePicker; + MockFilePicker.init(window); + + function onTransferComplete(downloadSuccess) { + ok( + downloadSuccess, + "Image file should have been downloaded successfully " + fileName + ); + + // Give the request a chance to finish and create a cache entry + resolve(fileName); + } + + // Create the folder the image will be saved into. + var destDir = createTemporarySaveDirectory(); + var destFile = destDir.clone(); + + MockFilePicker.displayDirectory = destDir; + MockFilePicker.showCallback = function (fp) { + fileName = fp.defaultString; + destFile.append(fileName); + MockFilePicker.setFiles([destFile]); + MockFilePicker.filterIndex = 1; // kSaveAsType_URL + }; + + mockTransferCallback = onTransferComplete; + mockTransferRegisterer.register(); + + registerCleanupFunction(function () { + mockTransferCallback = null; + mockTransferRegisterer.unregister(); + MockFilePicker.cleanup(); + destDir.remove(true); + }); + }); +} + +add_task(async function () { + let testURI = + "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.html"; + let privateWindow = await BrowserTestUtils.openNewBrowserWindow({ + private: true, + }); + let tab = await BrowserTestUtils.openNewForegroundTab( + privateWindow.gBrowser, + testURI + ); + + let contextMenu = privateWindow.document.getElementById( + "contentAreaContextMenu" + ); + let popupShown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown"); + let popupHidden = BrowserTestUtils.waitForEvent(contextMenu, "popuphidden"); + await BrowserTestUtils.synthesizeMouseAtCenter( + "#img", + { + type: "contextmenu", + button: 2, + }, + tab.linkedBrowser + ); + await popupShown; + + Services.cache2.clear(); + + let imageDownloaded = promiseImageDownloaded(); + // Select "Save Image As" option from context menu + privateWindow.document.getElementById("context-saveimage").doCommand(); + + contextMenu.hidePopup(); + await popupHidden; + + // wait for image download + let fileName = await imageDownloaded; + await promiseNoCacheEntry(fileName); + + await BrowserTestUtils.closeWindow(privateWindow); +}); + +Services.scriptloader.loadSubScript( + "chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", + this +); |