browser_privatebrowsing_viewsource.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 entering the private browsing mode closes
39 // all view source windows, and leaving it restores them
40 
41 function test() {
42  // initialization
43  let pb = Cc["@mozilla.org/privatebrowsing;1"].
44  getService(Ci.nsIPrivateBrowsingService);
45 
47 
48  let tabAbout = gBrowser.addTab();
49  gBrowser.selectedTab = tabAbout;
50  let aboutBrowser = gBrowser.getBrowserForTab(tabAbout);
51  aboutBrowser.addEventListener("load", function () {
52  aboutBrowser.removeEventListener("load", arguments.callee, true);
53 
54  let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
55  getService(Ci.nsIWindowWatcher);
56  let observer = {
57  observe: function(aSubject, aTopic, aData) {
58  if (aTopic == "domwindowopened") {
59  ww.unregisterNotification(this);
60 
61  let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
62  win.addEventListener("load", function() {
63  win.removeEventListener("load", arguments.callee, false);
64 
65  let browser = win.document.defaultView.getBrowser();
66  browser.addEventListener("load", function() {
67  browser.removeEventListener("load", arguments.callee, true);
68 
69  // view source window is loaded, proceed with the rest of the test
70  step1();
71  }, true);
72  }, false);
73  }
74  }
75  };
76  ww.registerNotification(observer);
77 
78  openViewSource();
79 
80  function openViewSource() {
81  // invoke the View Source command
82  let event = document.createEvent("Events");
83  event.initEvent("command", true, true);
84  document.getElementById("View:PageSource").dispatchEvent(event);
85  }
86 
87  function step1() {
88  observer = {
89  observe: function(aSubject, aTopic, aData) {
90  if (aTopic == "domwindowclosed") {
91  ok(true, "Entering the private browsing mode should close the view source window");
92  ww.unregisterNotification(observer);
93 
94  step2();
95  }
96  else if (aTopic == "domwindowopened")
97  ok(false, "Entering the private browsing mode should not open any view source window");
98  }
99  };
100  ww.registerNotification(observer);
101 
102  gBrowser.addTabsProgressListener({
103  onLocationChange: function() {},
104  onProgressChange: function() {},
105  onSecurityChange: function() {},
106  onStatusChange: function() {},
107  onRefreshAttempted: function() {},
108  onLinkIconAvailable: function() {},
109  onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStatus) {
110  if (aStateFlags & (Ci.nsIWebProgressListener.STATE_STOP |
111  Ci.nsIWebProgressListener.STATE_IS_WINDOW)) {
112  gBrowser.removeTabsProgressListener(this);
113 
114  step3();
115  }
116  }
117  });
118 
119  // enter private browsing mode
120  pb.privateBrowsingEnabled = true;
121  }
122 
123  let events = 0, step2, step3;
124  step2 = step3 = function() {
125  if (++events == 2)
126  step4();
127  }
128 
129  function step4() {
130  observer = {
131  observe: function(aSubject, aTopic, aData) {
132  if (aTopic == "domwindowopened") {
133  ww.unregisterNotification(this);
134 
135  let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
136  win.addEventListener("load", function() {
137  win.removeEventListener("load", arguments.callee, false);
138 
139  let browser = win.document.defaultView.getBrowser();
140  browser.addEventListener("load", function() {
141  browser.removeEventListener("load", arguments.callee, true);
142 
143  // view source window inside private browsing mode opened
144  step5();
145  }, true);
146  }, false);
147  }
148  }
149  };
150  ww.registerNotification(observer);
151 
152  openViewSource();
153  }
154 
155  function step5() {
156  let events = 0;
157 
158  observer = {
159  observe: function(aSubject, aTopic, aData) {
160  if (aTopic == "domwindowclosed") {
161  ok(true, "Leaving the private browsing mode should close the existing view source window");
162  if (++events == 2)
163  ww.unregisterNotification(observer);
164  }
165  else if (aTopic == "domwindowopened") {
166  ok(true, "Leaving the private browsing mode should restore the previous view source window");
167  if (++events == 2)
168  ww.unregisterNotification(observer);
169 
170  let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
171  win.addEventListener("load", function() {
172  win.removeEventListener("load", arguments.callee, false);
173 
174  let browser = win.document.defaultView.getBrowser();
175  browser.addEventListener("load", function() {
176  browser.removeEventListener("load", arguments.callee, true);
177 
178  is(browser.currentURI.spec, "view-source:about:",
179  "The correct view source window should be restored");
180 
181  // cleanup
182  win.close();
183  gBrowser.removeTab(gBrowser.selectedTab);
184  finish();
185  }, true);
186  }, false);
187  }
188  }
189  };
190  ww.registerNotification(observer);
191 
192  // exit private browsing mode
193  pb.privateBrowsingEnabled = false;
194  }
195  }, true);
196  aboutBrowser.loadURI("about:");
197 }
const Cc
function step3()
function step2()
var event
Element Properties events
getService(Ci.sbIFaceplateManager)
function step5()
waitForExplicitFinish()
return!aWindow arguments!aWindow arguments[0]
_updateTextAndScrollDataForTab aBrowser
const Ci
ContinuingWebProgressListener prototype onStateChange
var browser
Definition: openLocation.js:42
function step4()
let observer
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe