michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function test() { michael@0: /** Test for Bug 522545 **/ michael@0: michael@0: waitForExplicitFinish(); michael@0: requestLongerTimeout(2); michael@0: michael@0: // This tests the following use case: michael@0: // User opens a new tab which gets focus. The user types something into the michael@0: // address bar, then crashes or quits. michael@0: function test_newTabFocused() { michael@0: let state = { michael@0: windows: [{ michael@0: tabs: [ michael@0: { entries: [{ url: "about:mozilla" }] }, michael@0: { entries: [], userTypedValue: "example.com", userTypedClear: 0 } michael@0: ], michael@0: selected: 2 michael@0: }] michael@0: }; michael@0: michael@0: waitForBrowserState(state, function() { michael@0: let browser = gBrowser.selectedBrowser; michael@0: is(browser.currentURI.spec, "about:blank", michael@0: "No history entries still sets currentURI to about:blank"); michael@0: is(browser.userTypedValue, "example.com", michael@0: "userTypedValue was correctly restored"); michael@0: is(browser.userTypedClear, 0, michael@0: "userTypeClear restored as expected"); michael@0: is(gURLBar.value, "example.com", michael@0: "Address bar's value correctly restored"); michael@0: // Change tabs to make sure address bar value gets updated michael@0: gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(0); michael@0: is(gURLBar.value, "about:mozilla", michael@0: "Address bar's value correctly updated"); michael@0: runNextTest(); michael@0: }); michael@0: } michael@0: michael@0: // This tests the following use case: michael@0: // User opens a new tab which gets focus. The user types something into the michael@0: // address bar, switches back to the first tab, then crashes or quits. michael@0: function test_newTabNotFocused() { michael@0: let state = { michael@0: windows: [{ michael@0: tabs: [ michael@0: { entries: [{ url: "about:mozilla" }] }, michael@0: { entries: [], userTypedValue: "example.org", userTypedClear: 0 } michael@0: ], michael@0: selected: 1 michael@0: }] michael@0: }; michael@0: michael@0: waitForBrowserState(state, function() { michael@0: let browser = gBrowser.getBrowserAtIndex(1); michael@0: is(browser.currentURI.spec, "about:blank", michael@0: "No history entries still sets currentURI to about:blank"); michael@0: is(browser.userTypedValue, "example.org", michael@0: "userTypedValue was correctly restored"); michael@0: is(browser.userTypedClear, 0, michael@0: "userTypeClear restored as expected"); michael@0: is(gURLBar.value, "about:mozilla", michael@0: "Address bar's value correctly restored"); michael@0: // Change tabs to make sure address bar value gets updated michael@0: gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(1); michael@0: is(gURLBar.value, "example.org", michael@0: "Address bar's value correctly updated"); michael@0: runNextTest(); michael@0: }); michael@0: } michael@0: michael@0: // This tests the following use case: michael@0: // User is in a tab with session history, then types something in the michael@0: // address bar, then crashes or quits. michael@0: function test_existingSHEnd_noClear() { michael@0: let state = { michael@0: windows: [{ michael@0: tabs: [{ michael@0: entries: [{ url: "about:mozilla" }, { url: "about:config" }], michael@0: index: 2, michael@0: userTypedValue: "example.com", michael@0: userTypedClear: 0 michael@0: }] michael@0: }] michael@0: }; michael@0: michael@0: waitForBrowserState(state, function() { michael@0: let browser = gBrowser.selectedBrowser; michael@0: is(browser.currentURI.spec, "about:config", michael@0: "browser.currentURI set to current entry in SH"); michael@0: is(browser.userTypedValue, "example.com", michael@0: "userTypedValue was correctly restored"); michael@0: is(browser.userTypedClear, 0, michael@0: "userTypeClear restored as expected"); michael@0: is(gURLBar.value, "example.com", michael@0: "Address bar's value correctly restored to userTypedValue"); michael@0: runNextTest(); michael@0: }); michael@0: } michael@0: michael@0: // This tests the following use case: michael@0: // User is in a tab with session history, presses back at some point, then michael@0: // types something in the address bar, then crashes or quits. michael@0: function test_existingSHMiddle_noClear() { michael@0: let state = { michael@0: windows: [{ michael@0: tabs: [{ michael@0: entries: [{ url: "about:mozilla" }, { url: "about:config" }], michael@0: index: 1, michael@0: userTypedValue: "example.org", michael@0: userTypedClear: 0 michael@0: }] michael@0: }] michael@0: }; michael@0: michael@0: waitForBrowserState(state, function() { michael@0: let browser = gBrowser.selectedBrowser; michael@0: is(browser.currentURI.spec, "about:mozilla", michael@0: "browser.currentURI set to current entry in SH"); michael@0: is(browser.userTypedValue, "example.org", michael@0: "userTypedValue was correctly restored"); michael@0: is(browser.userTypedClear, 0, michael@0: "userTypeClear restored as expected"); michael@0: is(gURLBar.value, "example.org", michael@0: "Address bar's value correctly restored to userTypedValue"); michael@0: runNextTest(); michael@0: }); michael@0: } michael@0: michael@0: // This test simulates lots of tabs opening at once and then quitting/crashing. michael@0: function test_getBrowserState_lotsOfTabsOpening() { michael@0: gBrowser.stop(); michael@0: michael@0: let uris = []; michael@0: for (let i = 0; i < 25; i++) michael@0: uris.push("http://example.com/" + i); michael@0: michael@0: // We're waiting for the first location change, which should indicate michael@0: // one of the tabs has loaded and the others haven't. So one should michael@0: // be in a non-userTypedValue case, while others should still have michael@0: // userTypedValue and userTypedClear set. michael@0: gBrowser.addTabsProgressListener({ michael@0: onLocationChange: function (aBrowser) { michael@0: if (uris.indexOf(aBrowser.currentURI.spec) > -1) { michael@0: gBrowser.removeTabsProgressListener(this); michael@0: firstLocationChange(); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: function firstLocationChange() { michael@0: let state = JSON.parse(ss.getBrowserState()); michael@0: let hasUTV = state.windows[0].tabs.some(function(aTab) { michael@0: return aTab.userTypedValue && aTab.userTypedClear && !aTab.entries.length; michael@0: }); michael@0: michael@0: ok(hasUTV, "At least one tab has a userTypedValue with userTypedClear with no loaded URL"); michael@0: michael@0: gBrowser.addEventListener("load", firstLoad, true); michael@0: } michael@0: michael@0: function firstLoad() { michael@0: gBrowser.removeEventListener("load", firstLoad, true); michael@0: michael@0: let state = JSON.parse(ss.getBrowserState()); michael@0: let hasSH = state.windows[0].tabs.some(function(aTab) { michael@0: return !("userTypedValue" in aTab) && aTab.entries[0].url; michael@0: }); michael@0: michael@0: ok(hasSH, "At least one tab has its entry in SH"); michael@0: michael@0: runNextTest(); michael@0: } michael@0: michael@0: gBrowser.loadTabs(uris); michael@0: } michael@0: michael@0: // This simulates setting a userTypedValue and ensures that just typing in the michael@0: // URL bar doesn't set userTypedClear as well. michael@0: function test_getBrowserState_userTypedValue() { michael@0: let state = { michael@0: windows: [{ michael@0: tabs: [{ entries: [] }] michael@0: }] michael@0: }; michael@0: michael@0: waitForBrowserState(state, function() { michael@0: let browser = gBrowser.selectedBrowser; michael@0: // Make sure this tab isn't loading and state is clear before we test. michael@0: is(browser.userTypedValue, null, "userTypedValue is empty to start"); michael@0: is(browser.userTypedClear, 0, "userTypedClear is 0 to start"); michael@0: michael@0: gURLBar.value = "example.org"; michael@0: let event = document.createEvent("Events"); michael@0: event.initEvent("input", true, false); michael@0: gURLBar.dispatchEvent(event); michael@0: michael@0: executeSoon(function () { michael@0: is(browser.userTypedValue, "example.org", michael@0: "userTypedValue was set when changing gURLBar.value"); michael@0: is(browser.userTypedClear, 0, michael@0: "userTypedClear was not changed when changing gURLBar.value"); michael@0: michael@0: // Now make sure ss gets these values too michael@0: let newState = JSON.parse(ss.getBrowserState()); michael@0: is(newState.windows[0].tabs[0].userTypedValue, "example.org", michael@0: "sessionstore got correct userTypedValue"); michael@0: is(newState.windows[0].tabs[0].userTypedClear, 0, michael@0: "sessionstore got correct userTypedClear"); michael@0: runNextTest(); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: // test_getBrowserState_lotsOfTabsOpening tested userTypedClear in a few cases, michael@0: // but not necessarily any that had legitimate URIs in the state of loading michael@0: // (eg, "http://example.com"), so this test will cover that case. michael@0: function test_userTypedClearLoadURI() { michael@0: let state = { michael@0: windows: [{ michael@0: tabs: [ michael@0: { entries: [], userTypedValue: "http://example.com", userTypedClear: 2 } michael@0: ] michael@0: }] michael@0: }; michael@0: michael@0: waitForBrowserState(state, function() { michael@0: let browser = gBrowser.selectedBrowser; michael@0: is(browser.currentURI.spec, "http://example.com/", michael@0: "userTypedClear=2 caused userTypedValue to be loaded"); michael@0: is(browser.userTypedValue, null, michael@0: "userTypedValue was null after loading a URI"); michael@0: is(browser.userTypedClear, 0, michael@0: "userTypeClear reset to 0"); michael@0: is(gURLBar.value, gURLBar.trimValue("http://example.com/"), michael@0: "Address bar's value set after loading URI"); michael@0: runNextTest(); michael@0: }); michael@0: } michael@0: michael@0: michael@0: let tests = [test_newTabFocused, test_newTabNotFocused, michael@0: test_existingSHEnd_noClear, test_existingSHMiddle_noClear, michael@0: test_getBrowserState_lotsOfTabsOpening, michael@0: test_getBrowserState_userTypedValue, test_userTypedClearLoadURI]; michael@0: let originalState = JSON.parse(ss.getBrowserState()); michael@0: let state = { michael@0: windows: [{ michael@0: tabs: [{ entries: [{ url: "about:blank" }] }] michael@0: }] michael@0: }; michael@0: function runNextTest() { michael@0: if (tests.length) { michael@0: waitForBrowserState(state, tests.shift()); michael@0: } else { michael@0: waitForBrowserState(originalState, finish); michael@0: } michael@0: } michael@0: michael@0: // Run the tests! michael@0: runNextTest(); michael@0: }