browser/components/sessionstore/test/browser_522545.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:9a50e62b1646
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 function test() {
6 /** Test for Bug 522545 **/
7
8 waitForExplicitFinish();
9 requestLongerTimeout(2);
10
11 // This tests the following use case:
12 // User opens a new tab which gets focus. The user types something into the
13 // address bar, then crashes or quits.
14 function test_newTabFocused() {
15 let state = {
16 windows: [{
17 tabs: [
18 { entries: [{ url: "about:mozilla" }] },
19 { entries: [], userTypedValue: "example.com", userTypedClear: 0 }
20 ],
21 selected: 2
22 }]
23 };
24
25 waitForBrowserState(state, function() {
26 let browser = gBrowser.selectedBrowser;
27 is(browser.currentURI.spec, "about:blank",
28 "No history entries still sets currentURI to about:blank");
29 is(browser.userTypedValue, "example.com",
30 "userTypedValue was correctly restored");
31 is(browser.userTypedClear, 0,
32 "userTypeClear restored as expected");
33 is(gURLBar.value, "example.com",
34 "Address bar's value correctly restored");
35 // Change tabs to make sure address bar value gets updated
36 gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(0);
37 is(gURLBar.value, "about:mozilla",
38 "Address bar's value correctly updated");
39 runNextTest();
40 });
41 }
42
43 // This tests the following use case:
44 // User opens a new tab which gets focus. The user types something into the
45 // address bar, switches back to the first tab, then crashes or quits.
46 function test_newTabNotFocused() {
47 let state = {
48 windows: [{
49 tabs: [
50 { entries: [{ url: "about:mozilla" }] },
51 { entries: [], userTypedValue: "example.org", userTypedClear: 0 }
52 ],
53 selected: 1
54 }]
55 };
56
57 waitForBrowserState(state, function() {
58 let browser = gBrowser.getBrowserAtIndex(1);
59 is(browser.currentURI.spec, "about:blank",
60 "No history entries still sets currentURI to about:blank");
61 is(browser.userTypedValue, "example.org",
62 "userTypedValue was correctly restored");
63 is(browser.userTypedClear, 0,
64 "userTypeClear restored as expected");
65 is(gURLBar.value, "about:mozilla",
66 "Address bar's value correctly restored");
67 // Change tabs to make sure address bar value gets updated
68 gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(1);
69 is(gURLBar.value, "example.org",
70 "Address bar's value correctly updated");
71 runNextTest();
72 });
73 }
74
75 // This tests the following use case:
76 // User is in a tab with session history, then types something in the
77 // address bar, then crashes or quits.
78 function test_existingSHEnd_noClear() {
79 let state = {
80 windows: [{
81 tabs: [{
82 entries: [{ url: "about:mozilla" }, { url: "about:config" }],
83 index: 2,
84 userTypedValue: "example.com",
85 userTypedClear: 0
86 }]
87 }]
88 };
89
90 waitForBrowserState(state, function() {
91 let browser = gBrowser.selectedBrowser;
92 is(browser.currentURI.spec, "about:config",
93 "browser.currentURI set to current entry in SH");
94 is(browser.userTypedValue, "example.com",
95 "userTypedValue was correctly restored");
96 is(browser.userTypedClear, 0,
97 "userTypeClear restored as expected");
98 is(gURLBar.value, "example.com",
99 "Address bar's value correctly restored to userTypedValue");
100 runNextTest();
101 });
102 }
103
104 // This tests the following use case:
105 // User is in a tab with session history, presses back at some point, then
106 // types something in the address bar, then crashes or quits.
107 function test_existingSHMiddle_noClear() {
108 let state = {
109 windows: [{
110 tabs: [{
111 entries: [{ url: "about:mozilla" }, { url: "about:config" }],
112 index: 1,
113 userTypedValue: "example.org",
114 userTypedClear: 0
115 }]
116 }]
117 };
118
119 waitForBrowserState(state, function() {
120 let browser = gBrowser.selectedBrowser;
121 is(browser.currentURI.spec, "about:mozilla",
122 "browser.currentURI set to current entry in SH");
123 is(browser.userTypedValue, "example.org",
124 "userTypedValue was correctly restored");
125 is(browser.userTypedClear, 0,
126 "userTypeClear restored as expected");
127 is(gURLBar.value, "example.org",
128 "Address bar's value correctly restored to userTypedValue");
129 runNextTest();
130 });
131 }
132
133 // This test simulates lots of tabs opening at once and then quitting/crashing.
134 function test_getBrowserState_lotsOfTabsOpening() {
135 gBrowser.stop();
136
137 let uris = [];
138 for (let i = 0; i < 25; i++)
139 uris.push("http://example.com/" + i);
140
141 // We're waiting for the first location change, which should indicate
142 // one of the tabs has loaded and the others haven't. So one should
143 // be in a non-userTypedValue case, while others should still have
144 // userTypedValue and userTypedClear set.
145 gBrowser.addTabsProgressListener({
146 onLocationChange: function (aBrowser) {
147 if (uris.indexOf(aBrowser.currentURI.spec) > -1) {
148 gBrowser.removeTabsProgressListener(this);
149 firstLocationChange();
150 }
151 }
152 });
153
154 function firstLocationChange() {
155 let state = JSON.parse(ss.getBrowserState());
156 let hasUTV = state.windows[0].tabs.some(function(aTab) {
157 return aTab.userTypedValue && aTab.userTypedClear && !aTab.entries.length;
158 });
159
160 ok(hasUTV, "At least one tab has a userTypedValue with userTypedClear with no loaded URL");
161
162 gBrowser.addEventListener("load", firstLoad, true);
163 }
164
165 function firstLoad() {
166 gBrowser.removeEventListener("load", firstLoad, true);
167
168 let state = JSON.parse(ss.getBrowserState());
169 let hasSH = state.windows[0].tabs.some(function(aTab) {
170 return !("userTypedValue" in aTab) && aTab.entries[0].url;
171 });
172
173 ok(hasSH, "At least one tab has its entry in SH");
174
175 runNextTest();
176 }
177
178 gBrowser.loadTabs(uris);
179 }
180
181 // This simulates setting a userTypedValue and ensures that just typing in the
182 // URL bar doesn't set userTypedClear as well.
183 function test_getBrowserState_userTypedValue() {
184 let state = {
185 windows: [{
186 tabs: [{ entries: [] }]
187 }]
188 };
189
190 waitForBrowserState(state, function() {
191 let browser = gBrowser.selectedBrowser;
192 // Make sure this tab isn't loading and state is clear before we test.
193 is(browser.userTypedValue, null, "userTypedValue is empty to start");
194 is(browser.userTypedClear, 0, "userTypedClear is 0 to start");
195
196 gURLBar.value = "example.org";
197 let event = document.createEvent("Events");
198 event.initEvent("input", true, false);
199 gURLBar.dispatchEvent(event);
200
201 executeSoon(function () {
202 is(browser.userTypedValue, "example.org",
203 "userTypedValue was set when changing gURLBar.value");
204 is(browser.userTypedClear, 0,
205 "userTypedClear was not changed when changing gURLBar.value");
206
207 // Now make sure ss gets these values too
208 let newState = JSON.parse(ss.getBrowserState());
209 is(newState.windows[0].tabs[0].userTypedValue, "example.org",
210 "sessionstore got correct userTypedValue");
211 is(newState.windows[0].tabs[0].userTypedClear, 0,
212 "sessionstore got correct userTypedClear");
213 runNextTest();
214 });
215 });
216 }
217
218 // test_getBrowserState_lotsOfTabsOpening tested userTypedClear in a few cases,
219 // but not necessarily any that had legitimate URIs in the state of loading
220 // (eg, "http://example.com"), so this test will cover that case.
221 function test_userTypedClearLoadURI() {
222 let state = {
223 windows: [{
224 tabs: [
225 { entries: [], userTypedValue: "http://example.com", userTypedClear: 2 }
226 ]
227 }]
228 };
229
230 waitForBrowserState(state, function() {
231 let browser = gBrowser.selectedBrowser;
232 is(browser.currentURI.spec, "http://example.com/",
233 "userTypedClear=2 caused userTypedValue to be loaded");
234 is(browser.userTypedValue, null,
235 "userTypedValue was null after loading a URI");
236 is(browser.userTypedClear, 0,
237 "userTypeClear reset to 0");
238 is(gURLBar.value, gURLBar.trimValue("http://example.com/"),
239 "Address bar's value set after loading URI");
240 runNextTest();
241 });
242 }
243
244
245 let tests = [test_newTabFocused, test_newTabNotFocused,
246 test_existingSHEnd_noClear, test_existingSHMiddle_noClear,
247 test_getBrowserState_lotsOfTabsOpening,
248 test_getBrowserState_userTypedValue, test_userTypedClearLoadURI];
249 let originalState = JSON.parse(ss.getBrowserState());
250 let state = {
251 windows: [{
252 tabs: [{ entries: [{ url: "about:blank" }] }]
253 }]
254 };
255 function runNextTest() {
256 if (tests.length) {
257 waitForBrowserState(state, tests.shift());
258 } else {
259 waitForBrowserState(originalState, finish);
260 }
261 }
262
263 // Run the tests!
264 runNextTest();
265 }

mercurial