tor-browser

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

browser_newwindow_focus.js (3183B)


      1 "use strict";
      2 
      3 /**
      4 * These tests are for the auto-focus behaviour on the initial browser
      5 * when a window is opened from content.
      6 */
      7 
      8 const PAGE = `data:text/html,<a id="target" href="%23" onclick="window.open('http://www.example.com', '_blank', 'width=100,height=100');">Click me</a>`;
      9 
     10 add_setup(async function () {
     11  await SpecialPowers.pushPrefEnv({
     12    set: [["test.wait300msAfterTabSwitch", true]],
     13  });
     14 });
     15 
     16 /**
     17 * Test that when a new window is opened from content, focus moves
     18 * to the initial browser in that window once the window has finished
     19 * painting.
     20 */
     21 add_task(async function test_focus_browser() {
     22  await BrowserTestUtils.withNewTab(
     23    {
     24      url: PAGE,
     25      gBrowser,
     26    },
     27    async function (browser) {
     28      let newWinPromise = BrowserTestUtils.domWindowOpenedAndLoaded(null);
     29      let delayedStartupPromise = BrowserTestUtils.waitForNewWindow();
     30 
     31      await BrowserTestUtils.synthesizeMouseAtCenter("#target", {}, browser);
     32      let newWin = await newWinPromise;
     33 
     34      // Set up listener for focus event to bubble up on the window
     35      let focusPromise = BrowserTestUtils.waitForEvent(newWin, "focus", true);
     36 
     37      await BrowserTestUtils.waitForContentEvent(
     38        newWin.gBrowser.selectedBrowser,
     39        "MozAfterPaint"
     40      );
     41      await delayedStartupPromise;
     42 
     43      Assert.greaterOrEqual(
     44        Glean.browserTimings.startupTimeline.delayedStartupFinished.testGetValue(),
     45        Glean.browserTimings.startupTimeline.delayedStartupStarted.testGetValue(),
     46        "Delayed startup timings in correct order."
     47      );
     48 
     49      // Wait for the focus event to occur
     50      await focusPromise;
     51 
     52      let focusedElement = Services.focus.getFocusedElementForWindow(
     53        newWin,
     54        false,
     55        {}
     56      );
     57 
     58      Assert.equal(
     59        focusedElement,
     60        newWin.gBrowser.selectedBrowser,
     61        "Initial browser should be focused"
     62      );
     63 
     64      await BrowserTestUtils.closeWindow(newWin);
     65    }
     66  );
     67 });
     68 
     69 /**
     70 * Test that when a new window is opened from content and focus
     71 * shifts in that window before the content has a chance to paint
     72 * that we _don't_ steal focus once content has painted.
     73 */
     74 add_task(async function test_no_steal_focus() {
     75  await BrowserTestUtils.withNewTab(
     76    {
     77      url: PAGE,
     78      gBrowser,
     79    },
     80    async function (browser) {
     81      let newWinPromise = BrowserTestUtils.domWindowOpenedAndLoaded(null);
     82      let delayedStartupPromise = BrowserTestUtils.waitForNewWindow();
     83 
     84      await BrowserTestUtils.synthesizeMouseAtCenter("#target", {}, browser);
     85      let newWin = await newWinPromise;
     86 
     87      // Because we're switching focus, we shouldn't steal it once
     88      // content paints.
     89      newWin.gURLBar.focus();
     90 
     91      await BrowserTestUtils.waitForContentEvent(
     92        newWin.gBrowser.selectedBrowser,
     93        "MozAfterPaint"
     94      );
     95      await delayedStartupPromise;
     96 
     97      let focusedElement = Services.focus.getFocusedElementForWindow(
     98        newWin,
     99        false,
    100        {}
    101      );
    102 
    103      Assert.equal(
    104        focusedElement,
    105        newWin.gURLBar.inputField,
    106        "URLBar should be focused"
    107      );
    108 
    109      await BrowserTestUtils.closeWindow(newWin);
    110    }
    111  );
    112 });