tor-browser

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

browser_closed_objects_changed_notifications_tabs.js (3810B)


      1 "use strict";
      2 
      3 /**
      4 * This test is for the sessionstore-closed-objects-changed notifications.
      5 */
      6 
      7 const MAX_TABS_UNDO_PREF = "browser.sessionstore.max_tabs_undo";
      8 const TOPIC = "sessionstore-closed-objects-changed";
      9 
     10 let notificationsCount = 0;
     11 
     12 async function openWindow(url) {
     13  let win = await promiseNewWindowLoaded();
     14  let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY;
     15  BrowserTestUtils.startLoadingURIString(win.gBrowser.selectedBrowser, url, {
     16    flags,
     17  });
     18  await promiseBrowserLoaded(win.gBrowser.selectedBrowser, true, url);
     19  return win;
     20 }
     21 
     22 async function closeWindow(win) {
     23  await awaitNotification(() => BrowserTestUtils.closeWindow(win));
     24 }
     25 
     26 async function openAndCloseWindow(url) {
     27  let win = await openWindow(url);
     28  await closeWindow(win);
     29 }
     30 
     31 async function openTab(window, url) {
     32  let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, url);
     33  await TabStateFlusher.flush(tab.linkedBrowser);
     34  return tab;
     35 }
     36 
     37 async function openAndCloseTab(window, url) {
     38  let tab = await openTab(window, url);
     39  await promiseRemoveTabAndSessionState(tab);
     40 }
     41 
     42 function countingObserver() {
     43  notificationsCount++;
     44 }
     45 
     46 function assertNotificationCount(count) {
     47  is(
     48    notificationsCount,
     49    count,
     50    "The expected number of notifications was received."
     51  );
     52 }
     53 
     54 async function awaitNotification(callback) {
     55  let notification = TestUtils.topicObserved(TOPIC);
     56  executeSoon(callback);
     57  await notification;
     58 }
     59 
     60 add_task(async function test_closedObjectsChangedNotifications() {
     61  // Create a closed window so that when we do the purge we can expect a notification.
     62  await openAndCloseWindow("about:robots");
     63 
     64  // Forget any previous closed windows or tabs from other tests that may have
     65  // run in the same session.
     66  await awaitNotification(() =>
     67    Services.obs.notifyObservers(null, "browser:purge-session-history")
     68  );
     69 
     70  // Add an observer to count the number of notifications.
     71  Services.obs.addObserver(countingObserver, TOPIC);
     72 
     73  // Open a new window.
     74  let win = await openWindow("about:robots");
     75 
     76  info("Opening and closing a tab.");
     77  await openAndCloseTab(win, "about:mozilla");
     78  assertNotificationCount(1);
     79 
     80  info("Opening and closing a second tab.");
     81  await openAndCloseTab(win, "about:mozilla");
     82  assertNotificationCount(2);
     83 
     84  info(`Changing the ${MAX_TABS_UNDO_PREF} pref.`);
     85  registerCleanupFunction(function () {
     86    Services.prefs.clearUserPref(MAX_TABS_UNDO_PREF);
     87  });
     88  await awaitNotification(() =>
     89    Services.prefs.setIntPref(MAX_TABS_UNDO_PREF, 1)
     90  );
     91  assertNotificationCount(3);
     92 
     93  info("Undoing close of remaining closed tab.");
     94  let tab = SessionStore.undoCloseTab(win, 0);
     95  await promiseTabRestored(tab);
     96  assertNotificationCount(4);
     97 
     98  info("Closing tab again.");
     99  await promiseRemoveTabAndSessionState(tab);
    100  assertNotificationCount(5);
    101 
    102  info("Purging session history.");
    103  await awaitNotification(() =>
    104    Services.obs.notifyObservers(null, "browser:purge-session-history")
    105  );
    106  assertNotificationCount(6);
    107 
    108  info("Opening and closing another tab.");
    109  await openAndCloseTab(win, "http://example.com/");
    110  assertNotificationCount(7);
    111 
    112  info("Purging domain data with no matches.");
    113  Services.obs.notifyObservers(
    114    null,
    115    "browser:purge-session-history-for-domain",
    116    "mozilla.com"
    117  );
    118  assertNotificationCount(7);
    119 
    120  info("Purging domain data with matches.");
    121  await awaitNotification(() =>
    122    Services.obs.notifyObservers(
    123      null,
    124      "browser:purge-session-history-for-domain",
    125      "example.com"
    126    )
    127  );
    128  assertNotificationCount(8);
    129 
    130  info("Opening and closing another tab.");
    131  await openAndCloseTab(win, "http://example.com/");
    132  assertNotificationCount(9);
    133 
    134  await closeWindow(win);
    135  assertNotificationCount(10);
    136 
    137  Services.obs.removeObserver(countingObserver, TOPIC);
    138 });