Skip to content

Commit

Permalink
Bug 511449, when a window is shown, update the system focus, as the f…
Browse files Browse the repository at this point in the history
…ocus may have been put into an about:blank page loaded beforehand, test by mstange, r=smaug
  • Loading branch information
EnnDeakin2 committed Oct 16, 2009
1 parent c67caab commit 8491400
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 13 deletions.
7 changes: 7 additions & 0 deletions docshell/test/Makefile.in
Expand Up @@ -80,5 +80,12 @@ _TEST_FILES = \
file_bug509055.html \
$(NULL)

ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
_TEST_FILES += \
test_bug511449.html \
file_bug511449.html \
$(NULL)
endif

libs:: $(_TEST_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
6 changes: 6 additions & 0 deletions docshell/test/file_bug511449.html
@@ -0,0 +1,6 @@
<!DOCTYPE HTML>
<title>Used in test for bug 511449</title>
<input type="text" id="input">
<script type="text/javascript">
document.getElementById("input").focus();
</script>
56 changes: 56 additions & 0 deletions docshell/test/test_bug511449.html
@@ -0,0 +1,56 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=511449
-->
<head>
<title>Test for Bug 511449</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=511449">Mozilla Bug 511449</a>
<p id="display"></p>
<div id="status"></div>
<div id="content">
</div>
<input type="text" id="input">
<pre id="test">
<script type="application/javascript;version=1.7">

/** Test for Bug 511449 **/

SimpleTest.waitForExplicitFinish();
window.addEventListener('load', runTest, false);

var win = null;

function runTest() {
document.getElementById("input").focus();
win = window.open("file_bug511449.html", "");
SimpleTest.waitForFocus(runNextTest, win);
}

function runNextTest() {
var didClose = false;
win.onunload = function() {
didClose = true;
}
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var utils = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
getInterface(Components.interfaces.nsIDOMWindowUtils);
utils.sendNativeKeyEvent(0, 13 /* w */, 0x4000 /* cmd */, "w", "w");

ok(didClose, "Cmd+W should have closed the tab");
if (!didClose) {
win.close();
}
SimpleTest.finish();
}

</script>

</body>
</html>
20 changes: 14 additions & 6 deletions dom/base/nsFocusManager.cpp
Expand Up @@ -772,7 +772,7 @@ nsFocusManager::ContentRemoved(nsIDocument* aDocument, nsIContent* aContent)
}

NS_IMETHODIMP
nsFocusManager::WindowShown(nsIDOMWindow* aWindow)
nsFocusManager::WindowShown(nsIDOMWindow* aWindow, PRBool aNeedsFocus)
{
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aWindow);
NS_ENSURE_TRUE(window, NS_ERROR_INVALID_ARG);
Expand Down Expand Up @@ -801,11 +801,19 @@ nsFocusManager::WindowShown(nsIDOMWindow* aWindow)
if (mFocusedWindow != window)
return NS_OK;

nsCOMPtr<nsPIDOMWindow> currentWindow;
nsCOMPtr<nsIContent> currentFocus =
GetFocusedDescendant(window, PR_TRUE, getter_AddRefs(currentWindow));
if (currentWindow)
Focus(currentWindow, currentFocus, 0, PR_TRUE, PR_FALSE, PR_FALSE);
if (aNeedsFocus) {
nsCOMPtr<nsPIDOMWindow> currentWindow;
nsCOMPtr<nsIContent> currentFocus =
GetFocusedDescendant(window, PR_TRUE, getter_AddRefs(currentWindow));
if (currentWindow)
Focus(currentWindow, currentFocus, 0, PR_TRUE, PR_FALSE, PR_FALSE);
}
else {
// Sometimes, an element in a window can be focused before the window is
// visible, which would mean that the widget may not be properly focused.
// When the window becomes visible, make sure the right widget is focused.
EnsureCurrentWidgetFocused();
}

return NS_OK;
}
Expand Down
7 changes: 2 additions & 5 deletions dom/base/nsGlobalWindow.cpp
Expand Up @@ -6836,15 +6836,12 @@ nsGlobalWindow::SetReadyForFocus()
{
FORWARD_TO_INNER_VOID(SetReadyForFocus, ());

// if we don't need to be focused, then just return
if (!mNeedsFocus)
return;

PRBool oldNeedsFocus = mNeedsFocus;
mNeedsFocus = PR_FALSE;

nsIFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm)
fm->WindowShown(this);
fm->WindowShown(this, oldNeedsFocus);
}

void
Expand Down
7 changes: 5 additions & 2 deletions dom/interfaces/base/nsIFocusManager.idl
Expand Up @@ -39,7 +39,7 @@
interface nsIDocument;
interface nsIContent;

[scriptable, uuid(CD6040A8-243F-412A-8A16-0BF2AA1083B9)]
[scriptable, uuid(64CAE24B-8C2D-4B75-9047-BC9668D85975)]
/**
* The focus manager deals with all focus related behaviour. Only one element
* in the entire application may have the focus at a time; this element
Expand Down Expand Up @@ -245,8 +245,11 @@ interface nsIFocusManager : nsISupports

/**
* Called when a new document in a window is shown.
*
* If aNeedsFocus is true, then focus events are expected to be fired on the
* window if this window is in the focused window chain.
*/
[noscript] void windowShown(in nsIDOMWindow aWindow);
[noscript] void windowShown(in nsIDOMWindow aWindow, in PRBool aNeedsFocus);

/**
* Called when a document in a window has been hidden or otherwise can no
Expand Down

0 comments on commit 8491400

Please sign in to comment.