WordIteratorTest.java revision 1f7ab84b0ebd38c0054d7f840f9a649555e21591
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text.method;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import java.text.BreakIterator;
23import java.util.Locale;
24
25// TODO(Bug: 24062099): Add more tests for non-ascii text.
26public class WordIteratorTest  extends AndroidTestCase {
27
28    @SmallTest
29    public void testSetCharSequence() {
30        final String text = "text";
31        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
32
33        try {
34            wordIterator.setCharSequence(text, 100, 100);
35            fail("setCharSequence with invalid start and end values should throw "
36                    + "IndexOutOfBoundsException.");
37        } catch (IndexOutOfBoundsException e) {
38        }
39        try {
40            wordIterator.setCharSequence(text, -100, -100);
41            fail("setCharSequence with invalid start and end values should throw "
42                    + "IndexOutOfBoundsException.");
43        } catch (IndexOutOfBoundsException e) {
44        }
45
46        wordIterator.setCharSequence(text, 0, text.length());
47        wordIterator.setCharSequence(text, 0, 0);
48        wordIterator.setCharSequence(text, text.length(), text.length());
49    }
50
51    @SmallTest
52    public void testWindowWidth() {
53        final String text = "aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn";
54        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
55
56        // The first 'n' is more than 50 characters into the string.
57        wordIterator.setCharSequence(text, text.indexOf('n'), text.length());
58        final int expectedWindowStart = text.indexOf('n') - 50;
59        assertEquals(expectedWindowStart, wordIterator.preceding(expectedWindowStart + 1));
60        assertEquals(BreakIterator.DONE, wordIterator.preceding(expectedWindowStart));
61
62        wordIterator.setCharSequence(text, 0, 1);
63        final int expectedWindowEnd = 1 + 50;
64        assertEquals(expectedWindowEnd, wordIterator.following(expectedWindowEnd - 1));
65        assertEquals(BreakIterator.DONE, wordIterator.following(expectedWindowEnd));
66    }
67
68    @SmallTest
69    public void testPreceding() {
70        final String text = "abc def-ghi. jkl";
71        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
72        wordIterator.setCharSequence(text, 0, text.length());
73
74        try {
75            wordIterator.preceding(-1);
76            fail("preceding with invalid offset should throw IllegalArgumentException.");
77        } catch (IllegalArgumentException e) {
78        }
79        try {
80            wordIterator.preceding(text.length() + 1);
81            fail("preceding with invalid offset should throw IllegalArgumentException.");
82        } catch (IllegalArgumentException e) {
83        }
84
85        assertEquals(BreakIterator.DONE, wordIterator.preceding(text.indexOf('a')));
86        assertEquals(text.indexOf('a'), wordIterator.preceding(text.indexOf('c')));
87        assertEquals(text.indexOf('a'), wordIterator.preceding(text.indexOf('d')));
88        assertEquals(text.indexOf('d'), wordIterator.preceding(text.indexOf('e')));
89        assertEquals(text.indexOf('d'), wordIterator.preceding(text.indexOf('g')));
90        assertEquals(text.indexOf('g'), wordIterator.preceding(text.indexOf('h')));
91        assertEquals(text.indexOf('g'), wordIterator.preceding(text.indexOf('j')));
92        assertEquals(text.indexOf('j'), wordIterator.preceding(text.indexOf('l')));
93
94        // The results should be the same even if we set an smaller window, since WordIterator
95        // enlargens the window by 50 code units on each side anyway.
96        wordIterator.setCharSequence(text, text.indexOf('d'), text.indexOf('e'));
97
98        assertEquals(BreakIterator.DONE, wordIterator.preceding(text.indexOf('a')));
99        assertEquals(text.indexOf('a'), wordIterator.preceding(text.indexOf('c')));
100        assertEquals(text.indexOf('a'), wordIterator.preceding(text.indexOf('d')));
101        assertEquals(text.indexOf('d'), wordIterator.preceding(text.indexOf('e')));
102        assertEquals(text.indexOf('d'), wordIterator.preceding(text.indexOf('g')));
103        assertEquals(text.indexOf('g'), wordIterator.preceding(text.indexOf('h')));
104        assertEquals(text.indexOf('g'), wordIterator.preceding(text.indexOf('j')));
105        assertEquals(text.indexOf('j'), wordIterator.preceding(text.indexOf('l')));
106    }
107
108    @SmallTest
109    public void testFollowing() {
110        final String text = "abc def-ghi. jkl";
111        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
112        wordIterator.setCharSequence(text, 0, text.length());
113
114        try {
115            wordIterator.following(-1);
116            fail("following with invalid offset should throw IllegalArgumentException.");
117        } catch (IllegalArgumentException e) {
118        }
119        try {
120            wordIterator.following(text.length() + 1);
121            fail("following with invalid offset should throw IllegalArgumentException.");
122        } catch (IllegalArgumentException e) {
123        }
124
125        assertEquals(text.indexOf('c') + 1, wordIterator.following(text.indexOf('a')));
126        assertEquals(text.indexOf('c') + 1, wordIterator.following(text.indexOf('c')));
127        assertEquals(text.indexOf('f') + 1, wordIterator.following(text.indexOf('c') + 1));
128        assertEquals(text.indexOf('f') + 1, wordIterator.following(text.indexOf('d')));
129        assertEquals(text.indexOf('i') + 1, wordIterator.following(text.indexOf('-')));
130        assertEquals(text.indexOf('i') + 1, wordIterator.following(text.indexOf('g')));
131        assertEquals(text.length(), wordIterator.following(text.indexOf('j')));
132        assertEquals(BreakIterator.DONE, wordIterator.following(text.length()));
133
134        // The results should be the same even if we set an smaller window, since WordIterator
135        // enlargens the window by 50 code units on each side anyway.
136        wordIterator.setCharSequence(text, text.indexOf('d'), text.indexOf('e'));
137
138        assertEquals(text.indexOf('c') + 1, wordIterator.following(text.indexOf('a')));
139        assertEquals(text.indexOf('c') + 1, wordIterator.following(text.indexOf('c')));
140        assertEquals(text.indexOf('f') + 1, wordIterator.following(text.indexOf('c') + 1));
141        assertEquals(text.indexOf('f') + 1, wordIterator.following(text.indexOf('d')));
142        assertEquals(text.indexOf('i') + 1, wordIterator.following(text.indexOf('-')));
143        assertEquals(text.indexOf('i') + 1, wordIterator.following(text.indexOf('g')));
144        assertEquals(text.length(), wordIterator.following(text.indexOf('j')));
145        assertEquals(BreakIterator.DONE, wordIterator.following(text.length()));
146    }
147
148    @SmallTest
149    public void testIsBoundary() {
150        final String text = "abc def-ghi. jkl";
151        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
152        wordIterator.setCharSequence(text, 0, text.length());
153
154        try {
155            wordIterator.isBoundary(-1);
156            fail("isBoundary with invalid offset should throw IllegalArgumentException.");
157        } catch (IllegalArgumentException e) {
158        }
159        try {
160            wordIterator.isBoundary(text.length() + 1);
161            fail("isBoundary with invalid offset should throw IllegalArgumentException.");
162        } catch (IllegalArgumentException e) {
163        }
164
165        assertTrue(wordIterator.isBoundary(text.indexOf('a')));
166        assertFalse(wordIterator.isBoundary(text.indexOf('b')));
167        assertTrue(wordIterator.isBoundary(text.indexOf('c') + 1));
168        assertTrue(wordIterator.isBoundary(text.indexOf('d')));
169        assertTrue(wordIterator.isBoundary(text.indexOf('-')));
170        assertTrue(wordIterator.isBoundary(text.indexOf('g')));
171        assertTrue(wordIterator.isBoundary(text.indexOf('.')));
172        assertTrue(wordIterator.isBoundary(text.indexOf('j')));
173        assertTrue(wordIterator.isBoundary(text.length()));
174    }
175
176    @SmallTest
177    public void testNextBoundary() {
178        final String text = "abc def-ghi. jkl";
179        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
180        wordIterator.setCharSequence(text, 0, text.length());
181
182        try {
183            wordIterator.nextBoundary(-1);
184            fail("nextBoundary with invalid offset should throw IllegalArgumentException.");
185        } catch (IllegalArgumentException e) {
186        }
187        try {
188            wordIterator.nextBoundary(text.length() + 1);
189            fail("nextBoundary with invalid offset should throw IllegalArgumentException.");
190        } catch (IllegalArgumentException e) {
191        }
192
193
194        int currentOffset = 0;
195        currentOffset = wordIterator.nextBoundary(currentOffset);
196        assertEquals(text.indexOf('c') + 1, currentOffset);
197
198        currentOffset = wordIterator.nextBoundary(currentOffset);
199        assertEquals(text.indexOf('d'), currentOffset);
200
201        currentOffset = wordIterator.nextBoundary(currentOffset);
202        assertEquals(text.indexOf('f') + 1, currentOffset);
203
204        currentOffset = wordIterator.nextBoundary(currentOffset);
205        assertEquals(text.indexOf('g'), currentOffset);
206
207        currentOffset = wordIterator.nextBoundary(currentOffset);
208        assertEquals(text.indexOf('i') + 1, currentOffset);
209
210        currentOffset = wordIterator.nextBoundary(currentOffset);
211        assertEquals(text.indexOf('.') + 1, currentOffset);
212
213        currentOffset = wordIterator.nextBoundary(currentOffset);
214        assertEquals(text.indexOf('j'), currentOffset);
215
216        currentOffset = wordIterator.nextBoundary(currentOffset);
217        assertEquals(text.length(), currentOffset);
218
219        currentOffset = wordIterator.nextBoundary(currentOffset);
220        assertEquals(BreakIterator.DONE, currentOffset);
221    }
222
223    @SmallTest
224    public void testPrevBoundary() {
225        final String text = "abc def-ghi. jkl";
226        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
227        wordIterator.setCharSequence(text, 0, text.length());
228
229        try {
230            wordIterator.prevBoundary(-1);
231            fail("prevBoundary with invalid offset should throw IllegalArgumentException.");
232        } catch (IllegalArgumentException e) {
233        }
234        try {
235            wordIterator.prevBoundary(text.length() + 1);
236            fail("prevBoundary with invalid offset should throw IllegalArgumentException.");
237        } catch (IllegalArgumentException e) {
238        }
239
240        int currentOffset = text.length();
241        currentOffset = wordIterator.prevBoundary(currentOffset);
242        assertEquals(text.indexOf('j'), currentOffset);
243
244        currentOffset = wordIterator.prevBoundary(currentOffset);
245        assertEquals(text.indexOf('.') + 1, currentOffset);
246
247        currentOffset = wordIterator.prevBoundary(currentOffset);
248        assertEquals(text.indexOf('i') + 1, currentOffset);
249
250        currentOffset = wordIterator.prevBoundary(currentOffset);
251        assertEquals(text.indexOf('g'), currentOffset);
252
253        currentOffset = wordIterator.prevBoundary(currentOffset);
254        assertEquals(text.indexOf('f') + 1, currentOffset);
255
256        currentOffset = wordIterator.prevBoundary(currentOffset);
257        assertEquals(text.indexOf('d'), currentOffset);
258
259        currentOffset = wordIterator.prevBoundary(currentOffset);
260        assertEquals(text.indexOf('c') + 1, currentOffset);
261
262        currentOffset = wordIterator.prevBoundary(currentOffset);
263        assertEquals(text.indexOf('a'), currentOffset);
264
265        currentOffset = wordIterator.prevBoundary(currentOffset);
266        assertEquals(BreakIterator.DONE, currentOffset);
267    }
268
269    @SmallTest
270    public void testGetBeginning() {
271        {
272            final String text = "abc def-ghi. jkl";
273            WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
274            wordIterator.setCharSequence(text, 0, text.length());
275            try {
276                wordIterator.getBeginning(-1);
277                fail("getBeginning with invalid offset should throw IllegalArgumentException.");
278            } catch (IllegalArgumentException e) {
279            }
280            try {
281                wordIterator.getBeginning(text.length() + 1);
282                fail("getBeginning with invalid offset should throw IllegalArgumentException.");
283            } catch (IllegalArgumentException e) {
284            }
285            try {
286                wordIterator.getPrevWordBeginningOnTwoWordsBoundary(-1);
287                fail("getPrevWordBeginningOnTwoWordsBoundary with invalid offset should throw "
288                        + "IllegalArgumentException.");
289            } catch (IllegalArgumentException e) {
290            }
291            try {
292                wordIterator.getPrevWordBeginningOnTwoWordsBoundary(text.length() + 1);
293                fail("getPrevWordBeginningOnTwoWordsBoundary with invalid offset should throw "
294                        + "IllegalArgumentException.");
295            } catch (IllegalArgumentException e) {
296            }
297        }
298
299        {
300            final String text = "abc def-ghi. jkl";
301            WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
302            wordIterator.setCharSequence(text, 0, text.length());
303
304            assertEquals(text.indexOf('a'), wordIterator.getBeginning(text.indexOf('a')));
305            assertEquals(text.indexOf('a'), wordIterator.getBeginning(text.indexOf('c')));
306            assertEquals(text.indexOf('a'), wordIterator.getBeginning(text.indexOf('c') + 1));
307            assertEquals(text.indexOf('d'), wordIterator.getBeginning(text.indexOf('d')));
308            assertEquals(text.indexOf('d'), wordIterator.getBeginning(text.indexOf('-')));
309            assertEquals(text.indexOf('g'), wordIterator.getBeginning(text.indexOf('g')));
310            assertEquals(text.indexOf('g'), wordIterator.getBeginning(text.indexOf('.')));
311            assertEquals(BreakIterator.DONE, wordIterator.getBeginning(text.indexOf('.') + 1));
312            assertEquals(text.indexOf('j'), wordIterator.getBeginning(text.indexOf('j')));
313            assertEquals(text.indexOf('j'), wordIterator.getBeginning(text.indexOf('l') + 1));
314
315            for (int i = 0; i < text.length(); i++) {
316                assertEquals(wordIterator.getBeginning(i),
317                        wordIterator.getPrevWordBeginningOnTwoWordsBoundary(i));
318            }
319        }
320
321        {
322            // Japanese HIRAGANA letter + KATAKANA letters
323            final String text = "\u3042\u30A2\u30A3\u30A4";
324            WordIterator wordIterator = new WordIterator(Locale.JAPANESE);
325            wordIterator.setCharSequence(text, 0, text.length());
326
327            assertEquals(text.indexOf('\u3042'), wordIterator.getBeginning(text.indexOf('\u3042')));
328            assertEquals(text.indexOf('\u30A2'), wordIterator.getBeginning(text.indexOf('\u30A2')));
329            assertEquals(text.indexOf('\u30A2'), wordIterator.getBeginning(text.indexOf('\u30A4')));
330            assertEquals(text.indexOf('\u30A2'), wordIterator.getBeginning(text.length()));
331
332            assertEquals(text.indexOf('\u3042'),
333                    wordIterator.getPrevWordBeginningOnTwoWordsBoundary(text.indexOf('\u3042')));
334            assertEquals(text.indexOf('\u3042'),
335                    wordIterator.getPrevWordBeginningOnTwoWordsBoundary(text.indexOf('\u30A2')));
336            assertEquals(text.indexOf('\u30A2'),
337                    wordIterator.getPrevWordBeginningOnTwoWordsBoundary(text.indexOf('\u30A4')));
338            assertEquals(text.indexOf('\u30A2'),
339                    wordIterator.getPrevWordBeginningOnTwoWordsBoundary(text.length()));
340        }
341    }
342
343    @SmallTest
344    public void testGetEnd() {
345        {
346            final String text = "abc def-ghi. jkl";
347            WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
348            wordIterator.setCharSequence(text, 0, text.length());
349            try {
350                wordIterator.getEnd(-1);
351                fail("getEnd with invalid offset should throw IllegalArgumentException.");
352            } catch (IllegalArgumentException e) {
353            }
354            try {
355                wordIterator.getEnd(text.length() + 1);
356                fail("getEnd with invalid offset should throw IllegalArgumentException.");
357            } catch (IllegalArgumentException e) {
358            }
359            try {
360                wordIterator.getNextWordEndOnTwoWordBoundary(-1);
361                fail("getNextWordEndOnTwoWordBoundary with invalid offset should throw "
362                        + "IllegalArgumentException.");
363            } catch (IllegalArgumentException e) {
364            }
365            try {
366                wordIterator.getNextWordEndOnTwoWordBoundary(text.length() + 1);
367                fail("getNextWordEndOnTwoWordBoundary with invalid offset should throw "
368                        + "IllegalArgumentException.");
369            } catch (IllegalArgumentException e) {
370            }
371        }
372
373        {
374            final String text = "abc def-ghi. jkl";
375            WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
376            wordIterator.setCharSequence(text, 0, text.length());
377
378            assertEquals(text.indexOf('c') + 1, wordIterator.getEnd(text.indexOf('a')));
379            assertEquals(text.indexOf('c') + 1, wordIterator.getEnd(text.indexOf('c')));
380            assertEquals(text.indexOf('c') + 1, wordIterator.getEnd(text.indexOf('c') + 1));
381            assertEquals(text.indexOf('f') + 1, wordIterator.getEnd(text.indexOf('d')));
382            assertEquals(text.indexOf('f') + 1, wordIterator.getEnd(text.indexOf('f') + 1));
383            assertEquals(text.indexOf('i') + 1, wordIterator.getEnd(text.indexOf('g')));
384            assertEquals(text.indexOf('i') + 1, wordIterator.getEnd(text.indexOf('i') + 1));
385            assertEquals(BreakIterator.DONE, wordIterator.getEnd(text.indexOf('.') + 1));
386            assertEquals(text.indexOf('l') + 1, wordIterator.getEnd(text.indexOf('j')));
387            assertEquals(text.indexOf('l') + 1, wordIterator.getEnd(text.indexOf('l') + 1));
388
389            for (int i = 0; i < text.length(); i++) {
390                assertEquals(wordIterator.getEnd(i),
391                        wordIterator.getNextWordEndOnTwoWordBoundary(i));
392            }
393        }
394
395        {
396            // Japanese HIRAGANA letter + KATAKANA letters
397            final String text = "\u3042\u30A2\u30A3\u30A4";
398            WordIterator wordIterator = new WordIterator(Locale.JAPANESE);
399            wordIterator.setCharSequence(text, 0, text.length());
400
401            assertEquals(text.indexOf('\u3042') + 1, wordIterator.getEnd(text.indexOf('\u3042')));
402            assertEquals(text.indexOf('\u3042') + 1, wordIterator.getEnd(text.indexOf('\u30A2')));
403            assertEquals(text.indexOf('\u30A4') + 1, wordIterator.getEnd(text.indexOf('\u30A4')));
404            assertEquals(text.indexOf('\u30A4') + 1,
405                    wordIterator.getEnd(text.indexOf('\u30A4') + 1));
406
407            assertEquals(text.indexOf('\u3042') + 1,
408                    wordIterator.getNextWordEndOnTwoWordBoundary(text.indexOf('\u3042')));
409            assertEquals(text.indexOf('\u30A4') + 1,
410                    wordIterator.getNextWordEndOnTwoWordBoundary(text.indexOf('\u30A2')));
411            assertEquals(text.indexOf('\u30A4') + 1,
412                    wordIterator.getNextWordEndOnTwoWordBoundary(text.indexOf('\u30A4')));
413            assertEquals(text.indexOf('\u30A4') + 1,
414                    wordIterator.getNextWordEndOnTwoWordBoundary(text.indexOf('\u30A4') + 1));
415        }
416    }
417
418    @SmallTest
419    public void testGetPunctuationBeginning() {
420        final String text = "abc!? (^^;) def";
421        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
422        wordIterator.setCharSequence(text, 0, text.length());
423
424        try {
425            wordIterator.getPunctuationBeginning(BreakIterator.DONE);
426            fail("getPunctuationBeginning with invalid offset should throw "
427                    + "IllegalArgumentException.");
428        } catch (IllegalArgumentException e) {
429        }
430        try {
431            wordIterator.getPunctuationBeginning(-2);
432            fail("getPunctuationBeginning with invalid offset should throw "
433                    + "IllegalArgumentException.");
434        } catch (IllegalArgumentException e) {
435        }
436        try {
437            wordIterator.getPunctuationBeginning(text.length() + 1);
438            fail("getPunctuationBeginning with invalid offset should throw "
439                    + "IllegalArgumentException.");
440        } catch (IllegalArgumentException e) {
441        }
442
443        assertEquals(BreakIterator.DONE, wordIterator.getPunctuationBeginning(text.indexOf('a')));
444        assertEquals(BreakIterator.DONE, wordIterator.getPunctuationBeginning(text.indexOf('c')));
445        assertEquals(text.indexOf('!'), wordIterator.getPunctuationBeginning(text.indexOf('!')));
446        assertEquals(text.indexOf('!'),
447                wordIterator.getPunctuationBeginning(text.indexOf('?') + 1));
448        assertEquals(text.indexOf(';'), wordIterator.getPunctuationBeginning(text.indexOf(';')));
449        assertEquals(text.indexOf(';'), wordIterator.getPunctuationBeginning(text.indexOf(')')));
450        assertEquals(text.indexOf(';'), wordIterator.getPunctuationBeginning(text.length()));
451    }
452
453    @SmallTest
454    public void testGetPunctuationEnd() {
455        final String text = "abc!? (^^;) def";
456        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
457        wordIterator.setCharSequence(text, 0, text.length());
458
459        try {
460            wordIterator.getPunctuationEnd(BreakIterator.DONE);
461            fail("getPunctuationEnd with invalid offset should throw IllegalArgumentException.");
462        } catch (IllegalArgumentException e) {
463        }
464        try {
465            wordIterator.getPunctuationEnd(-2);
466            fail("getPunctuationEnd with invalid offset should throw IllegalArgumentException.");
467        } catch (IllegalArgumentException e) {
468        }
469        try {
470            wordIterator.getPunctuationEnd(text.length() + 1);
471            fail("getPunctuationBeginning with invalid offset should throw "
472                    + "IllegalArgumentException.");
473        } catch (IllegalArgumentException e) {
474        }
475
476        assertEquals(text.indexOf('?') + 1, wordIterator.getPunctuationEnd(text.indexOf('a')));
477        assertEquals(text.indexOf('?') + 1, wordIterator.getPunctuationEnd(text.indexOf('?') + 1));
478        assertEquals(text.indexOf('(') + 1, wordIterator.getPunctuationEnd(text.indexOf('(')));
479        assertEquals(text.indexOf(')') + 1, wordIterator.getPunctuationEnd(text.indexOf('(') + 2));
480        assertEquals(text.indexOf(')') + 1, wordIterator.getPunctuationEnd(text.indexOf(')') + 1));
481        assertEquals(BreakIterator.DONE, wordIterator.getPunctuationEnd(text.indexOf('d')));
482        assertEquals(BreakIterator.DONE, wordIterator.getPunctuationEnd(text.length()));
483    }
484
485    @SmallTest
486    public void testIsAfterPunctuation() {
487        final String text = "abc!? (^^;) def";
488        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
489        wordIterator.setCharSequence(text, 0, text.length());
490
491        assertFalse(wordIterator.isAfterPunctuation(text.indexOf('a')));
492        assertFalse(wordIterator.isAfterPunctuation(text.indexOf('!')));
493        assertTrue(wordIterator.isAfterPunctuation(text.indexOf('?')));
494        assertTrue(wordIterator.isAfterPunctuation(text.indexOf('?') + 1));
495        assertFalse(wordIterator.isAfterPunctuation(text.indexOf('d')));
496
497        assertFalse(wordIterator.isAfterPunctuation(BreakIterator.DONE));
498        assertFalse(wordIterator.isAfterPunctuation(text.length() + 1));
499    }
500
501    @SmallTest
502    public void testIsOnPunctuation() {
503        final String text = "abc!? (^^;) def";
504        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
505        wordIterator.setCharSequence(text, 0, text.length());
506
507        assertFalse(wordIterator.isOnPunctuation(text.indexOf('a')));
508        assertTrue(wordIterator.isOnPunctuation(text.indexOf('!')));
509        assertTrue(wordIterator.isOnPunctuation(text.indexOf('?')));
510        assertFalse(wordIterator.isOnPunctuation(text.indexOf('?') + 1));
511        assertTrue(wordIterator.isOnPunctuation(text.indexOf(')')));
512        assertFalse(wordIterator.isOnPunctuation(text.indexOf(')') + 1));
513        assertFalse(wordIterator.isOnPunctuation(text.indexOf('d')));
514
515        assertFalse(wordIterator.isOnPunctuation(BreakIterator.DONE));
516        assertFalse(wordIterator.isOnPunctuation(text.length()));
517        assertFalse(wordIterator.isOnPunctuation(text.length() + 1));
518    }
519
520    @SmallTest
521    public void testApostropheMiddleOfWord() {
522        // These tests confirm that the word "isn't" is treated like one word.
523        final String text = "isn't he";
524        WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
525        wordIterator.setCharSequence(text, 0, text.length());
526
527        assertEquals(text.indexOf('i'), wordIterator.preceding(text.indexOf('h')));
528        assertEquals(text.indexOf('t') + 1, wordIterator.following(text.indexOf('i')));
529
530        assertTrue(wordIterator.isBoundary(text.indexOf('i')));
531        assertFalse(wordIterator.isBoundary(text.indexOf('\'')));
532        assertFalse(wordIterator.isBoundary(text.indexOf('t')));
533        assertTrue(wordIterator.isBoundary(text.indexOf('t') + 1));
534        assertTrue(wordIterator.isBoundary(text.indexOf('h')));
535
536        assertEquals(text.indexOf('i'), wordIterator.getBeginning(text.indexOf('i')));
537        assertEquals(text.indexOf('i'), wordIterator.getBeginning(text.indexOf('n')));
538        assertEquals(text.indexOf('i'), wordIterator.getBeginning(text.indexOf('\'')));
539        assertEquals(text.indexOf('i'), wordIterator.getBeginning(text.indexOf('t')));
540        assertEquals(text.indexOf('i'), wordIterator.getBeginning(text.indexOf('t') + 1));
541        assertEquals(text.indexOf('h'), wordIterator.getBeginning(text.indexOf('h')));
542
543        assertEquals(text.indexOf('t') + 1, wordIterator.getEnd(text.indexOf('i')));
544        assertEquals(text.indexOf('t') + 1, wordIterator.getEnd(text.indexOf('n')));
545        assertEquals(text.indexOf('t') + 1, wordIterator.getEnd(text.indexOf('\'')));
546        assertEquals(text.indexOf('t') + 1, wordIterator.getEnd(text.indexOf('t')));
547        assertEquals(text.indexOf('e') + 1, wordIterator.getEnd(text.indexOf('h')));
548    }
549}
550