summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_document_focus.js
blob: 070c97623d51f1da69e4f99011899946877ead15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Check that focus is restored to content page after closing the console. See Bug 588342.
const TEST_URI =
  "data:text/html;charset=utf-8,<!DOCTYPE html>Test content focus after closing console";

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);

  info("Focus after console is opened");
  ok(isInputFocused(hud), "input node is focused after console is opened");

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.onFocus = new Promise(resolve => {
      content.addEventListener("focus", resolve, { once: true });
    });
  });

  info("Closing console");
  await closeConsole();

  const isFocused = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    async function () {
      await content.onFocus;
      return Services.focus.focusedWindow == content;
    }
  );
  ok(isFocused, "content document has focus after closing the console");
});

add_task(async function testSeparateWindowToolbox() {
  const hud = await openNewTabAndConsole(TEST_URI, true, "window");

  info("Focus after console is opened");
  ok(isInputFocused(hud), "input node is focused after console is opened");

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.onFocus = new Promise(resolve => {
      content.addEventListener("focus", resolve, { once: true });
    });
  });

  info("Closing console");
  await closeConsole();

  const isFocused = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    async function () {
      await content.onFocus;
      return Services.focus.focusedWindow == content;
    }
  );
  ok(isFocused, "content document has focus after closing the console");
});

add_task(async function testSeparateWindowToolboxInactiveTab() {
  await openNewTabAndConsole(TEST_URI, true, "window");

  info("Focus after console is opened");
  const firstTab = gBrowser.selectedTab;
  await addTab(`data:text/html,<!DOCTYPE html><meta charset=utf8>New tab XXX`);

  await SpecialPowers.spawn(firstTab.linkedBrowser, [], async () => {
    // For some reason, there is no blur event fired on the document
    await ContentTaskUtils.waitForCondition(
      () => !content.browsingContext.isActive && !content.document.hasFocus(),
      "Waiting for first tab to become inactive"
    );
    content.onFocus = new Promise(resolve => {
      content.addEventListener("focus", resolve, { once: true });
    });
  });

  info("Closing console");
  await closeConsole(firstTab);

  const onFirstTabFocus = SpecialPowers.spawn(
    firstTab.linkedBrowser,
    [],
    async function () {
      await content.onFocus;
      return "focused";
    }
  );
  const timeoutRes = "time's out";
  const onTimeout = wait(2000).then(() => timeoutRes);
  const res = await Promise.race([onFirstTabFocus, onTimeout]);
  is(
    res,
    timeoutRes,
    "original tab wasn't focused when closing the toolbox window"
  );
});