Bug 597634 - Reset to a blank state for each test instead of relying on previous...
[mozilla-central.git] / browser / components / sessionstore / test / browser / browser_522545.js
blob0b56969625c863ff047eb3546a4298367ce488fc
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 ***** */
38 function browserWindowsCount() {
39   let count = 0;
40   let e = Services.wm.getEnumerator("navigator:browser");
41   while (e.hasMoreElements()) {
42     if (!e.getNext().closed)
43       ++count;
44   }
45   return count;
48 function test() {
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() {
62     let state = {
63       windows: [{
64         tabs: [
65           { entries: [{ url: "about:mozilla" }] },
66           { entries: [], userTypedValue: "example.com", userTypedClear: 0 }
67         ],
68         selected: 2
69       }]
70     };
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");
86       runNextTest();
87     });
88   }
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() {
94     let state = {
95       windows: [{
96         tabs: [
97           { entries: [{ url: "about:mozilla" }] },
98           { entries: [], userTypedValue: "example.org", userTypedClear: 0 }
99         ],
100         selected: 1
101       }]
102     };
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");
118       runNextTest();
119     });
120   }
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() {
126     let state = {
127       windows: [{
128         tabs: [{
129           entries: [{ url: "about:mozilla" }, { url: "about:config" }],
130           index: 2,
131           userTypedValue: "example.com",
132           userTypedClear: 0
133         }]
134       }]
135     };
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");
147       runNextTest();
148     });
149   }
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() {
155     let state = {
156       windows: [{
157         tabs: [{
158           entries: [{ url: "about:mozilla" }, { url: "about:config" }],
159           index: 1,
160           userTypedValue: "example.org",
161           userTypedClear: 0
162         }]
163       }]
164     };
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");
176       runNextTest();
177     });
178   }
180   // This test simulates lots of tabs opening at once and then quitting/crashing.
181   function test_getBrowserState_lotsOfTabsOpening() {
182     gBrowser.stop();
184     let uris = [];
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();
197         }
198       }
199     });
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;
205       });
207       ok(hasUTV, "At least one tab has a userTypedValue with userTypedClear with no loaded URL");
209       gBrowser.addEventListener("load", firstLoad, true);
210     }
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;
218       });
220       ok(hasSH, "At least one tab has its entry in SH");
222       runNextTest();
223     }
225     gBrowser.loadTabs(uris);
226   }
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() {
231     let state = {
232       windows: [{
233         tabs: [{ entries: [] }]
234       }]
235     };
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");
260         runNextTest();
261       });
262     });
263   }
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() {
269     let state = {
270       windows: [{
271         tabs: [
272           { entries: [], userTypedValue: "http://example.com", userTypedClear: 2 }
273         ]
274       }]
275     };
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");
287       runNextTest();
288     });
289   }
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();
297   let state = {
298     windows: [{
299       tabs: [{ entries: [{ url: "about:blank" }] }]
300     }]
301   };
302   function runNextTest() {
303     if (tests.length) {
304       waitForBrowserState(state, tests.shift());
305     } else {
306       ss.setBrowserState(originalState);
307       executeSoon(function () {
308         is(browserWindowsCount(), 1, "Only one browser window should be open eventually");
309         finish();
310       });
311     }
312   }
314   // Run the tests!
315   runNextTest();