Bug 722995 - Part 3: Convert test_DownloadLastDir.js to mochitest, r=ehsan

This commit is contained in:
Saurabh Anand 2012-07-21 11:22:30 +05:30
parent 9853a7a9a1
commit 6fc95fb768
4 changed files with 74 additions and 83 deletions

View File

@ -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 \

View File

@ -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");
}

View File

@ -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);
}

View File

@ -2,7 +2,6 @@
head = head_downloads.js
tail =
[test_DownloadLastDir.js]
[test_DownloadLastDirWithCPS.js]
[test_DownloadPaths.js]
[test_DownloadUtils.js]