browser_354894.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  * Nils Maier <maierman@web.de>
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Simon Bünzli <zeniko@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 
107 function test() {
109 
110  // Some urls that might be opened in tabs and/or popups
111  // Do not use about:blank:
112  // That one is reserved for special purposes in the tests
113  const TEST_URLS = ["about:mozilla", "about:buildconfig"];
114 
115  // Number of -request notifications to except
116  // remember to adjust when adding new tests
117  const NOTIFICATIONS_EXPECTED = 6;
118 
119  // Window features of popup windows
120  const POPUP_FEATURES = "toolbar=no,resizable=no,status=no";
121 
122  // Window features of browser windows
123  const CHROME_FEATURES = "chrome,all,dialog=no";
124 
125  // Store the old window type for cleanup
126  let oldWinType = "";
127  // Store the old tabs.warnOnClose pref so that we may reset it during
128  // cleanup
129  let oldWarnTabsOnClose = gPrefService.getBoolPref("browser.tabs.warnOnClose");
130 
131  // Observe these, and also use to count the number of hits
132  let observing = {
133  "browser-lastwindow-close-requested": 0,
134  "browser-lastwindow-close-granted": 0
135  };
136 
140  let observer = {
141  hitCount: 0,
142 
143  observe: function(aCancel, aTopic, aData) {
144  // count so that we later may compare
145  observing[aTopic]++;
146 
147  // handle some tests
148  if (++this.hitCount == 1) {
149  // Test 6
150  aCancel.QueryInterface(Ci.nsISupportsPRBool).data = true;
151  }
152  }
153  };
154  let observerService = Cc["@mozilla.org/observer-service;1"].
155  getService(Ci.nsIObserverService);
156 
161  function setPrefs() {
162  gPrefService.setIntPref("browser.startup.page", 3);
163  gPrefService.setBoolPref(
164  "browser.privatebrowsing.keep_current_session",
165  true
166  );
167  gPrefService.setBoolPref("browser.tabs.warnOnClose", false);
168  }
169 
173  function setupTestsuite(testFn) {
174  // Register our observers
175  for (let o in observing) {
176  observerService.addObserver(observer, o, false);
177  }
178 
179  // Make the main test window not count as a browser window any longer
180  oldWinType = document.documentElement.getAttribute("windowtype");
181  document.documentElement.setAttribute("windowtype", "navigator:testrunner");
182  }
183 
187  function cleanupTestsuite(callback) {
188  // Finally remove observers again
189  for (let o in observing) {
190  observerService.removeObserver(observer, o, false);
191  }
192  // Reset the prefs we touched
193  for each (let pref in [
194  "browser.startup.page",
195  "browser.privatebrowsing.keep_current_session"
196  ]) {
197  if (gPrefService.prefHasUserValue(pref)) {
198  gPrefService.clearUserPref(pref);
199  }
200  }
201  gPrefService.setBoolPref("browser.tabs.warnOnClose", oldWarnTabsOnClose);
202 
203  // Reset the window type
204  document.documentElement.setAttribute("windowtype", oldWinType);
205  }
206 
210  function setupTestAndRun(testFn) {
211  // Prepare the prefs
212  setPrefs();
213 
214  // Prepare a window; open it and add more tabs
215  let newWin = openDialog(location, "_blank", CHROME_FEATURES, "about:config");
216  newWin.addEventListener("load", function(aEvent) {
217  newWin.removeEventListener("load", arguments.callee, false);
218  newWin.gBrowser.addEventListener("load", function(aEvent) {
219  newWin.gBrowser.removeEventListener("load", arguments.callee, true);
220  for each (let url in TEST_URLS) {
221  newWin.gBrowser.addTab(url);
222  }
223 
224  executeSoon(function() testFn(newWin));
225  }, true);
226  }, false);
227  }
228 
233  function testOpenCloseNormal(nextFn) {
234  setupTestAndRun(function(newWin) {
235  // Close the window
236  // window.close doesn't push any close events,
237  // so use BrowserTryToCloseWindow
238  newWin.BrowserTryToCloseWindow();
239 
240  // The first request to close is denied by our observer (Test 6)
241  ok(!newWin.closed, "First close request was denied");
242  if (!newWin.closed) {
243  newWin.BrowserTryToCloseWindow();
244  ok(newWin.closed, "Second close request was granted");
245  }
246 
247  // Open a new window
248  // The previously closed window should be restored
249  newWin = openDialog(location, "_blank", CHROME_FEATURES);
250  newWin.addEventListener("load", function() {
251  executeSoon(function() {
252  is(newWin.gBrowser.browsers.length, TEST_URLS.length + 1,
253  "Restored window in-session with otherpopup windows around");
254 
255  // Cleanup
256  newWin.close();
257 
258  // Next please
259  executeSoon(nextFn);
260  });
261  }, true);
262  });
263  }
264 
269  function testOpenClosePrivateBrowsing(nextFn) {
270  setupTestAndRun(function(newWin) {
271  let pb = Cc["@mozilla.org/privatebrowsing;1"].
272  getService(Ci.nsIPrivateBrowsingService);
273 
274  // Close the window
275  newWin.BrowserTryToCloseWindow();
276 
277  // Enter private browsing mode
278  pb.privateBrowsingEnabled = true;
279 
280  // Open a new window.
281  // The previously closed window should NOT be restored
282  newWin = openDialog(location, "_blank", CHROME_FEATURES);
283  newWin.addEventListener("load", function() {
284  executeSoon(function() {
285  is(newWin.gBrowser.browsers.length, 1,
286  "Did not restore in private browing mode");
287 
288  // Cleanup
289  newWin.BrowserTryToCloseWindow();
290 
291  // Exit private browsing mode again
292  pb.privateBrowsingEnabled = false;
293 
294  newWin = openDialog(location, "_blank", CHROME_FEATURES);
295  newWin.addEventListener("load", function() {
296  executeSoon(function() {
297  is(newWin.gBrowser.browsers.length, TEST_URLS.length + 1,
298  "Restored after leaving private browsing again");
299 
300  newWin.close();
301 
302  // Next please
303  executeSoon(nextFn);
304  });
305  }, true);
306  });
307  }, true);
308  });
309  }
310 
316  function testOpenCloseWindowAndPopup(nextFn) {
317  setupTestAndRun(function(newWin) {
318  // open some popups
319  let popup = openDialog(location, "popup", POPUP_FEATURES, TEST_URLS[0]);
320  let popup2 = openDialog(location, "popup2", POPUP_FEATURES, TEST_URLS[1]);
321  popup2.addEventListener("load", function() {
322  popup2.removeEventListener("load", arguments.callee, false);
323  popup2.gBrowser.addEventListener("load", function() {
324  popup2.gBrowser.removeEventListener("load", arguments.callee, true);
325  popup2.gBrowser.addTab(TEST_URLS[0]);
326  // close the window
327  newWin.BrowserTryToCloseWindow();
328 
329  // Close the popup window
330  // The test is successful when not this popup window is restored
331  // but instead newWin
332  popup2.close();
333 
334  // open a new window the previously closed window should be restored to
335  newWin = openDialog(location, "_blank", CHROME_FEATURES);
336  newWin.addEventListener("load", function() {
337  executeSoon(function() {
338  is(newWin.gBrowser.browsers.length, TEST_URLS.length + 1,
339  "Restored window and associated tabs in session");
340 
341  // Cleanup
342  newWin.close();
343  popup.close();
344 
345  // Next please
346  executeSoon(nextFn);
347  });
348  }, true);
349  }, true);
350  }, false);
351  });
352  }
353 
359  function testOpenCloseOnlyPopup(nextFn) {
360  // prepare the prefs
361  setPrefs();
362 
363  // This will cause nsSessionStore to restore a window the next time it
364  // gets a chance.
365  let popup = openDialog(location, "popup", POPUP_FEATURES, TEST_URLS[1]);
366  popup.addEventListener("load", function() {
367  is(popup.gBrowser.browsers.length, 1,
368  "Did not restore the popup window (1)");
369  popup.BrowserTryToCloseWindow();
370 
371  // Real tests
372  popup = openDialog(location, "popup", POPUP_FEATURES, TEST_URLS[1]);
373  popup.addEventListener("load", function() {
374  popup.removeEventListener("load", arguments.callee, false);
375  popup.gBrowser.addEventListener("load", function() {
376  popup.gBrowser.removeEventListener("load", arguments.callee, true);
377  popup.gBrowser.addTab(TEST_URLS[0]);
378 
379  is(popup.gBrowser.browsers.length, 2,
380  "Did not restore to the popup window (2)");
381 
382  // Close the popup window
383  // The test is successful when not this popup window is restored
384  // but instead a new window is opened without restoring anything
385  popup.close();
386 
387  let newWin = openDialog(location, "_blank", CHROME_FEATURES, "about:blank");
388  newWin.addEventListener("load", function() {
389  newWin.removeEventListener("load", arguments.callee, true);
390  executeSoon(function() {
391  isnot(newWin.gBrowser.browsers.length, 2,
392  "Did not restore the popup window");
393  is(TEST_URLS.indexOf(newWin.gBrowser.browsers[0].currentURI.spec), -1,
394  "Did not restore the popup window (2)");
395 
396  // Cleanup
397  newWin.close();
398 
399  // Next please
400  executeSoon(nextFn);
401  });
402  }, true);
403  }, true);
404  }, false);
405  }, true);
406  }
407 
413  function testOpenCloseRestoreFromPopup(nextFn) {
414  setupTestAndRun(function(newWin) {
415  setupTestAndRun(function(newWin2) {
416  newWin.BrowserTryToCloseWindow();
417  newWin2.BrowserTryToCloseWindow();
418 
419  newWin = undoCloseWindow(0);
420 
421  newWin2 = openDialog(location, "_blank", CHROME_FEATURES);
422  newWin2.addEventListener("load", function() {
423  newWin2.removeEventListener("load", arguments.callee, true);
424  executeSoon(function() {
425  is(newWin2.gBrowser.browsers.length, 1,
426  "Did not restore, as undoCloseWindow() was last called");
427  is(TEST_URLS.indexOf(newWin2.gBrowser.browsers[0].currentURI.spec), -1,
428  "Did not restore, as undoCloseWindow() was last called (2)");
429 
430  // Cleanup
431  newWin.close();
432  newWin2.close();
433 
434  // Next please
435  executeSoon(nextFn);
436  });
437  }, true);
438  });
439  });
440  }
441 
446  function testNotificationCount(nextFn) {
447  is(observing["browser-lastwindow-close-requested"], NOTIFICATIONS_EXPECTED,
448  "browser-lastwindow-close-requested notifications observed");
449 
450  // -request must be one more as we cancel the first one we hit,
451  // and hence won't produce a corresponding -grant
452  // @see observer.observe
453  is(observing["browser-lastwindow-close-requested"],
454  observing["browser-lastwindow-close-granted"] + 1,
455  "Notification count for -request and -grant matches");
456 
457  executeSoon(nextFn);
458  }
459 
465  function testMacNotifications(nextFn, iteration) {
466  iteration = iteration || 1;
467  setupTestAndRun(function(newWin) {
468  // close the window
469  // window.close doesn't push any close events,
470  // so use BrowserTryToCloseWindow
471  newWin.BrowserTryToCloseWindow();
472  if (iteration == 1) {
473  ok(!newWin.closed, "First close attempt denied");
474  if (!newWin.closed) {
475  newWin.BrowserTryToCloseWindow();
476  ok(newWin.closed, "Second close attempt granted");
477  }
478  }
479 
480  if (iteration < NOTIFICATIONS_EXPECTED - 1) {
481  executeSoon(function() testMacNotifications(nextFn, ++iteration));
482  }
483  else {
484  executeSoon(nextFn);
485  }
486  });
487  }
488 
489  // Execution starts here
490 
491  setupTestsuite();
492  if (navigator.platform.match(/Mac/)) {
493  // Mac tests
494  testMacNotifications(
495  function() testNotificationCount(
496  function() cleanupTestsuite() + finish()
497  )
498  );
499  }
500  else {
501  // Non-Mac Tests
502  testOpenCloseNormal(
503  function() testOpenClosePrivateBrowsing(
504  function() testOpenCloseWindowAndPopup(
505  function() testOpenCloseOnlyPopup(
506  function() testOpenCloseRestoreFromPopup (
507  function() testNotificationCount(
508  function() cleanupTestsuite() + finish()
509  )
510  )
511  )
512  )
513  )
514  );
515  }
516 }
const Cc
static nsCOMPtr< nsIObserverService > observerService
Definition: UnityProxy.cpp:6
var pref
Definition: openLocation.js:44
function undoCloseWindow(aIndex)
Definition: browser.js:6380
function test()
getService(Ci.sbIFaceplateManager)
waitForExplicitFinish()
grep callback
return!aWindow arguments!aWindow arguments[0]
var gPrefService
Definition: overlay.js:34
function url(spec)
const Ci
let observer
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe