tor-browser

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

browser_reopen_all_windows.js (4644B)


      1 "use strict";
      2 
      3 const PATH = "browser/browser/components/sessionstore/test/empty.html";
      4 var URLS_WIN1 = [
      5  "https://example.com/" + PATH,
      6  "https://example.org/" + PATH,
      7  "http://test1.mochi.test:8888/" + PATH,
      8  "http://test1.example.com/" + PATH,
      9 ];
     10 var EXPECTED_URLS_WIN1 = ["about:blank", ...URLS_WIN1];
     11 
     12 var URLS_WIN2 = [
     13  "http://sub1.test1.mochi.test:8888/" + PATH,
     14  "http://sub2.xn--lt-uia.mochi.test:8888/" + PATH,
     15  "http://test2.mochi.test:8888/" + PATH,
     16  "http://sub1.test2.example.org/" + PATH,
     17  "http://sub2.test1.example.org/" + PATH,
     18  "http://test2.example.com/" + PATH,
     19 ];
     20 var EXPECTED_URLS_WIN2 = ["about:blank", ...URLS_WIN2];
     21 
     22 requestLongerTimeout(4);
     23 
     24 function allTabsRestored(win, expectedUrls) {
     25  return new Promise(resolve => {
     26    let tabsRestored = 0;
     27    function handler(event) {
     28      let spec = event.target.linkedBrowser.currentURI.spec;
     29      if (expectedUrls.includes(spec)) {
     30        tabsRestored++;
     31      }
     32      info(`Got SSTabRestored for ${spec}, tabsRestored=${tabsRestored}`);
     33      if (tabsRestored === expectedUrls.length) {
     34        win.gBrowser.tabContainer.removeEventListener(
     35          "SSTabRestored",
     36          handler,
     37          true
     38        );
     39        resolve();
     40      }
     41    }
     42    win.gBrowser.tabContainer.addEventListener("SSTabRestored", handler, true);
     43  });
     44 }
     45 
     46 async function windowAndTabsRestored(win, expectedUrls) {
     47  await TestUtils.topicObserved(
     48    "browser-window-before-show",
     49    subject => subject === win
     50  );
     51  return allTabsRestored(win, expectedUrls);
     52 }
     53 
     54 add_task(async () => {
     55  await SpecialPowers.pushPrefEnv({
     56    set: [
     57      // Set the pref to true so we know exactly how many tabs should be restoring at
     58      // any given time. This guarantees that a finishing load won't start another.
     59      ["browser.sessionstore.restore_on_demand", false],
     60    ],
     61  });
     62 
     63  forgetClosedWindows();
     64  is(SessionStore.getClosedWindowCount(), 0, "starting with no closed windows");
     65 
     66  // Open window 1, with different tabs
     67  let win1 = await BrowserTestUtils.openNewBrowserWindow();
     68  for (let url of URLS_WIN1) {
     69    await BrowserTestUtils.openNewForegroundTab(win1.gBrowser, url);
     70  }
     71 
     72  // Open window 2, with different tabs
     73  let win2 = await BrowserTestUtils.openNewBrowserWindow();
     74  for (let url of URLS_WIN2) {
     75    await BrowserTestUtils.openNewForegroundTab(win2.gBrowser, url);
     76  }
     77 
     78  await TabStateFlusher.flushWindow(win1);
     79  await TabStateFlusher.flushWindow(win2);
     80 
     81  // Close both windows
     82  await BrowserTestUtils.closeWindow(win1);
     83  await BrowserTestUtils.closeWindow(win2);
     84  await forceSaveState();
     85 
     86  // Verify both windows were accounted for by session store
     87  is(
     88    ss.getClosedWindowCount(),
     89    2,
     90    "The closed windows was added to Recently Closed Windows"
     91  );
     92 
     93  // We previously used to manually navigate the Library menu to click the
     94  // "Reopen all Windows" button, but that reopens all windows at once without
     95  // returning a reference to each window. Since we need to attach listeners to
     96  // these windows *before* they start restoring tabs, we now manually call
     97  // undoCloseWindow() here, which has the same effect, but also gives us the
     98  // window references.
     99  info("Reopening windows");
    100  let restoredWindows = [];
    101  while (SessionStore.getClosedWindowCount() > 0) {
    102    restoredWindows.unshift(SessionWindowUI.undoCloseWindow());
    103  }
    104  is(restoredWindows.length, 2, "Reopened correct number of windows");
    105 
    106  let win1Restored = windowAndTabsRestored(
    107    restoredWindows[0],
    108    EXPECTED_URLS_WIN1
    109  );
    110  let win2Restored = windowAndTabsRestored(
    111    restoredWindows[1],
    112    EXPECTED_URLS_WIN2
    113  );
    114 
    115  info("About to wait for tabs to be restored");
    116  await Promise.all([win1Restored, win2Restored]);
    117 
    118  const sessionRestoreInitialized =
    119    Glean.sessionRestore.startupTimeline.sessionRestoreInitialized.testGetValue();
    120  Assert.greater(
    121    sessionRestoreInitialized,
    122    0,
    123    "Session store initialize recorded."
    124  );
    125 
    126  is(
    127    restoredWindows[0].gBrowser.tabs.length,
    128    EXPECTED_URLS_WIN1.length,
    129    "All tabs restored"
    130  );
    131  is(
    132    restoredWindows[1].gBrowser.tabs.length,
    133    EXPECTED_URLS_WIN2.length,
    134    "All tabs restored"
    135  );
    136 
    137  // Verify that tabs opened as expected
    138  Assert.deepEqual(
    139    restoredWindows[0].gBrowser.tabs.map(
    140      tab => tab.linkedBrowser.currentURI.spec
    141    ),
    142    EXPECTED_URLS_WIN1
    143  );
    144  Assert.deepEqual(
    145    restoredWindows[1].gBrowser.tabs.map(
    146      tab => tab.linkedBrowser.currentURI.spec
    147    ),
    148    EXPECTED_URLS_WIN2
    149  );
    150 
    151  info("About to close windows");
    152  await BrowserTestUtils.closeWindow(restoredWindows[0]);
    153  await BrowserTestUtils.closeWindow(restoredWindows[1]);
    154 });