tor-browser

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

browser_should_restore_tab.js (4043B)


      1 const NOTIFY_CLOSED_OBJECTS_CHANGED = "sessionstore-closed-objects-changed";
      2 
      3 add_setup(async () => {
      4  registerCleanupFunction(async () => {
      5    Services.obs.notifyObservers(null, "browser:purge-session-history");
      6  });
      7 });
      8 
      9 async function check_tab_close_notification(openedTab, expectNotification) {
     10  let tabUrl = openedTab.linkedBrowser.currentURI.spec;
     11  let win = openedTab.ownerGlobal;
     12  let initialTabCount = SessionStore.getClosedTabCountForWindow(win);
     13 
     14  let tabClosed = BrowserTestUtils.waitForTabClosing(openedTab);
     15  let notified = false;
     16  function topicObserver() {
     17    notified = true;
     18  }
     19  Services.obs.addObserver(topicObserver, NOTIFY_CLOSED_OBJECTS_CHANGED);
     20 
     21  BrowserTestUtils.removeTab(openedTab);
     22  await tabClosed;
     23  // SessionStore does a setTimeout(notify, 0) to notifyObservers when it handles TabClose
     24  // We need to wait long enough to be confident the observer would have been notified
     25  // if it was going to be.
     26  let ticks = 0;
     27  await TestUtils.waitForCondition(() => {
     28    return ++ticks > 1;
     29  });
     30 
     31  Services.obs.removeObserver(topicObserver, NOTIFY_CLOSED_OBJECTS_CHANGED);
     32  if (expectNotification) {
     33    Assert.ok(
     34      notified,
     35      `Expected ${NOTIFY_CLOSED_OBJECTS_CHANGED} when the ${tabUrl} tab closed`
     36    );
     37    Assert.equal(
     38      SessionStore.getClosedTabCountForWindow(win),
     39      initialTabCount + 1,
     40      "Expected closed tab count to have incremented"
     41    );
     42  } else {
     43    Assert.ok(
     44      !notified,
     45      `Expected no ${NOTIFY_CLOSED_OBJECTS_CHANGED} when the ${tabUrl} tab closed`
     46    );
     47    Assert.equal(
     48      SessionStore.getClosedTabCountForWindow(win),
     49      initialTabCount,
     50      "Expected closed tab count to have not changed"
     51    );
     52  }
     53 }
     54 
     55 add_task(async function test_control_case() {
     56  let win = window;
     57  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
     58    win.gBrowser,
     59    () => {}
     60  );
     61  let tab = await BrowserTestUtils.openNewForegroundTab(
     62    win.gBrowser,
     63    "https://example.com/"
     64  );
     65  await tabOpenedAndSwitchedTo;
     66  await check_tab_close_notification(tab, true);
     67 });
     68 
     69 add_task(async function test_about_new_tab() {
     70  let win = window;
     71  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
     72    win.gBrowser,
     73    () => {}
     74  );
     75  // This opens about:newtab:
     76  win.BrowserCommands.openTab();
     77  let tab = await tabOpenedAndSwitchedTo;
     78  await check_tab_close_notification(tab, false);
     79 });
     80 
     81 add_task(async function test_about_home() {
     82  let win = window;
     83  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
     84    win.gBrowser,
     85    () => {}
     86  );
     87  let tab = await BrowserTestUtils.openNewForegroundTab(
     88    win.gBrowser,
     89    "about:home"
     90  );
     91  await tabOpenedAndSwitchedTo;
     92  await check_tab_close_notification(tab, false);
     93 });
     94 
     95 add_task(async function test_navigated_about_home() {
     96  let win = window;
     97  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
     98    win.gBrowser,
     99    () => {}
    100  );
    101  let tab = await BrowserTestUtils.openNewForegroundTab(
    102    win.gBrowser,
    103    "https://example.com/"
    104  );
    105  await tabOpenedAndSwitchedTo;
    106  BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, "about:home");
    107  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    108  // even if we end up on an ignorable URL,
    109  // if there's meaningful history, we should save this tab
    110  await check_tab_close_notification(tab, true);
    111 });
    112 
    113 add_task(async function test_about_blank() {
    114  let win = window;
    115  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
    116    win.gBrowser,
    117    () => {}
    118  );
    119  let tab = await BrowserTestUtils.openNewForegroundTab(
    120    win.gBrowser,
    121    "about:blank"
    122  );
    123  await tabOpenedAndSwitchedTo;
    124  await check_tab_close_notification(tab, false);
    125 });
    126 
    127 add_task(async function test_about_privatebrowsing() {
    128  let win = window;
    129  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
    130    win.gBrowser,
    131    () => {}
    132  );
    133  let tab = await BrowserTestUtils.openNewForegroundTab(
    134    win.gBrowser,
    135    "about:privatebrowsing"
    136  );
    137  await tabOpenedAndSwitchedTo;
    138  await check_tab_close_notification(tab, false);
    139 });