tor-browser

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

browser_dying_cache.js (2434B)


      1 "use strict";
      2 
      3 /**
      4 * This test ensures that after closing a window we keep its state data around
      5 * as long as something keeps a reference to it. It should only be possible to
      6 * read data after closing - writing should fail.
      7 */
      8 
      9 add_task(async function test() {
     10  // Open a new window.
     11  let win = await promiseNewWindowLoaded();
     12 
     13  // Load some URL in the current tab.
     14  let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY;
     15  BrowserTestUtils.startLoadingURIString(
     16    win.gBrowser.selectedBrowser,
     17    "about:robots",
     18    {
     19      flags,
     20    }
     21  );
     22  await promiseBrowserLoaded(win.gBrowser.selectedBrowser);
     23 
     24  // Open a second tab and close the first one.
     25  let tab = BrowserTestUtils.addTab(win.gBrowser, "about:mozilla");
     26  await promiseBrowserLoaded(tab.linkedBrowser);
     27  await TabStateFlusher.flush(tab.linkedBrowser);
     28  await promiseRemoveTabAndSessionState(win.gBrowser.tabs[0]);
     29 
     30  // Make sure our window is still tracked by sessionstore
     31  // and the window state is as expected.
     32  ok("__SSi" in win, "window is being tracked by sessionstore");
     33  ss.setCustomWindowValue(win, "foo", "bar");
     34  checkWindowState(win);
     35 
     36  // Close our window.
     37  await BrowserTestUtils.closeWindow(win);
     38 
     39  // SessionStore should no longer track our window
     40  // but it should still report the same state.
     41  ok(!("__SSi" in win), "sessionstore does no longer track our window");
     42  checkWindowState(win);
     43 
     44  // Make sure we're not allowed to modify state data.
     45  Assert.throws(
     46    () => ss.setWindowState(win, {}),
     47    /Window is not tracked/,
     48    "we're not allowed to modify state data anymore"
     49  );
     50  Assert.throws(
     51    () => ss.setCustomWindowValue(win, "foo", "baz"),
     52    /Window is not tracked/,
     53    "we're not allowed to modify state data anymore"
     54  );
     55 });
     56 
     57 function checkWindowState(window) {
     58  let {
     59    windows: [{ tabs }],
     60  } = ss.getWindowState(window);
     61  is(tabs.length, 1, "the window has a single tab");
     62  is(tabs[0].entries[0].url, "about:mozilla", "the tab is about:mozilla");
     63 
     64  is(ss.getClosedTabCountForWindow(window), 1, "the window has one closed tab");
     65  let [
     66    {
     67      state: {
     68        entries: [{ url }],
     69      },
     70    },
     71  ] = ss.getClosedTabDataForWindow(window);
     72  is(url, "about:robots", "the closed tab is about:robots");
     73 
     74  is(ss.getCustomWindowValue(window, "foo"), "bar", "correct extData value");
     75 }
     76 
     77 function shouldThrow(f) {
     78  try {
     79    f();
     80  } catch (e) {
     81    return true;
     82  }
     83  return null;
     84 }