Bug 1600619 - Don't focus the content page when closing the console when the tab it was opened for is now inactive. r=ochameau.

This patch checks for window.docShell.isActive to ensure
we should indeed restore the focus on the page.
A test case is added to make sure it works as expected
and that we don't regress.

Differential Revision: https://phabricator.services.mozilla.com/D60277

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Nicolas Chevobbe 2020-02-10 16:27:24 +00:00
Родитель ec1ab205e9
Коммит ccf7c09e21
3 изменённых файлов: 73 добавлений и 5 удалений

Просмотреть файл

@ -14,7 +14,7 @@ add_task(async function() {
ok(isInputFocused(hud), "input node is focused after console is opened");
await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function() {
this.onFocus = new Promise(resolve => {
content.onFocus = new Promise(resolve => {
content.addEventListener("focus", resolve, { once: true });
});
});
@ -26,9 +26,74 @@ add_task(async function() {
gBrowser.selectedBrowser,
[],
async function() {
await this.onFocus;
await content.onFocus;
return Services.focus.focusedWindow == content;
}
);
ok(isFocused, "content document has focus after closing the console");
});
add_task(async function testSeparateWindowToolbox() {
const hud = await openNewTabAndConsole(TEST_URI, true, "window");
info("Focus after console is opened");
ok(isInputFocused(hud), "input node is focused after console is opened");
await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function() {
content.onFocus = new Promise(resolve => {
content.addEventListener("focus", resolve, { once: true });
});
});
info("Closing console");
await closeConsole();
const isFocused = await SpecialPowers.spawn(
gBrowser.selectedBrowser,
[],
async function() {
await content.onFocus;
return Services.focus.focusedWindow == content;
}
);
ok(isFocused, "content document has focus after closing the console");
});
add_task(async function testSeparateWindowToolboxInactiveTab() {
await openNewTabAndConsole(TEST_URI, true, "window");
info("Focus after console is opened");
const firstTab = gBrowser.selectedTab;
await addTab(`data:text/html,<meta charset=utf8>New tab XXX`);
await SpecialPowers.spawn(firstTab.linkedBrowser, [], async () => {
// For some reason, there is no blur event fired on the document
await ContentTaskUtils.waitForCondition(
() => !docShell.isActive && !content.document.hasFocus(),
"Waiting for first tab to become inactive"
);
content.onFocus = new Promise(resolve => {
content.addEventListener("focus", resolve, { once: true });
});
});
info("Closing console");
await closeConsole(firstTab);
const onFirstTabFocus = SpecialPowers.spawn(
firstTab.linkedBrowser,
[],
async function() {
await content.onFocus;
return "focused";
}
);
const timeoutRes = "time's out";
const onTimeout = wait(2000).then(() => timeoutRes);
const res = await Promise.race([onFirstTabFocus, onTimeout]);
is(
res,
timeoutRes,
"original tab wasn't focused when closing the toolbox window"
);
});

Просмотреть файл

@ -74,12 +74,14 @@ registerCleanupFunction(async function() {
* The URL for the tab to be opened.
* @param Boolean clearJstermHistory
* true (default) if the jsterm history should be cleared.
* @param String hostId (optional)
* The type of toolbox host to be used.
* @return Promise
* Resolves when the tab has been added, loaded and the toolbox has been opened.
* Resolves to the toolbox.
*/
async function openNewTabAndConsole(url, clearJstermHistory = true) {
const toolbox = await openNewTabAndToolbox(url, "webconsole");
async function openNewTabAndConsole(url, clearJstermHistory = true, hostId) {
const toolbox = await openNewTabAndToolbox(url, "webconsole", hostId);
const hud = toolbox.getCurrentPanel().hud;
if (clearJstermHistory) {

Просмотреть файл

@ -1168,7 +1168,8 @@ const browsingContextTargetPrototype = {
this._setCacheDisabled(false);
this._setServiceWorkersTestingEnabled(false);
this._setPaintFlashingEnabled(false);
if (this._restoreFocus) {
if (this._restoreFocus && this.window.docShell.isActive) {
this.window.focus();
}
},