Bug 725581 - caretOffset for textarea should be -1 when textarea doesn't have a focus, r=tbsaunde
authorAlexander Surkov <surkov.alexander@gmail.com>
Mon, 13 Feb 2012 12:37:52 +0900
changeset 86703 c646a6e4dd2e9adb76ebc9ff7af0974d101e26aa
parent 86702 128d8b6066741de1c8f0ce542f5ded825fdff9f0
child 86704 075fe28b4d34bd4e0ebe868f2aba134978743db8
push id22046
push usermak77@bonardo.net
push dateMon, 13 Feb 2012 16:18:19 +0000
treeherdermozilla-central@ebafee0cea36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstbsaunde
bugs725581
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 725581 - caretOffset for textarea should be -1 when textarea doesn't have a focus, r=tbsaunde
accessible/src/html/nsHyperTextAccessible.cpp
accessible/tests/mochitest/events/test_caretmove.html
accessible/tests/mochitest/textcaret/test_general.html
--- a/accessible/src/html/nsHyperTextAccessible.cpp
+++ b/accessible/src/html/nsHyperTextAccessible.cpp
@@ -1623,18 +1623,26 @@ nsHyperTextAccessible::SetCaretOffset(PR
 }
 
 /*
  * Gets the offset position of the caret (cursor).
  */
 NS_IMETHODIMP
 nsHyperTextAccessible::GetCaretOffset(PRInt32 *aCaretOffset)
 {
+  NS_ENSURE_ARG_POINTER(aCaretOffset);
   *aCaretOffset = -1;
 
+  // Not focused focusable accessible except document accessible doesn't have
+  // a caret.
+  if (!IsDoc() && !FocusMgr()->IsFocused(this) &&
+      (State() & states::FOCUSABLE)) {
+    return NS_OK;
+  }
+
   // No caret if the focused node is not inside this DOM node and this DOM node
   // is not inside of focused node.
   FocusManager::FocusDisposition focusDisp =
     FocusMgr()->IsInOrContainsFocus(this);
   if (focusDisp == FocusManager::eNone)
     return NS_OK;
 
   // Turn the focus node and offset of the selection into caret hypretext
--- a/accessible/tests/mochitest/events/test_caretmove.html
+++ b/accessible/tests/mochitest/events/test_caretmove.html
@@ -41,31 +41,18 @@
     /**
      * Do tests.
      */
     var gQueue = null;
 
     // gA11yEventDumpID = "eventdump"; // debug stuff
     //gA11yEventDumpToConsole = true;
 
-    function testCaretOffset(aAccOrElmOrID, aCaretOffset)
-    {
-      var acc = getAccessible(aAccOrElmOrID, [nsIAccessibleText]);
-      is(acc.caretOffset, aCaretOffset,
-         "Wrong caret offset for " + aAccOrElmOrID);
-    }
-
     function doTests()
     {
-      todo(false, "enable commented tests Bug 510128 is fixed");
-      // test no focused accessibles
-      //testCaretOffset("textbox", -1);
-      //testCaretOffset("textarea", -1);
-      testCaretOffset("p", -1);
-
       // test caret move events and caret offsets
       gQueue = new eventQueue();
 
       var id = "textbox";
       gQueue.push(new synthFocus(id, new caretMoveChecker(5, id)));
       gQueue.push(new synthSelectAll(id, new caretMoveChecker(5, id)));
       gQueue.push(new synthClick(id, new caretMoveChecker(0, id)));
       gQueue.push(new synthRightKey(id, new caretMoveChecker(1, id)));
--- a/accessible/tests/mochitest/textcaret/test_general.html
+++ b/accessible/tests/mochitest/textcaret/test_general.html
@@ -21,27 +21,43 @@
     function turnCaretBrowsing(aIsOn)
     {
       var prefs = Components.classes["@mozilla.org/preferences-service;1"].
         getService(Components.interfaces.nsIPrefBranch);
       prefs.setBoolPref("accessibility.browsewithcaret", aIsOn);
     }
 
     /**
+     * Test caret offset for the given accessible.
+     */
+    function testCaretOffset(aID, aCaretOffset)
+    {
+      var acc = getAccessible(aID, [nsIAccessibleText]);
+      is(acc.caretOffset, aCaretOffset,
+         "Wrong caret offset for " + aID);
+    }
+
+    /**
      * Do tests.
      */
     var gQueue = null;
 
     //gA11yEventDumpID = "eventdump"; // debug stuff
     //gA11yEventDumpToConsole = true;
 
     function doTests()
     {
       turnCaretBrowsing(true);
 
+      // test caret offsets
+      testCaretOffset(document, 14);
+      testCaretOffset("textbox", -1);
+      testCaretOffset("textarea", -1);
+      testCaretOffset("p", -1);
+
       // test caret move events and caret offsets
       gQueue = new eventQueue();
 
       gQueue.push(new setCaretOffset("textbox", 1, "textbox"));
       gQueue.push(new setCaretOffset("link", 1, "link"));
       gQueue.push(new setCaretOffset("heading", 1, document));
       gQueue.onFinish = function()
       {
@@ -54,29 +70,41 @@
     SimpleTest.waitForExplicitFinish();
     addA11yLoadEvent(doTests);
   </script>
 </head>
 
 <body>
 
   <a target="_blank"
+     href="https://bugzilla.mozilla.org/show_bug.cgi?id=448744"
+     title="caretOffset should return -1 if the system caret is not currently with in that particular object">
+    Mozilla Bug 448744
+  </a>
+  <a target="_blank"
      href="https://bugzilla.mozilla.org/show_bug.cgi?id=524115"
      title="HyperText accessible should get focus when the caret is positioned inside of it, text is changed or copied into clipboard by ATs">
     Mozilla Bug 524115
   </a>
   <a target="_blank"
      href="https://bugzilla.mozilla.org/show_bug.cgi?id=546068"
      title="Position is not being updated when atk_text_set_caret_offset is used">
     Mozilla Bug 546068
   </a>
+  <a target="_blank"
+     href="https://bugzilla.mozilla.org/show_bug.cgi?id=725581"
+     title="caretOffset for textarea should be -1 when textarea doesn't have a focus">
+    Mozilla Bug 725581
+  </a>
   <p id="display"></p>
   <div id="content" style="display: none"></div>
   <pre id="test">
   </pre>
 
   <input id="textbox" value="hello"/>
+  <textarea id="textarea">text<br>text</textarea>
+  <p id="p" contentEditable="true"><span>text</span><br/>text</p>
   <a id="link" href="about:">about mozilla</a>
   <h5 id="heading">heading</h5>
 
   <div id="eventdump"></div>
 </body>
 </html>