613959 - mochitest word boundaries nsIAccessibleText, r=surkov.alexander, a=test
authorFernando Herrera <fherrera@onirica.com>
Tue, 30 Nov 2010 00:02:43 +0800
changeset 58324 21762517cdea401bd9a1399764a320adc976c6f6
parent 58323 a5500a75626857a4521ddb01cca5b409411a2dff
child 58325 ff0e38477a8db97a3f59cf9fc9e7e8c6e7897fa4
push id17245
push usersurkov.alexander@gmail.com
push dateMon, 29 Nov 2010 16:04:07 +0000
treeherdermozilla-central@ff0e38477a8d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssurkov.alexander, test
bugs613959
milestone2.0b8pre
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
613959 - mochitest word boundaries nsIAccessibleText, r=surkov.alexander, a=test
accessible/tests/mochitest/text.js
accessible/tests/mochitest/text/Makefile.in
accessible/tests/mochitest/text/test_words.html
--- a/accessible/tests/mochitest/text.js
+++ b/accessible/tests/mochitest/text.js
@@ -119,16 +119,113 @@ function testTextBeforeOffset(aOffset, a
 
     testTextHelper(ID, aOffset, aBoundaryType,
                    aText, aStartOffset, aEndOffset,
                    toDoFlag1, toDoFlag2, toDoFlag3,
                    acc.getTextBeforeOffset, "getTextBeforeOffset ");
   }
 }
 
