From 7a8fd6fede5ee48d1a7b670a78b8a842c205788a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 5 Feb 2020 13:14:42 +0000 Subject: [PATCH] Bug 1484213 - Try to make browser_autofocus_background.js more reliable. r=dao I think the focus shenanigans instead of waiting for tab-switching or what not may be tripping this tests and causing the background tab to be blurred, but I didn't spend too much time trying to reproduce the failure. Differential Revision: https://phabricator.services.mozilla.com/D61096 --HG-- extra : moz-landing-system : lando --- .../browser/browser_autofocus_background.js | 73 ++++++++++--------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/dom/tests/browser/browser_autofocus_background.js b/dom/tests/browser/browser_autofocus_background.js index f31a9532cd85..b47f0f6860bd 100644 --- a/dom/tests/browser/browser_autofocus_background.js +++ b/dom/tests/browser/browser_autofocus_background.js @@ -1,49 +1,52 @@ add_task(async function() { - let tabs = [gBrowser.selectedTab, BrowserTestUtils.addTab(gBrowser)]; + const URL = + "data:text/html,"; + const foregroundTab = gBrowser.selectedTab; + const backgroundTab = BrowserTestUtils.addTab(gBrowser); - // The first tab has an autofocused element. - // The second tab is exactly like the first one without the autofocus. - let testingList = [ - { - uri: - "data:text/html,", - tagName: "INPUT", - }, - ]; - - // Set the focus to the first tab. - tabs[0].linkedBrowser.focus(); + // Ensure tab is still in the foreground. + is( + gBrowser.selectedTab, + foregroundTab, + "foregroundTab should still be selected" + ); // Load the second tab in the background. - let loadedPromise = BrowserTestUtils.browserLoaded(tabs[1].linkedBrowser); - BrowserTestUtils.loadURI(tabs[1].linkedBrowser, testingList[0].uri); + const loadedPromise = BrowserTestUtils.browserLoaded( + backgroundTab.linkedBrowser, + /* includesubframes */ false, + URL + ); + BrowserTestUtils.loadURI(backgroundTab.linkedBrowser, URL); await loadedPromise; - for (var i = 0; i < testingList.length; ++i) { - // Get active element in the tab. - let tagName = await SpecialPowers.spawn( - tabs[i + 1].linkedBrowser, - [], - async function() { - return content.document.activeElement.tagName; - } - ); + // Get active element in the tab. + let tagName = await SpecialPowers.spawn( + backgroundTab.linkedBrowser, + [], + async function() { + return content.document.activeElement.tagName; + } + ); - is( - tagName, - testingList[i].tagName, - "The background tab's focused element should be " + testingList[i].tagName - ); - } + is( + tagName, + "INPUT", + "The background tab's focused element should be the " + ); + + is( + gBrowser.selectedTab, + foregroundTab, + "foregroundTab tab should still be selected, shouldn't cause a tab switch" + ); is( document.activeElement, - tabs[0].linkedBrowser, - "The background tab's focused element should not cause the tab to be focused" + foregroundTab.linkedBrowser, + "The background tab's focused element should not cause the tab to be selected" ); // Cleaning up. - for (let i = 1; i < tabs.length; i++) { - BrowserTestUtils.removeTab(tabs[i]); - } + BrowserTestUtils.removeTab(backgroundTab); });