browser_autofocus_background.js (1610B)
1 add_task(async function () { 2 const URL = 3 "data:text/html,<!DOCTYPE html><html><body><input autofocus id='target'></body></html>"; 4 const foregroundTab = gBrowser.selectedTab; 5 const backgroundTab = BrowserTestUtils.addTab(gBrowser); 6 7 // Ensure tab is still in the foreground. 8 is( 9 gBrowser.selectedTab, 10 foregroundTab, 11 "foregroundTab should still be selected" 12 ); 13 14 // Load the second tab in the background. 15 const loadedPromise = BrowserTestUtils.browserLoaded( 16 backgroundTab.linkedBrowser, 17 /* includesubframes */ false, 18 URL 19 ); 20 BrowserTestUtils.startLoadingURIString(backgroundTab.linkedBrowser, URL); 21 await loadedPromise; 22 23 // Get active element in the tab. 24 let tagName = await SpecialPowers.spawn( 25 backgroundTab.linkedBrowser, 26 [], 27 async function () { 28 // Spec asks us to flush autofocus candidates in the 29 // `update-the-rendering` step, so we need to wait 30 // for a rAF to ensure autofocus candidates are 31 // flushed. 32 await new Promise(r => { 33 content.requestAnimationFrame(r); 34 }); 35 return content.document.activeElement.tagName; 36 } 37 ); 38 39 is( 40 tagName, 41 "INPUT", 42 "The background tab's focused element should be the <input>" 43 ); 44 45 is( 46 gBrowser.selectedTab, 47 foregroundTab, 48 "foregroundTab tab should still be selected, shouldn't cause a tab switch" 49 ); 50 51 is( 52 document.activeElement, 53 foregroundTab.linkedBrowser, 54 "The background tab's focused element should not cause the tab to be selected" 55 ); 56 57 // Cleaning up. 58 BrowserTestUtils.removeTab(backgroundTab); 59 });