Home | History | Annotate | Download | only in latin
      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;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 import java.util.Locale;
     23 
     24 @SmallTest
     25 public class StringUtilsTests extends AndroidTestCase {
     26     public void testContainsInArray() {
     27         assertFalse("empty array", StringUtils.containsInArray("key", new String[0]));
     28         assertFalse("not in 1 element", StringUtils.containsInArray("key", new String[] {
     29                 "key1"
     30         }));
     31         assertFalse("not in 2 elements", StringUtils.containsInArray("key", new String[] {
     32                 "key1", "key2"
     33         }));
     34 
     35         assertTrue("in 1 element", StringUtils.containsInArray("key", new String[] {
     36                 "key"
     37         }));
     38         assertTrue("in 2 elements", StringUtils.containsInArray("key", new String[] {
     39                 "key1", "key"
     40         }));
     41     }
     42 
     43     public void testContainsInCsv() {
     44         assertFalse("null", StringUtils.containsInCsv("key", null));
     45         assertFalse("empty", StringUtils.containsInCsv("key", ""));
     46         assertFalse("not in 1 element", StringUtils.containsInCsv("key", "key1"));
     47         assertFalse("not in 2 elements", StringUtils.containsInCsv("key", "key1,key2"));
     48 
     49         assertTrue("in 1 element", StringUtils.containsInCsv("key", "key"));
     50         assertTrue("in 2 elements", StringUtils.containsInCsv("key", "key1,key"));
     51     }
     52 
     53     public void testAppendToCsvIfNotExists() {
     54         assertEquals("null", "key", StringUtils.appendToCsvIfNotExists("key", null));
     55         assertEquals("empty", "key", StringUtils.appendToCsvIfNotExists("key", ""));
     56 
     57         assertEquals("not in 1 element", "key1,key",
     58                 StringUtils.appendToCsvIfNotExists("key", "key1"));
     59         assertEquals("not in 2 elements", "key1,key2,key",
     60                 StringUtils.appendToCsvIfNotExists("key", "key1,key2"));
     61 
     62         assertEquals("in 1 element", "key",
     63                 StringUtils.appendToCsvIfNotExists("key", "key"));
     64         assertEquals("in 2 elements at position 1", "key,key2",
     65                 StringUtils.appendToCsvIfNotExists("key", "key,key2"));
     66         assertEquals("in 2 elements at position 2", "key1,key",
     67                 StringUtils.appendToCsvIfNotExists("key", "key1,key"));
     68         assertEquals("in 3 elements at position 2", "key1,key,key3",
     69                 StringUtils.appendToCsvIfNotExists("key", "key1,key,key3"));
     70     }
     71 
     72     public void testRemoveFromCsvIfExists() {
     73         assertEquals("null", "", StringUtils.removeFromCsvIfExists("key", null));
     74         assertEquals("empty", "", StringUtils.removeFromCsvIfExists("key", ""));
     75 
     76         assertEquals("not in 1 element", "key1",
     77                 StringUtils.removeFromCsvIfExists("key", "key1"));
     78         assertEquals("not in 2 elements", "key1,key2",
     79                 StringUtils.removeFromCsvIfExists("key", "key1,key2"));
     80 
     81         assertEquals("in 1 element", "",
     82                 StringUtils.removeFromCsvIfExists("key", "key"));
     83         assertEquals("in 2 elements at position 1", "key2",
     84                 StringUtils.removeFromCsvIfExists("key", "key,key2"));
     85         assertEquals("in 2 elements at position 2", "key1",
     86                 StringUtils.removeFromCsvIfExists("key", "key1,key"));
     87         assertEquals("in 3 elements at position 2", "key1,key3",
     88                 StringUtils.removeFromCsvIfExists("key", "key1,key,key3"));
     89 
     90         assertEquals("in 3 elements at position 1,2,3", "",
     91                 StringUtils.removeFromCsvIfExists("key", "key,key,key"));
     92         assertEquals("in 5 elements at position 2,4", "key1,key3,key5",
     93                 StringUtils.removeFromCsvIfExists("key", "key1,key,key3,key,key5"));
     94     }
     95 
     96 
     97     public void testCapitalizeFirstCodePoint() {
     98         assertEquals("SSaa",
     99                 StringUtils.capitalizeFirstCodePoint("aa", Locale.GERMAN));
    100         assertEquals("Aa",
    101                 StringUtils.capitalizeFirstCodePoint("aa", Locale.GERMAN));
    102         assertEquals("Iab",
    103                 StringUtils.capitalizeFirstCodePoint("iab", Locale.ENGLISH));
    104         assertEquals("CAmElCaSe",
    105                 StringUtils.capitalizeFirstCodePoint("cAmElCaSe", Locale.ENGLISH));
    106         assertEquals("ab",
    107                 StringUtils.capitalizeFirstCodePoint("iab", new Locale("tr")));
    108         assertEquals("AB",
    109                 StringUtils.capitalizeFirstCodePoint("AB", new Locale("tr")));
    110         assertEquals("A",
    111                 StringUtils.capitalizeFirstCodePoint("a", Locale.ENGLISH));
    112         assertEquals("A",
    113                 StringUtils.capitalizeFirstCodePoint("A", Locale.ENGLISH));
    114     }
    115 
    116     public void testCapitalizeFirstAndDowncaseRest() {
    117         assertEquals("SSaa",
    118                 StringUtils.capitalizeFirstAndDowncaseRest("aa", Locale.GERMAN));
    119         assertEquals("Aa",
    120                 StringUtils.capitalizeFirstAndDowncaseRest("aa", Locale.GERMAN));
    121         assertEquals("Iab",
    122                 StringUtils.capitalizeFirstAndDowncaseRest("iab", Locale.ENGLISH));
    123         assertEquals("Camelcase",
    124                 StringUtils.capitalizeFirstAndDowncaseRest("cAmElCaSe", Locale.ENGLISH));
    125         assertEquals("ab",
    126                 StringUtils.capitalizeFirstAndDowncaseRest("iab", new Locale("tr")));
    127         assertEquals("Aib",
    128                 StringUtils.capitalizeFirstAndDowncaseRest("AB", new Locale("tr")));
    129         assertEquals("A",
    130                 StringUtils.capitalizeFirstAndDowncaseRest("a", Locale.ENGLISH));
    131         assertEquals("A",
    132                 StringUtils.capitalizeFirstAndDowncaseRest("A", Locale.ENGLISH));
    133     }
    134 
    135     public void testGetCapitalizationType() {
    136         assertEquals(StringUtils.CAPITALIZE_NONE,
    137                 StringUtils.getCapitalizationType("capitalize"));
    138         assertEquals(StringUtils.CAPITALIZE_NONE,
    139                 StringUtils.getCapitalizationType("cApITalize"));
    140         assertEquals(StringUtils.CAPITALIZE_NONE,
    141                 StringUtils.getCapitalizationType("capitalizE"));
    142         assertEquals(StringUtils.CAPITALIZE_NONE,
    143                 StringUtils.getCapitalizationType("__c a piu$@tali56ze"));
    144         assertEquals(StringUtils.CAPITALIZE_FIRST,
    145                 StringUtils.getCapitalizationType("A__c a piu$@tali56ze"));
    146         assertEquals(StringUtils.CAPITALIZE_FIRST,
    147                 StringUtils.getCapitalizationType("Capitalize"));
    148         assertEquals(StringUtils.CAPITALIZE_FIRST,
    149                 StringUtils.getCapitalizationType("     Capitalize"));
    150         assertEquals(StringUtils.CAPITALIZE_ALL,
    151                 StringUtils.getCapitalizationType("CAPITALIZE"));
    152         assertEquals(StringUtils.CAPITALIZE_ALL,
    153                 StringUtils.getCapitalizationType("  PI26LIE"));
    154         assertEquals(StringUtils.CAPITALIZE_NONE,
    155                 StringUtils.getCapitalizationType(""));
    156     }
    157 
    158     public void testIsIdenticalAfterUpcaseIsIdenticalAfterDowncase() {
    159         assertFalse(StringUtils.isIdenticalAfterUpcase("capitalize"));
    160         assertTrue(StringUtils.isIdenticalAfterDowncase("capitalize"));
    161         assertFalse(StringUtils.isIdenticalAfterUpcase("cApITalize"));
    162         assertFalse(StringUtils.isIdenticalAfterDowncase("cApITalize"));
    163         assertFalse(StringUtils.isIdenticalAfterUpcase("capitalizE"));
    164         assertFalse(StringUtils.isIdenticalAfterDowncase("capitalizE"));
    165         assertFalse(StringUtils.isIdenticalAfterUpcase("__c a piu$@tali56ze"));
    166         assertTrue(StringUtils.isIdenticalAfterDowncase("__c a piu$@tali56ze"));
    167         assertFalse(StringUtils.isIdenticalAfterUpcase("A__c a piu$@tali56ze"));
    168         assertFalse(StringUtils.isIdenticalAfterDowncase("A__c a piu$@tali56ze"));
    169         assertFalse(StringUtils.isIdenticalAfterUpcase("Capitalize"));
    170         assertFalse(StringUtils.isIdenticalAfterDowncase("Capitalize"));
    171         assertFalse(StringUtils.isIdenticalAfterUpcase("     Capitalize"));
    172         assertFalse(StringUtils.isIdenticalAfterDowncase("     Capitalize"));
    173         assertTrue(StringUtils.isIdenticalAfterUpcase("CAPITALIZE"));
    174         assertFalse(StringUtils.isIdenticalAfterDowncase("CAPITALIZE"));
    175         assertTrue(StringUtils.isIdenticalAfterUpcase("  PI26LIE"));
    176         assertFalse(StringUtils.isIdenticalAfterDowncase("  PI26LIE"));
    177         assertTrue(StringUtils.isIdenticalAfterUpcase(""));
    178         assertTrue(StringUtils.isIdenticalAfterDowncase(""));
    179     }
    180 
    181     private static void checkCapitalize(final String src, final String dst, final String separators,
    182             final Locale locale) {
    183         assertEquals(dst, StringUtils.capitalizeEachWord(src, separators, locale));
    184         assert(src.equals(dst)
    185                 == StringUtils.isIdenticalAfterCapitalizeEachWord(src, separators));
    186     }
    187 
    188     public void testCapitalizeEachWord() {
    189         checkCapitalize("", "", " ", Locale.ENGLISH);
    190         checkCapitalize("test", "Test", " ", Locale.ENGLISH);
    191         checkCapitalize("    test", "    Test", " ", Locale.ENGLISH);
    192         checkCapitalize("Test", "Test", " ", Locale.ENGLISH);
    193         checkCapitalize("    Test", "    Test", " ", Locale.ENGLISH);
    194         checkCapitalize(".Test", ".test", " ", Locale.ENGLISH);
    195         checkCapitalize(".Test", ".Test", " .", Locale.ENGLISH);
    196         checkCapitalize(".Test", ".Test", ". ", Locale.ENGLISH);
    197         checkCapitalize("test and retest", "Test And Retest", " .", Locale.ENGLISH);
    198         checkCapitalize("Test and retest", "Test And Retest", " .", Locale.ENGLISH);
    199         checkCapitalize("Test And Retest", "Test And Retest", " .", Locale.ENGLISH);
    200         checkCapitalize("Test And.Retest  ", "Test And.Retest  ", " .", Locale.ENGLISH);
    201         checkCapitalize("Test And.retest  ", "Test And.Retest  ", " .", Locale.ENGLISH);
    202         checkCapitalize("Test And.retest  ", "Test And.retest  ", " ", Locale.ENGLISH);
    203         checkCapitalize("Test And.Retest  ", "Test And.retest  ", " ", Locale.ENGLISH);
    204         checkCapitalize("test and ietest", "Test And etest", " .", new Locale("tr"));
    205         checkCapitalize("test and ietest", "Test And Ietest", " .", Locale.ENGLISH);
    206         checkCapitalize("Test&Retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
    207         checkCapitalize("Test&retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
    208         checkCapitalize("test&Retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
    209         checkCapitalize("rest\nrecreation! And in the end...",
    210                 "Rest\nRecreation! And In The End...", " \n.!?*,();&", Locale.ENGLISH);
    211         checkCapitalize("lorem ipsum dolor sit amet", "Lorem Ipsum Dolor Sit Amet",
    212                 " \n.,!?*()&;", Locale.ENGLISH);
    213         checkCapitalize("Lorem!Ipsum (Dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
    214                 " \n,.;!?*()&", Locale.ENGLISH);
    215         checkCapitalize("Lorem!Ipsum (dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
    216                 " \n,.;!?*()&", Locale.ENGLISH);
    217     }
    218 
    219     public void testLooksLikeURL() {
    220         assertTrue(StringUtils.lastPartLooksLikeURL("http://www.google."));
    221         assertFalse(StringUtils.lastPartLooksLikeURL("word wo"));
    222         assertTrue(StringUtils.lastPartLooksLikeURL("/etc/foo"));
    223         assertFalse(StringUtils.lastPartLooksLikeURL("left/right"));
    224         assertTrue(StringUtils.lastPartLooksLikeURL("www.goo"));
    225         assertTrue(StringUtils.lastPartLooksLikeURL("www."));
    226         assertFalse(StringUtils.lastPartLooksLikeURL("U.S.A"));
    227         assertFalse(StringUtils.lastPartLooksLikeURL("U.S.A."));
    228         assertTrue(StringUtils.lastPartLooksLikeURL("rtsp://foo."));
    229         assertTrue(StringUtils.lastPartLooksLikeURL("://"));
    230         assertFalse(StringUtils.lastPartLooksLikeURL("abc/"));
    231         assertTrue(StringUtils.lastPartLooksLikeURL("abc.def/ghi"));
    232         assertFalse(StringUtils.lastPartLooksLikeURL("abc.def"));
    233         // TODO: ideally this would not look like a URL, but to keep down the complexity of the
    234         // code for now True is acceptable.
    235         assertTrue(StringUtils.lastPartLooksLikeURL("abc./def"));
    236         // TODO: ideally this would not look like a URL, but to keep down the complexity of the
    237         // code for now True is acceptable.
    238         assertTrue(StringUtils.lastPartLooksLikeURL(".abc/def"));
    239     }
    240 }
    241