2010-12-14 06:31:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
2011-06-22 02:53:02 +00:00
|
|
|
package com.android.inputmethod.keyboard.internal;
|
2010-12-14 06:31:47 +00:00
|
|
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.test.AndroidTestCase;
|
|
|
|
|
2011-06-21 14:38:42 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
|
2011-08-31 06:26:32 +00:00
|
|
|
public class MoreKeySpecParserTests extends AndroidTestCase {
|
2010-12-14 06:31:47 +00:00
|
|
|
private Resources mRes;
|
|
|
|
|
2011-06-21 14:38:42 +00:00
|
|
|
private static final int ICON_SETTINGS_KEY = 5;
|
|
|
|
private static final int ICON_UNDEFINED = KeyboardIconsSet.ICON_UNDEFINED;
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
private static final String CODE_SETTINGS = "@integer/key_settings";
|
2011-06-21 14:38:42 +00:00
|
|
|
private static final String ICON_SETTINGS = "@icon/" + ICON_SETTINGS_KEY;
|
2010-12-14 06:31:47 +00:00
|
|
|
private static final String CODE_NON_EXISTING = "@integer/non_existing";
|
2011-06-21 14:38:42 +00:00
|
|
|
private static final String ICON_NON_EXISTING = "@icon/non_existing";
|
2010-12-14 06:31:47 +00:00
|
|
|
|
|
|
|
private int mCodeSettings;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() {
|
|
|
|
Resources res = getContext().getResources();
|
|
|
|
mRes = res;
|
|
|
|
|
|
|
|
final String packageName = res.getResourcePackageName(R.string.english_ime_name);
|
|
|
|
final int codeId = res.getIdentifier(CODE_SETTINGS.substring(1), null, packageName);
|
|
|
|
mCodeSettings = res.getInteger(codeId);
|
|
|
|
}
|
|
|
|
|
2011-08-31 06:26:32 +00:00
|
|
|
private void assertParser(String message, String moreKeySpec, String expectedLabel,
|
2011-06-21 14:38:42 +00:00
|
|
|
String expectedOutputText, int expectedIcon, int expectedCode) {
|
2011-08-31 06:26:32 +00:00
|
|
|
String actualLabel = MoreKeySpecParser.getLabel(moreKeySpec);
|
2010-12-14 06:31:47 +00:00
|
|
|
assertEquals(message + ": label:", expectedLabel, actualLabel);
|
|
|
|
|
2011-08-31 06:26:32 +00:00
|
|
|
String actualOutputText = MoreKeySpecParser.getOutputText(moreKeySpec);
|
2010-12-14 06:31:47 +00:00
|
|
|
assertEquals(message + ": ouptputText:", expectedOutputText, actualOutputText);
|
|
|
|
|
2011-08-31 06:26:32 +00:00
|
|
|
int actualIcon = MoreKeySpecParser.getIconId(moreKeySpec);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertEquals(message + ": icon:", expectedIcon, actualIcon);
|
2010-12-14 06:31:47 +00:00
|
|
|
|
2011-08-31 06:26:32 +00:00
|
|
|
int actualCode = MoreKeySpecParser.getCode(mRes, moreKeySpec);
|
2010-12-20 11:30:26 +00:00
|
|
|
assertEquals(message + ": codes value:", expectedCode, actualCode);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 06:26:32 +00:00
|
|
|
private void assertParserError(String message, String moreKeySpec, String expectedLabel,
|
2011-06-21 14:38:42 +00:00
|
|
|
String expectedOutputText, int expectedIcon, int expectedCode) {
|
2010-12-14 06:31:47 +00:00
|
|
|
try {
|
2011-08-31 06:26:32 +00:00
|
|
|
assertParser(message, moreKeySpec, expectedLabel, expectedOutputText, expectedIcon,
|
2010-12-20 11:30:26 +00:00
|
|
|
expectedCode);
|
2010-12-14 06:31:47 +00:00
|
|
|
fail(message);
|
2012-01-20 06:47:22 +00:00
|
|
|
} catch (Exception pcpe) {
|
2010-12-14 06:31:47 +00:00
|
|
|
// success.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSingleLetter() {
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single letter", "a",
|
|
|
|
"a", null, ICON_UNDEFINED, 'a');
|
|
|
|
assertParser("Single escaped bar", "\\|",
|
|
|
|
"|", null, ICON_UNDEFINED, '|');
|
|
|
|
assertParser("Single escaped escape", "\\\\",
|
|
|
|
"\\", null, ICON_UNDEFINED, '\\');
|
|
|
|
assertParser("Single comma", ",",
|
|
|
|
",", null, ICON_UNDEFINED, ',');
|
|
|
|
assertParser("Single escaped comma", "\\,",
|
|
|
|
",", null, ICON_UNDEFINED, ',');
|
|
|
|
assertParser("Single escaped letter", "\\a",
|
|
|
|
"a", null, ICON_UNDEFINED, 'a');
|
|
|
|
assertParser("Single at", "@",
|
|
|
|
"@", null, ICON_UNDEFINED, '@');
|
|
|
|
assertParser("Single escaped at", "\\@",
|
|
|
|
"@", null, ICON_UNDEFINED, '@');
|
|
|
|
assertParser("Single letter with outputText", "a|abc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a", "abc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single letter with escaped outputText", "a|a\\|c",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a", "a|c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single letter with comma outputText", "a|a,b",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a", "a,b", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single letter with escaped comma outputText", "a|a\\,b",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a", "a,b", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single letter with outputText starts with at", "a|@bc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a", "@bc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single letter with outputText contains at", "a|a@c",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a", "a@c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single letter with escaped at outputText", "a|\\@bc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a", "@bc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single escaped escape with outputText", "\\\\|\\\\",
|
2012-01-19 03:42:15 +00:00
|
|
|
"\\", "\\", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single escaped bar with outputText", "\\||\\|",
|
2012-01-19 03:42:15 +00:00
|
|
|
"|", "|", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Single letter with code", "a|" + CODE_SETTINGS,
|
|
|
|
"a", null, ICON_UNDEFINED, mCodeSettings);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testLabel() {
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Simple label", "abc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "abc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped bar", "a\\|c",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a|c", "a|c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped escape", "a\\\\c",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a\\c", "a\\c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with comma", "a,c",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a,c", "a,c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped comma", "a\\,c",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a,c", "a,c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label starts with at", "@bc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"@bc", "@bc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label contains at", "a@c",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a@c", "a@c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped at", "\\@bc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"@bc", "@bc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped letter", "\\abc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "abc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with outputText", "abc|def",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "def", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with comma and outputText", "a,c|def",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a,c", "def", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Escaped comma label with outputText", "a\\,c|def",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a,c", "def", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Escaped label with outputText", "a\\|c|def",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a|c", "def", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped bar outputText", "abc|d\\|f",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "d|f", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Escaped escape label with outputText", "a\\\\|def",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a\\", "def", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label starts with at and outputText", "@bc|def",
|
2012-01-19 03:42:15 +00:00
|
|
|
"@bc", "def", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label contains at label and outputText", "a@c|def",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a@c", "def", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Escaped at label with outputText", "\\@bc|def",
|
2012-01-19 03:42:15 +00:00
|
|
|
"@bc", "def", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with comma outputText", "abc|a,b",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "a,b", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped comma outputText", "abc|a\\,b",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "a,b", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with outputText starts with at", "abc|@bc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "@bc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with outputText contains at", "abc|a@c",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "a@c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped at outputText", "abc|\\@bc",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "@bc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with escaped bar outputText", "abc|d\\|f",
|
2012-01-19 03:42:15 +00:00
|
|
|
"abc", "d|f", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Escaped bar label with escaped bar outputText", "a\\|c|d\\|f",
|
2012-01-19 03:42:15 +00:00
|
|
|
"a|c", "d|f", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label with code", "abc|" + CODE_SETTINGS,
|
|
|
|
"abc", null, ICON_UNDEFINED, mCodeSettings);
|
|
|
|
assertParser("Escaped label with code", "a\\|c|" + CODE_SETTINGS,
|
|
|
|
"a|c", null, ICON_UNDEFINED, mCodeSettings);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testIconAndCode() {
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Icon with outputText", ICON_SETTINGS + "|abc",
|
2012-01-19 03:42:15 +00:00
|
|
|
null, "abc", ICON_SETTINGS_KEY, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Icon with outputText starts with at", ICON_SETTINGS + "|@bc",
|
2012-01-19 03:42:15 +00:00
|
|
|
null, "@bc", ICON_SETTINGS_KEY, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Icon with outputText contains at", ICON_SETTINGS + "|a@c",
|
2012-01-19 03:42:15 +00:00
|
|
|
null, "a@c", ICON_SETTINGS_KEY, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Icon with escaped at outputText", ICON_SETTINGS + "|\\@bc",
|
2012-01-19 03:42:15 +00:00
|
|
|
null, "@bc", ICON_SETTINGS_KEY, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Label starts with at and code", "@bc|" + CODE_SETTINGS,
|
|
|
|
"@bc", null, ICON_UNDEFINED, mCodeSettings);
|
|
|
|
assertParser("Label contains at and code", "a@c|" + CODE_SETTINGS,
|
|
|
|
"a@c", null, ICON_UNDEFINED, mCodeSettings);
|
|
|
|
assertParser("Escaped at label with code", "\\@bc|" + CODE_SETTINGS,
|
|
|
|
"@bc", null, ICON_UNDEFINED, mCodeSettings);
|
|
|
|
assertParser("Icon with code", ICON_SETTINGS + "|" + CODE_SETTINGS,
|
|
|
|
null, null, ICON_SETTINGS_KEY, mCodeSettings);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testFormatError() {
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParserError("Empty spec", "", null,
|
|
|
|
null, ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);
|
|
|
|
assertParserError("Empty label with outputText", "|a",
|
2012-01-10 08:24:00 +00:00
|
|
|
null, "a", ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParserError("Empty label with code", "|" + CODE_SETTINGS,
|
|
|
|
null, null, ICON_UNDEFINED, mCodeSettings);
|
|
|
|
assertParserError("Empty outputText with label", "a|",
|
|
|
|
"a", null, ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);
|
|
|
|
assertParserError("Empty outputText with icon", ICON_SETTINGS + "|",
|
|
|
|
null, null, ICON_SETTINGS_KEY, Keyboard.CODE_UNSPECIFIED);
|
|
|
|
assertParserError("Empty icon and code", "|",
|
|
|
|
null, null, ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);
|
|
|
|
assertParserError("Icon without code", ICON_SETTINGS,
|
2012-01-10 08:24:00 +00:00
|
|
|
null, null, ICON_SETTINGS_KEY, Keyboard.CODE_UNSPECIFIED);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParser("Non existing icon", ICON_NON_EXISTING + "|abc",
|
2012-01-19 03:42:15 +00:00
|
|
|
null, "abc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
|
2011-06-21 14:38:42 +00:00
|
|
|
assertParserError("Non existing code", "abc|" + CODE_NON_EXISTING,
|
|
|
|
"abc", null, ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);
|
|
|
|
assertParserError("Third bar at end", "a|b|",
|
|
|
|
"a", null, ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);
|
|
|
|
assertParserError("Multiple bar", "a|b|c",
|
|
|
|
"a", null, ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);
|
|
|
|
assertParserError("Multiple bar with label and code", "a|" + CODE_SETTINGS + "|c",
|
|
|
|
"a", null, ICON_UNDEFINED, mCodeSettings);
|
|
|
|
assertParserError("Multiple bar with icon and outputText", ICON_SETTINGS + "|b|c",
|
|
|
|
null, null, ICON_SETTINGS_KEY, Keyboard.CODE_UNSPECIFIED);
|
2010-12-14 06:31:47 +00:00
|
|
|
assertParserError("Multiple bar with icon and code",
|
2011-06-21 14:38:42 +00:00
|
|
|
ICON_SETTINGS + "|" + CODE_SETTINGS + "|c",
|
|
|
|
null, null, ICON_SETTINGS_KEY, mCodeSettings);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
}
|