summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/browser/browser_waitForFocus.js
blob: b41b07f4238aba8c4f6cd34ac5c0398af5902964 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const gBaseURL = "https://example.com/browser/testing/mochitest/tests/browser/";

function promiseTabLoadEvent(tab, url) {
  let promise = BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, url);
  if (url) {
    tab.linkedBrowser.loadURI(Services.io.newURI(url));
  }
  return promise;
}

// Load a new blank tab
add_task(async function () {
  await BrowserTestUtils.openNewForegroundTab(gBrowser);

  gURLBar.focus();

  let browser = gBrowser.selectedBrowser;
  await SimpleTest.promiseFocus(browser, true);

  is(
    document.activeElement,
    browser,
    "Browser is focused when about:blank is loaded"
  );

  gBrowser.removeCurrentTab();
  gURLBar.focus();
});

add_task(async function () {
  await BrowserTestUtils.openNewForegroundTab(gBrowser);

  gURLBar.focus();

  let browser = gBrowser.selectedBrowser;
  // If we're running in e10s, we don't have access to the content
  // window, so only test window arguments in non-e10s mode.
  if (browser.contentWindow) {
    await SimpleTest.promiseFocus(browser.contentWindow, true);

    is(
      document.activeElement,
      browser,
      "Browser is focused when about:blank is loaded"
    );
  }

  gBrowser.removeCurrentTab();
  gURLBar.focus();
});

// Load a tab with a subframe inside it and wait until the subframe is focused
add_task(async function () {
  let tab = BrowserTestUtils.addTab(gBrowser);
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  // If we're running in e10s, we don't have access to the content
  // window, so only test <iframe> arguments in non-e10s mode.
  if (browser.contentWindow) {
    await promiseTabLoadEvent(tab, gBaseURL + "waitForFocusPage.html");

    await SimpleTest.promiseFocus(browser.contentWindow);

    is(
      document.activeElement,
      browser,
      "Browser is focused when page is loaded"
    );

    await SimpleTest.promiseFocus(browser.contentWindow.frames[0]);

    is(
      browser.contentWindow.document.activeElement.localName,
      "iframe",
      "Child iframe is focused"
    );
  }

  gBrowser.removeCurrentTab();
});

// Pass a browser to promiseFocus
add_task(async function () {
  await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    gBaseURL + "waitForFocusPage.html"
  );

  gURLBar.focus();

  await SimpleTest.promiseFocus(gBrowser.selectedBrowser);

  is(
    document.activeElement,
    gBrowser.selectedBrowser,
    "Browser is focused when promiseFocus is passed a browser"
  );

  gBrowser.removeCurrentTab();
});

// Tests focusing the sidebar, which is in a parent process subframe
// and then switching the focus to another window.
add_task(async function () {
  await SidebarUI.show("viewBookmarksSidebar");

  gURLBar.focus();

  // Focus the sidebar.
  await SimpleTest.promiseFocus(SidebarUI.browser);
  is(
    document.activeElement,
    document.getElementById("sidebar"),
    "sidebar focused"
  );
  ok(
    document.activeElement.contentDocument.hasFocus(),
    "sidebar document hasFocus"
  );

  // Focus the sidebar again, which should cause no change.
  await SimpleTest.promiseFocus(SidebarUI.browser);
  is(
    document.activeElement,
    document.getElementById("sidebar"),
    "sidebar focused"
  );
  ok(
    document.activeElement.contentDocument.hasFocus(),
    "sidebar document hasFocus"
  );

  // Focus another window. The sidebar should no longer be focused.
  let window2 = await BrowserTestUtils.openNewBrowserWindow();
  is(
    document.activeElement,
    document.getElementById("sidebar"),
    "sidebar focused after window 2 opened"
  );
  ok(
    !document.activeElement.contentDocument.hasFocus(),
    "sidebar document hasFocus after window 2 opened"
  );

  // Focus the first window again and the sidebar should be focused again.
  await SimpleTest.promiseFocus(window);
  is(
    document.activeElement,
    document.getElementById("sidebar"),
    "sidebar focused after window1 refocused"
  );
  ok(
    document.activeElement.contentDocument.hasFocus(),
    "sidebar document hasFocus after window1 refocused"
  );

  await BrowserTestUtils.closeWindow(window2);
  await SidebarUI.hide();
});