From 3591814a19a565baf1ba972549f601281ed7ada9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A3o=20Gottwald?= Date: Sat, 31 Oct 2009 01:04:07 +0100 Subject: [PATCH] Bug 509298 - updateCurrentBrowser leaves focus in location bar if no specific element is focused in that browser. r=enn, ui-r=beltzner --- .../tests/mochitest/name_nsRootAcc_wnd.xul | 5 +++- browser/base/content/tabbrowser.xml | 15 +++------- browser/base/content/test/browser_tabfocus.js | 28 +++++++++---------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/accessible/tests/mochitest/name_nsRootAcc_wnd.xul b/accessible/tests/mochitest/name_nsRootAcc_wnd.xul index 528f9750e64..70efbe5695d 100644 --- a/accessible/tests/mochitest/name_nsRootAcc_wnd.xul +++ b/accessible/tests/mochitest/name_nsRootAcc_wnd.xul @@ -21,7 +21,10 @@ isBusy: false, setOverLink: function (link, b) { } - } + }; + var gFindBar = { + hidden: true + }; //////////////////////////////////////////////////////////////////////////// // Invoker implementation. diff --git a/browser/base/content/tabbrowser.xml b/browser/base/content/tabbrowser.xml index b97ccb01386..a39de196a2f 100644 --- a/browser/base/content/tabbrowser.xml +++ b/browser/base/content/tabbrowser.xml @@ -872,10 +872,6 @@ this._lastRelatedTab = null; - var fm = Components.classes["@mozilla.org/focus-manager;1"]. - getService(Components.interfaces.nsIFocusManager); - var focusedChromeElement = fm.getFocusedElementForWindow(window, false, {}); - var oldBrowser = this.mCurrentBrowser; if (oldBrowser) oldBrowser.setAttribute("type", "content-targetable"); @@ -970,13 +966,10 @@ event.initEvent("TabSelect", true, false); this.mCurrentTab.dispatchEvent(event); - // change focus to the new tab if nothing is focused, the old tab - // is focused or there is something in the new tab to focus. One - // specific case where focus is not changed is when the new tab - // has no focused element and a chrome element is focused. - if ((!focusedChromeElement || focusedChromeElement == oldBrowser || - fm.getFocusedElementForWindow(window.content, true, {}))) - fm.setFocus(newBrowser, fm.FLAG_NOSCROLL); + // Change focus to the new browser unless the findbar is focused. + if (gFindBar.hidden || + gFindBar.getElement("findbar-textbox").getAttribute("focused") != "true") + newBrowser.focus(); } ]]> diff --git a/browser/base/content/test/browser_tabfocus.js b/browser/base/content/test/browser_tabfocus.js index b311ff9a24c..d1983dabaf4 100644 --- a/browser/base/content/test/browser_tabfocus.js +++ b/browser/base/content/test/browser_tabfocus.js @@ -6,13 +6,11 @@ let testPage1 = "data:text/html,"; let testPage3 = "data:text/html,"; -var browser1; - function test() { waitForExplicitFinish(); var tab1 = gBrowser.addTab(); - browser1 = gBrowser.getBrowserForTab(tab1); + var browser1 = gBrowser.getBrowserForTab(tab1); var tab2 = gBrowser.addTab(); var browser2 = gBrowser.getBrowserForTab(tab2); @@ -34,25 +32,26 @@ function test() { window.addEventListener("focus", _browser_tabfocus_test_eventOccured, true); window.addEventListener("blur", _browser_tabfocus_test_eventOccured, true); - gBrowser.selectedTab = tab2; - - var fm = Components.classes["@mozilla.org/focus-manager;1"]. - getService(Components.interfaces.nsIFocusManager); - is(fm.focusedWindow, window, "focusedWindow after tab load"); - is(fm.focusedElement, gURLBar.inputField, "focusedElement after tab load"); - // make sure that the focus initially starts out blank + var fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager); var focusedWindow = {}; is(fm.getFocusedElementForWindow(browser1.contentWindow, false, focusedWindow), null, "initial focus in tab 1"); is(focusedWindow.value, browser1.contentWindow, "initial frame focus in tab 1"); is(fm.getFocusedElementForWindow(browser2.contentWindow, false, focusedWindow), null, "initial focus in tab 2"); is(focusedWindow.value, browser2.contentWindow, "initial frame focus in tab 2"); + expectFocusShift(function () gBrowser.selectedTab = tab2, + browser2.contentWindow, null, true, + "focusedElement after tab change, focus in new tab"); + // switching tabs when the urlbar is focused and nothing in the new tab is focused - // should keep focus in the urlbar + // should focus the browser + expectFocusShift(function () gURLBar.focus(), + window, gURLBar.inputField, true, + "url field focused"); expectFocusShift(function () gBrowser.selectedTab = tab1, - window, gURLBar.inputField, false, - "focusedElement after tab change, focus in url field, no focus in new tab"); + browser1.contentWindow, null, true, + "focusedElement after tab change, focus in new tab"); // focusing a button in the current tab should focus it var button1 = browser1.contentDocument.getElementById("button1"); @@ -223,8 +222,7 @@ function expectFocusShift(callback, expectedWindow, expectedElement, focusChange is(_browser_tabfocus_test_events, expectedEvents, testid + " events"); _browser_tabfocus_test_events = ""; - var fm = Components.classes["@mozilla.org/focus-manager;1"]. - getService(Components.interfaces.nsIFocusManager); + var fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager); var focusedElement = fm.focusedElement; is(focusedElement ? getId(focusedElement) : "none",