browser_394759_privatebrowsing.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 sessionstore test code.
15  *
16  * The Initial Developer of the Original Code is
17  * Aaron Train <aaron.train@gmail.com>.
18  * Portions created by the Initial Developer are Copyright (C) 2008
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Ehsan Akhgari <ehsan.akhgari@gmail.com>
23  * Paul O’Shannessy <paul@oshannessy.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 
39 function test() {
42  // test setup
44 
45  // private browsing service
46  let pb = Cc["@mozilla.org/privatebrowsing;1"].
47  getService(Ci.nsIPrivateBrowsingService);
48  let profilePath = Cc["@mozilla.org/file/directory_service;1"].
49  getService(Ci.nsIProperties).
50  get("ProfD", Ci.nsIFile);
51 
52  // sessionstore service
53  let ss = Cc["@mozilla.org/browser/sessionstore;1"].
54  getService(Ci.nsISessionStore);
55  // Remove the sessionstore.js file before setting the interval to 0
56  let sessionStoreJS = profilePath.clone();
57  sessionStoreJS.append("sessionstore.js");
58  if (sessionStoreJS.exists())
59  sessionStoreJS.remove(false);
60  ok(sessionStoreJS.exists() == false, "sessionstore.js was removed");
61  // Make sure that sessionstore.js can be forced to be created by setting
62  // the interval pref to 0
63  gPrefService.setIntPref("browser.sessionstore.interval", 0);
64  // sessionstore.js should be re-created at this point
65  sessionStoreJS = profilePath.clone();
66  sessionStoreJS.append("sessionstore.js");
67 
68  // Set up the browser in a blank state. Popup windows in previous tests result
69  // in different states on different platforms.
70  let blankState = JSON.stringify({
71  windows: [{
72  tabs: [{ entries: [{ url: "about:blank" }] }],
73  _closedTabs: []
74  }],
75  _closedWindows: []
76  });
77  ss.setBrowserState(blankState);
78 
79  let closedWindowCount = ss.getClosedWindowCount();
80 
81  let testURL_A = "about:config";
82  let testURL_B = "about:mozilla";
83 
84  let uniqueKey_A = "bug 394759 Non-PB";
85  let uniqueValue_A = "unik" + Date.now();
86  let uniqueKey_B = "bug 394759 PB";
87  let uniqueValue_B = "uniq" + Date.now();
88 
89 
90  // Open a window
91  let newWin = openDialog(location, "_blank", "chrome,all,dialog=no", testURL_A);
92  newWin.addEventListener("load", function(aEvent) {
93  newWin.gBrowser.addEventListener("load", function(aEvent) {
94  newWin.gBrowser.removeEventListener("load", arguments.callee, true);
95 
96  executeSoon(function() {
97  newWin.gBrowser.addTab();
98 
99  // mark the window with some unique data to be restored later on
100  ss.setWindowValue(newWin, uniqueKey_A, uniqueValue_A);
101 
102  newWin.close();
103 
104  // ensure that we incremented # of close windows
105  is(ss.getClosedWindowCount(), closedWindowCount + 1,
106  "The closed window was added to the list");
107 
108  // ensure we added window to undo list
109  let data = JSON.parse(ss.getClosedWindowData())[0];
110  ok(data.toSource().indexOf(uniqueValue_A) > -1,
111  "The closed window data was stored correctly");
112 
113  // enter private browsing mode
114  pb.privateBrowsingEnabled = true;
115  ok(pb.privateBrowsingEnabled, "private browsing enabled");
116 
117  // ensure that we have 0 undo windows when entering PB
118  is(ss.getClosedWindowCount(), 0,
119  "Recently Closed Windows are removed when entering Private Browsing");
120  is(ss.getClosedWindowData(), "[]",
121  "Recently Closed Windows data is cleared when entering Private Browsing");
122 
123  // open another window in PB
124  let pbWin = openDialog(location, "_blank", "chrome,all,dialog=no", testURL_B);
125  pbWin.addEventListener("load", function(aEvent) {
126  pbWin.gBrowser.addEventListener("load", function(aEvent) {
127  pbWin.gBrowser.removeEventListener("load", arguments.callee, true);
128 
129  executeSoon(function() {
130  // Add another tab, though it's not strictly needed
131  pbWin.gBrowser.addTab();
132 
133  // mark the window with some unique data to be restored later on
134  ss.setWindowValue(pbWin, uniqueKey_B, uniqueValue_B);
135 
136  pbWin.close();
137 
138  // ensure we added window to undo list
139  let data = JSON.parse(ss.getClosedWindowData())[0];
140  ok(data.toSource().indexOf(uniqueValue_B) > -1,
141  "The closed window data was stored correctly in PB mode");
142 
143  // exit private browsing mode
144  pb.privateBrowsingEnabled = false;
145  ok(!pb.privateBrowsingEnabled, "private browsing disabled");
146 
147  // ensure that we still have the closed windows from before
148  is(ss.getClosedWindowCount(), closedWindowCount + 1,
149  "The correct number of recently closed windows were restored " +
150  "when exiting PB mode");
151 
152  let data = JSON.parse(ss.getClosedWindowData())[0];
153  ok(data.toSource().indexOf(uniqueValue_A) > -1,
154  "The data associated with the recently closed window was " +
155  "restored when exiting PB mode");
156 
157  // cleanup
158  if (gPrefService.prefHasUserValue("browser.sessionstore.interval"))
159  gPrefService.clearUserPref("browser.sessionstore.interval");
160  finish();
161  });
162  }, true);
163  }, true);
164  });
165  }, true);
166  }, true);
167 }
const Cc
var windows
function test()
getService(Ci.sbIFaceplateManager)
var tabs
waitForExplicitFinish()
return!aWindow arguments!aWindow arguments[0]
var gPrefService
Definition: overlay.js:34
function url(spec)
const Ci
var JSON
observe data
Definition: FeedWriter.js:1329