browser_490040.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  * Mozilla Corporation.
18  * Portions created by the Initial Developer are Copyright (C) 2008
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Paul O’Shannessy <paul@oshannessy.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * 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 function test() {
41  let ss = Cc["@mozilla.org/browser/sessionstore;1"].
42  getService(Ci.nsISessionStore);
43  let os = Cc["@mozilla.org/observer-service;1"].
44  getService(Ci.nsIObserverService);
45  let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
46  getService(Ci.nsIWindowWatcher);
47 
49 
50  function testWithState(aState) {
51  // Ensure we can store the window if needed.
52  let curClosedWindowCount = ss.getClosedWindowCount();
53  gPrefService.setIntPref("browser.sessionstore.max_windows_undo",
54  curClosedWindowCount + 1);
55 
56  let windowObserver = {
57  observe: function(aSubject, aTopic, aData) {
58  let theWin = aSubject.QueryInterface(Ci.nsIDOMWindow);
59 
60  switch(aTopic) {
61  case "domwindowopened":
62  theWin.addEventListener("load", function () {
63  theWin.removeEventListener("load", arguments.callee, false);
64  executeSoon(function() {
65  // Close the window as soon as the first tab loads, or
66  // immediately if there are no tabs.
67  if (aState.windowState.windows[0].tabs[0].entries.length) {
68  theWin.gBrowser.addEventListener("load", function() {
69  theWin.gBrowser.removeEventListener("load",
70  arguments.callee, true);
71  theWin.close();
72  }, true);
73  } else {
74  executeSoon(function() {
75  theWin.close();
76  });
77  }
78  ss.setWindowState(theWin, JSON.stringify(aState.windowState),
79  true);
80  });
81  }, false);
82  break;
83 
84  case "domwindowclosed":
85  ww.unregisterNotification(this);
86  // Use executeSoon to ensure this happens after SS observer.
87  executeSoon(function() {
88  is(ss.getClosedWindowCount(),
89  curClosedWindowCount + (aState.shouldBeAdded ? 1 : 0),
90  "That window should " + (aState.shouldBeAdded ? "" : "not ") +
91  "be restorable");
92  executeSoon(runNextTest);
93  });
94  break;
95  }
96  }
97  }
98  ww.registerNotification(windowObserver);
99  ww.openWindow(null,
100  location,
101  "_blank",
102  "chrome,all,dialog=no",
103  null);
104  }
105 
106  // Only windows with open tabs are restorable. Windows where a lone tab is
107  // detached may have _closedTabs, but is left with just an empty tab.
108  let states = [
109  {
110  shouldBeAdded: true,
111  windowState: {
112  windows: [{
113  tabs: [{ entries: [{ url: "http://example.com", title: "example.com" }] }],
114  selected: 1,
115  _closedTabs: []
116  }]
117  }
118  },
119  {
120  shouldBeAdded: false,
121  windowState: {
122  windows: [{
123  tabs: [{ entries: [] }],
124  _closedTabs: []
125  }]
126  }
127  },
128  {
129  shouldBeAdded: false,
130  windowState: {
131  windows: [{
132  tabs: [{ entries: [] }],
133  _closedTabs: [{ state: { entries: [{ url: "http://example.com", index: 1 }] } }]
134  }]
135  }
136  },
137  {
138  shouldBeAdded: false,
139  windowState: {
140  windows: [{
141  tabs: [{ entries: [] }],
142  _closedTabs: [],
143  extData: { keyname: "pi != " + Math.random() }
144  }]
145  }
146  }
147  ];
148 
149  function runNextTest() {
150  if (states.length) {
151  let state = states.shift();
152  testWithState(state);
153  }
154  else {
155  if (gPrefService.prefHasUserValue("browser.sessionstore.max_windows_undo"))
156  gPrefService.clearUserPref("browser.sessionstore.max_windows_undo");
157  finish();
158  }
159  }
160  runNextTest();
161 }
162 
const Cc
var windows
getService(Ci.sbIFaceplateManager)
var tabs
waitForExplicitFinish()
return null
Definition: FeedWriter.js:1143
return!aWindow arguments!aWindow arguments[0]
var gPrefService
Definition: overlay.js:34
var os
function url(spec)
const Ci
var JSON
function runNextTest()
restoreWindow aState
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe
function test()