summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_newwindow_focus.js
blob: dbf99f12335b9246000b76f1d406322f0c5a72ec (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
"use strict";

/**
 * These tests are for the auto-focus behaviour on the initial browser
 * when a window is opened from content.
 */

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>`;

/**
 * Test that when a new window is opened from content, focus moves
 * to the initial browser in that window once the window has finished
 * painting.
 */
add_task(async function test_focus_browser() {
  await BrowserTestUtils.withNewTab(
    {
      url: PAGE,
      gBrowser,
    },
    async function (browser) {
      let newWinPromise = BrowserTestUtils.domWindowOpenedAndLoaded(null);
      let delayedStartupPromise = BrowserTestUtils.waitForNewWindow();

      await BrowserTestUtils.synthesizeMouseAtCenter("#target", {}, browser);
      let newWin = await newWinPromise;
      await BrowserTestUtils.waitForContentEvent(
        newWin.gBrowser.selectedBrowser,
        "MozAfterPaint"
      );
      await delayedStartupPromise;

      let focusedElement = Services.focus.getFocusedElementForWindow(
        newWin,
        false,
        {}
      );

      Assert.equal(
        focusedElement,
        newWin.gBrowser.selectedBrowser,
        "Initial browser should be focused"
      );

      await BrowserTestUtils.closeWindow(newWin);
    }
  );
});

/**
 * Test that when a new window is opened from content and focus
 * shifts in that window before the content has a chance to paint
 * that we _don't_ steal focus once content has painted.
 */
add_task(async function test_no_steal_focus() {
  await BrowserTestUtils.withNewTab(
    {
      url: PAGE,
      gBrowser,
    },
    async function (browser) {
      let newWinPromise = BrowserTestUtils.domWindowOpenedAndLoaded(null);
      let delayedStartupPromise = BrowserTestUtils.waitForNewWindow();

      await BrowserTestUtils.synthesizeMouseAtCenter("#target", {}, browser);
      let newWin = await newWinPromise;

      // Because we're switching focus, we shouldn't steal it once
      // content paints.
      newWin.gURLBar.focus();

      await BrowserTestUtils.waitForContentEvent(
        newWin.gBrowser.selectedBrowser,
        "MozAfterPaint"
      );
      await delayedStartupPromise;

      let focusedElement = Services.focus.getFocusedElementForWindow(
        newWin,
        false,
        {}
      );

      Assert.equal(
        focusedElement,
        newWin.gURLBar.inputField,
        "URLBar should be focused"
      );

      await BrowserTestUtils.closeWindow(newWin);
    }
  );
});