browser_privatebrowsing_opendir.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Private Browsing Tests.
15  *
16  * The Initial Developer of the Original Code is
17  * Ehsan Akhgari.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either of the GNU General Public License Version 2 or later (the "GPL"),
26  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 // This test makes sure that the last open directory used inside the private
39 // browsing mode is not remembered after leaving that mode.
40 
41 function test() {
42  // initialization
43  let pb = Cc["@mozilla.org/privatebrowsing;1"].
44  getService(Ci.nsIPrivateBrowsingService);
45  let ds = Cc["@mozilla.org/file/directory_service;1"].
46  getService(Ci.nsIProperties);
47  let dir1 = ds.get("ProfD", Ci.nsIFile);
48  let dir2 = ds.get("TmpD", Ci.nsIFile);
49  let file = dir2.clone();
50  file.append("pbtest.file");
51  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
52 
53  const kPrefName = "browser.open.lastDir";
54 
55  function setupCleanSlate() {
56  gLastOpenDirectory.reset();
57  if (gPrefService.prefHasUserValue(kPrefName))
58  gPrefService.clearUserPref(kPrefName);
59  }
60 
61  setupCleanSlate();
62 
63  // Test 1: general workflow test
64 
65  // initial checks
66  ok(!gLastOpenDirectory.path,
67  "Last open directory path should be initially empty");
68  gLastOpenDirectory.path = dir2;
69  is(gLastOpenDirectory.path.path, dir2.path,
70  "The path should be successfully set");
71  gLastOpenDirectory.path = null;
72  is(gLastOpenDirectory.path.path, dir2.path,
73  "The path should be not change when assigning it to null");
74  gLastOpenDirectory.path = dir1;
75  is(gLastOpenDirectory.path.path, dir1.path,
76  "The path should be successfully outside of the private browsing mode");
77 
78  // enter private browsing mode
79  pb.privateBrowsingEnabled = true;
80 
81  is(gLastOpenDirectory.path.path, dir1.path,
82  "The path should not change when entering the private browsing mode");
83  gLastOpenDirectory.path = dir2;
84  is(gLastOpenDirectory.path.path, dir2.path,
85  "The path should successfully change inside the private browsing mode");
86 
87  // leave private browsing mode
88  pb.privateBrowsingEnabled = false;
89 
90  is(gLastOpenDirectory.path.path, dir1.path,
91  "The path should be reset to the same path as before entering the private browsing mode");
92 
93  setupCleanSlate();
94 
95  // Test 2: the user first tries to open a file inside the private browsing mode
96 
97  pb.privateBrowsingEnabled = true;
98  ok(!gLastOpenDirectory.path,
99  "No original path should exist inside the private browsing mode");
100  gLastOpenDirectory.path = dir1;
101  is(gLastOpenDirectory.path.path, dir1.path,
102  "The path should be successfully set inside the private browsing mode");
103  pb.privateBrowsingEnabled = false;
104  ok(!gLastOpenDirectory.path,
105  "The path set inside the private browsing mode should not leak when leaving that mode");
106 
107  setupCleanSlate();
108 
109  // Test 3: the last open directory is set from a previous session, it should be used
110  // in normal mode
111 
112  gPrefService.setComplexValue(kPrefName, Ci.nsILocalFile, dir1);
113  is(gLastOpenDirectory.path.path, dir1.path,
114  "The pref set from last session should take effect outside the private browsing mode");
115 
116  setupCleanSlate();
117 
118  // Test 4: the last open directory is set from a previous session, it should be used
119  // in private browsing mode mode
120 
121  gPrefService.setComplexValue(kPrefName, Ci.nsILocalFile, dir1);
122  pb.privateBrowsingEnabled = true;
123  is(gLastOpenDirectory.path.path, dir1.path,
124  "The pref set from last session should take effect inside the private browsing mode");
125  pb.privateBrowsingEnabled = false;
126  is(gLastOpenDirectory.path.path, dir1.path,
127  "The pref set from last session should remain in effect after leaving the private browsing mode");
128 
129  setupCleanSlate();
130 
131  // Test 5: setting the path to a file shouldn't work
132 
133  gLastOpenDirectory.path = file;
134  ok(!gLastOpenDirectory.path,
135  "Setting the path to a file shouldn't work when it's originally null");
136  gLastOpenDirectory.path = dir1;
137  gLastOpenDirectory.path = file;
138  is(gLastOpenDirectory.path.path, dir1.path,
139  "Setting the path to a file shouldn't work when it's not originally null");
140 
141  // cleanup
142  file.remove(false);
143 }
const Cc
getService(Ci.sbIFaceplateManager)
return null
Definition: FeedWriter.js:1143
var gPrefService
Definition: overlay.js:34
const Ci
var file