tor-browser

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

browser_bug1691214.js (4137B)


      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_setup(async function () {
     10  await SpecialPowers.pushPrefEnv({
     11    set: [["test.wait300msAfterTabSwitch", true]],
     12  });
     13 });
     14 
     15 add_task(async function bug1691214() {
     16  await BrowserTestUtils.withNewTab(
     17    BASE_URL + "file_bug1691214.html",
     18    async function (browser) {
     19      let win = await newFocusedWindow(function () {
     20        return BrowserTestUtils.synthesizeMouseAtCenter("#link-1", {}, browser);
     21      });
     22      is(Services.focus.focusedWindow, win, "New window should be focused");
     23 
     24      info("re-focusing the original window");
     25 
     26      {
     27        let focusBack = BrowserTestUtils.waitForEvent(window, "focus", true);
     28        window.focus();
     29        await focusBack;
     30        is(Services.focus.focusedWindow, window, "should focus back");
     31      }
     32 
     33      info("Clicking on the second link.");
     34 
     35      {
     36        let focus = BrowserTestUtils.waitForEvent(win, "focus", true);
     37        await BrowserTestUtils.synthesizeMouseAtCenter("#link-2", {}, browser);
     38        info("Waiting for re-focus of the opened window.");
     39        await focus;
     40      }
     41 
     42      is(
     43        Services.focus.focusedWindow,
     44        win,
     45        "Existing window should've been targeted and focused"
     46      );
     47 
     48      win.close();
     49    }
     50  );
     51 });
     52 
     53 // The tab has a form infinitely submitting to an iframe, and that shouldn't
     54 // switch focus back. For that, we open a window from the tab, make sure it's
     55 // focused, and then wait for three submissions (but since we need to listen to
     56 // trusted events from chrome code, we use the iframe load event instead).
     57 add_task(async function bug1700871() {
     58  await BrowserTestUtils.withNewTab(
     59    BASE_URL + "file_bug1700871.html",
     60    async function (browser) {
     61      let win = await newFocusedWindow(function () {
     62        return BrowserTestUtils.synthesizeMouseAtCenter("#link-1", {}, browser);
     63      });
     64 
     65      is(Services.focus.focusedWindow, win, "New window should be focused");
     66 
     67      info("waiting for three submit events and ensuring focus hasn't moved");
     68 
     69      await SpecialPowers.spawn(browser, [], function () {
     70        let pending = 3;
     71        return new Promise(resolve => {
     72          content.document
     73            .querySelector("iframe")
     74            .addEventListener("load", function (e) {
     75              info("Got load on the frame: " + pending);
     76              if (!--pending) {
     77                resolve();
     78              }
     79            });
     80        });
     81      });
     82 
     83      is(Services.focus.focusedWindow, win, "Focus shouldn't have moved");
     84      win.close();
     85    }
     86  );
     87 });
     88 
     89 add_task(async function bug1793829() {
     90  await BrowserTestUtils.withNewTab(
     91    BASE_URL + "file_bug1691214.html",
     92    async function (browser) {
     93      let win = await newFocusedWindow(function () {
     94        return BrowserTestUtils.synthesizeMouseAtCenter("#link-1", {}, browser);
     95      });
     96      is(Services.focus.focusedWindow, win, "New window should be focused");
     97 
     98      info("re-focusing the original window");
     99 
    100      {
    101        let focusBack = BrowserTestUtils.waitForEvent(window, "focus", true);
    102        window.focus();
    103        await focusBack;
    104        is(Services.focus.focusedWindow, window, "should focus back");
    105      }
    106 
    107      info("Trying to steal focus.");
    108 
    109      await SpecialPowers.spawn(browser, [], function () {
    110        content.document.clearUserGestureActivation();
    111        content.document.getElementById("link-2").click();
    112      });
    113 
    114      // We need to test that nothing happened, which is hard without an
    115      // arbitrary timeout.
    116      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    117      await new Promise(c => setTimeout(c, 2000));
    118 
    119      is(
    120        Services.focus.focusedWindow,
    121        window,
    122        "Shouldn't steal focus without user gesture"
    123      );
    124 
    125      win.close();
    126    }
    127  );
    128 });