browser_522545.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() {
42 
43  let ss = Cc["@mozilla.org/browser/sessionstore;1"].
44  getService(Ci.nsISessionStore);
45 
46  function waitForBrowserState(aState, aSetStateCallback) {
47  var locationChanges = 0;
48  gBrowser.addTabsProgressListener({
49  onLocationChange: function (aBrowser) {
50  if (++locationChanges == aState.windows[0].tabs.length) {
51  gBrowser.removeTabsProgressListener(this);
52  executeSoon(aSetStateCallback);
53  }
54  },
55  onProgressChange: function () {},
56  onSecurityChange: function () {},
57  onStateChange: function () {},
58  onStatusChange: function () {}
59  });
60  ss.setBrowserState(JSON.stringify(aState));
61  }
62 
63  // This tests the following use case:
64  // User opens a new tab which gets focus. The user types something into the
65  // address bar, then crashes or quits.
66  function test_newTabFocused() {
67  let state = {
68  windows: [{
69  tabs: [
70  { entries: [{ url: "about:mozilla" }] },
71  { entries: [], userTypedValue: "example.com", userTypedClear: 0 }
72  ],
73  selected: 2
74  }]
75  };
76 
77  waitForBrowserState(state, function() {
78  let browser = gBrowser.selectedBrowser;
79  is(browser.currentURI.spec, "about:blank",
80  "No history entries still sets currentURI to about:blank");
81  is(browser.userTypedValue, "example.com",
82  "userTypedValue was correctly restored");
83  is(browser.userTypedClear, 0,
84  "userTypeClear restored as expected");
85  is(gURLBar.value, "example.com",
86  "Address bar's value correctly restored");
87  // Change tabs to make sure address bar value gets updated
88  gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(0);
89  is(gURLBar.value, "about:mozilla",
90  "Address bar's value correctly updated");
91  runNextTest();
92  });
93  }
94 
95  // This tests the following use case:
96  // User opens a new tab which gets focus. The user types something into the
97  // address bar, switches back to the first tab, then crashes or quits.
98  function test_newTabNotFocused() {
99  let state = {
100  windows: [{
101  tabs: [
102  { entries: [{ url: "about:mozilla" }] },
103  { entries: [], userTypedValue: "example.org", userTypedClear: 0 }
104  ],
105  selected: 1
106  }]
107  };
108 
109  waitForBrowserState(state, function() {
110  let browser = gBrowser.getBrowserAtIndex(1);
111  is(browser.currentURI.spec, "about:blank",
112  "No history entries still sets currentURI to about:blank");
113  is(browser.userTypedValue, "example.org",
114  "userTypedValue was correctly restored");
115  is(browser.userTypedClear, 0,
116  "userTypeClear restored as expected");
117  is(gURLBar.value, "about:mozilla",
118  "Address bar's value correctly restored");
119  // Change tabs to make sure address bar value gets updated
120  gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(1);
121  is(gURLBar.value, "example.org",
122  "Address bar's value correctly updated");
123  runNextTest();
124  });
125  }
126 
127  // This tests the following use case:
128  // User is in a tab with session history, then types something in the
129  // address bar, then crashes or quits.
130  function test_existingSHEnd_noClear() {
131  let state = {
132  windows: [{
133  tabs: [{
134  entries: [{ url: "about:mozilla" }, { url: "about:config" }],
135  index: 2,
136  userTypedValue: "example.com",
137  userTypedClear: 0
138  }]
139  }]
140  };
141 
142  waitForBrowserState(state, function() {
143  let browser = gBrowser.selectedBrowser;
144  is(browser.currentURI.spec, "about:config",
145  "browser.currentURI set to current entry in SH");
146  is(browser.userTypedValue, "example.com",
147  "userTypedValue was correctly restored");
148  is(browser.userTypedClear, 0,
149  "userTypeClear restored as expected");
150  is(gURLBar.value, "example.com",
151  "Address bar's value correctly restored to userTypedValue");
152  runNextTest();
153  });
154  }
155 
156  // This tests the following use case:
157  // User is in a tab with session history, presses back at some point, then
158  // types something in the address bar, then crashes or quits.
159  function test_existingSHMiddle_noClear() {
160  let state = {
161  windows: [{
162  tabs: [{
163  entries: [{ url: "about:mozilla" }, { url: "about:config" }],
164  index: 1,
165  userTypedValue: "example.org",
166  userTypedClear: 0
167  }]
168  }]
169  };
170 
171  waitForBrowserState(state, function() {
172  let browser = gBrowser.selectedBrowser;
173  is(browser.currentURI.spec, "about:mozilla",
174  "browser.currentURI set to current entry in SH");
175  is(browser.userTypedValue, "example.org",
176  "userTypedValue was correctly restored");
177  is(browser.userTypedClear, 0,
178  "userTypeClear restored as expected");
179  is(gURLBar.value, "example.org",
180  "Address bar's value correctly restored to userTypedValue");
181  runNextTest();
182  });
183  }
184 
185  // This test simulates lots of tabs opening at once and then quitting/crashing.
186  function test_getBrowserState_lotsOfTabsOpening() {
187  let uris = [];
188  for (let i = 0; i < 25; i++)
189  uris.push("http://example.com/" + i);
190 
191  // We're listening for the first non-"about:blank" load event, which should
192  // indicate one of the tabs has loaded and the others haven't. So one should
193  // be in a non-userTypedValue case, while others should still have
194  // userTypedValue and userTypedClear set.
195  gBrowser.addEventListener("load", function(aEvent) {
196  if (gBrowser.currentURI.spec == "about:blank")
197  return;
198  gBrowser.removeEventListener("load", arguments.callee, true);
199 
200  let state = JSON.parse(ss.getBrowserState());
201 
202  let hasSH = state.windows[0].tabs.some(function(aTab) {
203  return !("userTypedValue" in aTab) && aTab.entries[0].url;
204  });
205  let hasUTV = state.windows[0].tabs.some(function(aTab) {
206  return aTab.userTypedValue && aTab.userTypedClear && !aTab.entries.length;
207  });
208 
209  ok(hasSH, "At least one tab has it's entry in SH");
210  ok(hasUTV, "At least one tab has a userTypedValue with userTypedClear with no loaded URL");
211 
212  runNextTest();
213 
214  }, true);
215  gBrowser.loadTabs(uris);
216  }
217 
218  // This simulates setting a userTypedValue and ensures that just typing in the
219  // URL bar doesn't set userTypedClear as well.
220  function test_getBrowserState_userTypedValue() {
221  let state = {
222  windows: [{
223  tabs: [{ entries: [] }]
224  }]
225  };
226 
227  waitForBrowserState(state, function() {
228  let browser = gBrowser.selectedBrowser;
229  // Make sure this tab isn't loading and state is clear before we test.
230  is(browser.userTypedValue, null, "userTypedValue is empty to start");
231  is(browser.userTypedClear, 0, "userTypedClear is 0 to start");
232 
233  gURLBar.value = "mozilla.org";
234  let event = document.createEvent("Events");
235  event.initEvent("input", true, false);
236  gURLBar.dispatchEvent(event);
237 
238  is(browser.userTypedValue, "mozilla.org",
239  "userTypedValue was set when changing gURLBar.value");
240  is(browser.userTypedClear, 0,
241  "userTypedClear was not changed when changing gURLBar.value");
242 
243  // Now make sure ss gets these values too
244  let newState = JSON.parse(ss.getBrowserState());
245  is(newState.windows[0].tabs[0].userTypedValue, "mozilla.org",
246  "sessionstore got correct userTypedValue");
247  is(newState.windows[0].tabs[0].userTypedClear, 0,
248  "sessionstore got correct userTypedClear");
249  runNextTest();
250  });
251  }
252 
253  // test_getBrowserState_lotsOfTabsOpening tested userTypedClear in a few cases,
254  // but not necessarily any that had legitimate URIs in the state of loading
255  // (eg, "http://example.com"), so this test will cover that case.
256  function test_userTypedClearLoadURI() {
257  let state = {
258  windows: [{
259  tabs: [
260  { entries: [], userTypedValue: "http://example.com", userTypedClear: 2 }
261  ]
262  }]
263  };
264 
265  // Set state here and listen for load event because waitForBrowserState
266  // doesn't guarantee all the tabs have loaded, so the test could continue
267  // before we're in a testable state. This is important here because of the
268  // distinction between "http://example.com" and "http://example.com/".
269  ss.setBrowserState(JSON.stringify(state));
270  gBrowser.addEventListener("load", function(aEvent) {
271  if (gBrowser.currentURI.spec == "about:blank")
272  return;
273  gBrowser.removeEventListener("load", arguments.callee, true);
274 
275  let browser = gBrowser.selectedBrowser;
276  is(browser.currentURI.spec, "http://example.com/",
277  "userTypedClear=2 caused userTypedValue to be loaded");
278  is(browser.userTypedValue, null,
279  "userTypedValue was null after loading a URI");
280  is(browser.userTypedClear, 0,
281  "userTypeClear reset to 0");
282  is(gURLBar.value, "http://example.com/",
283  "Address bar's value set after loading URI");
284  runNextTest();
285  }, true);
286  }
287 
288 
289  let tests = [test_newTabFocused, test_newTabNotFocused,
290  test_existingSHEnd_noClear, test_existingSHMiddle_noClear,
291  test_getBrowserState_lotsOfTabsOpening,
292  test_getBrowserState_userTypedValue, test_userTypedClearLoadURI];
293  let originalState = ss.getBrowserState();
294  function runNextTest() {
295  if (tests.length) {
296  tests.shift()();
297  } else {
298  ss.setBrowserState(originalState);
299  executeSoon(finish);
300  }
301  }
302 
303  // Run the tests!
304  runNextTest();
305 }
const Cc
var windows
var uris
var event
function test()
getService(Ci.sbIFaceplateManager)
var tabs
waitForExplicitFinish()
return null
Definition: FeedWriter.js:1143
return!aWindow arguments!aWindow arguments[0]
function url(spec)
_updateTextAndScrollDataForTab aBrowser
const Ci
var JSON
_replaceLoadingTitle aTab
ContinuingWebProgressListener prototype onStateChange
var browser
Definition: openLocation.js:42
function runNextTest()
restoreWindow aState
_getSelectedPageStyle s i