diff --git a/browser/components/privatebrowsing/test/browser/Makefile.in b/browser/components/privatebrowsing/test/browser/Makefile.in index 1596963ed6b..b1cc747eb5b 100644 --- a/browser/components/privatebrowsing/test/browser/Makefile.in +++ b/browser/components/privatebrowsing/test/browser/Makefile.in @@ -22,6 +22,7 @@ MOCHITEST_BROWSER_FILES = \ browser_privatebrowsing_crh.js \ browser_privatebrowsing_downloadLastDir.js \ browser_privatebrowsing_downloadLastDir_c.js \ + browser_privatebrowsing_downloadLastDir_toggle.js \ browser_privatebrowsing_fastswitch.js \ browser_privatebrowsing_findbar.js \ browser_privatebrowsing_forgetthissite.js \ diff --git a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_toggle.js b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_toggle.js new file mode 100644 index 00000000000..4e6a2b5ae0f --- /dev/null +++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_toggle.js @@ -0,0 +1,73 @@ +/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */ + +Cu.import("resource://gre/modules/DownloadLastDir.jsm"); +Cu.import("resource://gre/modules/FileUtils.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); + +let pb = Cc["@mozilla.org/privatebrowsing;1"]. + getService(Ci.nsIPrivateBrowsingService); + +function test() { + function clearHistory() { + // simulate clearing the private data + Services.obs.notifyObservers(null, "browser:purge-session-history", ""); + } + + is(typeof gDownloadLastDir, "object", "gDownloadLastDir should be a valid object"); + is(gDownloadLastDir.file, null, "gDownloadLastDir.file should be null to start with"); + let tmpDir = FileUtils.getDir("TmpD", [], true); + let newDir = tmpDir.clone(); + + registerCleanupFunction(function () { + Services.prefs.clearUserPref("browser.privatebrowsing.keep_current_session"); + Services.prefs.clearUserPref("browser.download.lastDir"); + newDir.remove(true); + gDownloadLastDir.cleanupPrivateFile(); + delete FileUtils; + delete gDownloadLastDir; + }); + + newDir.append("testdir"); + newDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0700); + + gDownloadLastDir.file = tmpDir; + is(gDownloadLastDir.file.path, tmpDir.path, "LastDir should point to the temporary directory"); + isnot(gDownloadLastDir.file, tmpDir, "gDownloadLastDir.file should not be pointing to the tmpDir"); + + gDownloadLastDir.file = 1; // not an nsIFile + is(gDownloadLastDir.file, null, "gDownloadLastDir.file should be null"); + gDownloadLastDir.file = tmpDir; + + clearHistory(); + is(gDownloadLastDir.file, null, "gDownloadLastDir.file should be null"); + gDownloadLastDir.file = tmpDir; + + Services.prefs.setBoolPref("browser.privatebrowsing.keep_current_session", true); + + pb.privateBrowsingEnabled = true; + is(gDownloadLastDir.file.path, tmpDir.path, "LastDir should point to the temporary directory"); + isnot(gDownloadLastDir.file, tmpDir, "gDownloadLastDir.file should not be pointing to the tmpDir"); + + pb.privateBrowsingEnabled = false; + is(gDownloadLastDir.file.path, tmpDir.path, "LastDir should point to the tmpDir"); + pb.privateBrowsingEnabled = true; + + gDownloadLastDir.file = newDir; + is(gDownloadLastDir.file.path, newDir.path, "gDownloadLastDir should be modified in PB mode"); + isnot(gDownloadLastDir.file, newDir, "gDownloadLastDir should not point to the newDir"); + + pb.privateBrowsingEnabled = false; + is(gDownloadLastDir.file.path, tmpDir.path, "gDownloadLastDir should point to the earlier directory outside PB mode"); + isnot(gDownloadLastDir.file, tmpDir, "gDownloadLastDir should not be modifief outside PB mode"); + + pb.privateBrowsingEnabled = true; + isnot(gDownloadLastDir.file, null, "gDownloadLastDir should not be null inside PB mode"); + clearHistory(); + is(gDownloadLastDir.file, null, "gDownloadLastDir should be null after clearing history"); + + pb.privateBrowsingEnabled = false; + is(gDownloadLastDir.file, null, "gDownloadLastDir should be null outside PB mode"); +} diff --git a/toolkit/mozapps/downloads/tests/unit/test_DownloadLastDir.js b/toolkit/mozapps/downloads/tests/unit/test_DownloadLastDir.js deleted file mode 100644 index 771ecd23d5f..00000000000 --- a/toolkit/mozapps/downloads/tests/unit/test_DownloadLastDir.js +++ /dev/null @@ -1,82 +0,0 @@ -/* 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 run_test() -{ - let Cc = Components.classes; - let Ci = Components.interfaces; - let Cu = Components.utils; - do_get_profile(); - Cu.import("resource://gre/modules/DownloadLastDir.jsm"); - - function clearHistory() { - // simulate clearing the private data - Cc["@mozilla.org/observer-service;1"]. - getService(Ci.nsIObserverService). - notifyObservers(null, "browser:purge-session-history", ""); - } - - do_check_eq(typeof gDownloadLastDir, "object"); - do_check_eq(gDownloadLastDir.file, null); - - let dirSvc = Cc["@mozilla.org/file/directory_service;1"]. - getService(Ci.nsIProperties); - let tmpDir = dirSvc.get("TmpD", Ci.nsILocalFile); - let newDir = tmpDir.clone(); - newDir.append("testdir" + Math.floor(Math.random() * 10000)); - newDir.QueryInterface(Ci.nsILocalFile); - newDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0700); - - gDownloadLastDir.file = tmpDir; - do_check_eq(gDownloadLastDir.file.path, tmpDir.path); - do_check_neq(gDownloadLastDir.file, tmpDir); - - gDownloadLastDir.file = 1; // not an nsIFile - do_check_eq(gDownloadLastDir.file, null); - gDownloadLastDir.file = tmpDir; - - clearHistory(); - do_check_eq(gDownloadLastDir.file, null); - gDownloadLastDir.file = tmpDir; - - let pb; - try { - pb = Cc["@mozilla.org/privatebrowsing;1"]. - getService(Ci.nsIPrivateBrowsingService); - } catch (e) { - print("PB service is not available, bail out"); - return; - } - - let prefs = Cc["@mozilla.org/preferences-service;1"]. - getService(Ci.nsIPrefBranch); - prefs.setBoolPref("browser.privatebrowsing.keep_current_session", true); - - pb.privateBrowsingEnabled = true; - do_check_eq(gDownloadLastDir.file.path, tmpDir.path); - do_check_neq(gDownloadLastDir.file, tmpDir); - - pb.privateBrowsingEnabled = false; - do_check_eq(gDownloadLastDir.file.path, tmpDir.path); - pb.privateBrowsingEnabled = true; - - gDownloadLastDir.file = newDir; - do_check_eq(gDownloadLastDir.file.path, newDir.path); - do_check_neq(gDownloadLastDir.file, newDir); - - pb.privateBrowsingEnabled = false; - do_check_eq(gDownloadLastDir.file.path, tmpDir.path); - do_check_neq(gDownloadLastDir.file, tmpDir); - - pb.privateBrowsingEnabled = true; - do_check_neq(gDownloadLastDir.file, null); - clearHistory(); - do_check_eq(gDownloadLastDir.file, null); - - pb.privateBrowsingEnabled = false; - do_check_eq(gDownloadLastDir.file, null); - - prefs.clearUserPref("browser.privatebrowsing.keep_current_session"); - newDir.remove(true); -} diff --git a/toolkit/mozapps/downloads/tests/unit/xpcshell.ini b/toolkit/mozapps/downloads/tests/unit/xpcshell.ini index 0052d2be3bd..3f7bdff27d9 100644 --- a/toolkit/mozapps/downloads/tests/unit/xpcshell.ini +++ b/toolkit/mozapps/downloads/tests/unit/xpcshell.ini @@ -2,7 +2,6 @@ head = head_downloads.js tail = -[test_DownloadLastDir.js] [test_DownloadLastDirWithCPS.js] [test_DownloadPaths.js] [test_DownloadUtils.js]