browser/components/sessionstore/test/browser_522545.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_522545.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,265 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function test() {
     1.9 +  /** Test for Bug 522545 **/
    1.10 +
    1.11 +  waitForExplicitFinish();
    1.12 +  requestLongerTimeout(2);
    1.13 +
    1.14 +  // This tests the following use case:
    1.15 +  // User opens a new tab which gets focus. The user types something into the
    1.16 +  // address bar, then crashes or quits.
    1.17 +  function test_newTabFocused() {
    1.18 +    let state = {
    1.19 +      windows: [{
    1.20 +        tabs: [
    1.21 +          { entries: [{ url: "about:mozilla" }] },
    1.22 +          { entries: [], userTypedValue: "example.com", userTypedClear: 0 }
    1.23 +        ],
    1.24 +        selected: 2
    1.25 +      }]
    1.26 +    };
    1.27 +
    1.28 +    waitForBrowserState(state, function() {
    1.29 +      let browser = gBrowser.selectedBrowser;
    1.30 +      is(browser.currentURI.spec, "about:blank",
    1.31 +         "No history entries still sets currentURI to about:blank");
    1.32 +      is(browser.userTypedValue, "example.com",
    1.33 +         "userTypedValue was correctly restored");
    1.34 +      is(browser.userTypedClear, 0,
    1.35 +         "userTypeClear restored as expected");
    1.36 +      is(gURLBar.value, "example.com",
    1.37 +         "Address bar's value correctly restored");
    1.38 +      // Change tabs to make sure address bar value gets updated
    1.39 +      gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(0);
    1.40 +      is(gURLBar.value, "about:mozilla",
    1.41 +         "Address bar's value correctly updated");
    1.42 +      runNextTest();
    1.43 +    });
    1.44 +  }
    1.45 +
    1.46 +  // This tests the following use case:
    1.47 +  // User opens a new tab which gets focus. The user types something into the
    1.48 +  // address bar, switches back to the first tab, then crashes or quits.
    1.49 +  function test_newTabNotFocused() {
    1.50 +    let state = {
    1.51 +      windows: [{
    1.52 +        tabs: [
    1.53 +          { entries: [{ url: "about:mozilla" }] },
    1.54 +          { entries: [], userTypedValue: "example.org", userTypedClear: 0 }
    1.55 +        ],
    1.56 +        selected: 1
    1.57 +      }]
    1.58 +    };
    1.59 +
    1.60 +    waitForBrowserState(state, function() {
    1.61 +      let browser = gBrowser.getBrowserAtIndex(1);
    1.62 +      is(browser.currentURI.spec, "about:blank",
    1.63 +         "No history entries still sets currentURI to about:blank");
    1.64 +      is(browser.userTypedValue, "example.org",
    1.65 +         "userTypedValue was correctly restored");
    1.66 +      is(browser.userTypedClear, 0,
    1.67 +         "userTypeClear restored as expected");
    1.68 +      is(gURLBar.value, "about:mozilla",
    1.69 +         "Address bar's value correctly restored");
    1.70 +      // Change tabs to make sure address bar value gets updated
    1.71 +      gBrowser.selectedTab = gBrowser.tabContainer.getItemAtIndex(1);
    1.72 +      is(gURLBar.value, "example.org",
    1.73 +         "Address bar's value correctly updated");
    1.74 +      runNextTest();
    1.75 +    });
    1.76 +  }
    1.77 +
    1.78 +  // This tests the following use case:
    1.79 +  // User is in a tab with session history, then types something in the
    1.80 +  // address bar, then crashes or quits.
    1.81 +  function test_existingSHEnd_noClear() {
    1.82 +    let state = {
    1.83 +      windows: [{
    1.84 +        tabs: [{
    1.85 +          entries: [{ url: "about:mozilla" }, { url: "about:config" }],
    1.86 +          index: 2,
    1.87 +          userTypedValue: "example.com",
    1.88 +          userTypedClear: 0
    1.89 +        }]
    1.90 +      }]
    1.91 +    };
    1.92 +
    1.93 +    waitForBrowserState(state, function() {
    1.94 +      let browser = gBrowser.selectedBrowser;
    1.95 +      is(browser.currentURI.spec, "about:config",
    1.96 +         "browser.currentURI set to current entry in SH");
    1.97 +      is(browser.userTypedValue, "example.com",
    1.98 +         "userTypedValue was correctly restored");
    1.99 +      is(browser.userTypedClear, 0,
   1.100 +         "userTypeClear restored as expected");
   1.101 +      is(gURLBar.value, "example.com",
   1.102 +         "Address bar's value correctly restored to userTypedValue");
   1.103 +      runNextTest();
   1.104 +    });
   1.105 +  }
   1.106 +
   1.107 +  // This tests the following use case:
   1.108 +  // User is in a tab with session history, presses back at some point, then
   1.109 +  // types something in the address bar, then crashes or quits.
   1.110 +  function test_existingSHMiddle_noClear() {
   1.111 +    let state = {
   1.112 +      windows: [{
   1.113 +        tabs: [{
   1.114 +          entries: [{ url: "about:mozilla" }, { url: "about:config" }],
   1.115 +          index: 1,
   1.116 +          userTypedValue: "example.org",
   1.117 +          userTypedClear: 0
   1.118 +        }]
   1.119 +      }]
   1.120 +    };
   1.121 +
   1.122 +    waitForBrowserState(state, function() {
   1.123 +      let browser = gBrowser.selectedBrowser;
   1.124 +      is(browser.currentURI.spec, "about:mozilla",
   1.125 +         "browser.currentURI set to current entry in SH");
   1.126 +      is(browser.userTypedValue, "example.org",
   1.127 +         "userTypedValue was correctly restored");
   1.128 +      is(browser.userTypedClear, 0,
   1.129 +         "userTypeClear restored as expected");
   1.130 +      is(gURLBar.value, "example.org",
   1.131 +         "Address bar's value correctly restored to userTypedValue");
   1.132 +      runNextTest();
   1.133 +    });
   1.134 +  }
   1.135 +
   1.136 +  // This test simulates lots of tabs opening at once and then quitting/crashing.
   1.137 +  function test_getBrowserState_lotsOfTabsOpening() {
   1.138 +    gBrowser.stop();
   1.139 +
   1.140 +    let uris = [];
   1.141 +    for (let i = 0; i < 25; i++)
   1.142 +      uris.push("http://example.com/" + i);
   1.143 +
   1.144 +    // We're waiting for the first location change, which should indicate
   1.145 +    // one of the tabs has loaded and the others haven't. So one should
   1.146 +    // be in a non-userTypedValue case, while others should still have
   1.147 +    // userTypedValue and userTypedClear set.
   1.148 +    gBrowser.addTabsProgressListener({
   1.149 +      onLocationChange: function (aBrowser) {
   1.150 +        if (uris.indexOf(aBrowser.currentURI.spec) > -1) {
   1.151 +          gBrowser.removeTabsProgressListener(this);
   1.152 +          firstLocationChange();
   1.153 +        }
   1.154 +      }
   1.155 +    });
   1.156 +
   1.157 +    function firstLocationChange() {
   1.158 +      let state = JSON.parse(ss.getBrowserState());
   1.159 +      let hasUTV = state.windows[0].tabs.some(function(aTab) {
   1.160 +        return aTab.userTypedValue && aTab.userTypedClear && !aTab.entries.length;
   1.161 +      });
   1.162 +
   1.163 +      ok(hasUTV, "At least one tab has a userTypedValue with userTypedClear with no loaded URL");
   1.164 +
   1.165 +      gBrowser.addEventListener("load", firstLoad, true);
   1.166 +    }
   1.167 +
   1.168 +    function firstLoad() {
   1.169 +      gBrowser.removeEventListener("load", firstLoad, true);
   1.170 +
   1.171 +      let state = JSON.parse(ss.getBrowserState());
   1.172 +      let hasSH = state.windows[0].tabs.some(function(aTab) {
   1.173 +        return !("userTypedValue" in aTab) && aTab.entries[0].url;
   1.174 +      });
   1.175 +
   1.176 +      ok(hasSH, "At least one tab has its entry in SH");
   1.177 +
   1.178 +      runNextTest();
   1.179 +    }
   1.180 +
   1.181 +    gBrowser.loadTabs(uris);
   1.182 +  }
   1.183 +
   1.184 +  // This simulates setting a userTypedValue and ensures that just typing in the
   1.185 +  // URL bar doesn't set userTypedClear as well.
   1.186 +  function test_getBrowserState_userTypedValue() {
   1.187 +    let state = {
   1.188 +      windows: [{
   1.189 +        tabs: [{ entries: [] }]
   1.190 +      }]
   1.191 +    };
   1.192 +
   1.193 +    waitForBrowserState(state, function() {
   1.194 +      let browser = gBrowser.selectedBrowser;
   1.195 +      // Make sure this tab isn't loading and state is clear before we test.
   1.196 +      is(browser.userTypedValue, null, "userTypedValue is empty to start");
   1.197 +      is(browser.userTypedClear, 0, "userTypedClear is 0 to start");
   1.198 +
   1.199 +      gURLBar.value = "example.org";
   1.200 +      let event = document.createEvent("Events");
   1.201 +      event.initEvent("input", true, false);
   1.202 +      gURLBar.dispatchEvent(event);
   1.203 +
   1.204 +      executeSoon(function () {
   1.205 +        is(browser.userTypedValue, "example.org",
   1.206 +           "userTypedValue was set when changing gURLBar.value");
   1.207 +        is(browser.userTypedClear, 0,
   1.208 +           "userTypedClear was not changed when changing gURLBar.value");
   1.209 +
   1.210 +        // Now make sure ss gets these values too
   1.211 +        let newState = JSON.parse(ss.getBrowserState());
   1.212 +        is(newState.windows[0].tabs[0].userTypedValue, "example.org",
   1.213 +           "sessionstore got correct userTypedValue");
   1.214 +        is(newState.windows[0].tabs[0].userTypedClear, 0,
   1.215 +           "sessionstore got correct userTypedClear");
   1.216 +        runNextTest();
   1.217 +      });
   1.218 +    });
   1.219 +  }
   1.220 +
   1.221 +  // test_getBrowserState_lotsOfTabsOpening tested userTypedClear in a few cases,
   1.222 +  // but not necessarily any that had legitimate URIs in the state of loading
   1.223 +  // (eg, "http://example.com"), so this test will cover that case.
   1.224 +  function test_userTypedClearLoadURI() {
   1.225 +    let state = {
   1.226 +      windows: [{
   1.227 +        tabs: [
   1.228 +          { entries: [], userTypedValue: "http://example.com", userTypedClear: 2 }
   1.229 +        ]
   1.230 +      }]
   1.231 +    };
   1.232 +
   1.233 +    waitForBrowserState(state, function() {
   1.234 +      let browser = gBrowser.selectedBrowser;
   1.235 +      is(browser.currentURI.spec, "http://example.com/",
   1.236 +         "userTypedClear=2 caused userTypedValue to be loaded");
   1.237 +      is(browser.userTypedValue, null,
   1.238 +         "userTypedValue was null after loading a URI");
   1.239 +      is(browser.userTypedClear, 0,
   1.240 +         "userTypeClear reset to 0");
   1.241 +      is(gURLBar.value, gURLBar.trimValue("http://example.com/"),
   1.242 +         "Address bar's value set after loading URI");
   1.243 +      runNextTest();
   1.244 +    });
   1.245 +  }
   1.246 +
   1.247 +
   1.248 +  let tests = [test_newTabFocused, test_newTabNotFocused,
   1.249 +               test_existingSHEnd_noClear, test_existingSHMiddle_noClear,
   1.250 +               test_getBrowserState_lotsOfTabsOpening,
   1.251 +               test_getBrowserState_userTypedValue, test_userTypedClearLoadURI];
   1.252 +  let originalState = JSON.parse(ss.getBrowserState());
   1.253 +  let state = {
   1.254 +    windows: [{
   1.255 +      tabs: [{ entries: [{ url: "about:blank" }] }]
   1.256 +    }]
   1.257 +  };
   1.258 +  function runNextTest() {
   1.259 +    if (tests.length) {
   1.260 +      waitForBrowserState(state, tests.shift());
   1.261 +    } else {
   1.262 +      waitForBrowserState(originalState, finish);
   1.263 +    }
   1.264 +  }
   1.265 +
   1.266 +  // Run the tests!
   1.267 +  runNextTest();
   1.268 +}

mercurial