Bug 564115 - activating the browser window scrolls the document (to show the focused text control); r=roc
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 06 May 2010 19:19:35 -0400
changeset 42171 6a779c75c7bff36632a57a1830e4edb55eeb9644
parent 42170 cfa870bf8e382f2a4c9925d5d53b8dc5b6267c72
child 42172 36b3e529f9dbc76d27e790fc141830c530f80615
push id13212
push usereakhgari@mozilla.com
push dateTue, 11 May 2010 21:24:24 +0000
treeherdermozilla-central@36b3e529f9db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs564115
milestone1.9.3a5pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 564115 - activating the browser window scrolls the document (to show the focused text control); r=roc
layout/forms/nsTextControlFrame.cpp
layout/forms/test/Makefile.in
layout/forms/test/bug564115_window.html
layout/forms/test/test_bug564115.html
--- a/layout/forms/nsTextControlFrame.cpp
+++ b/layout/forms/nsTextControlFrame.cpp
@@ -1933,33 +1933,37 @@ void nsTextControlFrame::SetFocus(PRBool
   if (!weakFrame.IsAlive())
   {
     return;
   }
 
   if (NS_SUCCEEDED(InitFocusedValue()))
     MaybeBeginSecureKeyboardInput();
 
-  // Scroll the current selection into view
-  nsRefPtr<ScrollOnFocusEvent> event = new ScrollOnFocusEvent(this);
-  nsresult rv = NS_DispatchToCurrentThread(event);
-  if (NS_SUCCEEDED(rv)) {
-    mScrollEvent = event;
-  }
-
-  // tell the caret to use our selection
-
   nsCOMPtr<nsISelection> ourSel;
   mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL, 
     getter_AddRefs(ourSel));
   if (!ourSel) return;
 
   nsIPresShell* presShell = PresContext()->GetPresShell();
   nsRefPtr<nsCaret> caret = presShell->GetCaret();
   if (!caret) return;
+
+  // Scroll the current selection into view
+  nsISelection *caretSelection = caret->GetCaretDOMSelection();
+  const PRBool isFocusedRightNow = ourSel == caretSelection;
+  if (!isFocusedRightNow) {
+    nsRefPtr<ScrollOnFocusEvent> event = new ScrollOnFocusEvent(this);
+    nsresult rv = NS_DispatchToCurrentThread(event);
+    if (NS_SUCCEEDED(rv)) {
+      mScrollEvent = event;
+    }
+  }
+
+  // tell the caret to use our selection
   caret->SetCaretDOMSelection(ourSel);
 
   // mutual-exclusion: the selection is either controlled by the
   // document or by the text input/area. Clear any selection in the
   // document since the focus is now on our independent selection.
 
   nsCOMPtr<nsISelectionController> selCon(do_QueryInterface(presShell));
   nsCOMPtr<nsISelection> docSel;
--- a/layout/forms/test/Makefile.in
+++ b/layout/forms/test/Makefile.in
@@ -62,12 +62,14 @@ include $(topsrcdir)/config/rules.mk
 		test_textarea_resize.html \
 		test_bug478219.xhtml \
 		test_bug542914.html \
 		test_bug536567.html \
 		     bug536567_subframe.html \
 		test_bug549170.html \
 		test_bug562447.html \
 		test_bug563642.html \
+		test_bug564115.html \
+		bug564115_window.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $^ $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/layout/forms/test/bug564115_window.html
@@ -0,0 +1,17 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Window for Bug 564115</title>
+</head>
+<body>
+<input type="text">
+<div style="height: 10000px;"></div>
+<script>
+
+addEventListener("load", function() {
+  opener.childLoaded();
+}, false);
+
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/forms/test/test_bug564115.html
@@ -0,0 +1,56 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=564115
+-->
+<head>
+  <title>Test for Bug 564115</title>
+  <script type="text/javascript" src="/MochiKit/packed.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+
+</head>
+<body>
+<p><a target="_blank" href="https://bugzilla.mozilla.org/show_bug?id=564115">Mozilla Bug 564115</a>
+
+<pre id="test">
+<script>
+
+const TEST_URL = "/tests/layout/forms/test/bug564115_window.html";
+
+addLoadEvent(function() {
+  var win = open(TEST_URL, "", "width=600,height=600");
+  window.childLoaded = function() {
+    var doc = win.document;
+    var input = doc.querySelector("input");
+
+    // Focus the input box, and wait for the focus to actually happen
+    input.focus();
+    setTimeout(function() {
+      // Scroll down a bit
+      win.scrollTo(0, 5000);
+
+      setTimeout(function() {
+        is(win.pageYOffset, 5000, "Page should be scrolled correctly");
+
+        // Refocus the window
+        SimpleTest.waitForFocus(function() {
+          SimpleTest.waitForFocus(function() {
+            is(win.pageYOffset, 5000,
+               "The page's scroll offset should not have been changed");
+
+            win.close();
+            SimpleTest.finish();
+          }, win);
+        });
+      }, 0);
+    }, 0);
+  };
+});
+
+SimpleTest.waitForExplicitFinish();
+</script>
+</pre>
+
+</body>
+</html>