tor-browser

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

browser_bfcache_activebc.js (2387B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const FocusManager = Services.focus;
      7 
      8 const SITE_URL_1 =
      9  getRootDirectory(gTestPath).replace(
     10    "chrome://mochitests/content",
     11    "https://example.com"
     12  ) + "empty.html";
     13 
     14 const SITE_URL_2 =
     15  getRootDirectory(gTestPath).replace(
     16    "chrome://mochitests/content",
     17    "http://127.0.0.1:8888"
     18  ) + "empty.html";
     19 
     20 // Test ensures that when a page goes to BFCache, it won't
     21 // accidentally update the active browsing context to null to
     22 // the parent process.
     23 add_task(async function () {
     24  // Load Site 1
     25  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, SITE_URL_1);
     26 
     27  BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, SITE_URL_2);
     28  // Navigated to Site 2 in the same tab
     29  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
     30 
     31  const pageNavigatedBackToSite1 = BrowserTestUtils.waitForContentEvent(
     32    tab.linkedBrowser,
     33    "pageshow"
     34  );
     35  tab.linkedBrowser.goBack();
     36  // Navigated site 1 by going back
     37  await pageNavigatedBackToSite1;
     38 
     39  const pageHideForSite1Run = SpecialPowers.spawn(tab.linkedBrowser, [], () => {
     40    return new Promise(r => {
     41      content.addEventListener("pagehide", function () {
     42        const start = Date.now();
     43        // block the main thread for 2 seconds.
     44        while (Date.now() - start < 2000) {
     45          r();
     46        }
     47      });
     48    });
     49  });
     50 
     51  let pageNavigatedBackToSite2 = BrowserTestUtils.waitForContentEvent(
     52    tab.linkedBrowser,
     53    "pageshow"
     54  );
     55 
     56  // Navigate to site 2 again by going forward.
     57  //
     58  // In a buggy Firefox build, this navigation would trigger
     59  // two activeBrowsingContextInChrome updates. One from
     60  // site 1 to set it to nullptr, and one from the site 2
     61  // to itself.
     62  tab.linkedBrowser.goForward();
     63 
     64  await pageNavigatedBackToSite2;
     65  // Forcefully to make site 1 to update activeBrowsingContextInChrome
     66  // after site 2.
     67  await pageHideForSite1Run;
     68 
     69  // Give the parent process some opportunities to update
     70  // the activeBrowsingContextInChrome via IPC.
     71  await new Promise(r => {
     72    /* eslint-disable mozilla/no-arbitrary-setTimeout */
     73    setTimeout(r, 2000);
     74  });
     75 
     76  Assert.ok(
     77    !!FocusManager.activeContentBrowsingContext,
     78    "active browsing context in content should be non-null"
     79  );
     80  BrowserTestUtils.removeTab(tab);
     81 });