Bug 722995 - Part 1: Convert test_privatebrowsing_downloadLastDir.js to mochitest, r=ehsan

This commit is contained in:
Saurabh Anand 2012-07-21 11:20:24 +05:30
parent 0f8656ed35
commit 660e719f39
3 changed files with 40 additions and 57 deletions

View File

@ -20,6 +20,7 @@ MOCHITEST_BROWSER_FILES = \
browser_privatebrowsing_concurrent.js \
browser_privatebrowsing_concurrent_page.html \
browser_privatebrowsing_crh.js \
browser_privatebrowsing_downloadLastDir.js \
browser_privatebrowsing_fastswitch.js \
browser_privatebrowsing_findbar.js \
browser_privatebrowsing_forgetthissite.js \

View File

@ -1,65 +1,42 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* -*- 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/. */
const Ci = Components.interfaces;
const Cc = Components.classes;
const Cu = Components.utils;
const Cr = Components.results;
do_get_profile();
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/DownloadLastDir.jsm");
let context = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor]),
getInterface: XPCOMUtils.generateQI([Ci.nsIDOMWindow])
};
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://mochikit/MockFilePicker.jsm");
let launcher = {
source: Services.io.newURI("http://test1.com/file", null, null)
};
let pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
Cu.import("resource://test/MockFilePicker.jsm");
MockFilePicker.init();
MockFilePicker.returnValue = Ci.nsIFilePicker.returnOK;
function run_test()
{
let pb;
try {
pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
} catch (e) {
print("PB service is not available, bail out");
return;
}
let prefsService = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefService).
QueryInterface(Ci.nsIPrefBranch);
prefsService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
let prefs = prefsService.getBranch("browser.download.");
function test() {
Services.prefs.setBoolPref("browser.privatebrowsing.keep_current_session", true);
let prefs = Services.prefs.getBranch("browser.download.");
let launcherDialog = Cc["@mozilla.org/helperapplauncherdialog;1"].
getService(Ci.nsIHelperAppLauncherDialog);
let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
let tmpDir = dirSvc.get("TmpD", Ci.nsILocalFile);
let tmpDir = FileUtils.getDir("TmpD", [], true);
function newDirectory() {
let dir = tmpDir.clone();
dir.append("testdir" + Math.floor(Math.random() * 10000));
dir.append("testdir");
dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0700);
return dir;
}
function newFileInDirectory(dir) {
let file = dir.clone();
file.append("testfile" + Math.floor(Math.random() * 10000));
file.append("testfile");
file.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0600);
return file;
}
let dir1 = newDirectory();
let dir2 = newDirectory();
let dir3 = newDirectory();
@ -67,48 +44,54 @@ function run_test()
let file2 = newFileInDirectory(dir2);
let file3 = newFileInDirectory(dir3);
prefs.setComplexValue("lastDir", Ci.nsILocalFile, tmpDir);
// cleanup functions registration
registerCleanupFunction(function () {
Services.prefs.clearUserPref("browser.privatebrowsing.keep_current_session");
Services.prefs.clearUserPref("browser.download.lastDir");
[dir1, dir2, dir3].forEach(function(dir) dir.remove(true));
MockFilePicker.cleanup();
gDownloadLastDir.cleanupPrivateFile();
delete FileUtils;
delete gDownloadLastDir;
});
let context = gBrowser.selectedBrowser.contentWindow;
prefs.setComplexValue("lastDir", Ci.nsIFile, tmpDir);
MockFilePicker.returnFiles = [file1];
let file = launcherDialog.promptForSaveToFile(launcher, context, null, null, null);
do_check_true(!!file);
ok(!!file, "promptForSaveToFile correctly returned a file");
// file picker should start with browser.download.lastDir
do_check_eq(MockFilePicker.displayDirectory.path, tmpDir.path);
is(MockFilePicker.displayDirectory.path, tmpDir.path, "File picker should start with browser.download.lastDir");
// browser.download.lastDir should be modified before entering the private browsing mode
do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
is(prefs.getComplexValue("lastDir", Ci.nsIFile).path, dir1.path, "LastDir should be modified before entering the PB mode");
// gDownloadLastDir should be usable outside of the private browsing mode
do_check_eq(gDownloadLastDir.file.path, dir1.path);
is(gDownloadLastDir.file.path, dir1.path, "gDownloadLastDir should be usable outside of the PB mode");
pb.privateBrowsingEnabled = true;
do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
is(prefs.getComplexValue("lastDir", Ci.nsIFile).path, dir1.path, "LastDir should be that set before entering PB mode");
MockFilePicker.returnFiles = [file2];
MockFilePicker.displayDirectory = null;
file = launcherDialog.promptForSaveToFile(launcher, context, null, null, null);
do_check_true(!!file);
ok(!!file, "promptForSaveToFile correctly returned a file");
// file picker should start with browser.download.lastDir as set before entering the private browsing mode
do_check_eq(MockFilePicker.displayDirectory.path, dir1.path);
is(MockFilePicker.displayDirectory.path, dir1.path, "Start with LastDir as set before entering the PB mode");
// browser.download.lastDir should not be modified inside the private browsing mode
do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
is(prefs.getComplexValue("lastDir", Ci.nsIFile).path, dir1.path, "LastDir should not be modified inside the PB mode");
// but gDownloadLastDir should be modified
do_check_eq(gDownloadLastDir.file.path, dir2.path);
is(gDownloadLastDir.file.path, dir2.path, "gDownloadLastDir should be modified inside PB mode");
pb.privateBrowsingEnabled = false;
// gDownloadLastDir should be cleared after leaving the private browsing mode
do_check_eq(gDownloadLastDir.file.path, dir1.path);
is(gDownloadLastDir.file.path, dir1.path, "gDownloadLastDir should be cleared after leaving the PB mode");
MockFilePicker.returnFiles = [file3];
MockFilePicker.displayDirectory = null;
file = launcherDialog.promptForSaveToFile(launcher, context, null, null, null);
do_check_true(!!file);
ok(!!file, "promptForSaveToFile correctly returned a file");
// file picker should start with browser.download.lastDir as set before entering the private browsing mode
do_check_eq(MockFilePicker.displayDirectory.path, dir1.path);
is(MockFilePicker.displayDirectory.path, dir1.path, "Start with LastDir as set before entering the PB mode");
// browser.download.lastDir should be modified after leaving the private browsing mode
do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir3.path);
is(prefs.getComplexValue("lastDir", Ci.nsIFile).path, dir3.path, "LastDir should be modified after leaving the PB mode");
// gDownloadLastDir should be usable after leaving the private browsing mode
do_check_eq(gDownloadLastDir.file.path, dir3.path);
// cleanup
prefsService.clearUserPref("browser.privatebrowsing.keep_current_session");
[dir1, dir2, dir3].forEach(function(dir) dir.remove(true));
MockFilePicker.cleanup();
is(gDownloadLastDir.file.path, dir3.path, "gDownloadLastDir should be usable after leaving the PB mode");
}

View File

@ -7,6 +7,5 @@ tail =
[test_DownloadPaths.js]
[test_DownloadUtils.js]
[test_lowMinutes.js]
[test_privatebrowsing_downloadLastDir.js]
[test_syncedDownloadUtils.js]
[test_unspecified_arguments.js]