tor-browser

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

browser_bug1703472.js (2460B)


      1 /* -*- Mode: JavaScript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 const BASE_URL = "http://mochi.test:8888/browser/dom/base/test/";
      8 
      9 add_task(async function bug1703472() {
     10  await SpecialPowers.pushPrefEnv({
     11    set: [
     12      ["test.wait300msAfterTabSwitch", true],
     13      // Enable the popup blocker
     14      ["dom.disable_open_during_load", true],
     15    ],
     16  });
     17 
     18  await BrowserTestUtils.withNewTab(
     19    BASE_URL + "file_bug1703472.html",
     20    async function (browser) {
     21      info("Opening about:blank popup");
     22      let win = await newFocusedWindow(function () {
     23        return BrowserTestUtils.synthesizeMouseAtCenter(
     24          "#openWindow",
     25          {},
     26          browser
     27        );
     28      }, true);
     29      is(Services.focus.focusedWindow, win, "New window should be focused");
     30 
     31      info("re-focusing the original window");
     32      {
     33        let focusBack = BrowserTestUtils.waitForEvent(window, "focus", true);
     34        window.focus();
     35        await focusBack;
     36        is(Services.focus.focusedWindow, window, "should focus back");
     37      }
     38 
     39      // The click to do window.open() should've consumed the user interaction,
     40      // and an artificial .click() shouldn't count as a user interaction, so the
     41      // page shouldn't be allowed to focus it again without user interaction.
     42      info("Trying to steal focus without interaction");
     43      await SpecialPowers.spawn(browser, [], function () {
     44        content.document.querySelector("#focusWindow").click();
     45      });
     46 
     47      // We need to wait for something _not_ happening, so we need to use an arbitrary setTimeout.
     48      await new Promise(resolve => {
     49        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     50        setTimeout(resolve, 500);
     51      });
     52 
     53      is(Services.focus.focusedWindow, window, "should still be focused");
     54 
     55      info("Trying to move focus with user interaction");
     56      {
     57        let focus = BrowserTestUtils.waitForEvent(win, "focus", true);
     58        await BrowserTestUtils.synthesizeMouseAtCenter(
     59          "#focusWindow",
     60          {},
     61          browser
     62        );
     63        await focus;
     64        is(Services.focus.focusedWindow, win, "should focus back");
     65      }
     66 
     67      win.close();
     68    }
     69  );
     70 });