Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9dad5811 authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Get failing WordIterator unit tests to pass again

WordIterator is now following the pattern in java.text.BreakIterator,
throwing an IllegalArgumentException when receiving an invalid argument.
Previously, it failed inconsistently.

Also add @SmallTest annotations to the WordIterator tests.

Bug: 29868991
Change-Id: I8158f2f419c8c4467205031655c0e62cc9ef5c1c
parent c05bf612
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ public class WordIterator implements Selection.PositionIterator {
    /** {@inheritDoc} */
    public int preceding(int offset) {
        int shiftedOffset = offset - mOffsetShift;
        checkOffsetIsValid(shiftedOffset);
        do {
            shiftedOffset = mIterator.preceding(shiftedOffset);
            if (shiftedOffset == BreakIterator.DONE) {
@@ -84,6 +85,7 @@ public class WordIterator implements Selection.PositionIterator {
    /** {@inheritDoc} */
    public int following(int offset) {
        int shiftedOffset = offset - mOffsetShift;
        checkOffsetIsValid(shiftedOffset);
        do {
            shiftedOffset = mIterator.following(shiftedOffset);
            if (shiftedOffset == BreakIterator.DONE) {
@@ -111,6 +113,7 @@ public class WordIterator implements Selection.PositionIterator {
     */
    public int nextBoundary(int offset) {
        int shiftedOffset = offset - mOffsetShift;
        checkOffsetIsValid(shiftedOffset);
        shiftedOffset = mIterator.following(shiftedOffset);
        if (shiftedOffset == BreakIterator.DONE) {
            return BreakIterator.DONE;
@@ -127,6 +130,7 @@ public class WordIterator implements Selection.PositionIterator {
     */
    public int prevBoundary(int offset) {
        int shiftedOffset = offset - mOffsetShift;
        checkOffsetIsValid(shiftedOffset);
        shiftedOffset = mIterator.preceding(shiftedOffset);
        if (shiftedOffset == BreakIterator.DONE) {
            return BreakIterator.DONE;
@@ -286,6 +290,7 @@ public class WordIterator implements Selection.PositionIterator {
     * @param offset the offset to search from.
     */
    public int getPunctuationBeginning(int offset) {
        checkOffsetIsValid(offset - mOffsetShift);
        while (offset != BreakIterator.DONE && !isPunctuationStartBoundary(offset)) {
            offset = prevBoundary(offset);
        }
@@ -301,6 +306,7 @@ public class WordIterator implements Selection.PositionIterator {
     * @param offset the offset to search from.
     */
    public int getPunctuationEnd(int offset) {
        checkOffsetIsValid(offset - mOffsetShift);
        while (offset != BreakIterator.DONE && !isPunctuationEndBoundary(offset)) {
            offset = nextBoundary(offset);
        }
+24 −6
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.text.method;

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import java.text.BreakIterator;
import java.util.Locale;
@@ -24,6 +25,7 @@ import java.util.Locale;
// TODO(Bug: 24062099): Add more tests for non-ascii text.
public class WordIteratorTest  extends AndroidTestCase {

    @SmallTest
    public void testSetCharSequence() {
        final String text = "text";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -46,6 +48,7 @@ public class WordIteratorTest extends AndroidTestCase {
        wordIterator.setCharSequence(text, text.length(), text.length());
    }

    @SmallTest
    public void testPreceding() {
        final String text = "abc def-ghi. jkl";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -72,6 +75,7 @@ public class WordIteratorTest extends AndroidTestCase {
        assertEquals(text.indexOf('j'), wordIterator.preceding(text.indexOf('l')));
    }

    @SmallTest
    public void testFollowing() {
        final String text = "abc def-ghi. jkl";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -98,6 +102,7 @@ public class WordIteratorTest extends AndroidTestCase {
        assertEquals(BreakIterator.DONE, wordIterator.following(text.length()));
    }

    @SmallTest
    public void testIsBoundary() {
        final String text = "abc def-ghi. jkl";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -125,6 +130,7 @@ public class WordIteratorTest extends AndroidTestCase {
        assertTrue(wordIterator.isBoundary(text.length()));
    }

    @SmallTest
    public void testNextBoundary() {
        final String text = "abc def-ghi. jkl";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -171,6 +177,7 @@ public class WordIteratorTest extends AndroidTestCase {
        assertEquals(BreakIterator.DONE, currentOffset);
    }

    @SmallTest
    public void testPrevBoundary() {
        final String text = "abc def-ghi. jkl";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -216,6 +223,7 @@ public class WordIteratorTest extends AndroidTestCase {
        assertEquals(BreakIterator.DONE, currentOffset);
    }

    @SmallTest
    public void testGetBeginning() {
        {
            final String text = "abc def-ghi. jkl";
@@ -289,6 +297,7 @@ public class WordIteratorTest extends AndroidTestCase {
        }
    }

    @SmallTest
    public void testGetEnd() {
        {
            final String text = "abc def-ghi. jkl";
@@ -363,14 +372,18 @@ public class WordIteratorTest extends AndroidTestCase {
        }
    }

    @SmallTest
    public void testGetPunctuationBeginning() {
        final String text = "abc!? (^^;) def";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
        wordIterator.setCharSequence(text, 0, text.length());

        // TODO: Shouldn't this throw an exception?
        assertEquals(BreakIterator.DONE, wordIterator.getPunctuationBeginning(BreakIterator.DONE));

        try {
            wordIterator.getPunctuationBeginning(BreakIterator.DONE);
            fail("getPunctuationBeginning with invalid offset should throw "
                    + "IllegalArgumentException.");
        } catch (IllegalArgumentException e) {
        }
        try {
            wordIterator.getPunctuationBeginning(-2);
            fail("getPunctuationBeginning with invalid offset should throw "
@@ -394,14 +407,17 @@ public class WordIteratorTest extends AndroidTestCase {
        assertEquals(text.indexOf(';'), wordIterator.getPunctuationBeginning(text.length()));
    }

    @SmallTest
    public void testGetPunctuationEnd() {
        final String text = "abc!? (^^;) def";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
        wordIterator.setCharSequence(text, 0, text.length());

        // TODO: Shouldn't this throw an exception?
        assertEquals(BreakIterator.DONE, wordIterator.getPunctuationEnd(BreakIterator.DONE));

        try {
            wordIterator.getPunctuationEnd(BreakIterator.DONE);
            fail("getPunctuationEnd with invalid offset should throw IllegalArgumentException.");
        } catch (IllegalArgumentException e) {
        }
        try {
            wordIterator.getPunctuationEnd(-2);
            fail("getPunctuationEnd with invalid offset should throw IllegalArgumentException.");
@@ -423,6 +439,7 @@ public class WordIteratorTest extends AndroidTestCase {
        assertEquals(BreakIterator.DONE, wordIterator.getPunctuationEnd(text.length()));
    }

    @SmallTest
    public void testIsAfterPunctuation() {
        final String text = "abc!? (^^;) def";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -438,6 +455,7 @@ public class WordIteratorTest extends AndroidTestCase {
        assertFalse(wordIterator.isAfterPunctuation(text.length() + 1));
    }

    @SmallTest
    public void testIsOnPunctuation() {
        final String text = "abc!? (^^;) def";
        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);