1 /*
2  * Copyright (C) 2014 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 com.android.inputmethod.latin.utils;
18 
19 import android.test.AndroidTestCase;
20 import android.test.suitebuilder.annotation.SmallTest;
21 
22 import com.android.inputmethod.latin.Constants;
23 import com.android.inputmethod.latin.utils.StringUtils;
24 
25 import java.util.Locale;
26 
27 @SmallTest
28 public class StringUtilsTests extends AndroidTestCase {
29     private static final Locale US = Locale.US;
30     private static final Locale GERMAN = Locale.GERMAN;
31     private static final Locale TURKEY = new Locale("tr", "TR");
32     private static final Locale GREECE = new Locale("el", "GR");
33 
assert_toUpperCaseOfStringForLocale(final Locale locale, final String lowerCase, final String expected)34     private static void assert_toUpperCaseOfStringForLocale(final Locale locale,
35             final String lowerCase, final String expected) {
36         assertEquals(lowerCase + " in " + locale, expected,
37                 StringUtils.toUpperCaseOfStringForLocale(
38                         lowerCase, true /* needsToUpperCase */, locale));
39     }
40 
test_toUpperCaseOfStringForLocale()41     public void test_toUpperCaseOfStringForLocale() {
42         assert_toUpperCaseOfStringForLocale(US, null, null);
43         assert_toUpperCaseOfStringForLocale(US, "", "");
44         assert_toUpperCaseOfStringForLocale(US, "aeiou", "AEIOU");
45         // U+00E0: "à" LATIN SMALL LETTER A WITH GRAVE
46         // U+00E8: "è" LATIN SMALL LETTER E WITH GRAVE
47         // U+00EE: "î" LATIN SMALL LETTER I WITH CIRCUMFLEX
48         // U+00F6: "ö" LATIN SMALL LETTER O WITH DIAERESIS
49         // U+016B: "ū" LATIN SMALL LETTER U WITH MACRON
50         // U+00F1: "ñ" LATIN SMALL LETTER N WITH TILDE
51         // U+00E7: "ç" LATIN SMALL LETTER C WITH CEDILLA
52         // U+00C0: "À" LATIN CAPITAL LETTER A WITH GRAVE
53         // U+00C8: "È" LATIN CAPITAL LETTER E WITH GRAVE
54         // U+00CE: "Î" LATIN CAPITAL LETTER I WITH CIRCUMFLEX
55         // U+00D6: "Ö" LATIN CAPITAL LETTER O WITH DIAERESIS
56         // U+016A: "Ū" LATIN CAPITAL LETTER U WITH MACRON
57         // U+00D1: "Ñ" LATIN CAPITAL LETTER N WITH TILDE
58         // U+00C7: "Ç" LATIN CAPITAL LETTER C WITH CEDILLA
59         assert_toUpperCaseOfStringForLocale(US,
60                 "\u00E0\u00E8\u00EE\u00F6\u016B\u00F1\u00E7",
61                 "\u00C0\u00C8\u00CE\u00D6\u016A\u00D1\u00C7");
62         // U+00DF: "ß" LATIN SMALL LETTER SHARP S
63         // U+015B: "ś" LATIN SMALL LETTER S WITH ACUTE
64         // U+0161: "š" LATIN SMALL LETTER S WITH CARON
65         // U+015A: "Ś" LATIN CAPITAL LETTER S WITH ACUTE
66         // U+0160: "Š" LATIN CAPITAL LETTER S WITH CARONZ
67         assert_toUpperCaseOfStringForLocale(GERMAN,
68                 "\u00DF\u015B\u0161",
69                 "SS\u015A\u0160");
70         // U+0259: "ə" LATIN SMALL LETTER SCHWA
71         // U+0069: "i" LATIN SMALL LETTER I
72         // U+0131: "ı" LATIN SMALL LETTER DOTLESS I
73         // U+018F: "Ə" LATIN SMALL LETTER SCHWA
74         // U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
75         // U+0049: "I" LATIN SMALL LETTER I
76         assert_toUpperCaseOfStringForLocale(TURKEY,
77                 "\u0259\u0069\u0131",
78                 "\u018F\u0130\u0049");
79         // U+03C3: "σ" GREEK SMALL LETTER SIGMA
80         // U+03C2: "ς" GREEK SMALL LETTER FINAL SIGMA
81         // U+03A3: "Σ" GREEK CAPITAL LETTER SIGMA
82         assert_toUpperCaseOfStringForLocale(GREECE,
83                 "\u03C3\u03C2",
84                 "\u03A3\u03A3");
85         // U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
86         // U+03AD: "έ" GREEK SMALL LETTER EPSILON WITH TONOS
87         // U+03AE: "ή" GREEK SMALL LETTER ETA WITH TONOS
88         // U+03AF: "ί" GREEK SMALL LETTER IOTA WITH TONOS
89         // U+03CC: "ό" GREEK SMALL LETTER OMICRON WITH TONOS
90         // U+03CD: "ύ" GREEK SMALL LETTER UPSILON WITH TONOS
91         // U+03CE: "ώ" GREEK SMALL LETTER OMEGA WITH TONOS
92         // U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
93         // U+0388: "Έ" GREEK CAPITAL LETTER EPSILON WITH TONOS
94         // U+0389: "Ή" GREEK CAPITAL LETTER ETA WITH TONOS
95         // U+038A: "Ί" GREEK CAPITAL LETTER IOTA WITH TONOS
96         // U+038C: "Ό" GREEK CAPITAL LETTER OMICRON WITH TONOS
97         // U+038E: "Ύ" GREEK CAPITAL LETTER UPSILON WITH TONOS
98         // U+038F: "Ώ" GREEK CAPITAL LETTER OMEGA WITH TONOS
99         assert_toUpperCaseOfStringForLocale(GREECE,
100                 "\u03AC\u03AD\u03AE\u03AF\u03CC\u03CD\u03CE",
101                 "\u0386\u0388\u0389\u038A\u038C\u038E\u038F");
102         // U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
103         // U+03CB: "ϋ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA
104         // U+0390: "ΐ" GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
105         // U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
106         // U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
107         // U+03AB: "Ϋ" GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA
108         // U+0399: "Ι" GREEK CAPITAL LETTER IOTA
109         // U+03A5: "Υ" GREEK CAPITAL LETTER UPSILON
110         // U+0308: COMBINING DIAERESIS
111         // U+0301: COMBINING GRAVE ACCENT
112         assert_toUpperCaseOfStringForLocale(GREECE,
113                 "\u03CA\u03CB\u0390\u03B0",
114                 "\u03AA\u03AB\u0399\u0308\u0301\u03A5\u0308\u0301");
115     }
116 
assert_toUpperCaseOfCodeForLocale(final Locale locale, final int lowerCase, final int expected)117     private static void assert_toUpperCaseOfCodeForLocale(final Locale locale, final int lowerCase,
118             final int expected) {
119         assertEquals(lowerCase + " in " + locale, expected,
120                 StringUtils.toUpperCaseOfCodeForLocale(
121                         lowerCase, true /* needsToUpperCase */, locale));
122     }
123 
test_toUpperCaseOfCodeForLocale()124     public void test_toUpperCaseOfCodeForLocale() {
125         assert_toUpperCaseOfCodeForLocale(US, Constants.CODE_ENTER, Constants.CODE_ENTER);
126         assert_toUpperCaseOfCodeForLocale(US, Constants.CODE_SPACE, Constants.CODE_SPACE);
127         assert_toUpperCaseOfCodeForLocale(US, Constants.CODE_COMMA, Constants.CODE_COMMA);
128         // U+0069: "i" LATIN SMALL LETTER I
129         // U+0131: "ı" LATIN SMALL LETTER DOTLESS I
130         // U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
131         // U+0049: "I" LATIN SMALL LETTER I
132         assert_toUpperCaseOfCodeForLocale(US, 0x0069, 0x0049); // i -> I
133         assert_toUpperCaseOfCodeForLocale(US, 0x0131, 0x0049); // ı -> I
134         assert_toUpperCaseOfCodeForLocale(TURKEY, 0x0069, 0x0130); // i -> İ
135         assert_toUpperCaseOfCodeForLocale(TURKEY, 0x0131, 0x0049); // ı -> I
136         // U+00DF: "ß" LATIN SMALL LETTER SHARP S
137         // The title case of "ß" is "SS".
138         assert_toUpperCaseOfCodeForLocale(US, 0x00DF, Constants.CODE_UNSPECIFIED);
139         // U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
140         // U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
141         assert_toUpperCaseOfCodeForLocale(GREECE, 0x03AC, 0x0386);
142         // U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
143         // U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
144         assert_toUpperCaseOfCodeForLocale(GREECE, 0x03CA, 0x03AA);
145         // U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
146         // The title case of "ΰ" is "\u03A5\u0308\u0301".
147         assert_toUpperCaseOfCodeForLocale(GREECE, 0x03B0, Constants.CODE_UNSPECIFIED);
148     }
149 
assert_capitalizeFirstCodePoint(final Locale locale, final String text, final String expected)150     private static void assert_capitalizeFirstCodePoint(final Locale locale, final String text,
151             final String expected) {
152         assertEquals(text + " in " + locale, expected,
153                 StringUtils.capitalizeFirstCodePoint(text, locale));
154     }
155 
test_capitalizeFirstCodePoint()156     public void test_capitalizeFirstCodePoint() {
157         assert_capitalizeFirstCodePoint(US, "", "");
158         assert_capitalizeFirstCodePoint(US, "a", "A");
159         assert_capitalizeFirstCodePoint(US, "à", "À");
160         assert_capitalizeFirstCodePoint(US, "ß", "SS");
161         assert_capitalizeFirstCodePoint(US, "text", "Text");
162         assert_capitalizeFirstCodePoint(US, "iGoogle", "IGoogle");
163         assert_capitalizeFirstCodePoint(TURKEY, "iyi", "İyi");
164         assert_capitalizeFirstCodePoint(TURKEY, "ısırdı", "Isırdı");
165         assert_capitalizeFirstCodePoint(GREECE, "ά", "Ά");
166         assert_capitalizeFirstCodePoint(GREECE, "άνεση", "Άνεση");
167     }
168 
assert_capitalizeFirstAndDowncaseRest(final Locale locale, final String text, final String expected)169     private static void assert_capitalizeFirstAndDowncaseRest(final Locale locale,
170             final String text, final String expected) {
171         assertEquals(text + " in " + locale, expected,
172                 StringUtils.capitalizeFirstAndDowncaseRest(text, locale));
173     }
174 
test_capitalizeFirstAndDowncaseRest()175     public void test_capitalizeFirstAndDowncaseRest() {
176         assert_capitalizeFirstAndDowncaseRest(US, "", "");
177         assert_capitalizeFirstAndDowncaseRest(US, "a", "A");
178         assert_capitalizeFirstAndDowncaseRest(US, "à", "À");
179         assert_capitalizeFirstAndDowncaseRest(US, "ß", "SS");
180         assert_capitalizeFirstAndDowncaseRest(US, "text", "Text");
181         assert_capitalizeFirstAndDowncaseRest(US, "iGoogle", "Igoogle");
182         assert_capitalizeFirstAndDowncaseRest(US, "invite", "Invite");
183         assert_capitalizeFirstAndDowncaseRest(US, "INVITE", "Invite");
184         assert_capitalizeFirstAndDowncaseRest(TURKEY, "iyi", "İyi");
185         assert_capitalizeFirstAndDowncaseRest(TURKEY, "İYİ", "İyi");
186         assert_capitalizeFirstAndDowncaseRest(TURKEY, "ısırdı", "Isırdı");
187         assert_capitalizeFirstAndDowncaseRest(TURKEY, "ISIRDI", "Isırdı");
188         assert_capitalizeFirstAndDowncaseRest(GREECE, "ά", "Ά");
189         assert_capitalizeFirstAndDowncaseRest(GREECE, "άνεση", "Άνεση");
190         assert_capitalizeFirstAndDowncaseRest(GREECE, "ΆΝΕΣΗ", "Άνεση");
191     }
192 }
193