PageRenderTime 16ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/toolkit/content/tests/unit/test_privatebrowsing_downloadLastDir_c.js

http://github.com/zpao/v8monkey
JavaScript | 163 lines | 96 code | 16 blank | 51 comment | 0 complexity | cea062d34b9a67097dc381b183da1008 MD5 | raw file
  1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2/* ***** BEGIN LICENSE BLOCK *****
  3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4 *
  5 * The contents of this file are subject to the Mozilla Public License Version
  6 * 1.1 (the "License"); you may not use this file except in compliance with
  7 * the License. You may obtain a copy of the License at
  8 * http://www.mozilla.org/MPL/
  9 *
 10 * Software distributed under the License is distributed on an "AS IS" basis,
 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12 * for the specific language governing rights and limitations under the
 13 * License.
 14 *
 15 * The Original Code is bug 464795 unit test.
 16 *
 17 * The Initial Developer of the Original Code is
 18 * Ehsan Akhgari.
 19 * Portions created by the Initial Developer are Copyright (C) 2009
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *  Ehsan Akhgari <ehsan.akhgari@gmail.com>
 24 *
 25 * Alternatively, the contents of this file may be used under the terms of
 26 * either the GNU General Public License Version 2 or later (the "GPL"), or
 27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 28 * in which case the provisions of the GPL or the LGPL are applicable instead
 29 * of those above. If you wish to allow use of your version of this file only
 30 * under the terms of either the GPL or the LGPL, and not to allow others to
 31 * use your version of this file under the terms of the MPL, indicate your
 32 * decision by deleting the provisions above and replace them with the notice
 33 * and other provisions required by the GPL or the LGPL. If you do not delete
 34 * the provisions above, a recipient may use your version of this file under
 35 * the terms of any one of the MPL, the GPL or the LGPL.
 36 *
 37 * ***** END LICENSE BLOCK ***** */
 38
 39const Ci = Components.interfaces;
 40const Cc = Components.classes;
 41
 42function loadUtilsScript() {
 43  let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
 44               getService(Ci.mozIJSSubScriptLoader);
 45  loader.loadSubScript("chrome://global/content/contentAreaUtils.js");
 46  Components.utils.import("resource://gre/modules/DownloadLastDir.jsm");
 47}
 48
 49do_get_profile();
 50
 51let window = {};
 52function run_test()
 53{
 54  let pb;
 55  try {
 56    pb = Cc["@mozilla.org/privatebrowsing;1"].
 57         getService(Ci.nsIPrivateBrowsingService);
 58  } catch (e) {
 59    print("PB service is not available, bail out");
 60    return;
 61  }
 62
 63  loadUtilsScript();
 64
 65  let prefsService = Cc["@mozilla.org/preferences-service;1"].
 66                     getService(Ci.nsIPrefService).
 67                     QueryInterface(Ci.nsIPrefBranch);
 68  prefsService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
 69  let prefs = prefsService.getBranch("browser.download.");
 70  let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
 71               getService(Ci.nsIProperties);
 72  let tmpDir = dirSvc.get("TmpD", Ci.nsILocalFile);
 73  function newDirectory() {
 74    let dir = tmpDir.clone();
 75    dir.append("testdir" + Math.floor(Math.random() * 10000));
 76    dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0700);
 77    return dir;
 78  }
 79  function newFileInDirectory(dir) {
 80    let file = dir.clone();
 81    file.append("testfile" + Math.floor(Math.random() * 10000));
 82    file.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0600);
 83    return file;
 84  }
 85  let dir1 = newDirectory();
 86  let dir2 = newDirectory();
 87  let dir3 = newDirectory();
 88  let file1 = newFileInDirectory(dir1);
 89  let file2 = newFileInDirectory(dir2);
 90  let file3 = newFileInDirectory(dir3);
 91
 92  // overwrite makeFilePicker, as we don't want to create a real filepicker object
 93  let fp = {
 94    appendFilter: function() {},
 95    appendFilters: function() {},
 96    init: function() {},
 97    show: function() Ci.nsIFilePicker.returnOK,
 98    displayDirectory: null,
 99    file: file1
100  };
101  makeFilePicker = function() fp;
102
103  // Overwrite stringBundle to return an object masquerading as a string bundle
104  delete ContentAreaUtils.stringBundle;
105  ContentAreaUtils.stringBundle = {
106    GetStringFromName: function() ""
107  };
108
109  // Overwrite validateFileName to validate everything
110  validateFileName = function(foo) foo;
111
112  let params = {
113    fpTitleKey: "test",
114    fileInfo: new FileInfo("test.txt", "test.txt", "test", "txt", "http://mozilla.org/test.txt"),
115    contentType: "text/plain",
116    saveMode: SAVEMODE_FILEONLY,
117    saveAsType: kSaveAsType_Complete,
118    file: null
119  };
120
121  prefs.setComplexValue("lastDir", Ci.nsILocalFile, tmpDir);
122
123  do_check_true(getTargetFile(params));
124  // file picker should start with browser.download.lastDir
125  do_check_eq(fp.displayDirectory.path, tmpDir.path);
126  // browser.download.lastDir should be modified before entering the private browsing mode
127  do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
128  // gDownloadLastDir should be usable outside of the private browsing mode
129  do_check_eq(gDownloadLastDir.file.path, dir1.path);
130
131  pb.privateBrowsingEnabled = true;
132  do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
133  fp.file = file2;
134  fp.displayDirectory = null;
135  do_check_true(getTargetFile(params));
136  // file picker should start with browser.download.lastDir as set before entering the private browsing mode
137  do_check_eq(fp.displayDirectory.path, dir1.path);
138  // browser.download.lastDir should not be modified inside the private browsing mode
139  do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
140  // but gDownloadLastDir should be modified
141  do_check_eq(gDownloadLastDir.file.path, dir2.path);
142
143  pb.privateBrowsingEnabled = false;
144  // gDownloadLastDir should be cleared after leaving the private browsing mode
145  do_check_eq(gDownloadLastDir.file.path, dir1.path);
146  fp.file = file3;
147  fp.displayDirectory = null;
148  do_check_true(getTargetFile(params));
149  // file picker should start with browser.download.lastDir as set before entering the private browsing mode
150  do_check_eq(fp.displayDirectory.path, dir1.path);
151  // browser.download.lastDir should be modified after leaving the private browsing mode
152  do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir3.path);
153  // gDownloadLastDir should be usable after leaving the private browsing mode
154  do_check_eq(gDownloadLastDir.file.path, dir3.path);
155
156  // cleanup
157  Cc["@mozilla.org/observer-service;1"]
158    .getService(Ci.nsIObserverService)
159    .notifyObservers(null, "quit-application", null);
160
161  prefsService.clearUserPref("browser.privatebrowsing.keep_current_session");
162  [dir1, dir2, dir3].forEach(function(dir) dir.remove(true));
163}