test_0-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 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) 2008
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 tests the private browsing service to make sure it implements its
39 // documented interface correctly.
40 
41 // This test should run before the rest of private browsing service unit tests,
42 // hence the naming used for this file.
43 
44 function run_test_on_service() {
45  // initialization
46  var os = Cc["@mozilla.org/observer-service;1"].
47  getService(Ci.nsIObserverService);
48 
49  // the contract ID should be available
50  do_check_true(PRIVATEBROWSING_CONTRACT_ID in Cc);
51 
52  // the interface should be available
53  do_check_true("nsIPrivateBrowsingService" in Ci);
54 
55  // it should be possible to initialize the component
56  try {
58  getService(Ci.nsIPrivateBrowsingService);
59  } catch (ex) {
60  LOG("exception thrown when trying to get the service: " + ex);
61  do_throw("private browsing service could not be initialized");
62  }
63 
64  // private browsing should be turned off initially
65  do_check_false(pb.privateBrowsingEnabled);
66  // private browsing not auto-started
67  do_check_false(pb.autoStarted);
68 
69  // it should be possible to toggle its status
70  pb.privateBrowsingEnabled = true;
71  do_check_true(pb.privateBrowsingEnabled);
72  do_check_false(pb.autoStarted);
73  pb.privateBrowsingEnabled = false;
74  do_check_false(pb.privateBrowsingEnabled);
75  do_check_false(pb.autoStarted);
76 
77  // test the private-browsing notification
78  var observer = {
79  observe: function(aSubject, aTopic, aData) {
80  if (aTopic == kPrivateBrowsingNotification)
81  this.data = aData;
82  },
83  data: null
84  };
85  os.addObserver(observer, kPrivateBrowsingNotification, false);
86  pb.privateBrowsingEnabled = true;
87  do_check_eq(observer.data, kEnter);
88  pb.privateBrowsingEnabled = false;
89  do_check_eq(observer.data, kExit);
90  os.removeObserver(observer, kPrivateBrowsingNotification);
91 
92  // make sure that setting the private browsing mode from within an observer throws
93  observer = {
94  observe: function(aSubject, aTopic, aData) {
95  if (aTopic == kPrivateBrowsingNotification) {
96  try {
97  pb.privateBrowsingEnabled = (aData == kEnter);
98  do_throw("Setting privateBrowsingEnabled inside the " + aData +
99  " notification should throw");
100  } catch (ex) {
101  if (!("result" in ex && ex.result == Cr.NS_ERROR_FAILURE))
102  do_throw("Unexpected exception caught: " + ex);
103  }
104  }
105  }
106  };
107  os.addObserver(observer, kPrivateBrowsingNotification, false);
108  pb.privateBrowsingEnabled = true;
109  do_check_true(pb.privateBrowsingEnabled); // the exception should not interfere with the mode change
110  pb.privateBrowsingEnabled = false;
111  do_check_false(pb.privateBrowsingEnabled); // the exception should not interfere with the mode change
112  os.removeObserver(observer, kPrivateBrowsingNotification);
113 
114  // make sure that getting the private browsing mode from within an observer doesn't throw
115  observer = {
116  observe: function(aSubject, aTopic, aData) {
117  if (aTopic == kPrivateBrowsingNotification) {
118  try {
119  var dummy = pb.privateBrowsingEnabled;
120  if (aData == kEnter)
121  do_check_true(dummy);
122  else if (aData == kExit)
123  do_check_false(dummy);
124  } catch (ex) {
125  do_throw("Unexpected exception caught: " + ex);
126  }
127  }
128  }
129  };
130  os.addObserver(observer, kPrivateBrowsingNotification, false);
131  pb.privateBrowsingEnabled = true;
132  do_check_true(pb.privateBrowsingEnabled); // just a sanity check
133  pb.privateBrowsingEnabled = false;
134  do_check_false(pb.privateBrowsingEnabled); // just a sanity check
135  os.removeObserver(observer, kPrivateBrowsingNotification);
136 
137  // check that the private-browsing-cancel-vote notification is sent before the
138  // private-browsing notification
139  observer = {
140  observe: function(aSubject, aTopic, aData) {
141  switch (aTopic) {
144  this.notifications.push(aTopic + " " + aData);
145  }
146  },
147  notifications: []
148  };
149  os.addObserver(observer, kPrivateBrowsingCancelVoteNotification, false);
150  os.addObserver(observer, kPrivateBrowsingNotification, false);
151  pb.privateBrowsingEnabled = true;
152  do_check_true(pb.privateBrowsingEnabled); // just a sanity check
153  pb.privateBrowsingEnabled = false;
154  do_check_false(pb.privateBrowsingEnabled); // just a sanity check
155  os.removeObserver(observer, kPrivateBrowsingNotification);
156  os.removeObserver(observer, kPrivateBrowsingCancelVoteNotification);
157  var reference_order = [
162  ];
163  do_check_eq(observer.notifications.join(","), reference_order.join(","));
164 
165  // make sure that the private-browsing-cancel-vote notification can be used
166  // to cancel the mode switch
167  observer = {
168  observe: function(aSubject, aTopic, aData) {
169  switch (aTopic) {
171  do_check_neq(aSubject, null);
172  try {
173  aSubject.QueryInterface(Ci.nsISupportsPRBool);
174  } catch (ex) {
175  do_throw("aSubject in " + kPrivateBrowsingCancelVoteNotification +
176  " should implement nsISupportsPRBool");
177  }
178  do_check_false(aSubject.data);
179  aSubject.data = true; // cancel the mode switch
180 
181  // fall through
183  this.notifications.push(aTopic + " " + aData);
184  }
185  },
186  nextPhase: function() {
187  this.notifications.push("enter phase " + (++this._phase));
188  },
189  notifications: [],
190  _phase: 0
191  };
192  os.addObserver(observer, kPrivateBrowsingCancelVoteNotification, false);
193  os.addObserver(observer, kPrivateBrowsingNotification, false);
194  pb.privateBrowsingEnabled = true;
195  do_check_false(pb.privateBrowsingEnabled); // should have been canceled
196  // temporarily disable the observer
197  os.removeObserver(observer, kPrivateBrowsingCancelVoteNotification);
198  observer.nextPhase();
199  pb.privateBrowsingEnabled = true; // this time, should enter successfully
200  do_check_true(pb.privateBrowsingEnabled); // should have been canceled
201  // re-enable the observer
202  os.addObserver(observer, kPrivateBrowsingCancelVoteNotification, false);
203  pb.privateBrowsingEnabled = false;
204  do_check_true(pb.privateBrowsingEnabled); // should have been canceled
205  os.removeObserver(observer, kPrivateBrowsingCancelVoteNotification);
206  observer.nextPhase();
207  pb.privateBrowsingEnabled = false; // this time, should exit successfully
208  do_check_false(pb.privateBrowsingEnabled);
209  os.removeObserver(observer, kPrivateBrowsingNotification);
210  reference_order = [
212  "enter phase 1",
215  "enter phase 2",
217  ];
218  do_check_eq(observer.notifications.join(","), reference_order.join(","));
219 
220  // make sure that the private browsing transition complete notification is
221  // raised correctly.
222  observer = {
223  observe: function(aSubject, aTopic, aData) {
224  this.notifications.push(aTopic + " " + aData);
225  },
226  notifications: []
227  };
228  os.addObserver(observer, kPrivateBrowsingNotification, false);
229  os.addObserver(observer, kPrivateBrowsingTransitionCompleteNotification, false);
230  pb.privateBrowsingEnabled = true;
231  pb.privateBrowsingEnabled = false;
232  os.removeObserver(observer, kPrivateBrowsingNotification);
233  os.removeObserver(observer, kPrivateBrowsingTransitionCompleteNotification);
234  reference_order = [
238  kPrivateBrowsingTransitionCompleteNotification + " ",
239  ];
240  do_check_eq(observer.notifications.join(","), reference_order.join(","));
241 }
242 
243 // Support running tests on both the service itself and its wrapper
244 function run_test() {
246 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
const kPrivateBrowsingCancelVoteNotification
#define LOG(args)
const kPrivateBrowsingTransitionCompleteNotification
const kExit
getService(Ci.sbIFaceplateManager)
const kEnter
function run_test()
return null
Definition: FeedWriter.js:1143
var os
const Cr
const kPrivateBrowsingNotification
const Ci
function run_test_on_all_services()
var PRIVATEBROWSING_CONTRACT_ID
observe data
Definition: FeedWriter.js:1329
let observer
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe
function run_test_on_service()