diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_c.js | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_c.js | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_c.js b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_c.js new file mode 100644 index 0000000000..6b1253aa67 --- /dev/null +++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_c.js @@ -0,0 +1,146 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* 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 test() { + waitForExplicitFinish(); + + let { FileUtils } = ChromeUtils.importESModule( + "resource://gre/modules/FileUtils.sys.mjs" + ); + let { DownloadLastDir } = ChromeUtils.importESModule( + "resource://gre/modules/DownloadLastDir.sys.mjs" + ); + let MockFilePicker = SpecialPowers.MockFilePicker; + + MockFilePicker.init(window); + MockFilePicker.returnValue = Ci.nsIFilePicker.returnOK; + + let validateFileNameToRestore = validateFileName; + let prefs = Services.prefs.getBranch("browser.download."); + let tmpDir = FileUtils.getDir("TmpD", [], true); + let dir1 = newDirectory(); + let dir2 = newDirectory(); + let dir3 = newDirectory(); + let file1 = newFileInDirectory(dir1); + let file2 = newFileInDirectory(dir2); + let file3 = newFileInDirectory(dir3); + + // cleanup function registration + registerCleanupFunction(function () { + Services.prefs.clearUserPref("browser.download.lastDir"); + [dir1, dir2, dir3].forEach(dir => dir.remove(true)); + MockFilePicker.cleanup(); + validateFileName = validateFileNameToRestore; + }); + + // Overwrite validateFileName to validate everything + validateFileName = foo => foo; + + let params = { + fileInfo: new FileInfo( + "test.txt", + "test.txt", + "test", + "txt", + "http://mozilla.org/test.txt" + ), + contentType: "text/plain", + saveMode: SAVEMODE_FILEONLY, + saveAsType: kSaveAsType_Complete, + file: null, + }; + + prefs.setComplexValue("lastDir", Ci.nsIFile, tmpDir); + + function testOnWindow(aPrivate, aCallback) { + whenNewWindowLoaded({ private: aPrivate }, function (win) { + let gDownloadLastDir = new DownloadLastDir(win); + aCallback(win, gDownloadLastDir); + }); + } + + function testDownloadDir( + aWin, + gDownloadLastDir, + aFile, + aDisplayDir, + aLastDir, + aGlobalLastDir, + aCallback + ) { + // Check lastDir preference. + is( + prefs.getComplexValue("lastDir", Ci.nsIFile).path, + aDisplayDir.path, + "LastDir should be the expected display dir" + ); + // Check gDownloadLastDir value. + is( + gDownloadLastDir.file.path, + aDisplayDir.path, + "gDownloadLastDir should be the expected display dir" + ); + + MockFilePicker.setFiles([aFile]); + MockFilePicker.displayDirectory = null; + aWin + .promiseTargetFile(params) + .then(function () { + // File picker should start with expected display dir. + is( + MockFilePicker.displayDirectory.path, + aDisplayDir.path, + "File picker should start with browser.download.lastDir" + ); + // browser.download.lastDir should be modified on not private windows + is( + prefs.getComplexValue("lastDir", Ci.nsIFile).path, + aLastDir.path, + "LastDir should be the expected last dir" + ); + // gDownloadLastDir should be usable outside of private windows + is( + gDownloadLastDir.file.path, + aGlobalLastDir.path, + "gDownloadLastDir should be the expected global last dir" + ); + + gDownloadLastDir.cleanupPrivateFile(); + aWin.close(); + aCallback(); + }) + .catch(function () { + ok(false); + }); + } + + testOnWindow(false, function (win, downloadDir) { + testDownloadDir(win, downloadDir, file1, tmpDir, dir1, dir1, function () { + testOnWindow(true, function (win1, downloadDir1) { + testDownloadDir( + win1, + downloadDir1, + file2, + dir1, + dir1, + dir2, + function () { + testOnWindow(false, function (win2, downloadDir2) { + testDownloadDir( + win2, + downloadDir2, + file3, + dir1, + dir3, + dir3, + finish + ); + }); + } + ); + }); + }); + }); +} |