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 17 package com.android.inputmethod.keyboard.internal; 18 19 import com.android.inputmethod.keyboard.internal.KeyStyles.EmptyKeyStyle; 20 21 import android.test.AndroidTestCase; 22 import android.text.TextUtils; 23 24 public class KeyStylesTests extends AndroidTestCase { 25 private static String format(String message, Object expected, Object actual) { 26 return message + " expected:<" + expected + "> but was:<" + actual + ">"; 27 } 28 29 private static void assertTextArray(String message, CharSequence value, 30 CharSequence ... expected) { 31 final CharSequence actual[] = EmptyKeyStyle.parseCsvText(value); 32 if (expected.length == 0) { 33 assertNull(message, actual); 34 return; 35 } 36 assertSame(message + ": result length", expected.length, actual.length); 37 for (int i = 0; i < actual.length; i++) { 38 final boolean equals = TextUtils.equals(expected[i], actual[i]); 39 assertTrue(format(message + ": result at " + i + ":", expected[i], actual[i]), equals); 40 } 41 } 42 43 public void testParseCsvTextZero() { 44 assertTextArray("Empty string", ""); 45 } 46 47 public void testParseCsvTextSingle() { 48 assertTextArray("Single char", "a", "a"); 49 assertTextArray("Space", " ", " "); 50 assertTextArray("Single label", "abc", "abc"); 51 assertTextArray("Spaces", " ", " "); 52 assertTextArray("Spaces in label", "a b c", "a b c"); 53 assertTextArray("Spaces at beginning of label", " abc", " abc"); 54 assertTextArray("Spaces at end of label", "abc ", "abc "); 55 assertTextArray("label surrounded by spaces", " abc ", " abc "); 56 } 57 58 public void testParseCsvTextSingleEscaped() { 59 assertTextArray("Escaped char", "\\a", "a"); 60 assertTextArray("Escaped comma", "\\,", ","); 61 assertTextArray("Escaped escape", "\\\\", "\\"); 62 assertTextArray("Escaped label", "a\\bc", "abc"); 63 assertTextArray("Escaped label at begininng", "\\abc", "abc"); 64 assertTextArray("Escaped label with comma", "a\\,c", "a,c"); 65 assertTextArray("Escaped label with comma at beginning", "\\,bc", ",bc"); 66 assertTextArray("Escaped label with successive", "\\,\\\\bc", ",\\bc"); 67 assertTextArray("Escaped label with escape", "a\\\\c", "a\\c"); 68 } 69 70 public void testParseCsvTextMulti() { 71 assertTextArray("Multiple chars", "a,b,c", "a", "b", "c"); 72 assertTextArray("Multiple chars surrounded by spaces", " a , b , c ", " a ", " b ", " c "); 73 assertTextArray("Multiple labels", "abc,def,ghi", "abc", "def", "ghi"); 74 assertTextArray("Multiple labels surrounded by spaces", " abc , def , ghi ", 75 " abc ", " def ", " ghi "); 76 } 77 78 public void testParseCsvTextMultiEscaped() { 79 assertTextArray("Multiple chars with comma", "a,\\,,c", "a", ",", "c"); 80 assertTextArray("Multiple chars with comma surrounded by spaces", " a , \\, , c ", 81 " a ", " , ", " c "); 82 assertTextArray("Multiple labels with escape", "\\abc,d\\ef,gh\\i", "abc", "def", "ghi"); 83 assertTextArray("Multiple labels with escape surrounded by spaces", 84 " \\abc , d\\ef , gh\\i ", " abc ", " def ", " ghi "); 85 assertTextArray("Multiple labels with comma and escape", 86 "ab\\\\,d\\\\\\,,g\\,i", "ab\\", "d\\,", "g,i"); 87 assertTextArray("Multiple labels with comma and escape surrounded by spaces", 88 " ab\\\\ , d\\\\\\, , g\\,i ", " ab\\ ", " d\\, ", " g,i "); 89 } 90 } 91