diff options
Diffstat (limited to 'tests/src')
11 files changed, 545 insertions, 105 deletions
diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java index 1ab577557..5bba961bd 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java @@ -16,13 +16,13 @@ package com.android.inputmethod.keyboard.internal; -import static com.android.inputmethod.keyboard.Keyboard.CODE_OUTPUT_TEXT; -import static com.android.inputmethod.keyboard.Keyboard.CODE_UNSPECIFIED; import static com.android.inputmethod.keyboard.internal.KeyboardIconsSet.ICON_UNDEFINED; +import static com.android.inputmethod.latin.Constants.CODE_OUTPUT_TEXT; +import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED; import android.test.AndroidTestCase; -import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.Constants; import java.util.Arrays; import java.util.Locale; @@ -69,8 +69,8 @@ public class KeySpecParserTests extends AndroidTestCase { KeyboardIconsSet.getIconName(expectedIcon), KeyboardIconsSet.getIconName(spec.mIconId)); assertEquals(message + " [code]", - Keyboard.printableCode(expectedCode), - Keyboard.printableCode(spec.mCode)); + Constants.printableCode(expectedCode), + Constants.printableCode(spec.mCode)); } private void assertParserError(String message, String moreKeySpec, String expectedLabel, diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateTestsBase.java b/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateTestsBase.java index 2770ed54a..c75f8269a 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateTestsBase.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateTestsBase.java @@ -19,7 +19,7 @@ package com.android.inputmethod.keyboard.internal; import android.test.AndroidTestCase; public class KeyboardStateTestsBase extends AndroidTestCase - implements MockKeyboardSwitcher.Constants { + implements MockKeyboardSwitcher.MockConstants { protected MockKeyboardSwitcher mSwitcher; private String mLayoutSwitchBackSymbols = ""; diff --git a/tests/src/com/android/inputmethod/keyboard/internal/MockKeyboardSwitcher.java b/tests/src/com/android/inputmethod/keyboard/internal/MockKeyboardSwitcher.java index dd4caadf1..0213744fb 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/MockKeyboardSwitcher.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/MockKeyboardSwitcher.java @@ -18,25 +18,24 @@ package com.android.inputmethod.keyboard.internal; import android.text.TextUtils; -import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.Constants; public class MockKeyboardSwitcher implements KeyboardState.SwitchActions { - public interface Constants { + public interface MockConstants { // Argument for {@link KeyboardState#onPressKey} and {@link KeyboardState#onReleaseKey}. public static final boolean NOT_SLIDING = false; public static final boolean SLIDING = true; // Argument for {@link KeyboardState#onCodeInput}. public static final boolean SINGLE = true; public static final boolean MULTI = false; - public static final int CAP_MODE_OFF = - com.android.inputmethod.latin.Constants.TextUtils.CAP_MODE_OFF; + public static final int CAP_MODE_OFF = Constants.TextUtils.CAP_MODE_OFF; public static final int CAP_MODE_WORDS = TextUtils.CAP_MODE_WORDS; public static final int CAP_MODE_CHARACTERS = TextUtils.CAP_MODE_CHARACTERS; - public static final int CODE_SHIFT = Keyboard.CODE_SHIFT; - public static final int CODE_SYMBOL = Keyboard.CODE_SWITCH_ALPHA_SYMBOL; - public static final int CODE_SPACE = Keyboard.CODE_SPACE; - public static final int CODE_AUTO_CAPS_TRIGGER = Keyboard.CODE_SPACE; + public static final int CODE_SHIFT = Constants.CODE_SHIFT; + public static final int CODE_SYMBOL = Constants.CODE_SWITCH_ALPHA_SYMBOL; + public static final int CODE_SPACE = Constants.CODE_SPACE; + public static final int CODE_AUTO_CAPS_TRIGGER = Constants.CODE_SPACE; public static final int ALPHABET_UNSHIFTED = 0; public static final int ALPHABET_MANUAL_SHIFTED = 1; @@ -47,11 +46,11 @@ public class MockKeyboardSwitcher implements KeyboardState.SwitchActions { public static final int SYMBOLS_SHIFTED = 6; } - private int mLayout = Constants.ALPHABET_UNSHIFTED; + private int mLayout = MockConstants.ALPHABET_UNSHIFTED; - private int mAutoCapsMode = Constants.CAP_MODE_OFF; + private int mAutoCapsMode = MockConstants.CAP_MODE_OFF; // Following InputConnection's behavior. Simulating InputType.TYPE_TEXT_FLAG_CAP_WORDS. - private int mAutoCapsState = Constants.CAP_MODE_OFF; + private int mAutoCapsState = MockConstants.CAP_MODE_OFF; private boolean mIsInDoubleTapTimeout; private int mLongPressTimeoutCode; @@ -64,13 +63,13 @@ public class MockKeyboardSwitcher implements KeyboardState.SwitchActions { public static String getLayoutName(int layoutId) { switch (layoutId) { - case Constants.ALPHABET_UNSHIFTED: return "ALPHABET_UNSHIFTED"; - case Constants.ALPHABET_MANUAL_SHIFTED: return "ALPHABET_MANUAL_SHIFTED"; - case Constants.ALPHABET_AUTOMATIC_SHIFTED: return "ALPHABET_AUTOMATIC_SHIFTED"; - case Constants.ALPHABET_SHIFT_LOCKED: return "ALPHABET_SHIFT_LOCKED"; - case Constants.ALPHABET_SHIFT_LOCK_SHIFTED: return "ALPHABET_SHIFT_LOCK_SHIFTED"; - case Constants.SYMBOLS_UNSHIFTED: return "SYMBOLS_UNSHIFTED"; - case Constants.SYMBOLS_SHIFTED: return "SYMBOLS_SHIFTED"; + case MockConstants.ALPHABET_UNSHIFTED: return "ALPHABET_UNSHIFTED"; + case MockConstants.ALPHABET_MANUAL_SHIFTED: return "ALPHABET_MANUAL_SHIFTED"; + case MockConstants.ALPHABET_AUTOMATIC_SHIFTED: return "ALPHABET_AUTOMATIC_SHIFTED"; + case MockConstants.ALPHABET_SHIFT_LOCKED: return "ALPHABET_SHIFT_LOCKED"; + case MockConstants.ALPHABET_SHIFT_LOCK_SHIFTED: return "ALPHABET_SHIFT_LOCK_SHIFTED"; + case MockConstants.SYMBOLS_UNSHIFTED: return "SYMBOLS_UNSHIFTED"; + case MockConstants.SYMBOLS_SHIFTED: return "SYMBOLS_SHIFTED"; default: return "UNKNOWN<" + layoutId + ">"; } } @@ -86,37 +85,37 @@ public class MockKeyboardSwitcher implements KeyboardState.SwitchActions { @Override public void setAlphabetKeyboard() { - mLayout = Constants.ALPHABET_UNSHIFTED; + mLayout = MockConstants.ALPHABET_UNSHIFTED; } @Override public void setAlphabetManualShiftedKeyboard() { - mLayout = Constants.ALPHABET_MANUAL_SHIFTED; + mLayout = MockConstants.ALPHABET_MANUAL_SHIFTED; } @Override public void setAlphabetAutomaticShiftedKeyboard() { - mLayout = Constants.ALPHABET_AUTOMATIC_SHIFTED; + mLayout = MockConstants.ALPHABET_AUTOMATIC_SHIFTED; } @Override public void setAlphabetShiftLockedKeyboard() { - mLayout = Constants.ALPHABET_SHIFT_LOCKED; + mLayout = MockConstants.ALPHABET_SHIFT_LOCKED; } @Override public void setAlphabetShiftLockShiftedKeyboard() { - mLayout = Constants.ALPHABET_SHIFT_LOCK_SHIFTED; + mLayout = MockConstants.ALPHABET_SHIFT_LOCK_SHIFTED; } @Override public void setSymbolsKeyboard() { - mLayout = Constants.SYMBOLS_UNSHIFTED; + mLayout = MockConstants.SYMBOLS_UNSHIFTED; } @Override public void setSymbolsShiftedKeyboard() { - mLayout = Constants.SYMBOLS_SHIFTED; + mLayout = MockConstants.SYMBOLS_SHIFTED; } @Override @@ -186,10 +185,10 @@ public class MockKeyboardSwitcher implements KeyboardState.SwitchActions { } public void onCodeInput(int code, boolean isSinglePointer) { - if (mAutoCapsMode == Constants.CAP_MODE_WORDS) { - if (Keyboard.isLetterCode(code)) { - mAutoCapsState = (code == Constants.CODE_AUTO_CAPS_TRIGGER) - ? mAutoCapsMode : Constants.CAP_MODE_OFF; + if (mAutoCapsMode == MockConstants.CAP_MODE_WORDS) { + if (Constants.isLetterCode(code)) { + mAutoCapsState = (code == MockConstants.CODE_AUTO_CAPS_TRIGGER) + ? mAutoCapsMode : MockConstants.CAP_MODE_OFF; } } else { mAutoCapsState = mAutoCapsMode; diff --git a/tests/src/com/android/inputmethod/latin/BlueUnderlineTests.java b/tests/src/com/android/inputmethod/latin/BlueUnderlineTests.java index a9947c1bd..02047cfc4 100644 --- a/tests/src/com/android/inputmethod/latin/BlueUnderlineTests.java +++ b/tests/src/com/android/inputmethod/latin/BlueUnderlineTests.java @@ -16,8 +16,6 @@ package com.android.inputmethod.latin; -import com.android.inputmethod.keyboard.Keyboard; - import android.text.style.SuggestionSpan; import android.text.style.UnderlineSpan; @@ -67,13 +65,13 @@ public class BlueUnderlineTests extends InputTestsBase { type(STRING_TO_TYPE); sleep(DELAY_TO_WAIT_FOR_UNDERLINE); runMessages(); - type(Keyboard.CODE_SPACE); + type(Constants.CODE_SPACE); sleep(DELAY_TO_WAIT_FOR_UNDERLINE); runMessages(); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); sleep(DELAY_TO_WAIT_FOR_UNDERLINE); runMessages(); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); sleep(DELAY_TO_WAIT_FOR_UNDERLINE); runMessages(); final SpanGetter suggestionSpan = new SpanGetter(mTextView.getText(), SuggestionSpan.class); @@ -88,17 +86,19 @@ public class BlueUnderlineTests extends InputTestsBase { public void testBlueUnderlineDisappearsWhenCursorMoved() { final String STRING_TO_TYPE = "tgis"; + final int typedLength = STRING_TO_TYPE.length(); final int NEW_CURSOR_POSITION = 0; type(STRING_TO_TYPE); sleep(DELAY_TO_WAIT_FOR_UNDERLINE); // Simulate the onUpdateSelection() event - mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); + mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); runMessages(); // Here the blue underline has been set. testBlueUnderline() is testing for this already, // so let's not test it here again. // Now simulate the user moving the cursor. mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION); - mLatinIME.onUpdateSelection(0, 0, NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1); + mLatinIME.onUpdateSelection(typedLength, typedLength, + NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1); sleep(DELAY_TO_WAIT_FOR_UNDERLINE); runMessages(); final SpanGetter span = new SpanGetter(mTextView.getText(), SuggestionSpan.class); diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTests.java b/tests/src/com/android/inputmethod/latin/InputLogicTests.java index 38e57aaed..38f5305de 100644 --- a/tests/src/com/android/inputmethod/latin/InputLogicTests.java +++ b/tests/src/com/android/inputmethod/latin/InputLogicTests.java @@ -16,8 +16,6 @@ package com.android.inputmethod.latin; -import com.android.inputmethod.keyboard.Keyboard; - public class InputLogicTests extends InputTestsBase { public void testTypeWord() { @@ -32,7 +30,7 @@ public class InputLogicTests extends InputTestsBase { type(WORD_TO_TYPE); pickSuggestionManually(0, WORD_TO_TYPE); mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); assertEquals("press suggestion then backspace", EXPECTED_RESULT, mTextView.getText().toString()); } @@ -48,7 +46,7 @@ public class InputLogicTests extends InputTestsBase { mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); assertEquals("pick typed word over auto-correction then backspace", WORD_TO_PICK, mTextView.getText().toString()); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); assertEquals("pick typed word over auto-correction then backspace", EXPECTED_RESULT, mTextView.getText().toString()); } @@ -63,7 +61,7 @@ public class InputLogicTests extends InputTestsBase { mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); assertEquals("pick typed word over auto-correction then backspace", WORD_TO_TYPE, mTextView.getText().toString()); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); assertEquals("pick typed word over auto-correction then backspace", EXPECTED_RESULT, mTextView.getText().toString()); } @@ -79,13 +77,14 @@ public class InputLogicTests extends InputTestsBase { mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); assertEquals("pick different suggestion then backspace", WORD_TO_PICK, mTextView.getText().toString()); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); assertEquals("pick different suggestion then backspace", EXPECTED_RESULT, mTextView.getText().toString()); } public void testDeleteSelection() { final String STRING_TO_TYPE = "some text delete me some text"; + final int typedLength = STRING_TO_TYPE.length(); final int SELECTION_START = 10; final int SELECTION_END = 19; final String EXPECTED_RESULT = "some text some text"; @@ -94,11 +93,12 @@ public class InputLogicTests extends InputTestsBase { // Send once to simulate the cursor actually responding to the move caused by typing. // This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor // move with a move triggered by LatinIME inputting stuff. - mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); + mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); mInputConnection.setSelection(SELECTION_START, SELECTION_END); // And now we simulate the user actually selecting some text. - mLatinIME.onUpdateSelection(0, 0, SELECTION_START, SELECTION_END, -1, -1); - type(Keyboard.CODE_DELETE); + mLatinIME.onUpdateSelection(typedLength, typedLength, + SELECTION_START, SELECTION_END, -1, -1); + type(Constants.CODE_DELETE); assertEquals("delete selection", EXPECTED_RESULT, mTextView.getText().toString()); } @@ -121,7 +121,7 @@ public class InputLogicTests extends InputTestsBase { final String EXPECTED_RESULT = "tgis."; type(STRING_TO_TYPE); mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); assertEquals("auto-correct with period then revert", EXPECTED_RESULT, mTextView.getText().toString()); } @@ -131,7 +131,7 @@ public class InputLogicTests extends InputTestsBase { final String EXPECTED_RESULT = "tgis "; type(STRING_TO_TYPE); mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); assertEquals("auto-correct with space then revert", EXPECTED_RESULT, mTextView.getText().toString()); } @@ -141,7 +141,7 @@ public class InputLogicTests extends InputTestsBase { final String EXPECTED_RESULT = "this"; type(STRING_TO_TYPE); mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); assertEquals("auto-correct with space does not revert", EXPECTED_RESULT, mTextView.getText().toString()); } @@ -157,32 +157,36 @@ public class InputLogicTests extends InputTestsBase { final String STRING_TO_TYPE = "this "; final String EXPECTED_RESULT = "this "; type(STRING_TO_TYPE); - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); assertEquals("double space make a period", EXPECTED_RESULT, mTextView.getText().toString()); } public void testBackspaceAtStartAfterAutocorrect() { final String STRING_TO_TYPE = "tgis "; + final int typedLength = STRING_TO_TYPE.length(); final String EXPECTED_RESULT = "this "; final int NEW_CURSOR_POSITION = 0; type(STRING_TO_TYPE); - mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); + mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION); - mLatinIME.onUpdateSelection(0, 0, NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1); - type(Keyboard.CODE_DELETE); + mLatinIME.onUpdateSelection(typedLength, typedLength, + NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1); + type(Constants.CODE_DELETE); assertEquals("auto correct then move cursor to start of line then backspace", EXPECTED_RESULT, mTextView.getText().toString()); } public void testAutoCorrectThenMoveCursorThenBackspace() { final String STRING_TO_TYPE = "and tgis "; + final int typedLength = STRING_TO_TYPE.length(); final String EXPECTED_RESULT = "andthis "; final int NEW_CURSOR_POSITION = STRING_TO_TYPE.indexOf('t'); type(STRING_TO_TYPE); - mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); + mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION); - mLatinIME.onUpdateSelection(0, 0, NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1); - type(Keyboard.CODE_DELETE); + mLatinIME.onUpdateSelection(typedLength, typedLength, + NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1); + type(Constants.CODE_DELETE); assertEquals("auto correct then move cursor then backspace", EXPECTED_RESULT, mTextView.getText().toString()); } @@ -258,7 +262,7 @@ public class InputLogicTests extends InputTestsBase { final String WORD_TO_TYPE = "this"; type(WORD_TO_TYPE); for (int i = 0; i < WORD_TO_TYPE.length(); ++i) { - type(Keyboard.CODE_DELETE); + type(Constants.CODE_DELETE); } assertEquals("delete whole composing word", "", mTextView.getText().toString()); } diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTestsNonEnglish.java b/tests/src/com/android/inputmethod/latin/InputLogicTestsNonEnglish.java index 78143ac5b..42823f538 100644 --- a/tests/src/com/android/inputmethod/latin/InputLogicTestsNonEnglish.java +++ b/tests/src/com/android/inputmethod/latin/InputLogicTestsNonEnglish.java @@ -16,7 +16,10 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.latin.suggestions.SuggestionStripView; + public class InputLogicTestsNonEnglish extends InputTestsBase { + final String NEXT_WORD_PREDICTION_OPTION = "next_word_prediction"; public void testAutoCorrectForFrench() { final String STRING_TO_TYPE = "irq "; @@ -43,16 +46,40 @@ public class InputLogicTestsNonEnglish extends InputTestsBase { final String WORD_TO_TYPE = "test "; final String PUNCTUATION_FROM_STRIP = "!"; final String EXPECTED_RESULT = "test !!"; + final boolean defaultNextWordPredictionOption = + mLatinIME.getResources().getBoolean(R.bool.config_default_next_word_prediction); + final boolean previousNextWordPredictionOption = + setBooleanPreference(NEXT_WORD_PREDICTION_OPTION, false, + defaultNextWordPredictionOption); + try { + changeLanguage("fr"); + type(WORD_TO_TYPE); + sleep(DELAY_TO_WAIT_FOR_UNDERLINE); + runMessages(); + assertTrue("type word then type space should display punctuation strip", + mLatinIME.isShowingPunctuationList()); + pickSuggestionManually(0, PUNCTUATION_FROM_STRIP); + pickSuggestionManually(0, PUNCTUATION_FROM_STRIP); + assertEquals("type word then type space then punctuation from strip twice for French", + EXPECTED_RESULT, mTextView.getText().toString()); + } finally { + setBooleanPreference(NEXT_WORD_PREDICTION_OPTION, previousNextWordPredictionOption, + defaultNextWordPredictionOption); + } + } + + public void testWordThenSpaceDisplaysPredictions() { + final String WORD_TO_TYPE = "beaujolais "; + final String EXPECTED_RESULT = "nouveau"; changeLanguage("fr"); type(WORD_TO_TYPE); sleep(DELAY_TO_WAIT_FOR_UNDERLINE); runMessages(); - assertTrue("type word then type space should display punctuation strip", - mLatinIME.isShowingPunctuationList()); - pickSuggestionManually(0, PUNCTUATION_FROM_STRIP); - pickSuggestionManually(0, PUNCTUATION_FROM_STRIP); - assertEquals("type word then type space then punctuation from strip twice for French", - EXPECTED_RESULT, mTextView.getText().toString()); + final SuggestionStripView suggestionStripView = + (SuggestionStripView)mInputView.findViewById(R.id.suggestion_strip_view); + final SuggestedWords suggestedWords = suggestionStripView.getSuggestions(); + assertEquals("type word then type space yields predictions for French", + EXPECTED_RESULT, suggestedWords.getWord(0)); } public void testAutoCorrectForGerman() { diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java index fe58cb84e..70330509f 100644 --- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java @@ -53,6 +53,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { protected LatinIME mLatinIME; protected Keyboard mKeyboard; protected MyTextView mTextView; + protected View mInputView; protected InputConnection mInputConnection; private final HashMap<String, InputMethodSubtype> mSubtypeMap = new HashMap<String, InputMethodSubtype>(); @@ -150,9 +151,9 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { final LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); final ViewGroup vg = new FrameLayout(getContext()); - final View inputView = inflater.inflate(R.layout.input_view, vg); + mInputView = inflater.inflate(R.layout.input_view, vg); mLatinIME.onCreateInputMethodInterface().startInput(ic, ei); - mLatinIME.setInputView(inputView); + mLatinIME.setInputView(mInputView); mLatinIME.onBindInput(); mLatinIME.onCreateInputView(); mLatinIME.onStartInputView(ei, false); @@ -211,7 +212,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { // any subsequent post in this queue. However the queue itself is still fully functional! // If we have a way of resetting "queue.mQuiting" then we can continue using it as normal, // coming back to this method to run the messages. - MessageQueue queue = looper.getQueue(); + MessageQueue queue = Looper.myQueue(); try { // However there is no way of doing it externally, and mQuiting is private. // So... get out the big guns. @@ -280,7 +281,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { waitForDictionaryToBeLoaded(); } - protected void pickSuggestionManually(final int index, final CharSequence suggestion) { + protected void pickSuggestionManually(final int index, final String suggestion) { mLatinIME.pickSuggestionManually(index, suggestion); } diff --git a/tests/src/com/android/inputmethod/latin/UserHistoryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/UserHistoryDictIOUtilsTests.java index 70f916c1a..66c9e1851 100644 --- a/tests/src/com/android/inputmethod/latin/UserHistoryDictIOUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/UserHistoryDictIOUtilsTests.java @@ -188,7 +188,7 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase File file = null; try { - file = File.createTempFile("testReadAndWrite", ".dict"); + file = File.createTempFile("testReadAndWrite", ".dict", getContext().getCacheDir()); } catch (IOException e) { Log.d(TAG, "IOException while creating a temporary file: " + e); } diff --git a/tests/src/com/android/inputmethod/latin/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/UserHistoryDictionaryTests.java index f2a17d206..7536f64ac 100644 --- a/tests/src/com/android/inputmethod/latin/UserHistoryDictionaryTests.java +++ b/tests/src/com/android/inputmethod/latin/UserHistoryDictionaryTests.java @@ -23,6 +23,8 @@ import android.preference.PreferenceManager; import android.test.AndroidTestCase; import android.util.Log; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -76,34 +78,43 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { } public void testRandomWords() { - Log.d(TAG, "This test can be used for profiling."); - Log.d(TAG, "Usage: please set UserHisotoryDictionary.PROFILE_SAVE_RESTORE to true."); - final int numberOfWords = 1000; - final Random random = new Random(123456); - List<String> words = generateWords(numberOfWords, random); - - final String locale = "testRandomWords"; - final UserHistoryDictionary dict = UserHistoryDictionary.getInstance(getContext(), - locale, mPrefs); - dict.isTest = true; - - addToDict(dict, words); - - try { - Log.d(TAG, "waiting for adding the word ..."); - Thread.sleep(2000); - } catch (InterruptedException e) { - Log.d(TAG, "InterruptedException: " + e); - } - - // write to file - dict.close(); - + File dictFile = null; try { - Log.d(TAG, "waiting for writing ..."); - Thread.sleep(5000); - } catch (InterruptedException e) { - Log.d(TAG, "InterruptedException: " + e); + Log.d(TAG, "This test can be used for profiling."); + Log.d(TAG, "Usage: please set UserHisotoryDictionary.PROFILE_SAVE_RESTORE to true."); + final int numberOfWords = 1000; + final Random random = new Random(123456); + List<String> words = generateWords(numberOfWords, random); + + final String locale = "testRandomWords"; + final String fileName = "UserHistoryDictionary." + locale + ".dict"; + dictFile = new File(getContext().getFilesDir(), fileName); + final UserHistoryDictionary dict = UserHistoryDictionary.getInstance(getContext(), + locale, mPrefs); + dict.isTest = true; + + addToDict(dict, words); + + try { + Log.d(TAG, "waiting for adding the word ..."); + Thread.sleep(2000); + } catch (InterruptedException e) { + Log.d(TAG, "InterruptedException: " + e); + } + + // write to file + dict.close(); + + try { + Log.d(TAG, "waiting for writing ..."); + Thread.sleep(5000); + } catch (InterruptedException e) { + Log.d(TAG, "InterruptedException: " + e); + } + } finally { + if (dictFile != null) { + dictFile.delete(); + } } } } diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java index 2f954318c..d080ed627 100644 --- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java @@ -271,7 +271,7 @@ public class BinaryDictIOTests extends AndroidTestCase { final String message) { File file = null; try { - file = File.createTempFile("runReadAndWrite", ".dict"); + file = File.createTempFile("runReadAndWrite", ".dict", getContext().getCacheDir()); } catch (IOException e) { Log.e(TAG, "IOException: " + e); } @@ -412,7 +412,7 @@ public class BinaryDictIOTests extends AndroidTestCase { final FormatSpec.FormatOptions formatOptions, final String message) { File file = null; try { - file = File.createTempFile("runReadUnigrams", ".dict"); + file = File.createTempFile("runReadUnigrams", ".dict", getContext().getCacheDir()); } catch (IOException e) { Log.e(TAG, "IOException: " + e); } @@ -484,7 +484,7 @@ public class BinaryDictIOTests extends AndroidTestCase { } if (header == null) return null; return BinaryDictInputOutput.getWordAtAddress(buffer, header.mHeaderSize, - address - header.mHeaderSize, header.mFormatOptions); + address - header.mHeaderSize, header.mFormatOptions).mWord; } private long runGetTerminalPosition(final FusionDictionaryBufferInterface buffer, @@ -510,7 +510,8 @@ public class BinaryDictIOTests extends AndroidTestCase { public void testGetTerminalPosition() { File file = null; try { - file = File.createTempFile("testGetTerminalPosition", ".dict"); + file = File.createTempFile("testGetTerminalPosition", ".dict", + getContext().getCacheDir()); } catch (IOException e) { // do nothing } @@ -561,7 +562,7 @@ public class BinaryDictIOTests extends AndroidTestCase { public void testDeleteWord() { File file = null; try { - file = File.createTempFile("testDeleteWord", ".dict"); + file = File.createTempFile("testDeleteWord", ".dict", getContext().getCacheDir()); } catch (IOException e) { // do nothing } diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java new file mode 100644 index 000000000..318516845 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java @@ -0,0 +1,397 @@ +/* + * Copyright (C) 2012 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. + */ + +package com.android.inputmethod.latin.makedict; + +import com.android.inputmethod.latin.CollectionUtils; +import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.ByteBufferWrapper; +import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.CharEncoding; +import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface; +import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader; +import com.android.inputmethod.latin.makedict.FusionDictionary.Node; +import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; + +import android.test.AndroidTestCase; +import android.test.MoreAsserts; +import android.util.Log; + +import java.io.BufferedOutputStream; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Random; + +public class BinaryDictIOUtilsTests extends AndroidTestCase { + private static final String TAG = BinaryDictIOUtilsTests.class.getSimpleName(); + private static final FormatSpec.FormatOptions FORMAT_OPTIONS = + new FormatSpec.FormatOptions(3, true); + private static final int MAX_UNIGRAMS = 1500; + + private static final ArrayList<String> sWords = CollectionUtils.newArrayList(); + + private static final String[] CHARACTERS = { + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", + "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", + "\u00FC" /* ü */, "\u00E2" /* â */, "\u00F1" /* ñ */, // accented characters + "\u4E9C" /* 亜 */, "\u4F0A" /* 伊 */, "\u5B87" /* 宇 */, // kanji + "\uD841\uDE28" /* 𠘨 */, "\uD840\uDC0B" /* 𠀋 */, "\uD861\uDeD7" /* 𨛗 */ // surrogate pair + }; + + public BinaryDictIOUtilsTests() { + super(); + final Random random = new Random(123456); + sWords.clear(); + for (int i = 0; i < MAX_UNIGRAMS; ++i) { + sWords.add(generateWord(random.nextInt())); + } + } + + // Utilities for test + private String generateWord(final int value) { + final int lengthOfChars = CHARACTERS.length; + StringBuilder builder = new StringBuilder(""); + long lvalue = Math.abs((long)value); + while (lvalue > 0) { + builder.append(CHARACTERS[(int)(lvalue % lengthOfChars)]); + lvalue /= lengthOfChars; + } + if (builder.toString().equals("")) return "a"; + return builder.toString(); + } + + private static void printCharGroup(final CharGroupInfo info) { + Log.d(TAG, " CharGroup at " + info.mOriginalAddress); + Log.d(TAG, " flags = " + info.mFlags); + Log.d(TAG, " parentAddress = " + info.mParentAddress); + Log.d(TAG, " characters = " + new String(info.mCharacters, 0, + info.mCharacters.length)); + if (info.mFrequency != -1) Log.d(TAG, " frequency = " + info.mFrequency); + if (info.mChildrenAddress == FormatSpec.NO_CHILDREN_ADDRESS) { + Log.d(TAG, " children address = no children address"); + } else { + Log.d(TAG, " children address = " + info.mChildrenAddress); + } + if (info.mShortcutTargets != null) { + for (final WeightedString ws : info.mShortcutTargets) { + Log.d(TAG, " shortcuts = " + ws.mWord); + } + } + if (info.mBigrams != null) { + for (final PendingAttribute attr : info.mBigrams) { + Log.d(TAG, " bigram = " + attr.mAddress); + } + } + Log.d(TAG, " end address = " + info.mEndAddress); + } + + private static void printNode(final FusionDictionaryBufferInterface buffer, + final FormatSpec.FormatOptions formatOptions) { + Log.d(TAG, "Node at " + buffer.position()); + final int count = BinaryDictInputOutput.readCharGroupCount(buffer); + Log.d(TAG, " charGroupCount = " + count); + for (int i = 0; i < count; ++i) { + final CharGroupInfo currentInfo = BinaryDictInputOutput.readCharGroup(buffer, + buffer.position(), formatOptions); + printCharGroup(currentInfo); + } + if (formatOptions.mSupportsDynamicUpdate) { + final int forwardLinkAddress = buffer.readUnsignedInt24(); + Log.d(TAG, " forwardLinkAddress = " + forwardLinkAddress); + } + } + + private static void printBinaryFile(final FusionDictionaryBufferInterface buffer) + throws IOException, UnsupportedFormatException { + FileHeader header = BinaryDictInputOutput.readHeader(buffer); + while (buffer.position() < buffer.limit()) { + printNode(buffer, header.mFormatOptions); + } + } + + private int getWordPosition(final File file, final String word) { + int position = FormatSpec.NOT_VALID_WORD; + FileInputStream inStream = null; + try { + inStream = new FileInputStream(file); + final FusionDictionaryBufferInterface buffer = new ByteBufferWrapper( + inStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length())); + position = BinaryDictIOUtils.getTerminalPosition(buffer, word); + } catch (IOException e) { + } catch (UnsupportedFormatException e) { + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (IOException e) { + // do nothing + } + } + } + return position; + } + + private CharGroupInfo findWordFromFile(final File file, final String word) { + FileInputStream inStream = null; + CharGroupInfo info = null; + try { + inStream = new FileInputStream(file); + final FusionDictionaryBufferInterface buffer = new ByteBufferWrapper( + inStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length())); + info = BinaryDictIOUtils.findWordFromBuffer(buffer, word); + } catch (IOException e) { + } catch (UnsupportedFormatException e) { + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (IOException e) { + // do nothing + } + } + } + return info; + } + + // return amount of time to insert a word + private long insertAndCheckWord(final File file, final String word, final int frequency, + final boolean exist, final ArrayList<WeightedString> bigrams, + final ArrayList<WeightedString> shortcuts) { + RandomAccessFile raFile = null; + BufferedOutputStream outStream = null; + FusionDictionaryBufferInterface buffer = null; + long amountOfTime = -1; + try { + raFile = new RandomAccessFile(file, "rw"); + buffer = new ByteBufferWrapper(raFile.getChannel().map( + FileChannel.MapMode.READ_WRITE, 0, file.length())); + outStream = new BufferedOutputStream(new FileOutputStream(file, true)); + + if (!exist) { + assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word)); + } + final long now = System.nanoTime(); + BinaryDictIOUtils.insertWord(buffer, outStream, word, frequency, bigrams, shortcuts, + false, false); + amountOfTime = System.nanoTime() - now; + outStream.flush(); + MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word)); + outStream.close(); + raFile.close(); + } catch (IOException e) { + } catch (UnsupportedFormatException e) { + } finally { + if (outStream != null) { + try { + outStream.close(); + } catch (IOException e) { + // do nothing + } + } + if (raFile != null) { + try { + raFile.close(); + } catch (IOException e) { + // do nothing + } + } + } + return amountOfTime; + } + + private void deleteWord(final File file, final String word) { + RandomAccessFile raFile = null; + FusionDictionaryBufferInterface buffer = null; + try { + raFile = new RandomAccessFile(file, "rw"); + buffer = new ByteBufferWrapper(raFile.getChannel().map( + FileChannel.MapMode.READ_WRITE, 0, file.length())); + BinaryDictIOUtils.deleteWord(buffer, word); + } catch (IOException e) { + } catch (UnsupportedFormatException e) { + } finally { + if (raFile != null) { + try { + raFile.close(); + } catch (IOException e) { + // do nothing + } + } + } + } + + private void checkReverseLookup(final File file, final String word, final int position) { + FileInputStream inStream = null; + try { + inStream = new FileInputStream(file); + final FusionDictionaryBufferInterface buffer = new ByteBufferWrapper( + inStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length())); + final FileHeader header = BinaryDictInputOutput.readHeader(buffer); + assertEquals(word, BinaryDictInputOutput.getWordAtAddress(buffer, header.mHeaderSize, + position - header.mHeaderSize, header.mFormatOptions)); + } catch (IOException e) { + } catch (UnsupportedFormatException e) { + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (IOException e) { + // do nothing + } + } + } + } + + public void testInsertWord() { + File file = null; + try { + file = File.createTempFile("testInsertWord", ".dict", getContext().getCacheDir()); + } catch (IOException e) { + fail("IOException while creating temporary file: " + e); + } + + // set an initial dictionary. + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions(new HashMap<String,String>(), false, false)); + dict.add("abcd", 10, null, false); + + try { + final FileOutputStream out = new FileOutputStream(file); + BinaryDictInputOutput.writeDictionaryBinary(out, dict, FORMAT_OPTIONS); + out.close(); + } catch (IOException e) { + fail("IOException while writing an initial dictionary : " + e); + } catch (UnsupportedFormatException e) { + fail("UnsupportedFormatException while writing an initial dictionary : " + e); + } + + MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "abcd")); + insertAndCheckWord(file, "abcde", 10, false, null, null); + + insertAndCheckWord(file, "abcdefghijklmn", 10, false, null, null); + checkReverseLookup(file, "abcdefghijklmn", getWordPosition(file, "abcdefghijklmn")); + + insertAndCheckWord(file, "abcdabcd", 10, false, null, null); + checkReverseLookup(file, "abcdabcd", getWordPosition(file, "abcdabcd")); + + // update the existing word. + insertAndCheckWord(file, "abcdabcd", 15, true, null, null); + + // split 1 + insertAndCheckWord(file, "ab", 20, false, null, null); + + // split 2 + insertAndCheckWord(file, "ami", 30, false, null, null); + + deleteWord(file, "ami"); + assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "ami")); + + insertAndCheckWord(file, "abcdabfg", 30, false, null, null); + + deleteWord(file, "abcd"); + assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "abcd")); + } + + public void testInsertWordWithBigrams() { + File file = null; + try { + file = File.createTempFile("testInsertWordWithBigrams", ".dict", + getContext().getCacheDir()); + } catch (IOException e) { + fail("IOException while creating temporary file: " + e); + } + + // set an initial dictionary. + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions(new HashMap<String,String>(), false, false)); + dict.add("abcd", 10, null, false); + dict.add("efgh", 15, null, false); + + try { + final FileOutputStream out = new FileOutputStream(file); + BinaryDictInputOutput.writeDictionaryBinary(out, dict, FORMAT_OPTIONS); + out.close(); + } catch (IOException e) { + fail("IOException while writing an initial dictionary : " + e); + } catch (UnsupportedFormatException e) { + fail("UnsupportedFormatException while writing an initial dictionary : " + e); + } + + final ArrayList<WeightedString> banana = new ArrayList<WeightedString>(); + banana.add(new WeightedString("banana", 10)); + + insertAndCheckWord(file, "banana", 0, false, null, null); + insertAndCheckWord(file, "recursive", 60, true, banana, null); + + final CharGroupInfo info = findWordFromFile(file, "recursive"); + int bananaPos = getWordPosition(file, "banana"); + assertNotNull(info.mBigrams); + assertEquals(info.mBigrams.size(), 1); + assertEquals(info.mBigrams.get(0).mAddress, bananaPos); + } + + public void testRandomWords() { + File file = null; + try { + file = File.createTempFile("testRandomWord", ".dict", getContext().getCacheDir()); + } catch (IOException e) { + } + assertNotNull(file); + + // set an initial dictionary. + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions(new HashMap<String, String>(), false, + false)); + dict.add("initial", 10, null, false); + + try { + final FileOutputStream out = new FileOutputStream(file); + BinaryDictInputOutput.writeDictionaryBinary(out, dict, FORMAT_OPTIONS); + out.close(); + } catch (IOException e) { + assertTrue(false); + } catch (UnsupportedFormatException e) { + assertTrue(false); + } + + long maxTimeToInsert = 0, sum = 0; + long minTimeToInsert = 100000000; // 1000000000 is an upper bound for minTimeToInsert. + int cnt = 0; + for (final String word : sWords) { + final long diff = insertAndCheckWord(file, word, + cnt % FormatSpec.MAX_TERMINAL_FREQUENCY, false, null, null); + maxTimeToInsert = Math.max(maxTimeToInsert, diff); + minTimeToInsert = Math.min(minTimeToInsert, diff); + sum += diff; + cnt++; + } + cnt = 0; + for (final String word : sWords) { + MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word)); + } + + Log.d(TAG, "max = " + ((double)maxTimeToInsert/1000000) + " ms."); + Log.d(TAG, "min = " + ((double)minTimeToInsert/1000000) + " ms."); + Log.d(TAG, "avg = " + ((double)sum/MAX_UNIGRAMS/1000000) + " ms."); + } +} |