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