/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. const Ci = Components.interfaces;
  39. const Cc = Components.classes;
  40. function loadUtilsScript() {
  41. let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
  42. getService(Ci.mozIJSSubScriptLoader);
  43. loader.loadSubScript("chrome://global/content/contentAreaUtils.js");
  44. Components.utils.import("resource://gre/modules/DownloadLastDir.jsm");
  45. }
  46. do_get_profile();
  47. let window = {};
  48. function run_test()
  49. {
  50. let pb;
  51. try {
  52. pb = Cc["@mozilla.org/privatebrowsing;1"].
  53. getService(Ci.nsIPrivateBrowsingService);
  54. } catch (e) {
  55. print("PB service is not available, bail out");
  56. return;
  57. }
  58. loadUtilsScript();
  59. let prefsService = Cc["@mozilla.org/preferences-service;1"].
  60. getService(Ci.nsIPrefService).
  61. QueryInterface(Ci.nsIPrefBranch);
  62. prefsService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
  63. let prefs = prefsService.getBranch("browser.download.");
  64. let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
  65. getService(Ci.nsIProperties);
  66. let tmpDir = dirSvc.get("TmpD", Ci.nsILocalFile);
  67. function newDirectory() {
  68. let dir = tmpDir.clone();
  69. dir.append("testdir" + Math.floor(Math.random() * 10000));
  70. dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0700);
  71. return dir;
  72. }
  73. function newFileInDirectory(dir) {
  74. let file = dir.clone();
  75. file.append("testfile" + Math.floor(Math.random() * 10000));
  76. file.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0600);
  77. return file;
  78. }
  79. let dir1 = newDirectory();
  80. let dir2 = newDirectory();
  81. let dir3 = newDirectory();
  82. let file1 = newFileInDirectory(dir1);
  83. let file2 = newFileInDirectory(dir2);
  84. let file3 = newFileInDirectory(dir3);
  85. // overwrite makeFilePicker, as we don't want to create a real filepicker object
  86. let fp = {
  87. appendFilter: function() {},
  88. appendFilters: function() {},
  89. init: function() {},
  90. show: function() Ci.nsIFilePicker.returnOK,
  91. displayDirectory: null,
  92. file: file1
  93. };
  94. makeFilePicker = function() fp;
  95. // Overwrite stringBundle to return an object masquerading as a string bundle
  96. delete ContentAreaUtils.stringBundle;
  97. ContentAreaUtils.stringBundle = {
  98. GetStringFromName: function() ""
  99. };
  100. // Overwrite validateFileName to validate everything
  101. validateFileName = function(foo) foo;
  102. let params = {
  103. fpTitleKey: "test",
  104. fileInfo: new FileInfo("test.txt", "test.txt", "test", "txt", "http://mozilla.org/test.txt"),
  105. contentType: "text/plain",
  106. saveMode: SAVEMODE_FILEONLY,
  107. saveAsType: kSaveAsType_Complete,
  108. file: null
  109. };
  110. prefs.setComplexValue("lastDir", Ci.nsILocalFile, tmpDir);
  111. do_check_true(getTargetFile(params));
  112. // file picker should start with browser.download.lastDir
  113. do_check_eq(fp.displayDirectory.path, tmpDir.path);
  114. // browser.download.lastDir should be modified before entering the private browsing mode
  115. do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
  116. // gDownloadLastDir should be usable outside of the private browsing mode
  117. do_check_eq(gDownloadLastDir.file.path, dir1.path);
  118. pb.privateBrowsingEnabled = true;
  119. do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
  120. fp.file = file2;
  121. fp.displayDirectory = null;
  122. do_check_true(getTargetFile(params));
  123. // file picker should start with browser.download.lastDir as set before entering the private browsing mode
  124. do_check_eq(fp.displayDirectory.path, dir1.path);
  125. // browser.download.lastDir should not be modified inside the private browsing mode
  126. do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path);
  127. // but gDownloadLastDir should be modified
  128. do_check_eq(gDownloadLastDir.file.path, dir2.path);
  129. pb.privateBrowsingEnabled = false;
  130. // gDownloadLastDir should be cleared after leaving the private browsing mode
  131. do_check_eq(gDownloadLastDir.file.path, dir1.path);
  132. fp.file = file3;
  133. fp.displayDirectory = null;
  134. do_check_true(getTargetFile(params));
  135. // file picker should start with browser.download.lastDir as set before entering the private browsing mode
  136. do_check_eq(fp.displayDirectory.path, dir1.path);
  137. // browser.download.lastDir should be modified after leaving the private browsing mode
  138. do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir3.path);
  139. // gDownloadLastDir should be usable after leaving the private browsing mode
  140. do_check_eq(gDownloadLastDir.file.path, dir3.path);
  141. // cleanup
  142. Cc["@mozilla.org/observer-service;1"]
  143. .getService(Ci.nsIObserverService)
  144. .notifyObservers(null, "quit-application", null);
  145. prefsService.clearUserPref("browser.privatebrowsing.keep_current_session");
  146. [dir1, dir2, dir3].forEach(function(dir) dir.remove(true));
  147. }