1/* 2 * Copyright (C) 2010 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 17package com.android.inputmethod.keyboard.internal; 18 19import android.app.Instrumentation; 20import android.test.InstrumentationTestCase; 21 22import com.android.inputmethod.latin.CollectionUtils; 23 24import java.lang.reflect.Field; 25import java.util.ArrayList; 26import java.util.Arrays; 27import java.util.Locale; 28 29public class KeySpecParserCsvTests extends InstrumentationTestCase { 30 private final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); 31 32 @Override 33 protected void setUp() throws Exception { 34 super.setUp(); 35 36 final Instrumentation instrumentation = getInstrumentation(); 37 mTextsSet.setLanguage(Locale.ENGLISH.getLanguage()); 38 mTextsSet.loadStringResources(instrumentation.getTargetContext()); 39 final String[] testResourceNames = getAllResourceIdNames( 40 com.android.inputmethod.latin.tests.R.string.class); 41 mTextsSet.loadStringResourcesInternal(instrumentation.getContext(), 42 testResourceNames, 43 com.android.inputmethod.latin.tests.R.string.empty_string); 44 } 45 46 private static String[] getAllResourceIdNames(final Class<?> resourceIdClass) { 47 final ArrayList<String> names = CollectionUtils.newArrayList(); 48 for (final Field field : resourceIdClass.getFields()) { 49 if (field.getType() == Integer.TYPE) { 50 names.add(field.getName()); 51 } 52 } 53 return names.toArray(new String[names.size()]); 54 } 55 56 private static void assertArrayEquals(String message, Object[] expected, Object[] actual) { 57 if (expected == actual) { 58 return; 59 } 60 if (expected == null || actual == null) { 61 assertEquals(message, Arrays.toString(expected), Arrays.toString(actual)); 62 return; 63 } 64 if (expected.length != actual.length) { 65 assertEquals(message + " [length]", Arrays.toString(expected), Arrays.toString(actual)); 66 return; 67 } 68 for (int i = 0; i < expected.length; i++) { 69 assertEquals(message + " [" + i + "]", 70 Arrays.toString(expected), Arrays.toString(actual)); 71 } 72 } 73 74 private void assertTextArray(String message, String value, String ... expectedArray) { 75 final String[] actual = KeySpecParser.parseCsvString(value, mTextsSet); 76 final String[] expected = (expectedArray.length == 0) ? null : expectedArray; 77 assertArrayEquals(message, expected, actual); 78 } 79 80 private void assertError(String message, String value, String ... expected) { 81 try { 82 assertTextArray(message, value, expected); 83 fail(message); 84 } catch (Exception pcpe) { 85 // success. 86 } 87 } 88 89 // \U001d11e: MUSICAL SYMBOL G CLEF 90 private static final String PAIR1 = "\ud834\udd1e"; 91 // \U001d122: MUSICAL SYMBOL F CLEF 92 private static final String PAIR2 = "\ud834\udd22"; 93 // \U002f8a6: CJK COMPATIBILITY IDEOGRAPH-2F8A6; variant character of \u6148. 94 private static final String PAIR3 = "\ud87e\udca6"; 95 private static final String SURROGATE1 = PAIR1 + PAIR2; 96 private static final String SURROGATE2 = PAIR1 + PAIR2 + PAIR3; 97 98 public void testParseCsvTextZero() { 99 assertTextArray("Empty string", ""); 100 assertTextArray("Empty entry", ","); 101 assertTextArray("Empty entry at beginning", ",a", "a"); 102 assertTextArray("Empty entry at end", "a,", "a"); 103 assertTextArray("Empty entry at middle", "a,,b", "a", "b"); 104 assertTextArray("Empty entries with escape", ",a,b\\,c,,d,", "a", "b\\,c", "d"); 105 } 106 107 public void testParseCsvTextSingle() { 108 assertTextArray("Single char", "a", "a"); 109 assertTextArray("Surrogate pair", PAIR1, PAIR1); 110 assertTextArray("Single escape", "\\", "\\"); 111 assertTextArray("Space", " ", " "); 112 assertTextArray("Single label", "abc", "abc"); 113 assertTextArray("Single surrogate pairs label", SURROGATE2, SURROGATE2); 114 assertTextArray("Spaces", " ", " "); 115 assertTextArray("Spaces in label", "a b c", "a b c"); 116 assertTextArray("Spaces at beginning of label", " abc", " abc"); 117 assertTextArray("Spaces at end of label", "abc ", "abc "); 118 assertTextArray("Label surrounded by spaces", " abc ", " abc "); 119 assertTextArray("Surrogate pair surrounded by space", 120 " " + PAIR1 + " ", 121 " " + PAIR1 + " "); 122 assertTextArray("Surrogate pair within characters", 123 "ab" + PAIR2 + "cd", 124 "ab" + PAIR2 + "cd"); 125 assertTextArray("Surrogate pairs within characters", 126 "ab" + SURROGATE1 + "cd", 127 "ab" + SURROGATE1 + "cd"); 128 129 assertTextArray("Incomplete resource reference 1", "text", "text"); 130 assertTextArray("Incomplete resource reference 2", "!text", "!text"); 131 assertTextArray("Incomplete RESOURCE REFERENCE 2", "!TEXT", "!TEXT"); 132 assertTextArray("Incomplete resource reference 3", "text/", "text/"); 133 assertTextArray("Incomplete resource reference 4", "!" + SURROGATE2, "!" + SURROGATE2); 134 } 135 136 public void testParseCsvTextSingleEscaped() { 137 assertTextArray("Escaped char", "\\a", "\\a"); 138 assertTextArray("Escaped surrogate pair", "\\" + PAIR1, "\\" + PAIR1); 139 assertTextArray("Escaped comma", "\\,", "\\,"); 140 assertTextArray("Escaped comma escape", "a\\,\\", "a\\,\\"); 141 assertTextArray("Escaped escape", "\\\\", "\\\\"); 142 assertTextArray("Escaped label", "a\\bc", "a\\bc"); 143 assertTextArray("Escaped surrogate", "a\\" + PAIR1 + "c", "a\\" + PAIR1 + "c"); 144 assertTextArray("Escaped label at beginning", "\\abc", "\\abc"); 145 assertTextArray("Escaped surrogate at beginning", "\\" + SURROGATE2, "\\" + SURROGATE2); 146 assertTextArray("Escaped label at end", "abc\\", "abc\\"); 147 assertTextArray("Escaped surrogate at end", SURROGATE2 + "\\", SURROGATE2 + "\\"); 148 assertTextArray("Escaped label with comma", "a\\,c", "a\\,c"); 149 assertTextArray("Escaped surrogate with comma", 150 PAIR1 + "\\," + PAIR2, PAIR1 + "\\," + PAIR2); 151 assertTextArray("Escaped label with comma at beginning", "\\,bc", "\\,bc"); 152 assertTextArray("Escaped surrogate with comma at beginning", 153 "\\," + SURROGATE1, "\\," + SURROGATE1); 154 assertTextArray("Escaped label with comma at end", "ab\\,", "ab\\,"); 155 assertTextArray("Escaped surrogate with comma at end", 156 SURROGATE2 + "\\,", SURROGATE2 + "\\,"); 157 assertTextArray("Escaped label with successive", "\\,\\\\bc", "\\,\\\\bc"); 158 assertTextArray("Escaped surrogate with successive", 159 "\\,\\\\" + SURROGATE1, "\\,\\\\" + SURROGATE1); 160 assertTextArray("Escaped label with escape", "a\\\\c", "a\\\\c"); 161 assertTextArray("Escaped surrogate with escape", 162 PAIR1 + "\\\\" + PAIR2, PAIR1 + "\\\\" + PAIR2); 163 164 assertTextArray("Escaped !text", "\\!text", "\\!text"); 165 assertTextArray("Escaped !text/", "\\!text/", "\\!text/"); 166 assertTextArray("Escaped !TEXT/", "\\!TEXT/", "\\!TEXT/"); 167 assertTextArray("Escaped !text/name", "\\!text/empty_string", "\\!text/empty_string"); 168 assertTextArray("Escaped !TEXT/NAME", "\\!TEXT/EMPTY_STRING", "\\!TEXT/EMPTY_STRING"); 169 } 170 171 public void testParseCsvTextMulti() { 172 assertTextArray("Multiple chars", "a,b,c", "a", "b", "c"); 173 assertTextArray("Multiple chars", "a,b,\\c", "a", "b", "\\c"); 174 assertTextArray("Multiple chars and escape at beginning and end", 175 "\\a,b,\\c\\", "\\a", "b", "\\c\\"); 176 assertTextArray("Multiple surrogates", PAIR1 + "," + PAIR2 + "," + PAIR3, 177 PAIR1, PAIR2, PAIR3); 178 assertTextArray("Multiple chars surrounded by spaces", " a , b , c ", " a ", " b ", " c "); 179 assertTextArray("Multiple labels", "abc,def,ghi", "abc", "def", "ghi"); 180 assertTextArray("Multiple surrogated", SURROGATE1 + "," + SURROGATE2, 181 SURROGATE1, SURROGATE2); 182 assertTextArray("Multiple labels surrounded by spaces", " abc , def , ghi ", 183 " abc ", " def ", " ghi "); 184 } 185 186 public void testParseCsvTextMultiEscaped() { 187 assertTextArray("Multiple chars with comma", "a,\\,,c", "a", "\\,", "c"); 188 assertTextArray("Multiple chars with comma surrounded by spaces", " a , \\, , c ", 189 " a ", " \\, ", " c "); 190 assertTextArray("Multiple labels with escape", 191 "\\abc,d\\ef,gh\\i", "\\abc", "d\\ef", "gh\\i"); 192 assertTextArray("Multiple labels with escape surrounded by spaces", 193 " \\abc , d\\ef , gh\\i ", " \\abc ", " d\\ef ", " gh\\i "); 194 assertTextArray("Multiple labels with comma and escape", 195 "ab\\\\,d\\\\\\,,g\\,i", "ab\\\\", "d\\\\\\,", "g\\,i"); 196 assertTextArray("Multiple labels with comma and escape surrounded by spaces", 197 " ab\\\\ , d\\\\\\, , g\\,i ", " ab\\\\ ", " d\\\\\\, ", " g\\,i "); 198 199 assertTextArray("Multiple escaped !text", "\\!,\\!text/empty_string", 200 "\\!", "\\!text/empty_string"); 201 assertTextArray("Multiple escaped !TEXT", "\\!,\\!TEXT/EMPTY_STRING", 202 "\\!", "\\!TEXT/EMPTY_STRING"); 203 } 204 205 public void testParseCsvResourceError() { 206 assertError("Incomplete resource name", "!text/", "!text/"); 207 assertError("Non existing resource", "!text/non_existing"); 208 } 209 210 public void testParseCsvResourceZero() { 211 assertTextArray("Empty string", 212 "!text/empty_string"); 213 } 214 215 public void testParseCsvResourceSingle() { 216 assertTextArray("Single char", 217 "!text/single_char", "a"); 218 assertTextArray("Space", 219 "!text/space", " "); 220 assertTextArray("Single label", 221 "!text/single_label", "abc"); 222 assertTextArray("Spaces", 223 "!text/spaces", " "); 224 assertTextArray("Spaces in label", 225 "!text/spaces_in_label", "a b c"); 226 assertTextArray("Spaces at beginning of label", 227 "!text/spaces_at_beginning_of_label", " abc"); 228 assertTextArray("Spaces at end of label", 229 "!text/spaces_at_end_of_label", "abc "); 230 assertTextArray("label surrounded by spaces", 231 "!text/label_surrounded_by_spaces", " abc "); 232 233 assertTextArray("Escape and single char", 234 "\\\\!text/single_char", "\\\\a"); 235 } 236 237 public void testParseCsvResourceSingleEscaped() { 238 assertTextArray("Escaped char", 239 "!text/escaped_char", "\\a"); 240 assertTextArray("Escaped comma", 241 "!text/escaped_comma", "\\,"); 242 assertTextArray("Escaped comma escape", 243 "!text/escaped_comma_escape", "a\\,\\"); 244 assertTextArray("Escaped escape", 245 "!text/escaped_escape", "\\\\"); 246 assertTextArray("Escaped label", 247 "!text/escaped_label", "a\\bc"); 248 assertTextArray("Escaped label at beginning", 249 "!text/escaped_label_at_beginning", "\\abc"); 250 assertTextArray("Escaped label at end", 251 "!text/escaped_label_at_end", "abc\\"); 252 assertTextArray("Escaped label with comma", 253 "!text/escaped_label_with_comma", "a\\,c"); 254 assertTextArray("Escaped label with comma at beginning", 255 "!text/escaped_label_with_comma_at_beginning", "\\,bc"); 256 assertTextArray("Escaped label with comma at end", 257 "!text/escaped_label_with_comma_at_end", "ab\\,"); 258 assertTextArray("Escaped label with successive", 259 "!text/escaped_label_with_successive", "\\,\\\\bc"); 260 assertTextArray("Escaped label with escape", 261 "!text/escaped_label_with_escape", "a\\\\c"); 262 } 263 264 public void testParseCsvResourceMulti() { 265 assertTextArray("Multiple chars", 266 "!text/multiple_chars", "a", "b", "c"); 267 assertTextArray("Multiple chars surrounded by spaces", 268 "!text/multiple_chars_surrounded_by_spaces", 269 " a ", " b ", " c "); 270 assertTextArray("Multiple labels", 271 "!text/multiple_labels", "abc", "def", "ghi"); 272 assertTextArray("Multiple labels surrounded by spaces", 273 "!text/multiple_labels_surrounded_by_spaces", " abc ", " def ", " ghi "); 274 } 275 276 public void testParseCsvResourcetMultiEscaped() { 277 assertTextArray("Multiple chars with comma", 278 "!text/multiple_chars_with_comma", 279 "a", "\\,", "c"); 280 assertTextArray("Multiple chars with comma surrounded by spaces", 281 "!text/multiple_chars_with_comma_surrounded_by_spaces", 282 " a ", " \\, ", " c "); 283 assertTextArray("Multiple labels with escape", 284 "!text/multiple_labels_with_escape", 285 "\\abc", "d\\ef", "gh\\i"); 286 assertTextArray("Multiple labels with escape surrounded by spaces", 287 "!text/multiple_labels_with_escape_surrounded_by_spaces", 288 " \\abc ", " d\\ef ", " gh\\i "); 289 assertTextArray("Multiple labels with comma and escape", 290 "!text/multiple_labels_with_comma_and_escape", 291 "ab\\\\", "d\\\\\\,", "g\\,i"); 292 assertTextArray("Multiple labels with comma and escape surrounded by spaces", 293 "!text/multiple_labels_with_comma_and_escape_surrounded_by_spaces", 294 " ab\\\\ ", " d\\\\\\, ", " g\\,i "); 295 } 296 297 public void testParseMultipleResources() { 298 assertTextArray("Literals and resources", 299 "1,!text/multiple_chars,z", "1", "a", "b", "c", "z"); 300 assertTextArray("Literals and resources and escape at end", 301 "\\1,!text/multiple_chars,z\\", "\\1", "a", "b", "c", "z\\"); 302 assertTextArray("Multiple single resource chars and labels", 303 "!text/single_char,!text/single_label,!text/escaped_comma", 304 "a", "abc", "\\,"); 305 assertTextArray("Multiple single resource chars and labels 2", 306 "!text/single_char,!text/single_label,!text/escaped_comma_escape", 307 "a", "abc", "a\\,\\"); 308 assertTextArray("Multiple multiple resource chars and labels", 309 "!text/multiple_chars,!text/multiple_labels,!text/multiple_chars_with_comma", 310 "a", "b", "c", "abc", "def", "ghi", "a", "\\,", "c"); 311 assertTextArray("Concatenated resources", 312 "!text/multiple_chars!text/multiple_labels!text/multiple_chars_with_comma", 313 "a", "b", "cabc", "def", "ghia", "\\,", "c"); 314 assertTextArray("Concatenated resource and literal", 315 "abc!text/multiple_labels", 316 "abcabc", "def", "ghi"); 317 } 318 319 public void testParseIndirectReference() { 320 assertTextArray("Indirect", 321 "!text/indirect_string", "a", "b", "c"); 322 assertTextArray("Indirect with literal", 323 "1,!text/indirect_string_with_literal,2", "1", "x", "a", "b", "c", "y", "2"); 324 assertTextArray("Indirect2", 325 "!text/indirect2_string", "a", "b", "c"); 326 } 327 328 public void testParseInfiniteIndirectReference() { 329 assertError("Infinite indirection", 330 "1,!text/infinite_indirection,2", "1", "infinite", "<infinite>", "loop", "2"); 331 } 332 333 public void testLabelReferece() { 334 assertTextArray("Label time am", "!text/label_time_am", "AM"); 335 336 assertTextArray("More keys for am pm", "!text/more_keys_for_am_pm", 337 "!fixedColumnOrder!2", "!hasLabels!", "AM", "PM"); 338 339 assertTextArray("Settings as more key", "!text/settings_as_more_key", 340 "!icon/settings_key|!code/key_settings"); 341 342 assertTextArray("Indirect naviagte actions as more key", 343 "!text/indirect_navigate_actions_as_more_key", 344 "!fixedColumnOrder!2", 345 "!hasLabels!", "Prev|!code/key_action_previous", 346 "!hasLabels!", "Next|!code/key_action_next"); 347 } 348 349 public void testUselessUpperCaseSpecifier() { 350 assertTextArray("EMPTY STRING", 351 "!TEXT/EMPTY_STRING", "!TEXT/EMPTY_STRING"); 352 353 assertTextArray("SINGLE CHAR", 354 "!TEXT/SINGLE_CHAR", "!TEXT/SINGLE_CHAR"); 355 assertTextArray("Escape and SINGLE CHAR", 356 "\\\\!TEXT/SINGLE_CHAR", "\\\\!TEXT/SINGLE_CHAR"); 357 358 assertTextArray("MULTIPLE CHARS", 359 "!TEXT/MULTIPLE_CHARS", "!TEXT/MULTIPLE_CHARS"); 360 361 assertTextArray("Literals and RESOURCES", 362 "1,!TEXT/MULTIPLE_CHARS,z", "1", "!TEXT/MULTIPLE_CHARS", "z"); 363 assertTextArray("Multiple single RESOURCE chars and LABELS 2", 364 "!TEXT/SINGLE_CHAR,!TEXT/SINGLE_LABEL,!TEXT/ESCAPED_COMMA_ESCAPE", 365 "!TEXT/SINGLE_CHAR", "!TEXT/SINGLE_LABEL", "!TEXT/ESCAPED_COMMA_ESCAPE"); 366 367 assertTextArray("INDIRECT", 368 "!TEXT/INDIRECT_STRING", "!TEXT/INDIRECT_STRING"); 369 assertTextArray("INDIRECT with literal", 370 "1,!TEXT/INDIRECT_STRING_WITH_LITERAL,2", 371 "1", "!TEXT/INDIRECT_STRING_WITH_LITERAL", "2"); 372 assertTextArray("INDIRECT2", 373 "!TEXT/INDIRECT2_STRING", "!TEXT/INDIRECT2_STRING"); 374 375 assertTextArray("Upper indirect", 376 "!text/upper_indirect_string", "!TEXT/MULTIPLE_CHARS"); 377 assertTextArray("Upper indirect with literal", 378 "1,!text/upper_indirect_string_with_literal,2", 379 "1", "x", "!TEXT/MULTIPLE_CHARS", "y", "2"); 380 assertTextArray("Upper indirect2", 381 "!text/upper_indirect2_string", "!TEXT/UPPER_INDIRECT_STRING"); 382 383 assertTextArray("UPPER INDIRECT", 384 "!TEXT/upper_INDIRECT_STRING", "!TEXT/upper_INDIRECT_STRING"); 385 assertTextArray("Upper INDIRECT with literal", 386 "1,!TEXT/upper_INDIRECT_STRING_WITH_LITERAL,2", 387 "1", "!TEXT/upper_INDIRECT_STRING_WITH_LITERAL", "2"); 388 assertTextArray("Upper INDIRECT2", 389 "!TEXT/upper_INDIRECT2_STRING", "!TEXT/upper_INDIRECT2_STRING"); 390 391 assertTextArray("INFINITE INDIRECTION", 392 "1,!TEXT/INFINITE_INDIRECTION,2", "1", "!TEXT/INFINITE_INDIRECTION", "2"); 393 394 assertTextArray("Upper infinite indirection", 395 "1,!text/upper_infinite_indirection,2", 396 "1", "infinite", "!TEXT/INFINITE_INDIRECTION", "loop", "2"); 397 assertTextArray("Upper INFINITE INDIRECTION", 398 "1,!TEXT/UPPER_INFINITE_INDIRECTION,2", 399 "1", "!TEXT/UPPER_INFINITE_INDIRECTION", "2"); 400 401 assertTextArray("LABEL TIME AM", "!TEXT/LABEL_TIME_AM", "!TEXT/LABEL_TIME_AM"); 402 assertTextArray("MORE KEYS FOR AM OM", "!TEXT/MORE_KEYS_FOR_AM_PM", 403 "!TEXT/MORE_KEYS_FOR_AM_PM"); 404 assertTextArray("SETTINGS AS MORE KEY", "!TEXT/SETTINGS_AS_MORE_KEY", 405 "!TEXT/SETTINGS_AS_MORE_KEY"); 406 assertTextArray("INDIRECT NAVIGATE ACTIONS AS MORE KEY", 407 "!TEXT/INDIRECT_NAVIGATE_ACTIONS_AS_MORE_KEY", 408 "!TEXT/INDIRECT_NAVIGATE_ACTIONS_AS_MORE_KEY"); 409 } 410} 411