Bug 509298 - updateCurrentBrowser leaves focus in location bar if no specific element is focused in that browser. r=enn, ui-r=beltzner
--- a/accessible/tests/mochitest/name_nsRootAcc_wnd.xul
+++ b/accessible/tests/mochitest/name_nsRootAcc_wnd.xul
@@ -16,17 +16,20 @@
gOpenerWnd.SimpleTest.is(aExpected, aActual, aMsg);
}
// Hack to make xul:tabbrowser work.
var XULBrowserWindow = {
isBusy: false,
setOverLink: function (link, b) {
}
- }
+ };
+ var gFindBar = {
+ hidden: true
+ };
////////////////////////////////////////////////////////////////////////////
// Invoker implementation.
function switchTabSelectChecker(aInvoker)
{
this.type = "select";
this.target getter = function() { return aInvoker.getTabsElm(); }
--- a/browser/base/content/tabbrowser.xml
+++ b/browser/base/content/tabbrowser.xml
@@ -867,20 +867,16 @@
return;
// Preview mode should not reset the owner
if (!this._previewMode && this.mCurrentTab != this.selectedTab)
this.mCurrentTab.owner = null;
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");
var updatePageReport = false;
if (!oldBrowser ||
(oldBrowser.pageReport && !newBrowser.pageReport) ||
(!oldBrowser.pageReport && newBrowser.pageReport))
@@ -965,23 +961,20 @@
// that might rely upon the other changes suppressed.
// Focus is suppressed in the event that the main browser window is minimized - focusing a tab would restore the window
if (!this._previewMode) {
// We've selected the new tab, so go ahead and notify listeners.
var event = document.createEvent("Events");
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();
}
]]>
</body>
</method>
<method name="onTabClick">
<parameter name="event"/>
<body>
--- a/browser/base/content/test/browser_tabfocus.js
+++ b/browser/base/content/test/browser_tabfocus.js
@@ -1,23 +1,21 @@
/*
* This test checks that focus is adjusted properly when switching tabs.
*/
let testPage1 = "data:text/html,<html id='tab1'><body><button id='button1'>Tab 1</button></body></html>";
let testPage2 = "data:text/html,<html id='tab2'><body><button id='button2'>Tab 2</button></body></html>";
let testPage3 = "data:text/html,<html id='tab3'><body><button id='button3'>Tab 3</button></body></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);
gURLBar.focus();
var loadCount = 0;
function check()
@@ -29,35 +27,36 @@ function test() {
window.focus();
_browser_tabfocus_test_lastfocus = gURLBar;
_browser_tabfocus_test_lastfocuswindow = window;
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");
expectFocusShift(function () button1.focus(),
browser1.contentWindow, button1, true,
"focusedWindow after focus in focused tab");
// focusing a button in a background tab should not change the actual
@@ -218,18 +217,17 @@ function expectFocusShift(callback, expe
_browser_tabfocus_test_lastfocuswindow = expectedWindow;
}
callback();
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",
expectedElement ? getId(expectedElement) : "none", testid + " focusedElement");
is(fm.focusedWindow, expectedWindow, testid + " focusedWindow");
var focusedWindow = {};
is(fm.getFocusedElementForWindow(expectedWindow, false, focusedWindow),
expectedElement, testid + " getFocusedElementForWindow");