Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 
     17 package android.text.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertNotNull;
     22 import static org.junit.Assert.fail;
     23 
     24 import android.text.AlteredCharSequence;
     25 import android.text.Spanned;
     26 
     27 import androidx.test.filters.SmallTest;
     28 import androidx.test.runner.AndroidJUnit4;
     29 
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 
     33 @SmallTest
     34 @RunWith(AndroidJUnit4.class)
     35 public class AlteredCharSequenceTest {
     36     private static final String SOURCE_STR = "This is a char sequence.";
     37 
     38     private AlteredCharSequence mAlteredCharSequence;
     39 
     40     @Test
     41     public void testCharAt() {
     42         mAlteredCharSequence = AlteredCharSequence.make("abcdefgh", new char[] {'i', 's'}, 0, 2);
     43         // chars in sub.
     44         assertEquals('i', mAlteredCharSequence.charAt(0));
     45         assertEquals('s', mAlteredCharSequence.charAt(1));
     46         // chars in source.
     47         assertEquals('c', mAlteredCharSequence.charAt(2));
     48         assertEquals('d', mAlteredCharSequence.charAt(3));
     49     }
     50 
     51     @Test(expected=StringIndexOutOfBoundsException.class)
     52     public void testCharAtTooLow() {
     53         mAlteredCharSequence = AlteredCharSequence.make("abcdefgh", new char[] {'i', 's'}, 0, 2);
     54 
     55         mAlteredCharSequence.charAt(-1);
     56     }
     57 
     58     @Test(expected=StringIndexOutOfBoundsException.class)
     59     public void testCharAtTooHigh() {
     60         mAlteredCharSequence = AlteredCharSequence.make("abcdefgh", new char[] {'i', 's'}, 0, 2);
     61 
     62         mAlteredCharSequence.charAt(mAlteredCharSequence.length() + 1);
     63     }
     64 
     65     @Test
     66     public void testGetChars() {
     67         char[] sub = { 'i', 's' };
     68         int start = 0;
     69         int end = 2;
     70         int off = 1;
     71 
     72         mAlteredCharSequence = AlteredCharSequence.make(SOURCE_STR, sub, 0, sub.length);
     73         char[] dest = new char[4];
     74         mAlteredCharSequence.getChars(start, end, dest, off);
     75 
     76         char[] expected = { 0, 'T', 'h', 0 };
     77         for (int i = off; i < end - start + off; i++) {
     78             assertEquals(expected[i], dest[i]);
     79         }
     80         end = 0;
     81         for (int i = 0; i < 4; i++) {
     82             dest[i] = 'a';
     83         }
     84         mAlteredCharSequence.getChars(start, end, dest, off);
     85         for (int i = off; i < end - start + off; i++) {
     86             assertEquals('a', dest[i]);
     87         }
     88         start = end + 1;
     89         try {
     90             mAlteredCharSequence.getChars(start, end, dest, off);
     91             fail("should raise a StringIndexOutOfBoundsException.");
     92         } catch (StringIndexOutOfBoundsException e) {
     93             // expected.
     94         }
     95     }
     96 
     97     @Test
     98     public void testLength() {
     99         char[] sub = { 'i', 's' };
    100 
    101         CharSequence source = SOURCE_STR;
    102         for (int i = 1; i < 10; i++) {
    103             source = source + "a";
    104             mAlteredCharSequence = AlteredCharSequence.make(source, sub, 0, sub.length);
    105             assertEquals(source.length(), mAlteredCharSequence.length());
    106         }
    107     }
    108 
    109     @Test
    110     public void testMake() {
    111         char[] sub = { 'i', 's' };
    112 
    113         CharSequence source = SOURCE_STR;
    114         mAlteredCharSequence = AlteredCharSequence.make(source, sub, 0, sub.length);
    115         assertNotNull(mAlteredCharSequence);
    116         assertEquals(source.toString(), mAlteredCharSequence.toString());
    117         String acsClassName = mAlteredCharSequence.getClass().getName();
    118 
    119         MockSpanned spanned = new MockSpanned("This is a spanned.");
    120         mAlteredCharSequence = AlteredCharSequence.make(spanned, sub, 0, sub.length);
    121         assertNotNull(mAlteredCharSequence);
    122         assertEquals(0, mAlteredCharSequence.length());
    123         String spanClassName = mAlteredCharSequence.getClass().getName();
    124         assertFalse(0 == acsClassName.compareTo(spanClassName));
    125     }
    126 
    127     @Test
    128     public void testSubSequence() {
    129         char[] sub = { 'i', 's' };
    130 
    131         CharSequence source = SOURCE_STR;
    132         mAlteredCharSequence = AlteredCharSequence.make(source, sub, 0, sub.length);
    133         assertEquals("Th", mAlteredCharSequence.subSequence(0, 2).toString());
    134 
    135         try {
    136             mAlteredCharSequence.subSequence(0, 100);
    137             fail("Should throw StringIndexOutOfBoundsException!");
    138         } catch (StringIndexOutOfBoundsException e) {
    139             // expected.
    140         }
    141     }
    142 
    143     @Test
    144     public void testToString() {
    145         char[] sub = { 'i', 's' };
    146         CharSequence source = SOURCE_STR;
    147         mAlteredCharSequence = AlteredCharSequence.make(source, sub, 0, sub.length);
    148         assertNotNull(mAlteredCharSequence.toString());
    149     }
    150 
    151     class MockSpanned implements Spanned {
    152         public MockSpanned(String sequence) {
    153         }
    154 
    155         public int getSpanEnd(Object tag) {
    156             return 0;
    157         }
    158 
    159         public int getSpanFlags(Object tag) {
    160             return 0;
    161         }
    162 
    163         public int getSpanStart(Object tag) {
    164             return 0;
    165         }
    166 
    167         public <T> T[] getSpans(int start, int end, Class<T> type) {
    168             return null;
    169         }
    170 
    171         public int nextSpanTransition(int start, int limit, Class type) {
    172             return 0;
    173         }
    174 
    175         public char charAt(int index) {
    176             return 0;
    177         }
    178 
    179         public int length() {
    180             return 0;
    181         }
    182 
    183         public CharSequence subSequence(int start, int end) {
    184             return null;
    185         }
    186     }
    187 }
    188 
    189