• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.inputmethod.latin;
18 
19 import android.test.AndroidTestCase;
20 import android.text.TextUtils;
21 
22 import java.util.Locale;
23 
24 public class StringUtilsTests extends AndroidTestCase {
testContainsInArray()25     public void testContainsInArray() {
26         assertFalse("empty array", StringUtils.containsInArray("key", new String[0]));
27         assertFalse("not in 1 element", StringUtils.containsInArray("key", new String[] {
28                 "key1"
29         }));
30         assertFalse("not in 2 elements", StringUtils.containsInArray("key", new String[] {
31                 "key1", "key2"
32         }));
33 
34         assertTrue("in 1 element", StringUtils.containsInArray("key", new String[] {
35                 "key"
36         }));
37         assertTrue("in 2 elements", StringUtils.containsInArray("key", new String[] {
38                 "key1", "key"
39         }));
40     }
41 
testContainsInCsv()42     public void testContainsInCsv() {
43         assertFalse("null", StringUtils.containsInCsv("key", null));
44         assertFalse("empty", StringUtils.containsInCsv("key", ""));
45         assertFalse("not in 1 element", StringUtils.containsInCsv("key", "key1"));
46         assertFalse("not in 2 elements", StringUtils.containsInCsv("key", "key1,key2"));
47 
48         assertTrue("in 1 element", StringUtils.containsInCsv("key", "key"));
49         assertTrue("in 2 elements", StringUtils.containsInCsv("key", "key1,key"));
50     }
51 
testAppendToCsvIfNotExists()52     public void testAppendToCsvIfNotExists() {
53         assertEquals("null", "key", StringUtils.appendToCsvIfNotExists("key", null));
54         assertEquals("empty", "key", StringUtils.appendToCsvIfNotExists("key", ""));
55 
56         assertEquals("not in 1 element", "key1,key",
57                 StringUtils.appendToCsvIfNotExists("key", "key1"));
58         assertEquals("not in 2 elements", "key1,key2,key",
59                 StringUtils.appendToCsvIfNotExists("key", "key1,key2"));
60 
61         assertEquals("in 1 element", "key",
62                 StringUtils.appendToCsvIfNotExists("key", "key"));
63         assertEquals("in 2 elements at position 1", "key,key2",
64                 StringUtils.appendToCsvIfNotExists("key", "key,key2"));
65         assertEquals("in 2 elements at position 2", "key1,key",
66                 StringUtils.appendToCsvIfNotExists("key", "key1,key"));
67         assertEquals("in 3 elements at position 2", "key1,key,key3",
68                 StringUtils.appendToCsvIfNotExists("key", "key1,key,key3"));
69     }
70 
testRemoveFromCsvIfExists()71     public void testRemoveFromCsvIfExists() {
72         assertEquals("null", "", StringUtils.removeFromCsvIfExists("key", null));
73         assertEquals("empty", "", StringUtils.removeFromCsvIfExists("key", ""));
74 
75         assertEquals("not in 1 element", "key1",
76                 StringUtils.removeFromCsvIfExists("key", "key1"));
77         assertEquals("not in 2 elements", "key1,key2",
78                 StringUtils.removeFromCsvIfExists("key", "key1,key2"));
79 
80         assertEquals("in 1 element", "",
81                 StringUtils.removeFromCsvIfExists("key", "key"));
82         assertEquals("in 2 elements at position 1", "key2",
83                 StringUtils.removeFromCsvIfExists("key", "key,key2"));
84         assertEquals("in 2 elements at position 2", "key1",
85                 StringUtils.removeFromCsvIfExists("key", "key1,key"));
86         assertEquals("in 3 elements at position 2", "key1,key3",
87                 StringUtils.removeFromCsvIfExists("key", "key1,key,key3"));
88 
89         assertEquals("in 3 elements at position 1,2,3", "",
90                 StringUtils.removeFromCsvIfExists("key", "key,key,key"));
91         assertEquals("in 5 elements at position 2,4", "key1,key3,key5",
92                 StringUtils.removeFromCsvIfExists("key", "key1,key,key3,key,key5"));
93     }
94 
onePathForCaps(final CharSequence cs, final int expectedResult, final int mask, final Locale l, final boolean hasSpaceBefore)95     private void onePathForCaps(final CharSequence cs, final int expectedResult, final int mask,
96             final Locale l, final boolean hasSpaceBefore) {
97         int oneTimeResult = expectedResult & mask;
98         assertEquals("After >" + cs + "<", oneTimeResult,
99                 StringUtils.getCapsMode(cs, mask, l, hasSpaceBefore));
100     }
101 
allPathsForCaps(final CharSequence cs, final int expectedResult, final Locale l, final boolean hasSpaceBefore)102     private void allPathsForCaps(final CharSequence cs, final int expectedResult, final Locale l,
103             final boolean hasSpaceBefore) {
104         final int c = TextUtils.CAP_MODE_CHARACTERS;
105         final int w = TextUtils.CAP_MODE_WORDS;
106         final int s = TextUtils.CAP_MODE_SENTENCES;
107         onePathForCaps(cs, expectedResult, c | w | s, l, hasSpaceBefore);
108         onePathForCaps(cs, expectedResult, w | s, l, hasSpaceBefore);
109         onePathForCaps(cs, expectedResult, c | s, l, hasSpaceBefore);
110         onePathForCaps(cs, expectedResult, c | w, l, hasSpaceBefore);
111         onePathForCaps(cs, expectedResult, c, l, hasSpaceBefore);
112         onePathForCaps(cs, expectedResult, w, l, hasSpaceBefore);
113         onePathForCaps(cs, expectedResult, s, l, hasSpaceBefore);
114     }
115 
testGetCapsMode()116     public void testGetCapsMode() {
117         final int c = TextUtils.CAP_MODE_CHARACTERS;
118         final int w = TextUtils.CAP_MODE_WORDS;
119         final int s = TextUtils.CAP_MODE_SENTENCES;
120         Locale l = Locale.ENGLISH;
121         allPathsForCaps("", c | w | s, l, false);
122         allPathsForCaps("Word", c, l, false);
123         allPathsForCaps("Word.", c, l, false);
124         allPathsForCaps("Word ", c | w, l, false);
125         allPathsForCaps("Word. ", c | w | s, l, false);
126         allPathsForCaps("Word..", c, l, false);
127         allPathsForCaps("Word.. ", c | w | s, l, false);
128         allPathsForCaps("Word... ", c | w | s, l, false);
129         allPathsForCaps("Word ... ", c | w | s, l, false);
130         allPathsForCaps("Word . ", c | w, l, false);
131         allPathsForCaps("In the U.S ", c | w, l, false);
132         allPathsForCaps("In the U.S. ", c | w, l, false);
133         allPathsForCaps("Some stuff (e.g. ", c | w, l, false);
134         allPathsForCaps("In the U.S.. ", c | w | s, l, false);
135         allPathsForCaps("\"Word.\" ", c | w | s, l, false);
136         allPathsForCaps("\"Word\". ", c | w | s, l, false);
137         allPathsForCaps("\"Word\" ", c | w, l, false);
138 
139         // Test for phantom space
140         allPathsForCaps("Word", c | w, l, true);
141         allPathsForCaps("Word.", c | w | s, l, true);
142 
143         // Tests after some whitespace
144         allPathsForCaps("Word\n", c | w | s, l, false);
145         allPathsForCaps("Word\n", c | w | s, l, true);
146         allPathsForCaps("Word\n ", c | w | s, l, true);
147         allPathsForCaps("Word.\n", c | w | s, l, false);
148         allPathsForCaps("Word.\n", c | w | s, l, true);
149         allPathsForCaps("Word.\n ", c | w | s, l, true);
150 
151         l = Locale.FRENCH;
152         allPathsForCaps("\"Word.\" ", c | w, l, false);
153         allPathsForCaps("\"Word\". ", c | w | s, l, false);
154         allPathsForCaps("\"Word\" ", c | w, l, false);
155     }
156 }
157