tor-browser

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

browser_forget_async_closings.js (5110B)


      1 "use strict";
      2 
      3 const PAGE = "http://example.com/";
      4 
      5 /**
      6 * Creates a tab in the current window worth storing in the
      7 * closedTabs array, and then closes it. Runs a synchronous
      8 * forgetFn passed in that should cause us to forget the tab,
      9 * and then ensures that after the tab has sent its final
     10 * update message that we didn't accidentally store it in
     11 * the closedTabs array.
     12 *
     13 * @param forgetFn (function)
     14 *        A synchronous function that should cause the tab
     15 *        to be forgotten.
     16 * @returns Promise
     17 */
     18 let forgetTabHelper = async function (forgetFn) {
     19  // We want to suppress all non-final updates from the browser tabs
     20  // so as to eliminate any racy-ness with this test.
     21  await pushPrefs(["browser.sessionstore.debug.no_auto_updates", true]);
     22 
     23  // Forget any previous closed tabs from other tests that may have
     24  // run in the same session.
     25  Services.obs.notifyObservers(null, "browser:purge-session-history");
     26 
     27  is(
     28    ss.getClosedTabCountForWindow(window),
     29    0,
     30    "We should have 0 closed tabs being stored."
     31  );
     32 
     33  // Create a tab worth remembering.
     34  let tab = BrowserTestUtils.addTab(gBrowser, PAGE);
     35  let browser = tab.linkedBrowser;
     36  await BrowserTestUtils.browserLoaded(browser, false, PAGE);
     37  await TabStateFlusher.flush(browser);
     38 
     39  // Now close the tab, and immediately choose to forget it.
     40  let promise = promiseRemoveTabAndSessionState(tab);
     41 
     42  // At this point, the tab will have closed, but the final update
     43  // to SessionStore hasn't come up yet. Now do the operation that
     44  // should cause us to forget the tab.
     45  forgetFn();
     46 
     47  is(
     48    ss.getClosedTabCountForWindow(window),
     49    0,
     50    "Should have forgotten the closed tab"
     51  );
     52 
     53  // Now wait for the final update to come up.
     54  await promise;
     55 
     56  is(
     57    ss.getClosedTabCountForWindow(window),
     58    0,
     59    "Should not have stored the forgotten closed tab"
     60  );
     61 };
     62 
     63 /**
     64 * Creates a new window worth storing in the closeWIndows array,
     65 * and then closes it. Runs a synchronous forgetFn passed in that
     66 * should cause us to forget the window, and then ensures that after
     67 * the window has sent its final update message that we didn't
     68 * accidentally store it in the closedWindows array.
     69 *
     70 * @param forgetFn (function)
     71 *        A synchronous function that should cause the window
     72 *        to be forgotten.
     73 * @returns Promise
     74 */
     75 let forgetWinHelper = async function (forgetFn) {
     76  // We want to suppress all non-final updates from the browser tabs
     77  // so as to eliminate any racy-ness with this test.
     78  await pushPrefs(["browser.sessionstore.debug.no_auto_updates", true]);
     79 
     80  // Forget any previous closed windows from other tests that may have
     81  // run in the same session.
     82  Services.obs.notifyObservers(null, "browser:purge-session-history");
     83 
     84  is(
     85    ss.getClosedWindowCount(),
     86    0,
     87    "We should have 0 closed windows being stored."
     88  );
     89 
     90  let newWin = await BrowserTestUtils.openNewBrowserWindow();
     91 
     92  // Create a tab worth remembering.
     93  let tab = newWin.gBrowser.selectedTab;
     94  let browser = tab.linkedBrowser;
     95  BrowserTestUtils.startLoadingURIString(browser, PAGE);
     96  await BrowserTestUtils.browserLoaded(browser, false, PAGE);
     97  await TabStateFlusher.flush(browser);
     98 
     99  // Now close the window and immediately choose to forget it.
    100  let windowClosed = BrowserTestUtils.windowClosed(newWin);
    101 
    102  let handled = false;
    103  whenDomWindowClosedHandled(() => {
    104    // At this point, the window will have closed and the onClose handler
    105    // has run, but the final update  to SessionStore hasn't come up yet.
    106    // Now do the oepration that should cause us to forget the window.
    107    forgetFn();
    108 
    109    is(ss.getClosedWindowCount(), 0, "Should have forgotten the closed window");
    110 
    111    handled = true;
    112  });
    113 
    114  newWin.close();
    115 
    116  // Now wait for the final update to come up.
    117  await windowClosed;
    118 
    119  ok(handled, "domwindowclosed should already be handled here");
    120 
    121  is(ss.getClosedWindowCount(), 0, "Should not have stored the closed window");
    122 };
    123 
    124 /**
    125 * Tests that if we choose to forget a tab while waiting for its
    126 * final flush to complete, we don't accidentally store it.
    127 */
    128 add_task(async function test_forget_closed_tab() {
    129  await forgetTabHelper(() => {
    130    ss.forgetClosedTab(window, 0);
    131  });
    132 });
    133 
    134 /**
    135 * Tests that if we choose to forget a tab while waiting for its
    136 * final flush to complete, we don't accidentally store it.
    137 */
    138 add_task(async function test_forget_closed_window() {
    139  await forgetWinHelper(() => {
    140    ss.forgetClosedWindow(0);
    141  });
    142 });
    143 
    144 /**
    145 * Tests that if we choose to purge history while waiting for a
    146 * final flush of a tab to complete, we don't accidentally store it.
    147 */
    148 add_task(async function test_forget_purged_tab() {
    149  await forgetTabHelper(() => {
    150    Services.obs.notifyObservers(null, "browser:purge-session-history");
    151  });
    152 });
    153 
    154 /**
    155 * Tests that if we choose to purge history while waiting for a
    156 * final flush of a window to complete, we don't accidentally
    157 * store it.
    158 */
    159 add_task(async function test_forget_purged_window() {
    160  await forgetWinHelper(() => {
    161    Services.obs.notifyObservers(null, "browser:purge-session-history");
    162  });
    163 });