tor-browser

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

browser_586068-multi_window.js (3302B)


      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, false);
      9  registerCleanupFunction(function () {
     10    Services.prefs.clearUserPref(PREF_RESTORE_ON_DEMAND);
     11  });
     12 
     13  // The first window will be put into the already open window and the second
     14  // window will be opened with _openWindowWithState, which is the source of the problem.
     15  let state = {
     16    windows: [
     17      {
     18        tabs: [
     19          {
     20            entries: [
     21              { url: "http://example.org#0", triggeringPrincipal_base64 },
     22            ],
     23            extData: { uniq: r() },
     24          },
     25        ],
     26        selected: 1,
     27      },
     28      {
     29        tabs: [
     30          {
     31            entries: [
     32              { url: "http://example.com#1", triggeringPrincipal_base64 },
     33            ],
     34            extData: { uniq: r() },
     35          },
     36          {
     37            entries: [
     38              { url: "http://example.com#2", triggeringPrincipal_base64 },
     39            ],
     40            extData: { uniq: r() },
     41          },
     42          {
     43            entries: [
     44              { url: "http://example.com#3", triggeringPrincipal_base64 },
     45            ],
     46            extData: { uniq: r() },
     47          },
     48          {
     49            entries: [
     50              { url: "http://example.com#4", triggeringPrincipal_base64 },
     51            ],
     52            extData: { uniq: r() },
     53          },
     54          {
     55            entries: [
     56              { url: "http://example.com#5", triggeringPrincipal_base64 },
     57            ],
     58            extData: { uniq: r() },
     59          },
     60          {
     61            entries: [
     62              { url: "http://example.com#6", triggeringPrincipal_base64 },
     63            ],
     64            extData: { uniq: r() },
     65          },
     66        ],
     67        selected: 4,
     68      },
     69    ],
     70  };
     71  let numTabs = state.windows[0].tabs.length + state.windows[1].tabs.length;
     72 
     73  let loadCount = 0;
     74  let promiseRestoringTabs = new Promise(resolve => {
     75    gProgressListener.setCallback(function (aBrowser, aNeedRestore) {
     76      if (++loadCount == numTabs) {
     77        // We don't actually care about load order in this test, just that they all
     78        // do load.
     79        is(loadCount, numTabs, "all tabs were restored");
     80        is(aNeedRestore, 0, "there are no tabs left needing restore");
     81 
     82        gProgressListener.unsetCallback();
     83        resolve();
     84      }
     85    });
     86  });
     87 
     88  // We also want to catch the 2nd window, so we need to observe domwindowopened
     89  Services.ww.registerNotification(function observer(aSubject, aTopic) {
     90    if (aTopic == "domwindowopened") {
     91      let win = aSubject;
     92      win.addEventListener(
     93        "load",
     94        function () {
     95          Services.ww.unregisterNotification(observer);
     96          win.gBrowser.addTabsProgressListener(gProgressListener);
     97        },
     98        { once: true }
     99      );
    100    }
    101  });
    102 
    103  let backupState = ss.getBrowserState();
    104  ss.setBrowserState(JSON.stringify(state));
    105  await promiseRestoringTabs;
    106 
    107  // Cleanup.
    108  await promiseAllButPrimaryWindowClosed();
    109  await promiseBrowserState(backupState);
    110 });