+/**
+ * Test word count for an element.
+ *
+ * @param aElement   [in] element identifier
+ * @param aCount     [in] Expected word count
+ * @param aToDoFlag  [in] kTodo or kOk for returned text
+ */
+function testWordCount(aElement, aCount, aToDoFlag)
+{
+  var isFunc = (aToDoFlag == kTodo) ? todo_is : is;
+  var acc = getAccessible(aElement, nsIAccessibleText);
+  var startOffsetObj = {}, endOffsetObj = {};
+  var length = acc.characterCount;
+  var offset = 0;
+  var wordCount = 0;
+  while (true) {
+    var text = acc.getTextAtOffset(offset, BOUNDARY_WORD_START,
+                                   startOffsetObj, endOffsetObj);
+    if (offset >= length)
+      break;
+
+    wordCount++;
+    offset = endOffsetObj.value;
+  }
+  isFunc(wordCount, aCount,  "wrong words count for '" + acc.getText(0, -1) + "': " +
+         wordCount);
+}
+
+/**
+ * Test word at a position for an element.
+ *
+ * @param aElement    [in] element identifier
+ * @param aWordIndex  [in] index of the word to test
+ * @param aText       [in] expected text for that word
+ * @param aToDoFlag   [in] kTodo or kOk for returned text
+ */
+function testWordAt(aElement, aWordIndex, aText, aToDoFlag)
+{
+  var isFunc = (aToDoFlag == kTodo) ? todo_is : is;
+  var acc = getAccessible(aElement, nsIAccessibleText);
+  var startOffsetObj = {}, endOffsetObj = {};
+  var length = acc.characterCount;
+  var offset = 0;
+  var wordIndex = -1;
+  var wordFountAtOffset = -1;
+  while (true) {
+    var text = acc.getTextAtOffset(offset, BOUNDARY_WORD_START,
+                                   startOffsetObj, endOffsetObj);
+    if (offset >= length)
+      break;
+
+    wordIndex++;
+    offset = endOffsetObj.value;
+    if (wordIndex == aWordIndex) {
+       wordFountAtOffset = startOffsetObj.value;
+       break;
+    }
+  } 
+  if (wordFountAtOffset >= 0) {
+    var text = acc.getTextAtOffset(wordFountAtOffset, BOUNDARY_WORD_END,
+                                   startOffsetObj, endOffsetObj);
+
+    if (endOffsetObj.value < wordFountAtOffset) {
+      todo(false,  "wrong start and end offset for word '" + aWordIndex + "': " +
+           " of text '" + acc.getText(0, -1) + "'");
+      return;
+    }
+
+    text = acc.getText(wordFountAtOffset, endOffsetObj.value);
+    isFunc(text, aText,  "wrong text for word at pos '" + aWordIndex + "': " +
+           " of text '" + acc.getText(0, -1) + "'");
+  }
+  else {
+    isFunc(false, "failed to find word " + aText + " at word pos " + aWordIndex +
+           " of text '" + acc.getText(0, -1) + "'");
+  }
+}
+
+/**
+ * Test words in a element.
+ *
+ * @param aElement   [in]           element identifier
+ * @param aWords     [in]           array of expected words
+ * @param aToDoFlag  [in, optional] kTodo or kOk for returned text
+ */
+function testWords(aElement, aWords, aToDoFlag)
+{
+  if (aToDoFlag == null)
+    aToDoFlag = kOk;
+
+  testWordCount(aElement, aWords.length, aToDoFlag);
+
+  for (var i = 0; i < aWords.length; i++) {
+    testWordAt(aElement, i, aWords[i], aToDoFlag);
+  }
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // Private
 
 function testTextHelper(aID, aOffset, aBoundaryType,
                         aText, aStartOffset, aEndOffset,
                         aToDoFlag1, aToDoFlag2, aToDoFlag3,
                         aTextFunc, aTextFuncName)
 {
--- a/accessible/tests/mochitest/text/Makefile.in
+++ b/accessible/tests/mochitest/text/Makefile.in
@@ -45,12 +45,13 @@ relativesrcdir  = accessible/text
 include $(DEPTH)/config/autoconf.mk
 include $(topsrcdir)/config/rules.mk
 
 _TEST_FILES = \
 		doc.html \
 		test_doc.html \
 		test_singleline.html \
 		test_whitespaces.html \
+		test_words.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/a11y/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/accessible/tests/mochitest/text/test_words.html
@@ -0,0 +1,125 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>nsIAccessibleText getText related function tests for html:input,html:div and html:textarea</title>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
+
+  <script type="application/javascript"
+          src="chrome://mochikit/content/MochiKit/packed.js"></script>
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript"
+          src="../common.js"></script>
+  <script type="application/javascript"
+          src="../text.js"></script>
+  <script type="application/javascript">
+    
+    function doTest()
+    {
+      // "one two"
+      testWords("div1", ["one", "two"]);
+
+      // "one  two"
+      testWords("div2", ["one", "two"]);
+
+      // "one,two"
+      testWordCount("div3", 2, kOk);
+      testWordAt("div3", 0, "one", kTodo);
+      testWordAt("div3", 1, "two", kOk);
+
+      // "one, two"
+      testWordCount("div4", 2, kOk);
+      testWordAt("div4", 0, "one", kTodo);
+      testWordAt("div4", 1, "two", kOk);
+
+      // "one+two"
+      testWordCount("div5", 2, kOk);
+      testWordAt("div5", 0, "one", kTodo);
+      testWordAt("div5", 1, "two", kOk);
+
+      // "one+two   "
+      testWordCount("div6", 2, kOk);
+      testWordAt("div6", 0, "one", kTodo);
+      testWordAt("div6", 1, "two", kOk);
+
+      // "one\ntwo"
+      testWordCount("div7", 2, kOk);
+      testWordAt("div7", 0, "one", kOk);
+      testWordAt("div7", 1, "two", kTodo);
+
+      // "one.two"
+      testWordCount("div8", 2, kOk);
+      testWordAt("div8", 0, "one", kTodo);
+      testWordAt("div8", 1, "two", kOk);
+
+      // "345"
+      testWords("div9", ["345"]);
+
+      // "3a A4"
+      testWords("div10", ["3a", "A4"]);
+
+      // "3.1416"
+      testWords("div11", ["3.1416"], kTodo);
+
+      // "4,261.01"
+      testWords("div12", ["4,261.01"], kTodo);
+
+      // "カタカナ"
+      testWords("div13", ["カタカナ"], kTodo);
+
+      // "Peter's car"
+      testWords("div14", ["Peter's", "car"], kTodo);
+
+      // "N.A.T.O."
+      testWords("div15", ["N.A.T.O."], kTodo);
+
+      // "3+4*5=23"
+      testWordCount("div16", 4, kOk);
+      testWordAt("div15", 0, "3", kTodo);
+      testWordAt("div15", 1, "4", kTodo);
+      testWordAt("div15", 2, "5", kTodo);
+      testWordAt("div15", 3, "23", kTodo);
+
+      // "Hello. Friend, are you here?!"
+      testWordCount("div17", 5, kOk);
+      testWordAt("div17", 0, "Hello", kTodo);
+      testWordAt("div17", 1, "Friend", kTodo);
+      testWordAt("div17", 2, "are", kOk);
+      testWordAt("div17", 3, "you", kOk);
+      testWordAt("div17", 4, "here", kTodo);
+
+      SimpleTest.finish();
+    }
+
+    SimpleTest.waitForExplicitFinish();
+    addA11yLoadEvent(doTest);
+  </script>
+</head>
+<body>
+
+  <a target="_blank"
+     title="nsIAccessibleText test word boundaries"
+     href="https://bugzilla.mozilla.org/show_bug.cgi?id=452769">Mozilla Bug 452769</a>
+  <p id="display"></p>
+  <div id="content" style="display: none"></div>
+  <pre id="test">
+  <div id="div1">one two</div>
+  <div id="div2">one  two</div>
+  <div id="div3">one,two</div>
+  <div id="div4">one, two</div>
+  <div id="div5">one+two</div>
+  <div id="div6">one+two   </div>
+  <div id="div7">one<br/>two</div>
+  <div id="div8">one.two</div>
+  <div id="div9">345</div>
+  <div id="div10">3a A4</div>
+  <div id="div11">3.1416</div>
+  <div id="div12">4,261.01</div>
+  <div id="div13">カタカナ</div>
+  <div id="div14">Peter's car</div>
+  <div id="div15">N.A.T.O.</div>
+  <div id="div16">3+4*5=23</div>
+  <div id="div17">Hello. Friend, are you here?!</div>
+  </pre>
+</body>
+</html>