1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is sessionstore test code.
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.
22 * Paul O’Shannessy <paul@oshannessy.com>
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.
36 * ***** END LICENSE BLOCK ***** */
38 function browserWindowsCount() {
40 let e = Services.wm.getEnumerator("navigator:browser");
41 while (e.hasMoreElements()) {
42 if (!e.getNext().closed)
49 /** Test for Bug 522545 **/
50 is(browserWindowsCount(), 1, "Only one browser window should be open initially");
52 waitForExplicitFinish();
53 requestLongerTimeout(2);
55 let ss = Cc["@mozilla.org/browser/sessionstore;1"].
56 getService(Ci.nsISessionStore);
58 // This tests the following use case:
59 // User opens a new tab which gets focus. The user types something into the
60 // address bar, then crashes or quits.
61 function test_newTabFocused() {
65 { entries: [{ url: "about:mozilla" }] },
66 { entries: [], userTypedValue: "example.com", userTypedClear: 0 }
72 waitForBrowserState(state, function() {
73 let browser = gBrowser.selectedBrowser;
74 is(browser.currentURI.spec, "about:blank",
75 "No history entries still sets currentURI to about:blank");
76 is(browser.userTypedValue, "example.com",
77 "userTypedValue was correctly restored");
78 is(browser.userTypedClear, 0,
79 "userTypeClear restored as expected");
80 is(gURLBar.value, "example.com",
81 "Address bar's value correctly restored");
82 // Change tabs to make sure address bar value gets updated
83 gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(0);
84 is(gURLBar.value, "about:mozilla",
85 "Address bar's value correctly updated");
90 // This tests the following use case:
91 // User opens a new tab which gets focus. The user types something into the
92 // address bar, switches back to the first tab, then crashes or quits.
93 function test_newTabNotFocused() {
97 { entries: [{ url: "about:mozilla" }] },
98 { entries: [], userTypedValue: "example.org", userTypedClear: 0 }
104 waitForBrowserState(state, function() {
105 let browser = gBrowser.getBrowserAtIndex(1);
106 is(browser.currentURI.spec, "about:blank",
107 "No history entries still sets currentURI to about:blank");
108 is(browser.userTypedValue, "example.org",
109 "userTypedValue was correctly restored");
110 is(browser.userTypedClear, 0,
111 "userTypeClear restored as expected");
112 is(gURLBar.value, "about:mozilla",
113 "Address bar's value correctly restored");
114 // Change tabs to make sure address bar value gets updated
115 gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(1);
116 is(gURLBar.value, "example.org",
117 "Address bar's value correctly updated");
122 // This tests the following use case:
123 // User is in a tab with session history, then types something in the
124 // address bar, then crashes or quits.
125 function test_existingSHEnd_noClear() {
129 entries: [{ url: "about:mozilla" }, { url: "about:config" }],
131 userTypedValue: "example.com",
137 waitForBrowserState(state, function() {
138 let browser = gBrowser.selectedBrowser;
139 is(browser.currentURI.spec, "about:config",
140 "browser.currentURI set to current entry in SH");
141 is(browser.userTypedValue, "example.com",
142 "userTypedValue was correctly restored");
143 is(browser.userTypedClear, 0,
144 "userTypeClear restored as expected");
145 is(gURLBar.value, "example.com",
146 "Address bar's value correctly restored to userTypedValue");
151 // This tests the following use case:
152 // User is in a tab with session history, presses back at some point, then
153 // types something in the address bar, then crashes or quits.
154 function test_existingSHMiddle_noClear() {
158 entries: [{ url: "about:mozilla" }, { url: "about:config" }],
160 userTypedValue: "example.org",
166 waitForBrowserState(state, function() {
167 let browser = gBrowser.selectedBrowser;
168 is(browser.currentURI.spec, "about:mozilla",
169 "browser.currentURI set to current entry in SH");
170 is(browser.userTypedValue, "example.org",
171 "userTypedValue was correctly restored");
172 is(browser.userTypedClear, 0,
173 "userTypeClear restored as expected");
174 is(gURLBar.value, "example.org",
175 "Address bar's value correctly restored to userTypedValue");
180 // This test simulates lots of tabs opening at once and then quitting/crashing.
181 function test_getBrowserState_lotsOfTabsOpening() {
185 for (let i = 0; i < 25; i++)
186 uris.push("http://example.com/" + i);
188 // We're waiting for the first location change, which should indicate
189 // one of the tabs has loaded and the others haven't. So one should
190 // be in a non-userTypedValue case, while others should still have
191 // userTypedValue and userTypedClear set.
192 gBrowser.addTabsProgressListener({
193 onLocationChange: function (aBrowser) {
194 if (uris.indexOf(aBrowser.currentURI.spec) > -1) {
195 gBrowser.removeTabsProgressListener(this);
196 firstLocationChange();
201 function firstLocationChange() {
202 let state = JSON.parse(ss.getBrowserState());
203 let hasUTV = state.windows[0].tabs.some(function(aTab) {
204 return aTab.userTypedValue && aTab.userTypedClear && !aTab.entries.length;
207 ok(hasUTV, "At least one tab has a userTypedValue with userTypedClear with no loaded URL");
209 gBrowser.addEventListener("load", firstLoad, true);
212 function firstLoad() {
213 gBrowser.removeEventListener("load", firstLoad, true);
215 let state = JSON.parse(ss.getBrowserState());
216 let hasSH = state.windows[0].tabs.some(function(aTab) {
217 return !("userTypedValue" in aTab) && aTab.entries[0].url;
220 ok(hasSH, "At least one tab has its entry in SH");
225 gBrowser.loadTabs(uris);
228 // This simulates setting a userTypedValue and ensures that just typing in the
229 // URL bar doesn't set userTypedClear as well.
230 function test_getBrowserState_userTypedValue() {
233 tabs: [{ entries: [] }]
237 waitForBrowserState(state, function() {
238 let browser = gBrowser.selectedBrowser;
239 // Make sure this tab isn't loading and state is clear before we test.
240 is(browser.userTypedValue, null, "userTypedValue is empty to start");
241 is(browser.userTypedClear, 0, "userTypedClear is 0 to start");
243 gURLBar.value = "example.org";
244 let event = document.createEvent("Events");
245 event.initEvent("input", true, false);
246 gURLBar.dispatchEvent(event);
248 executeSoon(function() {
249 is(browser.userTypedValue, "example.org",
250 "userTypedValue was set when changing gURLBar.value");
251 is(browser.userTypedClear, 0,
252 "userTypedClear was not changed when changing gURLBar.value");
254 // Now make sure ss gets these values too
255 let newState = JSON.parse(ss.getBrowserState());
256 is(newState.windows[0].tabs[0].userTypedValue, "example.org",
257 "sessionstore got correct userTypedValue");
258 is(newState.windows[0].tabs[0].userTypedClear, 0,
259 "sessionstore got correct userTypedClear");
265 // test_getBrowserState_lotsOfTabsOpening tested userTypedClear in a few cases,
266 // but not necessarily any that had legitimate URIs in the state of loading
267 // (eg, "http://example.com"), so this test will cover that case.
268 function test_userTypedClearLoadURI() {
272 { entries: [], userTypedValue: "http://example.com", userTypedClear: 2 }
277 waitForBrowserState(state, function() {
278 let browser = gBrowser.selectedBrowser;
279 is(browser.currentURI.spec, "http://example.com/",
280 "userTypedClear=2 caused userTypedValue to be loaded");
281 is(browser.userTypedValue, null,
282 "userTypedValue was null after loading a URI");
283 is(browser.userTypedClear, 0,
284 "userTypeClear reset to 0");
285 is(gURLBar.value, "http://example.com/",
286 "Address bar's value set after loading URI");
292 let tests = [test_newTabFocused, test_newTabNotFocused,
293 test_existingSHEnd_noClear, test_existingSHMiddle_noClear,
294 test_getBrowserState_lotsOfTabsOpening,
295 test_getBrowserState_userTypedValue, test_userTypedClearLoadURI];
296 let originalState = ss.getBrowserState();
299 tabs: [{ entries: [{ url: "about:blank" }] }]
302 function runNextTest() {
304 waitForBrowserState(state, tests.shift());
306 ss.setBrowserState(originalState);
307 executeSoon(function () {
308 is(browserWindowsCount(), 1, "Only one browser window should be open eventually");