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