tor-browser

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

browser_test_bfcache_eviction.js (4279B)


      1 add_task(async function () {
      2  // We don't want the number of total viewers to be calculated by the available size
      3  // for this test case. Instead, fix the number of viewers.
      4  await SpecialPowers.pushPrefEnv({
      5    set: [
      6      ["browser.sessionhistory.max_total_viewers", 3],
      7      ["docshell.shistory.testing.bfevict", true],
      8      // If Fission is disabled, the pref is no-op.
      9      ["fission.bfcacheInParent", true],
     10    ],
     11  });
     12 
     13  // 1. Open a tab
     14  var testPage =
     15    "data:text/html,<html id='html1'><body id='body1'>First tab ever opened</body></html>";
     16  await BrowserTestUtils.withNewTab(
     17    { gBrowser, url: testPage },
     18    async function (browser) {
     19      let testDone = {};
     20      if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
     21        // 2.  Add a promise that will be resolved when the 'content viewer evicted' event goes off
     22        testDone.promise = SpecialPowers.spawn(browser, [], async function () {
     23          return new Promise(resolve => {
     24            let webNavigation = content.docShell.QueryInterface(
     25              Ci.nsIWebNavigation
     26            );
     27            let { legacySHistory } = webNavigation.sessionHistory;
     28            // 3. Register a session history listener to listen for a 'content viewer evicted' event.
     29            let historyListener = {
     30              OnDocumentViewerEvicted() {
     31                ok(
     32                  true,
     33                  "History listener got called after a content viewer was evicted"
     34                );
     35                legacySHistory.removeSHistoryListener(historyListener);
     36                // 6. Resolve the promise when we got our 'content viewer evicted' event
     37                resolve();
     38              },
     39              QueryInterface: ChromeUtils.generateQI([
     40                "nsISHistoryListener",
     41                "nsISupportsWeakReference",
     42              ]),
     43            };
     44            legacySHistory.addSHistoryListener(historyListener);
     45            // Keep the weak shistory listener alive
     46            content._testListener = historyListener;
     47          });
     48        });
     49      } else {
     50        // 2.  Add a promise that will be resolved when the 'content viewer evicted' event goes off
     51        testDone.promise = new Promise(resolve => {
     52          testDone.resolve = resolve;
     53        });
     54        let shistory = browser.browsingContext.sessionHistory;
     55        // 3. Register a session history listener to listen for a 'content viewer evicted' event.
     56        let historyListener = {
     57          OnDocumentViewerEvicted() {
     58            ok(
     59              true,
     60              "History listener got called after a content viewer was evicted"
     61            );
     62            shistory.removeSHistoryListener(historyListener);
     63            delete window._testListener;
     64            // 6. Resolve the promise when we got our 'content viewer evicted' event
     65            testDone.resolve();
     66          },
     67          QueryInterface: ChromeUtils.generateQI([
     68            "nsISHistoryListener",
     69            "nsISupportsWeakReference",
     70          ]),
     71        };
     72        shistory.addSHistoryListener(historyListener);
     73        // Keep the weak shistory listener alive
     74        window._testListener = historyListener;
     75      }
     76 
     77      // 4. Open a second tab
     78      testPage = `data:text/html,<html id='html1'><body id='body1'>I am a second tab!</body></html>`;
     79      let tab2 = await BrowserTestUtils.openNewForegroundTab(
     80        gBrowser,
     81        testPage
     82      );
     83 
     84      // 5. Navigate the first tab to 4 different pages.
     85      // We should get 1 content viewer evicted because it will be outside of the range.
     86      // If we have the following pages in our session history: P1 P2 P3 P4 P5
     87      // and we are currently at P5, then P1 is outside of the range
     88      // (it is more than 3 entries away from current entry) and thus will be evicted.
     89      for (var i = 0; i < 4; i++) {
     90        testPage = `data:text/html,<html id='html1'><body id='body1'>${i}</body></html>`;
     91        let pagePromise = BrowserTestUtils.browserLoaded(browser);
     92        BrowserTestUtils.startLoadingURIString(browser, testPage);
     93        await pagePromise;
     94      }
     95      // 7. Wait for 'content viewer evicted' event to go off
     96      await testDone.promise;
     97 
     98      // 8. Close the second tab
     99      BrowserTestUtils.removeTab(tab2);
    100    }
    101  );
    102 });