tor-browser

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

browser_590268.js (5090B)


      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 const NUM_TABS = 12;
      6 
      7 var stateBackup = ss.getBrowserState();
      8 
      9 function test() {
     10  /** Test for Bug 590268 - Provide access to sessionstore tab data sooner */
     11  waitForExplicitFinish();
     12  requestLongerTimeout(2);
     13 
     14  // wasLoaded will be used to keep track of tabs that have already had SSTabRestoring
     15  // fired for them.
     16  let wasLoaded = {};
     17  let restoringTabsCount = 0;
     18  let restoredTabsCount = 0;
     19  let uniq2 = {};
     20  let uniq2Count = 0;
     21  let state = { windows: [{ tabs: [] }] };
     22  // We're going to put a bunch of tabs into this state
     23  for (let i = 0; i < NUM_TABS; i++) {
     24    let uniq = r();
     25    let tabData = {
     26      entries: [
     27        { url: "http://example.com/#" + i, triggeringPrincipal_base64 },
     28      ],
     29      extData: { uniq, baz: "qux" },
     30    };
     31    state.windows[0].tabs.push(tabData);
     32    wasLoaded[uniq] = false;
     33  }
     34 
     35  function onSSTabRestoring(aEvent) {
     36    restoringTabsCount++;
     37    let uniq = ss.getCustomTabValue(aEvent.originalTarget, "uniq");
     38    wasLoaded[uniq] = true;
     39 
     40    is(
     41      ss.getCustomTabValue(aEvent.originalTarget, "foo"),
     42      "",
     43      "There is no value for 'foo'"
     44    );
     45 
     46    // On the first SSTabRestoring we're going to run the the real test.
     47    // We'll keep this listener around so we can keep marking tabs as restored.
     48    if (restoringTabsCount == 1) {
     49      onFirstSSTabRestoring();
     50    } else if (restoringTabsCount == NUM_TABS) {
     51      onLastSSTabRestoring();
     52    }
     53  }
     54 
     55  function onSSTabRestored() {
     56    if (++restoredTabsCount < NUM_TABS) {
     57      return;
     58    }
     59    cleanup();
     60  }
     61 
     62  function onTabOpen(aEvent) {
     63    // To test bug 614708, we'll just set a value on the tab here. This value
     64    // would previously cause us to not recognize the values in extData until
     65    // much later. So testing "uniq" failed.
     66    ss.setCustomTabValue(aEvent.originalTarget, "foo", "bar");
     67  }
     68 
     69  // This does the actual testing. SSTabRestoring should be firing on tabs from
     70  // left to right, so we're going to start with the rightmost tab.
     71  function onFirstSSTabRestoring() {
     72    info("onFirstSSTabRestoring...");
     73    for (let i = gBrowser.tabs.length - 1; i >= 0; i--) {
     74      let tab = gBrowser.tabs[i];
     75      let actualUniq = ss.getCustomTabValue(tab, "uniq");
     76      let expectedUniq = state.windows[0].tabs[i].extData.uniq;
     77 
     78      if (wasLoaded[actualUniq]) {
     79        info("tab " + i + ": already restored");
     80        continue;
     81      }
     82      is(actualUniq, expectedUniq, "tab " + i + ": extData was correct");
     83 
     84      // Now we're going to set a piece of data back on the tab so it can be read
     85      // to test setting a value "early".
     86      uniq2[actualUniq] = r();
     87      ss.setCustomTabValue(tab, "uniq2", uniq2[actualUniq]);
     88 
     89      // Delete the value we have for "baz". This tests that deleteCustomTabValue
     90      // will delete "early access" values (c.f. bug 617175). If this doesn't throw
     91      // then the test is successful.
     92      try {
     93        ss.deleteCustomTabValue(tab, "baz");
     94      } catch (e) {
     95        ok(false, "no error calling deleteCustomTabValue - " + e);
     96      }
     97 
     98      // This will be used in the final comparison to make sure we checked the
     99      // same number as we set.
    100      uniq2Count++;
    101    }
    102  }
    103 
    104  function onLastSSTabRestoring() {
    105    let checked = 0;
    106    for (let i = 0; i < gBrowser.tabs.length; i++) {
    107      let tab = gBrowser.tabs[i];
    108      let uniq = ss.getCustomTabValue(tab, "uniq");
    109 
    110      // Look to see if we set a uniq2 value for this uniq value
    111      if (uniq in uniq2) {
    112        is(
    113          ss.getCustomTabValue(tab, "uniq2"),
    114          uniq2[uniq],
    115          "tab " + i + " has correct uniq2 value"
    116        );
    117        checked++;
    118      }
    119    }
    120    Assert.greater(
    121      uniq2Count,
    122      0,
    123      "at least 1 tab properly checked 'early access'"
    124    );
    125    is(checked, uniq2Count, "checked the same number of uniq2 as we set");
    126  }
    127 
    128  function cleanup() {
    129    // remove the event listener and clean up before finishing
    130    gBrowser.tabContainer.removeEventListener(
    131      "SSTabRestoring",
    132      onSSTabRestoring
    133    );
    134    gBrowser.tabContainer.removeEventListener(
    135      "SSTabRestored",
    136      onSSTabRestored,
    137      true
    138    );
    139    gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen);
    140    // Put this in an executeSoon because we still haven't called restoreNextTab
    141    // in sessionstore for the last tab (we'll call it after this). We end up
    142    // trying to restore the tab (since we then add a closed tab to the array).
    143    executeSoon(function () {
    144      ss.setBrowserState(stateBackup);
    145      executeSoon(finish);
    146    });
    147  }
    148 
    149  // Add the event listeners
    150  gBrowser.tabContainer.addEventListener("SSTabRestoring", onSSTabRestoring);
    151  gBrowser.tabContainer.addEventListener(
    152    "SSTabRestored",
    153    onSSTabRestored,
    154    true
    155  );
    156  gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen);
    157  // Restore state
    158  ss.setBrowserState(JSON.stringify(state));
    159 }