tor-browser

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

browser_speculative_connect.js (4250B)


      1 const TEST_URLS = [
      2  "about:buildconfig",
      3  "http://mochi.test:8888/browser/browser/components/sessionstore/test/browser_speculative_connect.html",
      4  "",
      5 ];
      6 
      7 /**
      8 * This will open tabs in browser. This will also make the last tab
      9 * inserted to be the selected tab.
     10 */
     11 async function openTabs(win) {
     12  for (let i = 0; i < TEST_URLS.length; ++i) {
     13    await BrowserTestUtils.openNewForegroundTab(win.gBrowser, TEST_URLS[i]);
     14  }
     15 }
     16 
     17 add_task(async function speculative_connect_restore_on_demand() {
     18  Services.prefs.setBoolPref("browser.sessionstore.restore_on_demand", true);
     19  is(
     20    Services.prefs.getBoolPref("browser.sessionstore.restore_on_demand"),
     21    true,
     22    "We're restoring on demand"
     23  );
     24  forgetClosedWindows();
     25 
     26  // Open a new window and populate with tabs.
     27  let win = await promiseNewWindowLoaded();
     28  await openTabs(win);
     29 
     30  // Close the window.
     31  await BrowserTestUtils.closeWindow(win);
     32 
     33  // Reopen a window.
     34  let newWin = SessionWindowUI.undoCloseWindow(0);
     35  // Make sure we wait until this window is restored.
     36  await promiseWindowRestored(newWin);
     37 
     38  let tabs = newWin.gBrowser.tabs;
     39  is(tabs.length, TEST_URLS.length + 1, "Restored right number of tabs");
     40 
     41  let e = new MouseEvent("mouseover");
     42 
     43  // First tab should be ignored, since it's the default blank tab when we open a new window.
     44 
     45  // Trigger a mouse enter on second tab.
     46  tabs[1].dispatchEvent(e);
     47  ok(
     48    !tabs[1].__test_connection_prepared,
     49    "Second tab doesn't have a connection prepared"
     50  );
     51  is(tabs[1].__test_connection_url, TEST_URLS[0], "Second tab has correct url");
     52  ok(
     53    SessionStore.getLazyTabValue(tabs[1], "connectionPrepared"),
     54    "Second tab should have connectionPrepared flag after hovered"
     55  );
     56 
     57  // Trigger a mouse enter on third tab.
     58  tabs[2].dispatchEvent(e);
     59  ok(tabs[2].__test_connection_prepared, "Third tab has a connection prepared");
     60  is(tabs[2].__test_connection_url, TEST_URLS[1], "Third tab has correct url");
     61  ok(
     62    SessionStore.getLazyTabValue(tabs[2], "connectionPrepared"),
     63    "Third tab should have connectionPrepared flag after hovered"
     64  );
     65 
     66  // Last tab is the previously selected tab.
     67  tabs[3].dispatchEvent(e);
     68  is(
     69    SessionStore.getLazyTabValue(tabs[3], "connectionPrepared"),
     70    undefined,
     71    "Previous selected tab shouldn't have connectionPrepared flag"
     72  );
     73  is(
     74    tabs[3].__test_connection_prepared,
     75    undefined,
     76    "Previous selected tab should not have a connection prepared"
     77  );
     78  is(
     79    tabs[3].__test_connection_url,
     80    undefined,
     81    "Previous selected tab should not have a connection prepared"
     82  );
     83 
     84  await BrowserTestUtils.closeWindow(newWin);
     85 });
     86 
     87 add_task(async function speculative_connect_restore_automatically() {
     88  Services.prefs.setBoolPref("browser.sessionstore.restore_on_demand", false);
     89  is(
     90    Services.prefs.getBoolPref("browser.sessionstore.restore_on_demand"),
     91    false,
     92    "We're restoring automatically"
     93  );
     94  forgetClosedWindows();
     95 
     96  // Open a new window and populate with tabs.
     97  let win = await promiseNewWindowLoaded();
     98  await openTabs(win);
     99 
    100  // Close the window.
    101  await BrowserTestUtils.closeWindow(win);
    102 
    103  // Reopen a window.
    104  let newWin = SessionWindowUI.undoCloseWindow(0);
    105  // Make sure we wait until this window is restored.
    106  await promiseWindowRestored(newWin);
    107 
    108  let tabs = newWin.gBrowser.tabs;
    109  is(tabs.length, TEST_URLS.length + 1, "Restored right number of tabs");
    110 
    111  // First tab is ignored, since it's the default tab open when we open new window
    112 
    113  // Second tab.
    114  ok(
    115    !tabs[1].__test_connection_prepared,
    116    "Second tab doesn't have a connection prepared"
    117  );
    118  is(
    119    tabs[1].__test_connection_url,
    120    TEST_URLS[0],
    121    "Second tab has correct host url"
    122  );
    123 
    124  // Third tab.
    125  ok(tabs[2].__test_connection_prepared, "Third tab has a connection prepared");
    126  is(
    127    tabs[2].__test_connection_url,
    128    TEST_URLS[1],
    129    "Third tab has correct host url"
    130  );
    131 
    132  // Last tab is the previously selected tab.
    133  is(
    134    tabs[3].__test_connection_prepared,
    135    undefined,
    136    "Selected tab should not have a connection prepared"
    137  );
    138  is(
    139    tabs[3].__test_connection_url,
    140    undefined,
    141    "Selected tab should not have a connection prepared"
    142  );
    143 
    144  await BrowserTestUtils.closeWindow(newWin);
    145 });