Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2012 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 com.android.inputmethod.latin.settings.SettingsValues;
     20 
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 import java.util.Arrays;
     25 import java.util.List;
     26 import java.util.Locale;
     27 
     28 @SmallTest
     29 public class StringUtilsTests extends AndroidTestCase {
     30     public void testContainsInArray() {
     31         assertFalse("empty array", StringUtils.containsInArray("key", new String[0]));
     32         assertFalse("not in 1 element", StringUtils.containsInArray("key", new String[] {
     33                 "key1"
     34         }));
     35         assertFalse("not in 2 elements", StringUtils.containsInArray("key", new String[] {
     36                 "key1", "key2"
     37         }));
     38 
     39         assertTrue("in 1 element", StringUtils.containsInArray("key", new String[] {
     40                 "key"
     41         }));
     42         assertTrue("in 2 elements", StringUtils.containsInArray("key", new String[] {
     43                 "key1", "key"
     44         }));
     45     }
     46 
     47     public void testContainsInExtraValues() {
     48         assertFalse("null", StringUtils.containsInCommaSplittableText("key", null));
     49         assertFalse("empty", StringUtils.containsInCommaSplittableText("key", ""));
     50         assertFalse("not in 1 element",
     51                 StringUtils.containsInCommaSplittableText("key", "key1"));
     52         assertFalse("not in 2 elements",
     53                 StringUtils.containsInCommaSplittableText("key", "key1,key2"));
     54 
     55         assertTrue("in 1 element", StringUtils.containsInCommaSplittableText("key", "key"));
     56         assertTrue("in 2 elements", StringUtils.containsInCommaSplittableText("key", "key1,key"));
     57     }
     58 
     59     public void testAppendToExtraValuesIfNotExists() {
     60         assertEquals("null", "key",
     61                 StringUtils.appendToCommaSplittableTextIfNotExists("key", null));
     62         assertEquals("empty", "key",
     63                 StringUtils.appendToCommaSplittableTextIfNotExists("key", ""));
     64 
     65         assertEquals("not in 1 element", "key1,key",
     66                 StringUtils.appendToCommaSplittableTextIfNotExists("key", "key1"));
     67         assertEquals("not in 2 elements", "key1,key2,key",
     68                 StringUtils.appendToCommaSplittableTextIfNotExists("key", "key1,key2"));
     69 
     70         assertEquals("in 1 element", "key",
     71                 StringUtils.appendToCommaSplittableTextIfNotExists("key", "key"));
     72         assertEquals("in 2 elements at position 1", "key,key2",
     73                 StringUtils.appendToCommaSplittableTextIfNotExists("key", "key,key2"));
     74         assertEquals("in 2 elements at position 2", "key1,key",
     75                 StringUtils.appendToCommaSplittableTextIfNotExists("key", "key1,key"));
     76         assertEquals("in 3 elements at position 2", "key1,key,key3",
     77                 StringUtils.appendToCommaSplittableTextIfNotExists("key", "key1,key,key3"));
     78     }
     79 
     80     public void testRemoveFromExtraValuesIfExists() {
     81         assertEquals("null", "", StringUtils.removeFromCommaSplittableTextIfExists("key", null));
     82         assertEquals("empty", "", StringUtils.removeFromCommaSplittableTextIfExists("key", ""));
     83 
     84         assertEquals("not in 1 element", "key1",
     85                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key1"));
     86         assertEquals("not in 2 elements", "key1,key2",
     87                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key2"));
     88 
     89         assertEquals("in 1 element", "",
     90                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key"));
     91         assertEquals("in 2 elements at position 1", "key2",
     92                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key,key2"));
     93         assertEquals("in 2 elements at position 2", "key1",
     94                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key"));
     95         assertEquals("in 3 elements at position 2", "key1,key3",
     96                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key,key3"));
     97 
     98         assertEquals("in 3 elements at position 1,2,3", "",
     99                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key,key,key"));
    100         assertEquals("in 5 elements at position 2,4", "key1,key3,key5",
    101                 StringUtils.removeFromCommaSplittableTextIfExists(
    102                         "key", "key1,key,key3,key,key5"));
    103     }
    104 
    105 
    106     public void testCapitalizeFirstCodePoint() {
    107         assertEquals("SSaa",
    108                 StringUtils.capitalizeFirstCodePoint("aa", Locale.GERMAN));
    109         assertEquals("Aa",
    110                 StringUtils.capitalizeFirstCodePoint("aa", Locale.GERMAN));
    111         assertEquals("Iab",
    112                 StringUtils.capitalizeFirstCodePoint("iab", Locale.ENGLISH));
    113         assertEquals("CAmElCaSe",
    114                 StringUtils.capitalizeFirstCodePoint("cAmElCaSe", Locale.ENGLISH));
    115         assertEquals("ab",
    116                 StringUtils.capitalizeFirstCodePoint("iab", new Locale("tr")));
    117         assertEquals("AB",
    118                 StringUtils.capitalizeFirstCodePoint("AB", new Locale("tr")));
    119         assertEquals("A",
    120                 StringUtils.capitalizeFirstCodePoint("a", Locale.ENGLISH));
    121         assertEquals("A",
    122                 StringUtils.capitalizeFirstCodePoint("A", Locale.ENGLISH));
    123     }
    124 
    125     public void testCapitalizeFirstAndDowncaseRest() {
    126         assertEquals("SSaa",
    127                 StringUtils.capitalizeFirstAndDowncaseRest("aa", Locale.GERMAN));
    128         assertEquals("Aa",
    129                 StringUtils.capitalizeFirstAndDowncaseRest("aa", Locale.GERMAN));
    130         assertEquals("Iab",
    131                 StringUtils.capitalizeFirstAndDowncaseRest("iab", Locale.ENGLISH));
    132         assertEquals("Camelcase",
    133                 StringUtils.capitalizeFirstAndDowncaseRest("cAmElCaSe", Locale.ENGLISH));
    134         assertEquals("ab",
    135                 StringUtils.capitalizeFirstAndDowncaseRest("iab", new Locale("tr")));
    136         assertEquals("Aib",
    137                 StringUtils.capitalizeFirstAndDowncaseRest("AB", new Locale("tr")));
    138         assertEquals("A",
    139                 StringUtils.capitalizeFirstAndDowncaseRest("a", Locale.ENGLISH));
    140         assertEquals("A",
    141                 StringUtils.capitalizeFirstAndDowncaseRest("A", Locale.ENGLISH));
    142     }
    143 
    144     public void testGetCapitalizationType() {
    145         assertEquals(StringUtils.CAPITALIZE_NONE,
    146                 StringUtils.getCapitalizationType("capitalize"));
    147         assertEquals(StringUtils.CAPITALIZE_NONE,
    148                 StringUtils.getCapitalizationType("cApITalize"));
    149         assertEquals(StringUtils.CAPITALIZE_NONE,
    150                 StringUtils.getCapitalizationType("capitalizE"));
    151         assertEquals(StringUtils.CAPITALIZE_NONE,
    152                 StringUtils.getCapitalizationType("__c a piu$@tali56ze"));
    153         assertEquals(StringUtils.CAPITALIZE_FIRST,
    154                 StringUtils.getCapitalizationType("A__c a piu$@tali56ze"));
    155         assertEquals(StringUtils.CAPITALIZE_FIRST,
    156                 StringUtils.getCapitalizationType("Capitalize"));
    157         assertEquals(StringUtils.CAPITALIZE_FIRST,
    158                 StringUtils.getCapitalizationType("     Capitalize"));
    159         assertEquals(StringUtils.CAPITALIZE_ALL,
    160                 StringUtils.getCapitalizationType("CAPITALIZE"));
    161         assertEquals(StringUtils.CAPITALIZE_ALL,
    162                 StringUtils.getCapitalizationType("  PI26LIE"));
    163         assertEquals(StringUtils.CAPITALIZE_NONE,
    164                 StringUtils.getCapitalizationType(""));
    165     }
    166 
    167     public void testIsIdenticalAfterUpcaseIsIdenticalAfterDowncase() {
    168         assertFalse(StringUtils.isIdenticalAfterUpcase("capitalize"));
    169         assertTrue(StringUtils.isIdenticalAfterDowncase("capitalize"));
    170         assertFalse(StringUtils.isIdenticalAfterUpcase("cApITalize"));
    171         assertFalse(StringUtils.isIdenticalAfterDowncase("cApITalize"));
    172         assertFalse(StringUtils.isIdenticalAfterUpcase("capitalizE"));
    173         assertFalse(StringUtils.isIdenticalAfterDowncase("capitalizE"));
    174         assertFalse(StringUtils.isIdenticalAfterUpcase("__c a piu$@tali56ze"));
    175         assertTrue(StringUtils.isIdenticalAfterDowncase("__c a piu$@tali56ze"));
    176         assertFalse(StringUtils.isIdenticalAfterUpcase("A__c a piu$@tali56ze"));
    177         assertFalse(StringUtils.isIdenticalAfterDowncase("A__c a piu$@tali56ze"));
    178         assertFalse(StringUtils.isIdenticalAfterUpcase("Capitalize"));
    179         assertFalse(StringUtils.isIdenticalAfterDowncase("Capitalize"));
    180         assertFalse(StringUtils.isIdenticalAfterUpcase("     Capitalize"));
    181         assertFalse(StringUtils.isIdenticalAfterDowncase("     Capitalize"));
    182         assertTrue(StringUtils.isIdenticalAfterUpcase("CAPITALIZE"));
    183         assertFalse(StringUtils.isIdenticalAfterDowncase("CAPITALIZE"));
    184         assertTrue(StringUtils.isIdenticalAfterUpcase("  PI26LIE"));
    185         assertFalse(StringUtils.isIdenticalAfterDowncase("  PI26LIE"));
    186         assertTrue(StringUtils.isIdenticalAfterUpcase(""));
    187         assertTrue(StringUtils.isIdenticalAfterDowncase(""));
    188     }
    189 
    190     public void testLooksValidForDictionaryInsertion() {
    191         final SettingsValues settings =
    192                 SettingsValues.makeDummySettingsValuesForTest(Locale.ENGLISH);
    193         assertTrue(StringUtils.looksValidForDictionaryInsertion("aochaueo", settings));
    194         assertFalse(StringUtils.looksValidForDictionaryInsertion("", settings));
    195         assertTrue(StringUtils.looksValidForDictionaryInsertion("ao-ch'aueo", settings));
    196         assertFalse(StringUtils.looksValidForDictionaryInsertion("2908743256", settings));
    197         assertTrue(StringUtils.looksValidForDictionaryInsertion("31aochaueo", settings));
    198         assertFalse(StringUtils.looksValidForDictionaryInsertion("akeo  raeoch oerch .", settings));
    199         assertFalse(StringUtils.looksValidForDictionaryInsertion("!!!", settings));
    200     }
    201 
    202     private static void checkCapitalize(final String src, final String dst, final String separators,
    203             final Locale locale) {
    204         assertEquals(dst, StringUtils.capitalizeEachWord(src, separators, locale));
    205         assert(src.equals(dst)
    206                 == StringUtils.isIdenticalAfterCapitalizeEachWord(src, separators));
    207     }
    208 
    209     public void testCapitalizeEachWord() {
    210         checkCapitalize("", "", " ", Locale.ENGLISH);
    211         checkCapitalize("test", "Test", " ", Locale.ENGLISH);
    212         checkCapitalize("    test", "    Test", " ", Locale.ENGLISH);
    213         checkCapitalize("Test", "Test", " ", Locale.ENGLISH);
    214         checkCapitalize("    Test", "    Test", " ", Locale.ENGLISH);
    215         checkCapitalize(".Test", ".test", " ", Locale.ENGLISH);
    216         checkCapitalize(".Test", ".Test", " .", Locale.ENGLISH);
    217         checkCapitalize(".Test", ".Test", ". ", Locale.ENGLISH);
    218         checkCapitalize("test and retest", "Test And Retest", " .", Locale.ENGLISH);
    219         checkCapitalize("Test and retest", "Test And Retest", " .", Locale.ENGLISH);
    220         checkCapitalize("Test And Retest", "Test And Retest", " .", Locale.ENGLISH);
    221         checkCapitalize("Test And.Retest  ", "Test And.Retest  ", " .", Locale.ENGLISH);
    222         checkCapitalize("Test And.retest  ", "Test And.Retest  ", " .", Locale.ENGLISH);
    223         checkCapitalize("Test And.retest  ", "Test And.retest  ", " ", Locale.ENGLISH);
    224         checkCapitalize("Test And.Retest  ", "Test And.retest  ", " ", Locale.ENGLISH);
    225         checkCapitalize("test and ietest", "Test And etest", " .", new Locale("tr"));
    226         checkCapitalize("test and ietest", "Test And Ietest", " .", Locale.ENGLISH);
    227         checkCapitalize("Test&Retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
    228         checkCapitalize("Test&retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
    229         checkCapitalize("test&Retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
    230         checkCapitalize("rest\nrecreation! And in the end...",
    231                 "Rest\nRecreation! And In The End...", " \n.!?*,();&", Locale.ENGLISH);
    232         checkCapitalize("lorem ipsum dolor sit amet", "Lorem Ipsum Dolor Sit Amet",
    233                 " \n.,!?*()&;", Locale.ENGLISH);
    234         checkCapitalize("Lorem!Ipsum (Dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
    235                 " \n,.;!?*()&", Locale.ENGLISH);
    236         checkCapitalize("Lorem!Ipsum (dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
    237                 " \n,.;!?*()&", Locale.ENGLISH);
    238     }
    239 
    240     public void testLooksLikeURL() {
    241         assertTrue(StringUtils.lastPartLooksLikeURL("http://www.google."));
    242         assertFalse(StringUtils.lastPartLooksLikeURL("word wo"));
    243         assertTrue(StringUtils.lastPartLooksLikeURL("/etc/foo"));
    244         assertFalse(StringUtils.lastPartLooksLikeURL("left/right"));
    245         assertTrue(StringUtils.lastPartLooksLikeURL("www.goo"));
    246         assertTrue(StringUtils.lastPartLooksLikeURL("www."));
    247         assertFalse(StringUtils.lastPartLooksLikeURL("U.S.A"));
    248         assertFalse(StringUtils.lastPartLooksLikeURL("U.S.A."));
    249         assertTrue(StringUtils.lastPartLooksLikeURL("rtsp://foo."));
    250         assertTrue(StringUtils.lastPartLooksLikeURL("://"));
    251         assertFalse(StringUtils.lastPartLooksLikeURL("abc/"));
    252         assertTrue(StringUtils.lastPartLooksLikeURL("abc.def/ghi"));
    253         assertFalse(StringUtils.lastPartLooksLikeURL("abc.def"));
    254         // TODO: ideally this would not look like a URL, but to keep down the complexity of the
    255         // code for now True is acceptable.
    256         assertTrue(StringUtils.lastPartLooksLikeURL("abc./def"));
    257         // TODO: ideally this would not look like a URL, but to keep down the complexity of the
    258         // code for now True is acceptable.
    259         assertTrue(StringUtils.lastPartLooksLikeURL(".abc/def"));
    260     }
    261 
    262     public void testHexStringUtils() {
    263         final byte[] bytes = new byte[] { (byte)0x01, (byte)0x11, (byte)0x22, (byte)0x33,
    264                 (byte)0x55, (byte)0x88, (byte)0xEE };
    265         final String bytesStr = StringUtils.byteArrayToHexString(bytes);
    266         final byte[] bytes2 = StringUtils.hexStringToByteArray(bytesStr);
    267         for (int i = 0; i < bytes.length; ++i) {
    268             assertTrue(bytes[i] == bytes2[i]);
    269         }
    270         final String bytesStr2 = StringUtils.byteArrayToHexString(bytes2);
    271         assertTrue(bytesStr.equals(bytesStr2));
    272     }
    273 
    274     public void testJsonStringUtils() {
    275         final Object[] objs = new Object[] { 1, "aaa", "bbb", 3 };
    276         final List<Object> objArray = Arrays.asList(objs);
    277         final String str = StringUtils.listToJsonStr(objArray);
    278         final List<Object> newObjArray = StringUtils.jsonStrToList(str);
    279         for (int i = 0; i < objs.length; ++i) {
    280             assertEquals(objs[i], newObjArray.get(i));
    281         }
    282     }
    283 }
    284