tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_522545.js (12714B)


      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(4);
     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        {
     18          tabs: [
     19            { entries: [{ url: "about:mozilla", triggeringPrincipal_base64 }] },
     20            { entries: [], userTypedValue: "example.com", userTypedClear: 0 },
     21          ],
     22          selected: 2,
     23        },
     24      ],
     25    };
     26 
     27    waitForBrowserState(state, function () {
     28      let browser = gBrowser.selectedBrowser;
     29      is(
     30        browser.currentURI.spec,
     31        "about:blank",
     32        "No history entries still sets currentURI to about:blank"
     33      );
     34      is(
     35        browser.userTypedValue,
     36        "example.com",
     37        "userTypedValue was correctly restored"
     38      );
     39      ok(
     40        !browser.didStartLoadSinceLastUserTyping(),
     41        "We still know that no load is ongoing"
     42      );
     43      is(
     44        gURLBar.value,
     45        "example.com",
     46        "Address bar's value correctly restored"
     47      );
     48 
     49      // Change tabs to make sure address bar value gets updated.  If tab is
     50      // lazy, wait for SSTabRestored to ensure address bar has time to update.
     51      let tabToSelect = gBrowser.tabContainer.getItemAtIndex(0);
     52      if (tabToSelect.linkedBrowser.isConnected) {
     53        gBrowser.selectedTab = tabToSelect;
     54        is(
     55          gURLBar.value,
     56          "about:mozilla",
     57          "Address bar's value correctly updated"
     58        );
     59        runNextTest();
     60      } else {
     61        gBrowser.tabContainer.addEventListener(
     62          "SSTabRestored",
     63          function SSTabRestored(event) {
     64            if (event.target == tabToSelect) {
     65              gBrowser.tabContainer.removeEventListener(
     66                "SSTabRestored",
     67                SSTabRestored,
     68                true
     69              );
     70              is(
     71                gURLBar.value,
     72                "about:mozilla",
     73                "Address bar's value correctly updated"
     74              );
     75              runNextTest();
     76            }
     77          },
     78          true
     79        );
     80        gBrowser.selectedTab = tabToSelect;
     81      }
     82    });
     83  }
     84 
     85  // This tests the following use case:
     86  // User opens a new tab which gets focus. The user types something into the
     87  // address bar, switches back to the first tab, then crashes or quits.
     88  function test_newTabNotFocused() {
     89    let state = {
     90      windows: [
     91        {
     92          tabs: [
     93            { entries: [{ url: "about:mozilla", triggeringPrincipal_base64 }] },
     94            { entries: [], userTypedValue: "example.org", userTypedClear: 0 },
     95          ],
     96          selected: 1,
     97        },
     98      ],
     99    };
    100 
    101    waitForBrowserState(state, function () {
    102      let browser = gBrowser.getBrowserAtIndex(1);
    103      is(
    104        browser.currentURI.spec,
    105        "about:blank",
    106        "No history entries still sets currentURI to about:blank"
    107      );
    108      is(
    109        browser.userTypedValue,
    110        "example.org",
    111        "userTypedValue was correctly restored"
    112      );
    113      // didStartLoadSinceLastUserTyping does not exist on lazy tabs.
    114      if (browser.didStartLoadSinceLastUserTyping) {
    115        ok(
    116          !browser.didStartLoadSinceLastUserTyping(),
    117          "We still know that no load is ongoing"
    118        );
    119      }
    120      is(
    121        gURLBar.value,
    122        "about:mozilla",
    123        "Address bar's value correctly restored"
    124      );
    125 
    126      // Change tabs to make sure address bar value gets updated.  If tab is
    127      // lazy, wait for SSTabRestored to ensure address bar has time to update.
    128      let tabToSelect = gBrowser.tabContainer.getItemAtIndex(1);
    129      if (tabToSelect.linkedBrowser.isConnected) {
    130        gBrowser.selectedTab = tabToSelect;
    131        is(
    132          gURLBar.value,
    133          "example.org",
    134          "Address bar's value correctly updated"
    135        );
    136        runNextTest();
    137      } else {
    138        gBrowser.tabContainer.addEventListener(
    139          "SSTabRestored",
    140          function SSTabRestored(event) {
    141            if (event.target == tabToSelect) {
    142              gBrowser.tabContainer.removeEventListener(
    143                "SSTabRestored",
    144                SSTabRestored,
    145                true
    146              );
    147              is(
    148                gURLBar.value,
    149                "example.org",
    150                "Address bar's value correctly updated"
    151              );
    152              runNextTest();
    153            }
    154          },
    155          true
    156        );
    157        gBrowser.selectedTab = tabToSelect;
    158      }
    159    });
    160  }
    161 
    162  // This tests the following use case:
    163  // User is in a tab with session history, then types something in the
    164  // address bar, then crashes or quits.
    165  function test_existingSHEnd_noClear() {
    166    let state = {
    167      windows: [
    168        {
    169          tabs: [
    170            {
    171              entries: [
    172                { url: "about:mozilla", triggeringPrincipal_base64 },
    173                { url: "about:config", triggeringPrincipal_base64 },
    174              ],
    175              index: 2,
    176              userTypedValue: "example.com",
    177              userTypedClear: 0,
    178            },
    179          ],
    180        },
    181      ],
    182    };
    183 
    184    waitForBrowserState(state, function () {
    185      let browser = gBrowser.selectedBrowser;
    186      is(
    187        browser.currentURI.spec,
    188        "about:config",
    189        "browser.currentURI set to current entry in SH"
    190      );
    191      is(
    192        browser.userTypedValue,
    193        "example.com",
    194        "userTypedValue was correctly restored"
    195      );
    196      ok(
    197        !browser.didStartLoadSinceLastUserTyping(),
    198        "We still know that no load is ongoing"
    199      );
    200      is(
    201        gURLBar.value,
    202        "example.com",
    203        "Address bar's value correctly restored to userTypedValue"
    204      );
    205      runNextTest();
    206    });
    207  }
    208 
    209  // This tests the following use case:
    210  // User is in a tab with session history, presses back at some point, then
    211  // types something in the address bar, then crashes or quits.
    212  function test_existingSHMiddle_noClear() {
    213    let state = {
    214      windows: [
    215        {
    216          tabs: [
    217            {
    218              entries: [
    219                { url: "about:mozilla", triggeringPrincipal_base64 },
    220                { url: "about:config", triggeringPrincipal_base64 },
    221              ],
    222              index: 1,
    223              userTypedValue: "example.org",
    224              userTypedClear: 0,
    225            },
    226          ],
    227        },
    228      ],
    229    };
    230 
    231    waitForBrowserState(state, function () {
    232      let browser = gBrowser.selectedBrowser;
    233      is(
    234        browser.currentURI.spec,
    235        "about:mozilla",
    236        "browser.currentURI set to current entry in SH"
    237      );
    238      is(
    239        browser.userTypedValue,
    240        "example.org",
    241        "userTypedValue was correctly restored"
    242      );
    243      ok(
    244        !browser.didStartLoadSinceLastUserTyping(),
    245        "We still know that no load is ongoing"
    246      );
    247      is(
    248        gURLBar.value,
    249        "example.org",
    250        "Address bar's value correctly restored to userTypedValue"
    251      );
    252      runNextTest();
    253    });
    254  }
    255 
    256  // This test simulates lots of tabs opening at once and then quitting/crashing.
    257  function test_getBrowserState_lotsOfTabsOpening() {
    258    gBrowser.stop();
    259 
    260    let uris = [];
    261    for (let i = 0; i < 25; i++) {
    262      uris.push("http://example.com/" + i);
    263    }
    264 
    265    // We're waiting for the first location change, which should indicate
    266    // one of the tabs has loaded and the others haven't. So one should
    267    // be in a non-userTypedValue case, while others should still have
    268    // userTypedValue and userTypedClear set.
    269    gBrowser.addTabsProgressListener({
    270      onLocationChange(aBrowser) {
    271        if (uris.indexOf(aBrowser.currentURI.spec) > -1) {
    272          gBrowser.removeTabsProgressListener(this);
    273          firstLocationChange();
    274        }
    275      },
    276    });
    277 
    278    function firstLocationChange() {
    279      let state = JSON.parse(ss.getBrowserState());
    280      let hasUTV = state.windows[0].tabs.some(function (aTab) {
    281        return (
    282          aTab.userTypedValue && aTab.userTypedClear && !aTab.entries.length
    283        );
    284      });
    285 
    286      ok(
    287        hasUTV,
    288        "At least one tab has a userTypedValue with userTypedClear with no loaded URL"
    289      );
    290 
    291      BrowserTestUtils.waitForMessage(
    292        gBrowser.selectedBrowser.messageManager,
    293        "SessionStore:update"
    294      ).then(firstLoad);
    295    }
    296 
    297    function firstLoad() {
    298      let state = JSON.parse(ss.getTabState(gBrowser.selectedTab));
    299      let hasSH = !("userTypedValue" in state) && state.entries[0].url;
    300      ok(hasSH, "The selected tab has its entry in SH");
    301 
    302      runNextTest();
    303    }
    304 
    305    gBrowser.loadTabs(uris, {
    306      triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
    307    });
    308  }
    309 
    310  // This simulates setting a userTypedValue and ensures that just typing in the
    311  // URL bar doesn't set userTypedClear as well.
    312  function test_getBrowserState_userTypedValue() {
    313    let state = {
    314      windows: [
    315        {
    316          tabs: [{ entries: [] }],
    317        },
    318      ],
    319    };
    320 
    321    waitForBrowserState(state, function () {
    322      let browser = gBrowser.selectedBrowser;
    323      // Make sure this tab isn't loading and state is clear before we test.
    324      is(browser.userTypedValue, null, "userTypedValue is empty to start");
    325      ok(
    326        !browser.didStartLoadSinceLastUserTyping(),
    327        "Initially, no load should be ongoing"
    328      );
    329 
    330      let inputText = "example.org";
    331      gURLBar.focus();
    332      gURLBar.value = inputText.slice(0, -1);
    333      EventUtils.sendString(inputText.slice(-1));
    334 
    335      executeSoon(function () {
    336        is(
    337          browser.userTypedValue,
    338          "example.org",
    339          "userTypedValue was set when changing URLBar value"
    340        );
    341        ok(
    342          !browser.didStartLoadSinceLastUserTyping(),
    343          "No load started since changing URLBar value"
    344        );
    345 
    346        // Now make sure ss gets these values too
    347        let newState = JSON.parse(ss.getBrowserState());
    348        is(
    349          newState.windows[0].tabs[0].userTypedValue,
    350          "example.org",
    351          "sessionstore got correct userTypedValue"
    352        );
    353        is(
    354          newState.windows[0].tabs[0].userTypedClear,
    355          0,
    356          "sessionstore got correct userTypedClear"
    357        );
    358        runNextTest();
    359      });
    360    });
    361  }
    362 
    363  // test_getBrowserState_lotsOfTabsOpening tested userTypedClear in a few cases,
    364  // but not necessarily any that had legitimate URIs in the state of loading
    365  // (eg, "http://example.com"), so this test will cover that case.
    366  function test_userTypedClearLoadURI() {
    367    let state = {
    368      windows: [
    369        {
    370          tabs: [
    371            {
    372              entries: [],
    373              userTypedValue: "http://example.com",
    374              userTypedClear: 2,
    375            },
    376          ],
    377        },
    378      ],
    379    };
    380 
    381    waitForBrowserState(state, function () {
    382      let browser = gBrowser.selectedBrowser;
    383      is(
    384        browser.currentURI.spec,
    385        "http://example.com/",
    386        "userTypedClear=2 caused userTypedValue to be loaded"
    387      );
    388      is(
    389        browser.userTypedValue,
    390        null,
    391        "userTypedValue was null after loading a URI"
    392      );
    393      ok(
    394        !browser.didStartLoadSinceLastUserTyping(),
    395        "We should have reset the load state when the tab loaded"
    396      );
    397      is(
    398        gURLBar.value,
    399        BrowserUIUtils.trimURL("http://example.com/"),
    400        "Address bar's value set after loading URI"
    401      );
    402      runNextTest();
    403    });
    404  }
    405 
    406  let tests = [
    407    test_newTabFocused,
    408    test_newTabNotFocused,
    409    test_existingSHEnd_noClear,
    410    test_existingSHMiddle_noClear,
    411    test_getBrowserState_lotsOfTabsOpening,
    412    test_getBrowserState_userTypedValue,
    413    test_userTypedClearLoadURI,
    414  ];
    415  let originalState = JSON.parse(ss.getBrowserState());
    416  let state = {
    417    windows: [
    418      {
    419        tabs: [
    420          { entries: [{ url: "about:blank", triggeringPrincipal_base64 }] },
    421        ],
    422      },
    423    ],
    424  };
    425  function runNextTest() {
    426    if (tests.length) {
    427      waitForBrowserState(state, function () {
    428        gBrowser.selectedBrowser.userTypedValue = null;
    429        gURLBar.setURI();
    430        tests.shift()();
    431      });
    432    } else {
    433      waitForBrowserState(originalState, function () {
    434        gBrowser.selectedBrowser.userTypedValue = null;
    435        gURLBar.setURI();
    436        finish();
    437      });
    438    }
    439  }
    440 
    441  // Run the tests!
    442  runNextTest();
    443 }