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 android.test.AndroidTestCase; 20 import android.text.AndroidCharacter; 21 22 public class AndroidCharacterTest extends AndroidTestCase { 23 testConstructor()24 public void testConstructor() { 25 new AndroidCharacter(); 26 } 27 testGetDirectionalities()28 public void testGetDirectionalities() { 29 char[] src = new char[128]; 30 for (int i = 0; i < src.length; i++) { 31 src[i] = (char) i; 32 } 33 byte[] dest = new byte[128]; 34 int count = 128; 35 AndroidCharacter.getDirectionalities(src, dest, count); 36 byte[] expected = {9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 10, 11, 12, 10, 9, 37 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 11, 12, 13, 38 13, 5, 5, 5, 13, 13, 13, 13, 13, 4, 7, 4, 7, 7, 3, 3, 3, 3, 3, 39 3, 3, 3, 3, 3, 7, 13, 13, 13, 13, 13, 13, 0, 0, 0, 0, 0, 0, 0, 40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 41 13, 13, 13, 13, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 13, 13, 13, 9}; 43 for (int i = 0; i < dest.length; i++) { 44 assertEquals(expected[i], dest[i]); 45 } 46 } 47 testGetEastAsianWidth()48 public void testGetEastAsianWidth() { 49 // LATIN CAPITAL LETTER U WITH CARON (U+01D3) 50 assertEquals(AndroidCharacter.EAST_ASIAN_WIDTH_NEUTRAL, 51 AndroidCharacter.getEastAsianWidth((char)0x01D3)); 52 53 // REPLACEMENT CHARACTER (U+FFFD) 54 assertEquals(AndroidCharacter.EAST_ASIAN_WIDTH_AMBIGUOUS, 55 AndroidCharacter.getEastAsianWidth((char)0xFFFD)); 56 57 // HALFWIDTH KATAKANA LETTER NI (U+FF86) 58 assertEquals(AndroidCharacter.EAST_ASIAN_WIDTH_HALF_WIDTH, 59 AndroidCharacter.getEastAsianWidth((char)0xFF86)); 60 61 // FULLWIDTH LATIN SMALL LETTER A (U+FF41) 62 assertEquals(AndroidCharacter.EAST_ASIAN_WIDTH_FULL_WIDTH, 63 AndroidCharacter.getEastAsianWidth((char)0xFF41)); 64 65 // LATIN CAPITAL LETTER A (U+0041) 66 assertEquals(AndroidCharacter.EAST_ASIAN_WIDTH_NARROW, 67 AndroidCharacter.getEastAsianWidth((char)0x0041)); 68 69 // IDEOGRAPHIC ANNOTATION MAN MARK (U+319F) 70 assertEquals(AndroidCharacter.EAST_ASIAN_WIDTH_WIDE, 71 AndroidCharacter.getEastAsianWidth((char)0x319F)); 72 } 73 testGetEastAsianWidths()74 public void testGetEastAsianWidths() { 75 char[] src = { 76 0x01D3, 0xFFFD, 0xFF86, 0xFF41, 0x0041, 0x319f, 77 0x319F, 0x0041, 0xFF41, 0xFF86, 0xFFFD, 0x01D3, 78 }; 79 int start = 2; 80 int count = 8; 81 byte[] dest = new byte[count]; 82 AndroidCharacter.getEastAsianWidths(src, start, count, dest); 83 byte[] expected = {2, 3, 4, 5, 5, 4, 3, 2}; 84 for (int i = 0; i < dest.length; i++) { 85 assertEquals(expected[i], dest[i]); 86 } 87 try { 88 AndroidCharacter.getEastAsianWidths(src, 24, 8, dest); 89 fail("Should throw ArrayIndexOutOfBoundsException."); 90 } catch (ArrayIndexOutOfBoundsException e) { 91 // expected. 92 } 93 try { 94 AndroidCharacter.getEastAsianWidths(src, -1024, 1, dest); 95 fail("Should throw ArrayIndexOutOfBoundsException."); 96 } catch (ArrayIndexOutOfBoundsException e) { 97 // expected. 98 } 99 try { 100 AndroidCharacter.getEastAsianWidths(src, 0, -1, dest); 101 fail("Should throw ArrayIndexOutOfBoundsException."); 102 } catch (ArrayIndexOutOfBoundsException e) { 103 // expected. 104 } 105 } 106 testGetMirror()107 public void testGetMirror() { 108 assertEquals('A', AndroidCharacter.getMirror('A')); 109 assertEquals('B', AndroidCharacter.getMirror('B')); 110 assertEquals('(', AndroidCharacter.getMirror(')')); 111 assertEquals('[', AndroidCharacter.getMirror(']')); 112 assertEquals('{', AndroidCharacter.getMirror('}')); 113 assertEquals('<', AndroidCharacter.getMirror('>')); 114 } 115 testMirror()116 public void testMirror() { 117 char[] src = new char[64]; 118 for (int i = 0; i < src.length; i++) { 119 src[i] = (char) i; 120 } 121 122 assertFalse(AndroidCharacter.mirror(src, 0, 0)); 123 assertTrue(AndroidCharacter.mirror(src, 40, 24)); 124 try { 125 AndroidCharacter.mirror(src, 65, 1); 126 fail("Should throw ArrayIndexOutOfBoundsException."); 127 } catch (ArrayIndexOutOfBoundsException e) { 128 // expected. 129 } 130 try { 131 AndroidCharacter.mirror(src, 60, 10); 132 fail("Should throw ArrayIndexOutOfBoundsException."); 133 } catch (ArrayIndexOutOfBoundsException e) { 134 // expected. 135 } 136 try { 137 AndroidCharacter.mirror(src, -1024, 1); 138 fail("Should throw ArrayIndexOutOfBoundsException."); 139 } catch (ArrayIndexOutOfBoundsException e) { 140 // expected. 141 } 142 try { 143 AndroidCharacter.mirror(src, 0, -1); 144 fail("Should throw ArrayIndexOutOfBoundsException."); 145 } catch (ArrayIndexOutOfBoundsException e) { 146 // expected. 147 } 148 String str = new String("if(a>b)"); 149 char[] strChar = str.toCharArray(); 150 assertTrue(AndroidCharacter.mirror(strChar, 0, str.length())); 151 assertEquals("if)a<b(", new String(strChar)); 152 assertFalse(AndroidCharacter.mirror(str.toCharArray(), 0, 2)); 153 } 154 } 155 156