tor-browser

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

browser_webconsole_document_focus.js (3053B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Check that focus is restored to content page after closing the console. See Bug 588342.
      7 const TEST_URI =
      8  "data:text/html;charset=utf-8,<!DOCTYPE html>Test content focus after closing console";
      9 
     10 add_task(async function () {
     11  const hud = await openNewTabAndConsole(TEST_URI);
     12 
     13  info("Focus after console is opened");
     14  ok(isInputFocused(hud), "input node is focused after console is opened");
     15 
     16  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     17    content.onFocus = new Promise(resolve => {
     18      content.addEventListener("focus", resolve, { once: true });
     19    });
     20  });
     21 
     22  info("Closing console");
     23  await closeConsole();
     24 
     25  const isFocused = await SpecialPowers.spawn(
     26    gBrowser.selectedBrowser,
     27    [],
     28    async function () {
     29      await content.onFocus;
     30      return Services.focus.focusedWindow == content;
     31    }
     32  );
     33  ok(isFocused, "content document has focus after closing the console");
     34 });
     35 
     36 add_task(async function testSeparateWindowToolbox() {
     37  const hud = await openNewTabAndConsole(TEST_URI, true, "window");
     38 
     39  info("Focus after console is opened");
     40  ok(isInputFocused(hud), "input node is focused after console is opened");
     41 
     42  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     43    content.onFocus = new Promise(resolve => {
     44      content.addEventListener("focus", resolve, { once: true });
     45    });
     46  });
     47 
     48  info("Closing console");
     49  await closeConsole();
     50 
     51  const isFocused = await SpecialPowers.spawn(
     52    gBrowser.selectedBrowser,
     53    [],
     54    async function () {
     55      await content.onFocus;
     56      return Services.focus.focusedWindow == content;
     57    }
     58  );
     59  ok(isFocused, "content document has focus after closing the console");
     60 });
     61 
     62 add_task(async function testSeparateWindowToolboxInactiveTab() {
     63  await openNewTabAndConsole(TEST_URI, true, "window");
     64 
     65  info("Focus after console is opened");
     66  const firstTab = gBrowser.selectedTab;
     67  await addTab(`data:text/html,<!DOCTYPE html><meta charset=utf8>New tab XXX`);
     68 
     69  await SpecialPowers.spawn(firstTab.linkedBrowser, [], async () => {
     70    // For some reason, there is no blur event fired on the document
     71    await ContentTaskUtils.waitForCondition(
     72      () => !content.browsingContext.isActive && !content.document.hasFocus(),
     73      "Waiting for first tab to become inactive"
     74    );
     75    content.onFocus = new Promise(resolve => {
     76      content.addEventListener("focus", resolve, { once: true });
     77    });
     78  });
     79 
     80  info("Closing console");
     81  await closeConsole(firstTab);
     82 
     83  const onFirstTabFocus = SpecialPowers.spawn(
     84    firstTab.linkedBrowser,
     85    [],
     86    async function () {
     87      await content.onFocus;
     88      return "focused";
     89    }
     90  );
     91  const timeoutRes = "time's out";
     92  const onTimeout = wait(2000).then(() => timeoutRes);
     93  const res = await Promise.race([onFirstTabFocus, onTimeout]);
     94  is(
     95    res,
     96    timeoutRes,
     97    "original tab wasn't focused when closing the toolbox window"
     98  );
     99 });