Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / testing / mochitest / tests / browser / browser_waitForFocus.js
blobb41b07f4238aba8c4f6cd34ac5c0398af5902964
1 const gBaseURL = "https://example.com/browser/testing/mochitest/tests/browser/";
3 function promiseTabLoadEvent(tab, url) {
4   let promise = BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, url);
5   if (url) {
6     tab.linkedBrowser.loadURI(Services.io.newURI(url));
7   }
8   return promise;
11 // Load a new blank tab
12 add_task(async function () {
13   await BrowserTestUtils.openNewForegroundTab(gBrowser);
15   gURLBar.focus();
17   let browser = gBrowser.selectedBrowser;
18   await SimpleTest.promiseFocus(browser, true);
20   is(
21     document.activeElement,
22     browser,
23     "Browser is focused when about:blank is loaded"
24   );
26   gBrowser.removeCurrentTab();
27   gURLBar.focus();
28 });
30 add_task(async function () {
31   await BrowserTestUtils.openNewForegroundTab(gBrowser);
33   gURLBar.focus();
35   let browser = gBrowser.selectedBrowser;
36   // If we're running in e10s, we don't have access to the content
37   // window, so only test window arguments in non-e10s mode.
38   if (browser.contentWindow) {
39     await SimpleTest.promiseFocus(browser.contentWindow, true);
41     is(
42       document.activeElement,
43       browser,
44       "Browser is focused when about:blank is loaded"
45     );
46   }
48   gBrowser.removeCurrentTab();
49   gURLBar.focus();
50 });
52 // Load a tab with a subframe inside it and wait until the subframe is focused
53 add_task(async function () {
54   let tab = BrowserTestUtils.addTab(gBrowser);
55   gBrowser.selectedTab = tab;
57   let browser = gBrowser.getBrowserForTab(tab);
58   // If we're running in e10s, we don't have access to the content
59   // window, so only test <iframe> arguments in non-e10s mode.
60   if (browser.contentWindow) {
61     await promiseTabLoadEvent(tab, gBaseURL + "waitForFocusPage.html");
63     await SimpleTest.promiseFocus(browser.contentWindow);
65     is(
66       document.activeElement,
67       browser,
68       "Browser is focused when page is loaded"
69     );
71     await SimpleTest.promiseFocus(browser.contentWindow.frames[0]);
73     is(
74       browser.contentWindow.document.activeElement.localName,
75       "iframe",
76       "Child iframe is focused"
77     );
78   }
80   gBrowser.removeCurrentTab();
81 });
83 // Pass a browser to promiseFocus
84 add_task(async function () {
85   await BrowserTestUtils.openNewForegroundTab(
86     gBrowser,
87     gBaseURL + "waitForFocusPage.html"
88   );
90   gURLBar.focus();
92   await SimpleTest.promiseFocus(gBrowser.selectedBrowser);
94   is(
95     document.activeElement,
96     gBrowser.selectedBrowser,
97     "Browser is focused when promiseFocus is passed a browser"
98   );
100   gBrowser.removeCurrentTab();
103 // Tests focusing the sidebar, which is in a parent process subframe
104 // and then switching the focus to another window.
105 add_task(async function () {
106   await SidebarUI.show("viewBookmarksSidebar");
108   gURLBar.focus();
110   // Focus the sidebar.
111   await SimpleTest.promiseFocus(SidebarUI.browser);
112   is(
113     document.activeElement,
114     document.getElementById("sidebar"),
115     "sidebar focused"
116   );
117   ok(
118     document.activeElement.contentDocument.hasFocus(),
119     "sidebar document hasFocus"
120   );
122   // Focus the sidebar again, which should cause no change.
123   await SimpleTest.promiseFocus(SidebarUI.browser);
124   is(
125     document.activeElement,
126     document.getElementById("sidebar"),
127     "sidebar focused"
128   );
129   ok(
130     document.activeElement.contentDocument.hasFocus(),
131     "sidebar document hasFocus"
132   );
134   // Focus another window. The sidebar should no longer be focused.
135   let window2 = await BrowserTestUtils.openNewBrowserWindow();
136   is(
137     document.activeElement,
138     document.getElementById("sidebar"),
139     "sidebar focused after window 2 opened"
140   );
141   ok(
142     !document.activeElement.contentDocument.hasFocus(),
143     "sidebar document hasFocus after window 2 opened"
144   );
146   // Focus the first window again and the sidebar should be focused again.
147   await SimpleTest.promiseFocus(window);
148   is(
149     document.activeElement,
150     document.getElementById("sidebar"),
151     "sidebar focused after window1 refocused"
152   );
153   ok(
154     document.activeElement.contentDocument.hasFocus(),
155     "sidebar document hasFocus after window1 refocused"
156   );
158   await BrowserTestUtils.closeWindow(window2);
159   await SidebarUI.hide();