browser_privatebrowsing_beforeunload.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  * Nochum Sossonko.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Nochum Sossonko <highmind63@gmail.com> (Original Author)
23  * Ehsan Akhgari <ehsan@mozilla.com>
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either of the GNU General Public License Version 2 or later (the "GPL"),
27  * or 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 // This test makes sure that cancelling the unloading of a page with a beforeunload
40 // handler prevents the private browsing mode transition.
41 
42 function test() {
43  const kTestPage1 = "data:text/html,<body%20onbeforeunload='return%20false;'>first</body>";
44  const kTestPage2 = "data:text/html,<body%20onbeforeunload='return%20false;'>second</body>";
45  let pb = Cc["@mozilla.org/privatebrowsing;1"]
46  .getService(Ci.nsIPrivateBrowsingService);
47 
48  let promptHelper = {
49  rejectDialog: 0,
50  acceptDialog: 0,
51  confirmCalls: 0,
52 
53  observe: function(aSubject, aTopic, aData) {
54  let dialogWin = aSubject.QueryInterface(Ci.nsIDOMWindow);
55  this.confirmCalls++;
56  let button;
57  if (this.acceptDialog-- > 0)
58  button = dialogWin.document.documentElement.getButton("accept").click();
59  else if (this.rejectDialog-- > 0)
60  button = dialogWin.document.documentElement.getButton("cancel").click();
61  }
62  };
63 
64  Cc["@mozilla.org/observer-service;1"]
65  .getService(Ci.nsIObserverService)
66  .addObserver(promptHelper, "common-dialog-loaded", false);
67 
69  let browser1 = gBrowser.getBrowserForTab(gBrowser.addTab());
70  browser1.addEventListener("load", function() {
71  browser1.removeEventListener("load", arguments.callee, true);
72 
73  let browser2 = gBrowser.getBrowserForTab(gBrowser.addTab());
74  browser2.addEventListener("load", function() {
75  browser2.removeEventListener("load", arguments.callee, true);
76 
77  promptHelper.rejectDialog = 1;
78  pb.privateBrowsingEnabled = true;
79 
80  ok(!pb.privateBrowsingEnabled, "Private browsing mode should not have been activated");
81  is(promptHelper.confirmCalls, 1, "Only one confirm box should be shown");
82  is(gBrowser.tabContainer.childNodes.length, 3,
83  "No tabs should be closed because private browsing mode transition was canceled");
84  is(gBrowser.getBrowserForTab(gBrowser.tabContainer.firstChild).currentURI.spec, "about:blank",
85  "The first tab should be a blank tab");
86  is(gBrowser.getBrowserForTab(gBrowser.tabContainer.firstChild.nextSibling).currentURI.spec, kTestPage1,
87  "The middle tab should be the same one we opened");
88  is(gBrowser.getBrowserForTab(gBrowser.tabContainer.lastChild).currentURI.spec, kTestPage2,
89  "The last tab should be the same one we opened");
90  is(promptHelper.rejectDialog, 0, "Only one confirm dialog should have been rejected");
91 
92  promptHelper.confirmCalls = 0;
93  promptHelper.acceptDialog = 2;
94  pb.privateBrowsingEnabled = true;
95 
96  ok(pb.privateBrowsingEnabled, "Private browsing mode should have been activated");
97  is(promptHelper.confirmCalls, 2, "Only two confirm boxes should be shown");
98  is(gBrowser.tabContainer.childNodes.length, 1,
99  "Incorrect number of tabs after transition into private browsing");
100  gBrowser.selectedBrowser.addEventListener("load", function() {
101  gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
102 
103  is(gBrowser.selectedBrowser.currentURI.spec, "about:privatebrowsing",
104  "Incorrect page displayed after private browsing transition");
105  is(promptHelper.acceptDialog, 0, "Two confirm dialogs should have been accepted");
106 
107  gBrowser.selectedBrowser.addEventListener("load", function() {
108  gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
109 
110  gBrowser.selectedTab = gBrowser.addTab();
111  gBrowser.selectedBrowser.addEventListener("load", function() {
112  gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
113 
114  promptHelper.confirmCalls = 0;
115  promptHelper.rejectDialog = 1;
116  pb.privateBrowsingEnabled = false;
117 
118  ok(pb.privateBrowsingEnabled, "Private browsing mode should not have been deactivated");
119  is(promptHelper.confirmCalls, 1, "Only one confirm box should be shown");
120  is(gBrowser.tabContainer.childNodes.length, 2,
121  "No tabs should be closed because private browsing mode transition was canceled");
122  is(gBrowser.getBrowserForTab(gBrowser.tabContainer.firstChild).currentURI.spec, kTestPage1,
123  "The first tab should be the same one we opened");
124  is(gBrowser.getBrowserForTab(gBrowser.tabContainer.lastChild).currentURI.spec, kTestPage2,
125  "The last tab should be the same one we opened");
126  is(promptHelper.rejectDialog, 0, "Only one confirm dialog should have been rejected");
127 
128  promptHelper.confirmCalls = 0;
129  promptHelper.acceptDialog = 2;
130  pb.privateBrowsingEnabled = false;
131 
132  ok(!pb.privateBrowsingEnabled, "Private browsing mode should have been deactivated");
133  is(promptHelper.confirmCalls, 2, "Only two confirm boxes should be shown");
134  is(gBrowser.tabContainer.childNodes.length, 3,
135  "Incorrect number of tabs after transition into private browsing");
136 
137  let loads = 0;
138  function waitForLoad(event) {
139  gBrowser.removeEventListener("load", arguments.callee, true);
140 
141  if (++loads != 3)
142  return;
143 
144  is(gBrowser.getBrowserForTab(gBrowser.tabContainer.firstChild).currentURI.spec, "about:blank",
145  "The first tab should be a blank tab");
146  is(gBrowser.getBrowserForTab(gBrowser.tabContainer.firstChild.nextSibling).currentURI.spec, kTestPage1,
147  "The middle tab should be the same one we opened");
148  is(gBrowser.getBrowserForTab(gBrowser.tabContainer.lastChild).currentURI.spec, kTestPage2,
149  "The last tab should be the same one we opened");
150  is(promptHelper.acceptDialog, 0, "Two confirm dialogs should have been accepted");
151  is(promptHelper.acceptDialog, 0, "Two prompts should have been raised");
152 
153  promptHelper.acceptDialog = 2;
154  gBrowser.removeTab(gBrowser.tabContainer.lastChild);
155  gBrowser.removeTab(gBrowser.tabContainer.lastChild);
156 
157  finish();
158  }
159  for (let i = 0; i < gBrowser.browsers.length; ++i)
160  gBrowser.browsers[i].addEventListener("load", waitForLoad, true);
161  }, true);
162  gBrowser.selectedBrowser.loadURI(kTestPage2);
163  }, true);
164  gBrowser.selectedBrowser.loadURI(kTestPage1);
165  }, true);
166  }, true);
167  browser2.loadURI(kTestPage2);
168  }, true);
169  browser1.loadURI(kTestPage1);
170 }
const Cc
var event
waitForExplicitFinish()
return!aWindow arguments!aWindow arguments[0]
const Ci
_getSelectedPageStyle s i
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe