tor-browser

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

browser_586068-select.js (3439B)


      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 PREF_RESTORE_ON_DEMAND = "browser.sessionstore.restore_on_demand";
      6 
      7 add_task(async function test() {
      8  Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, true);
      9  registerCleanupFunction(function () {
     10    Services.prefs.clearUserPref(PREF_RESTORE_ON_DEMAND);
     11  });
     12 
     13  let state = {
     14    windows: [
     15      {
     16        tabs: [
     17          {
     18            entries: [
     19              { url: "http://example.org", triggeringPrincipal_base64 },
     20            ],
     21            extData: { uniq: r() },
     22          },
     23          {
     24            entries: [
     25              { url: "http://example.org", triggeringPrincipal_base64 },
     26            ],
     27            extData: { uniq: r() },
     28          },
     29          {
     30            entries: [
     31              { url: "http://example.org", triggeringPrincipal_base64 },
     32            ],
     33            extData: { uniq: r() },
     34          },
     35          {
     36            entries: [
     37              { url: "http://example.org", triggeringPrincipal_base64 },
     38            ],
     39            extData: { uniq: r() },
     40          },
     41          {
     42            entries: [
     43              { url: "http://example.org", triggeringPrincipal_base64 },
     44            ],
     45            extData: { uniq: r() },
     46          },
     47          {
     48            entries: [
     49              { url: "http://example.org", triggeringPrincipal_base64 },
     50            ],
     51            extData: { uniq: r() },
     52          },
     53        ],
     54        selected: 1,
     55      },
     56    ],
     57  };
     58 
     59  let expectedCounts = [
     60    [5, 1, 0],
     61    [4, 1, 1],
     62    [3, 1, 2],
     63    [2, 1, 3],
     64    [1, 1, 4],
     65    [0, 1, 5],
     66  ];
     67  let tabOrder = [0, 5, 1, 4, 3, 2];
     68 
     69  let loadCount = 0;
     70  let promiseRestoringTabs = new Promise(resolve => {
     71    gProgressListener.setCallback(
     72      function (aBrowser, aNeedRestore, aRestoring, aRestored) {
     73        loadCount++;
     74        let expected = expectedCounts[loadCount - 1];
     75 
     76        is(
     77          aNeedRestore,
     78          expected[0],
     79          "load " + loadCount + " - # tabs that need to be restored"
     80        );
     81        is(
     82          aRestoring,
     83          expected[1],
     84          "load " + loadCount + " - # tabs that are restoring"
     85        );
     86        is(
     87          aRestored,
     88          expected[2],
     89          "load " + loadCount + " - # tabs that has been restored"
     90        );
     91 
     92        if (loadCount < state.windows[0].tabs.length) {
     93          // double check that this tab was the right one
     94          let expectedData =
     95            state.windows[0].tabs[tabOrder[loadCount - 1]].extData.uniq;
     96          let tab;
     97          for (let i = 0; i < window.gBrowser.tabs.length; i++) {
     98            if (!tab && window.gBrowser.tabs[i].linkedBrowser == aBrowser) {
     99              tab = window.gBrowser.tabs[i];
    100            }
    101          }
    102 
    103          is(
    104            ss.getCustomTabValue(tab, "uniq"),
    105            expectedData,
    106            "load " + loadCount + " - correct tab was restored"
    107          );
    108 
    109          // select the next tab
    110          window.gBrowser.selectTabAtIndex(tabOrder[loadCount]);
    111        } else {
    112          gProgressListener.unsetCallback();
    113          resolve();
    114        }
    115      }
    116    );
    117  });
    118 
    119  let backupState = ss.getBrowserState();
    120  ss.setBrowserState(JSON.stringify(state));
    121  await promiseRestoringTabs;
    122 
    123  // Cleanup.
    124  await promiseBrowserState(backupState);
    125 });