tor-browser

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

browser_sync_xhr_event_handing_switch_bcg.js (3892B)


      1 const baseURL = getRootDirectory(gTestPath).replace(
      2  "chrome://mochitests/content",
      3  "https://example.com"
      4 );
      5 
      6 const childURL = `${baseURL}empty.html`;
      7 const parentURL = `${baseURL}empty_parent.html`;
      8 
      9 add_setup(async function () {
     10  await SpecialPowers.pushPrefEnv({
     11    set: [
     12      ["test.wait300msAfterTabSwitch", true],
     13      ["dom.input_events.canSuspendInBCG.enabled", true],
     14    ],
     15  });
     16  if (!Services.appinfo.fissionAutostart) {
     17    // Make sure the tab that is opened with noopener
     18    // also in the same process as the parent.
     19    await SpecialPowers.pushPrefEnv({
     20      set: [["dom.ipc.processCount", 1]],
     21    });
     22  }
     23 });
     24 
     25 async function checkInputManagerStatus(openChildInSameBCG) {
     26  let childTabPromise = BrowserTestUtils.waitForNewTab(
     27    gBrowser,
     28    childURL,
     29    true,
     30    true
     31  );
     32 
     33  let xhrTab = await BrowserTestUtils.openNewForegroundTab(
     34    gBrowser,
     35    parentURL,
     36    true
     37  );
     38 
     39  let xhrTabIsHidden = BrowserTestUtils.waitForContentEvent(
     40    xhrTab.linkedBrowser,
     41    "visibilitychange"
     42  );
     43 
     44  await SpecialPowers.spawn(
     45    xhrTab.linkedBrowser.browsingContext,
     46    [openChildInSameBCG, childURL],
     47    async function (sameBCG, url) {
     48      if (sameBCG) {
     49        content.open(url);
     50      } else {
     51        content.open(url, "", "noopener");
     52      }
     53    }
     54  );
     55 
     56  await childTabPromise;
     57  await xhrTabIsHidden;
     58 
     59  let xhrRequestIsReady = BrowserTestUtils.waitForContentEvent(
     60    xhrTab.linkedBrowser,
     61    "xhrRequestIsReady"
     62  );
     63 
     64  let xhrRequest = SpecialPowers.spawn(
     65    xhrTab.linkedBrowser.browsingContext,
     66    [],
     67    () => {
     68      var xhr = new content.XMLHttpRequest();
     69      xhr.open("GET", "slow.sjs", false);
     70      content.document.dispatchEvent(
     71        new content.Event("xhrRequestIsReady", { bubbles: true })
     72      );
     73      xhr.send(null);
     74    }
     75  );
     76 
     77  // Need to wait for the xhrIsReady event because spawn is async,
     78  // so the content needs to give us a signal that the sync XHR request
     79  // has started
     80  await xhrRequestIsReady;
     81 
     82  let childTab = gBrowser.tabs[2];
     83 
     84  // Since the xhrTab has started the sync XHR request,
     85  // the InputTaskManager should be suspended here
     86  // if it is in the same browsing context as the opener
     87  let isSuspendedBeforeSwitch = await SpecialPowers.spawn(
     88    childTab.linkedBrowser.browsingContext,
     89    [],
     90    () => {
     91      var utils = SpecialPowers.getDOMWindowUtils(content);
     92      return utils.isInputTaskManagerSuspended;
     93    }
     94  );
     95 
     96  is(
     97    isSuspendedBeforeSwitch,
     98    openChildInSameBCG,
     99    "InputTaskManager should be suspended before tab switching"
    100  );
    101 
    102  // Switching away from the childTab and switching back to
    103  // test the status of InputTaskManager gets updated accordingly
    104  // based on whether the childTab is in the same BCG as the xhrTab or not.
    105  await BrowserTestUtils.switchTab(gBrowser, xhrTab);
    106  await BrowserTestUtils.switchTab(gBrowser, childTab);
    107 
    108  let isSuspendedAfterTabSwitch = await SpecialPowers.spawn(
    109    childTab.linkedBrowser.browsingContext,
    110    [],
    111    () => {
    112      var utils = SpecialPowers.getDOMWindowUtils(content);
    113      return utils.isInputTaskManagerSuspended;
    114    }
    115  );
    116 
    117  is(
    118    isSuspendedAfterTabSwitch,
    119    openChildInSameBCG,
    120    "InputTaskManager should be either suspended or not suspended based whether childTab was opened in the same BCG"
    121  );
    122 
    123  await xhrRequest;
    124 
    125  let isSuspendedAfterXHRRequest = await SpecialPowers.spawn(
    126    xhrTab.linkedBrowser.browsingContext,
    127    [],
    128    () => {
    129      var utils = SpecialPowers.getDOMWindowUtils(content);
    130      return utils.isInputTaskManagerSuspended;
    131    }
    132  );
    133 
    134  is(
    135    isSuspendedAfterXHRRequest,
    136    false,
    137    "InputTaskManager should not be suspended before after the sync XHR is done"
    138  );
    139 
    140  gBrowser.removeTab(xhrTab);
    141  gBrowser.removeTab(childTab);
    142 }
    143 
    144 add_task(async function switchBCG() {
    145  await checkInputManagerStatus(true);
    146  await checkInputManagerStatus(false);
    147 });