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);
6 tab.linkedBrowser.loadURI(Services.io.newURI(url));
11 // Load a new blank tab
12 add_task(async function () {
13 await BrowserTestUtils.openNewForegroundTab(gBrowser);
17 let browser = gBrowser.selectedBrowser;
18 await SimpleTest.promiseFocus(browser, true);
21 document.activeElement,
23 "Browser is focused when about:blank is loaded"
26 gBrowser.removeCurrentTab();
30 add_task(async function () {
31 await BrowserTestUtils.openNewForegroundTab(gBrowser);
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);
42 document.activeElement,
44 "Browser is focused when about:blank is loaded"
48 gBrowser.removeCurrentTab();
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);
66 document.activeElement,
68 "Browser is focused when page is loaded"
71 await SimpleTest.promiseFocus(browser.contentWindow.frames[0]);
74 browser.contentWindow.document.activeElement.localName,
76 "Child iframe is focused"
80 gBrowser.removeCurrentTab();
83 // Pass a browser to promiseFocus
84 add_task(async function () {
85 await BrowserTestUtils.openNewForegroundTab(
87 gBaseURL + "waitForFocusPage.html"
92 await SimpleTest.promiseFocus(gBrowser.selectedBrowser);
95 document.activeElement,
96 gBrowser.selectedBrowser,
97 "Browser is focused when promiseFocus is passed a browser"
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");
110 // Focus the sidebar.
111 await SimpleTest.promiseFocus(SidebarUI.browser);
113 document.activeElement,
114 document.getElementById("sidebar"),
118 document.activeElement.contentDocument.hasFocus(),
119 "sidebar document hasFocus"
122 // Focus the sidebar again, which should cause no change.
123 await SimpleTest.promiseFocus(SidebarUI.browser);
125 document.activeElement,
126 document.getElementById("sidebar"),
130 document.activeElement.contentDocument.hasFocus(),
131 "sidebar document hasFocus"
134 // Focus another window. The sidebar should no longer be focused.
135 let window2 = await BrowserTestUtils.openNewBrowserWindow();
137 document.activeElement,
138 document.getElementById("sidebar"),
139 "sidebar focused after window 2 opened"
142 !document.activeElement.contentDocument.hasFocus(),
143 "sidebar document hasFocus after window 2 opened"
146 // Focus the first window again and the sidebar should be focused again.
147 await SimpleTest.promiseFocus(window);
149 document.activeElement,
150 document.getElementById("sidebar"),
151 "sidebar focused after window1 refocused"
154 document.activeElement.contentDocument.hasFocus(),
155 "sidebar document hasFocus after window1 refocused"
158 await BrowserTestUtils.closeWindow(window2);
159 await SidebarUI.hide();