diff options
Diffstat (limited to 'tests/src')
21 files changed, 2336 insertions, 163 deletions
diff --git a/tests/src/com/android/inputmethod/keyboard/MoreKeysKeyboardBuilderFixedOrderTests.java b/tests/src/com/android/inputmethod/keyboard/MoreKeysKeyboardBuilderFixedOrderTests.java index 5c6c83432..2a244a772 100644 --- a/tests/src/com/android/inputmethod/keyboard/MoreKeysKeyboardBuilderFixedOrderTests.java +++ b/tests/src/com/android/inputmethod/keyboard/MoreKeysKeyboardBuilderFixedOrderTests.java @@ -18,7 +18,7 @@ package com.android.inputmethod.keyboard; import android.test.AndroidTestCase; -import com.android.inputmethod.keyboard.MoreKeysKeyboard.Builder.MoreKeysKeyboardParams; +import com.android.inputmethod.keyboard.MoreKeysKeyboard.MoreKeysKeyboardParams; public class MoreKeysKeyboardBuilderFixedOrderTests extends AndroidTestCase { private static final int WIDTH = 10; diff --git a/tests/src/com/android/inputmethod/keyboard/MoreKeysKeyboardBuilderTests.java b/tests/src/com/android/inputmethod/keyboard/MoreKeysKeyboardBuilderTests.java index 31f0e0fef..e6c76db85 100644 --- a/tests/src/com/android/inputmethod/keyboard/MoreKeysKeyboardBuilderTests.java +++ b/tests/src/com/android/inputmethod/keyboard/MoreKeysKeyboardBuilderTests.java @@ -18,7 +18,7 @@ package com.android.inputmethod.keyboard; import android.test.AndroidTestCase; -import com.android.inputmethod.keyboard.MoreKeysKeyboard.Builder.MoreKeysKeyboardParams; +import com.android.inputmethod.keyboard.MoreKeysKeyboard.MoreKeysKeyboardParams; public class MoreKeysKeyboardBuilderTests extends AndroidTestCase { private static final int WIDTH = 10; diff --git a/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java b/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java index a34e0ef8b..bc5043911 100644 --- a/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java +++ b/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java @@ -22,6 +22,7 @@ import android.test.AndroidTestCase; import android.view.inputmethod.InputMethodSubtype; import com.android.inputmethod.latin.AdditionalSubtype; +import com.android.inputmethod.latin.CollectionUtils; import com.android.inputmethod.latin.ImfUtils; import com.android.inputmethod.latin.StringUtils; import com.android.inputmethod.latin.SubtypeLocale; @@ -31,7 +32,7 @@ import java.util.Locale; public class SpacebarTextTests extends AndroidTestCase { // Locale to subtypes list. - private final ArrayList<InputMethodSubtype> mSubtypesList = new ArrayList<InputMethodSubtype>(); + private final ArrayList<InputMethodSubtype> mSubtypesList = CollectionUtils.newArrayList(); private Resources mRes; @@ -46,7 +47,7 @@ public class SpacebarTextTests extends AndroidTestCase { public void testAllFullDisplayName() { for (final InputMethodSubtype subtype : mSubtypesList) { final String subtypeName = SubtypeLocale.getSubtypeDisplayName(subtype, mRes); - final String spacebarText = LatinKeyboardView.getFullDisplayName(subtype, mRes); + final String spacebarText = MainKeyboardView.getFullDisplayName(subtype, mRes); final String languageName = SubtypeLocale.getSubtypeLocaleDisplayName(subtype.getLocale()); if (SubtypeLocale.isNoLanguage(subtype)) { @@ -60,7 +61,7 @@ public class SpacebarTextTests extends AndroidTestCase { public void testAllMiddleDisplayName() { for (final InputMethodSubtype subtype : mSubtypesList) { final String subtypeName = SubtypeLocale.getSubtypeDisplayName(subtype, mRes); - final String spacebarText = LatinKeyboardView.getMiddleDisplayName(subtype); + final String spacebarText = MainKeyboardView.getMiddleDisplayName(subtype); if (SubtypeLocale.isNoLanguage(subtype)) { assertEquals(subtypeName, SubtypeLocale.getKeyboardLayoutSetName(subtype), spacebarText); @@ -76,7 +77,7 @@ public class SpacebarTextTests extends AndroidTestCase { for (final InputMethodSubtype subtype : mSubtypesList) { final String subtypeName = SubtypeLocale.getSubtypeDisplayName(subtype, mRes); final Locale locale = SubtypeLocale.getSubtypeLocale(subtype); - final String spacebarText = LatinKeyboardView.getShortDisplayName(subtype); + final String spacebarText = MainKeyboardView.getShortDisplayName(subtype); final String languageCode = StringUtils.toTitleCase(locale.getLanguage(), locale); if (SubtypeLocale.isNoLanguage(subtype)) { assertEquals(subtypeName, "", spacebarText); @@ -117,31 +118,31 @@ public class SpacebarTextTests extends AndroidTestCase { context, SubtypeLocale.NO_LANGUAGE, "qwerty"); assertEquals("en_US", "English (US)", - LatinKeyboardView.getFullDisplayName(EN_US, mRes)); + MainKeyboardView.getFullDisplayName(EN_US, mRes)); assertEquals("en_GB", "English (UK)", - LatinKeyboardView.getFullDisplayName(EN_GB, mRes)); + MainKeyboardView.getFullDisplayName(EN_GB, mRes)); assertEquals("fr ", "Français", - LatinKeyboardView.getFullDisplayName(FR, mRes)); + MainKeyboardView.getFullDisplayName(FR, mRes)); assertEquals("fr_CA", "Français (Canada)", - LatinKeyboardView.getFullDisplayName(FR_CA, mRes)); + MainKeyboardView.getFullDisplayName(FR_CA, mRes)); assertEquals("de ", "Deutsch", - LatinKeyboardView.getFullDisplayName(DE, mRes)); + MainKeyboardView.getFullDisplayName(DE, mRes)); assertEquals("zz ", "QWERTY", - LatinKeyboardView.getFullDisplayName(ZZ, mRes)); - - assertEquals("en_US", "English", LatinKeyboardView.getMiddleDisplayName(EN_US)); - assertEquals("en_GB", "English", LatinKeyboardView.getMiddleDisplayName(EN_GB)); - assertEquals("fr ", "Français", LatinKeyboardView.getMiddleDisplayName(FR)); - assertEquals("fr_CA", "Français", LatinKeyboardView.getMiddleDisplayName(FR_CA)); - assertEquals("de ", "Deutsch", LatinKeyboardView.getMiddleDisplayName(DE)); - assertEquals("zz ", "QWERTY", LatinKeyboardView.getMiddleDisplayName(ZZ)); - - assertEquals("en_US", "En", LatinKeyboardView.getShortDisplayName(EN_US)); - assertEquals("en_GB", "En", LatinKeyboardView.getShortDisplayName(EN_GB)); - assertEquals("fr ", "Fr", LatinKeyboardView.getShortDisplayName(FR)); - assertEquals("fr_CA", "Fr", LatinKeyboardView.getShortDisplayName(FR_CA)); - assertEquals("de ", "De", LatinKeyboardView.getShortDisplayName(DE)); - assertEquals("zz ", "", LatinKeyboardView.getShortDisplayName(ZZ)); + MainKeyboardView.getFullDisplayName(ZZ, mRes)); + + assertEquals("en_US", "English", MainKeyboardView.getMiddleDisplayName(EN_US)); + assertEquals("en_GB", "English", MainKeyboardView.getMiddleDisplayName(EN_GB)); + assertEquals("fr ", "Français", MainKeyboardView.getMiddleDisplayName(FR)); + assertEquals("fr_CA", "Français", MainKeyboardView.getMiddleDisplayName(FR_CA)); + assertEquals("de ", "Deutsch", MainKeyboardView.getMiddleDisplayName(DE)); + assertEquals("zz ", "QWERTY", MainKeyboardView.getMiddleDisplayName(ZZ)); + + assertEquals("en_US", "En", MainKeyboardView.getShortDisplayName(EN_US)); + assertEquals("en_GB", "En", MainKeyboardView.getShortDisplayName(EN_GB)); + assertEquals("fr ", "Fr", MainKeyboardView.getShortDisplayName(FR)); + assertEquals("fr_CA", "Fr", MainKeyboardView.getShortDisplayName(FR_CA)); + assertEquals("de ", "De", MainKeyboardView.getShortDisplayName(DE)); + assertEquals("zz ", "", MainKeyboardView.getShortDisplayName(ZZ)); } public void testAdditionalSubtype() { @@ -155,22 +156,22 @@ public class SpacebarTextTests extends AndroidTestCase { SubtypeLocale.NO_LANGUAGE, "azerty", null); assertEquals("fr qwertz", "Français (QWERTZ)", - LatinKeyboardView.getFullDisplayName(FR_QWERTZ, mRes)); + MainKeyboardView.getFullDisplayName(FR_QWERTZ, mRes)); assertEquals("de qwerty", "Deutsch (QWERTY)", - LatinKeyboardView.getFullDisplayName(DE_QWERTY, mRes)); + MainKeyboardView.getFullDisplayName(DE_QWERTY, mRes)); assertEquals("en_US azerty", "English (US) (AZERTY)", - LatinKeyboardView.getFullDisplayName(US_AZERTY, mRes)); + MainKeyboardView.getFullDisplayName(US_AZERTY, mRes)); assertEquals("zz azerty", "AZERTY", - LatinKeyboardView.getFullDisplayName(ZZ_AZERTY, mRes)); + MainKeyboardView.getFullDisplayName(ZZ_AZERTY, mRes)); - assertEquals("fr qwertz", "Français", LatinKeyboardView.getMiddleDisplayName(FR_QWERTZ)); - assertEquals("de qwerty", "Deutsch", LatinKeyboardView.getMiddleDisplayName(DE_QWERTY)); - assertEquals("en_US azerty", "English", LatinKeyboardView.getMiddleDisplayName(US_AZERTY)); - assertEquals("zz azerty", "AZERTY", LatinKeyboardView.getMiddleDisplayName(ZZ_AZERTY)); + assertEquals("fr qwertz", "Français", MainKeyboardView.getMiddleDisplayName(FR_QWERTZ)); + assertEquals("de qwerty", "Deutsch", MainKeyboardView.getMiddleDisplayName(DE_QWERTY)); + assertEquals("en_US azerty", "English", MainKeyboardView.getMiddleDisplayName(US_AZERTY)); + assertEquals("zz azerty", "AZERTY", MainKeyboardView.getMiddleDisplayName(ZZ_AZERTY)); - assertEquals("fr qwertz", "Fr", LatinKeyboardView.getShortDisplayName(FR_QWERTZ)); - assertEquals("de qwerty", "De", LatinKeyboardView.getShortDisplayName(DE_QWERTY)); - assertEquals("en_US azerty", "En", LatinKeyboardView.getShortDisplayName(US_AZERTY)); - assertEquals("zz azerty", "", LatinKeyboardView.getShortDisplayName(ZZ_AZERTY)); + assertEquals("fr qwertz", "Fr", MainKeyboardView.getShortDisplayName(FR_QWERTZ)); + assertEquals("de qwerty", "De", MainKeyboardView.getShortDisplayName(DE_QWERTY)); + assertEquals("en_US azerty", "En", MainKeyboardView.getShortDisplayName(US_AZERTY)); + assertEquals("zz azerty", "", MainKeyboardView.getShortDisplayName(ZZ_AZERTY)); } } diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java index fa067f4c8..1346c00f4 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java @@ -16,31 +16,35 @@ package com.android.inputmethod.keyboard.internal; -import android.test.AndroidTestCase; +import android.app.Instrumentation; +import android.test.InstrumentationTestCase; + +import com.android.inputmethod.latin.CollectionUtils; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Locale; -public class KeySpecParserCsvTests extends AndroidTestCase { +public class KeySpecParserCsvTests extends InstrumentationTestCase { private final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); @Override protected void setUp() throws Exception { super.setUp(); + final Instrumentation instrumentation = getInstrumentation(); mTextsSet.setLanguage(Locale.ENGLISH.getLanguage()); - mTextsSet.loadStringResources(getContext()); + mTextsSet.loadStringResources(instrumentation.getTargetContext()); final String[] testResourceNames = getAllResourceIdNames( com.android.inputmethod.latin.tests.R.string.class); - mTextsSet.loadStringResourcesInternal(getTestContext(), + mTextsSet.loadStringResourcesInternal(instrumentation.getContext(), testResourceNames, com.android.inputmethod.latin.tests.R.string.empty_string); } private static String[] getAllResourceIdNames(final Class<?> resourceIdClass) { - final ArrayList<String> names = new ArrayList<String>(); + final ArrayList<String> names = CollectionUtils.newArrayList(); for (final Field field : resourceIdClass.getFields()) { if (field.getType() == Integer.TYPE) { names.add(field.getName()); diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java index 0b174a7e6..1ab577557 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTests.java @@ -23,7 +23,6 @@ import static com.android.inputmethod.keyboard.internal.KeyboardIconsSet.ICON_UN import android.test.AndroidTestCase; import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.internal.KeySpecParser.MoreKeySpec; import java.util.Arrays; import java.util.Locale; diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateMultiTouchTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateMultiTouchTests.java index 64cf7a61b..f5ad7239e 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateMultiTouchTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateMultiTouchTests.java @@ -420,38 +420,38 @@ public class KeyboardStateMultiTouchTests extends KeyboardStateTestsBase { public void testDoubleTapShiftAndChording() { // TODO: The following tests fail due to bug. Temporarily commented. - // First shift key tap. - pressAndReleaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED); - // Second shift key tap, maybe shift locked. - secondPressKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED); - // Press/release letter key, remain in manual shifted. - chordingPressAndReleaseKey('A', ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED); - // Release shift key, back to alphabet shifted (not shift locked). - releaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED); - - // Long press shift key, enter alphabet shift locked. - longPressAndReleaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED, - ALPHABET_SHIFT_LOCKED); - // First shift key tap. - pressAndReleaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED, ALPHABET_UNSHIFTED); - // Second shift key tap, maybe shift unlocked. - secondPressKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED); - // Press/release letter key, remain in manual shifted. - chordingPressAndReleaseKey('A', ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED); - // Release shift key, back to alphabet (not shift locked). - releaseKey(CODE_SHIFT, ALPHABET_UNSHIFTED); - - // Set capitalize the first character of all words mode. - setAutoCapsMode(CAP_MODE_WORDS); - // Load keyboard, should be in automatic shifted. - loadKeyboard(ALPHABET_AUTOMATIC_SHIFTED); - // First shift key tap. - pressAndReleaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED, ALPHABET_UNSHIFTED); - // Second shift key tap, maybe shift locked. - secondPressKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED); - // Press/release letter key, remain in manual shifted. - chordingPressAndReleaseKey('A', ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED); - // Release shift key, back to alphabet (not shift locked). - releaseKey(CODE_SHIFT, ALPHABET_UNSHIFTED); +// // First shift key tap. +// pressAndReleaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED); +// // Second shift key tap, maybe shift locked. +// secondPressKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED); +// // Press/release letter key, remain in manual shifted. +// chordingPressAndReleaseKey('A', ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED); +// // Release shift key, back to alphabet shifted (not shift locked). +// releaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED); +// +// // Long press shift key, enter alphabet shift locked. +// longPressAndReleaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED, +// ALPHABET_SHIFT_LOCKED); +// // First shift key tap. +// pressAndReleaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED, ALPHABET_UNSHIFTED); +// // Second shift key tap, maybe shift unlocked. +// secondPressKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED); +// // Press/release letter key, remain in manual shifted. +// chordingPressAndReleaseKey('A', ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED); +// // Release shift key, back to alphabet (not shift locked). +// releaseKey(CODE_SHIFT, ALPHABET_UNSHIFTED); +// +// // Set capitalize the first character of all words mode. +// setAutoCapsMode(CAP_MODE_WORDS); +// // Load keyboard, should be in automatic shifted. +// loadKeyboard(ALPHABET_AUTOMATIC_SHIFTED); +// // First shift key tap. +// pressAndReleaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED, ALPHABET_UNSHIFTED); +// // Second shift key tap, maybe shift locked. +// secondPressKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED); +// // Press/release letter key, remain in manual shifted. +// chordingPressAndReleaseKey('A', ALPHABET_MANUAL_SHIFTED, ALPHABET_MANUAL_SHIFTED); +// // Release shift key, back to alphabet (not shift locked). +// releaseKey(CODE_SHIFT, ALPHABET_UNSHIFTED); } } diff --git a/tests/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueueTests.java b/tests/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueueTests.java new file mode 100644 index 000000000..8fed28f9e --- /dev/null +++ b/tests/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueueTests.java @@ -0,0 +1,316 @@ +/* + * 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.keyboard.internal; + +import android.test.AndroidTestCase; + +public class PointerTrackerQueueTests extends AndroidTestCase { + public static class Element implements PointerTrackerQueue.Element { + public static int sPhantomUpCount; + public static final long NOT_HAPPENED = -1; + + public final int mId; + public boolean mIsModifier; + public boolean mIsInSlidingKeyInput; + public long mPhantomUpEventTime = NOT_HAPPENED; + + public Element(int id) { + mId = id; + } + + @Override + public boolean isModifier() { + return mIsModifier; + } + + @Override + public boolean isInSlidingKeyInput() { + return mIsInSlidingKeyInput; + } + + @Override + public void onPhantomUpEvent(long eventTime) { + sPhantomUpCount++; + mPhantomUpEventTime = eventTime + sPhantomUpCount; + } + + @Override + public String toString() { + return Integer.toString(mId); + } + } + + private final Element mElement1 = new Element(1); + private final Element mElement2 = new Element(2); + private final Element mElement3 = new Element(3); + private final Element mElement4 = new Element(4); + private final PointerTrackerQueue mQueue = new PointerTrackerQueue(); + + public void testEmpty() { + assertEquals("empty queue", 0, mQueue.size()); + assertEquals("empty queue", "[]", mQueue.toString()); + } + + public void testAdd() { + mQueue.add(mElement1); + assertEquals("add element1", 1, mQueue.size()); + assertEquals("after adding element1", "[1]", mQueue.toString()); + mQueue.add(mElement2); + assertEquals("add element2", 2, mQueue.size()); + assertEquals("after adding element2", "[1 2]", mQueue.toString()); + mQueue.add(mElement3); + assertEquals("add element3", 3, mQueue.size()); + assertEquals("after adding element3", "[1 2 3]", mQueue.toString()); + mQueue.add(mElement4); + assertEquals("add element4", 4, mQueue.size()); + assertEquals("after adding element4", "[1 2 3 4]", mQueue.toString()); + } + + public void testRemove() { + Element.sPhantomUpCount = 0; + + mQueue.add(mElement1); + mQueue.add(mElement2); + mQueue.add(mElement3); + mQueue.add(mElement4); + + mQueue.remove(mElement2); + assertEquals("remove element2", 3, mQueue.size()); + assertEquals("after removing element2", "[1 3 4]", mQueue.toString()); + mQueue.remove(mElement4); + assertEquals("remove element4", 2, mQueue.size()); + assertEquals("after removing element4", "[1 3]", mQueue.toString()); + mQueue.remove(mElement4); + assertEquals("remove element4 again", 2, mQueue.size()); + assertEquals("after removing element4 again", "[1 3]", mQueue.toString()); + mQueue.remove(mElement1); + assertEquals("remove element1", 1, mQueue.size()); + assertEquals("after removing element4", "[3]", mQueue.toString()); + mQueue.remove(mElement3); + assertEquals("remove element3", 0, mQueue.size()); + assertEquals("after removing element3", "[]", mQueue.toString()); + mQueue.remove(mElement1); + assertEquals("remove element1 again", 0, mQueue.size()); + assertEquals("after removing element1 again", "[]", mQueue.toString()); + + assertEquals("after remove elements", 0, Element.sPhantomUpCount); + assertEquals("after remove element1", + Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime); + assertEquals("after remove element2", + Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime); + assertEquals("after remove element3", + Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime); + assertEquals("after remove element4", + Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime); + } + + public void testAddAndRemove() { + Element.sPhantomUpCount = 0; + + mQueue.add(mElement1); + mQueue.add(mElement2); + mQueue.add(mElement3); + mQueue.add(mElement4); + + mQueue.remove(mElement2); + assertEquals("remove element2", 3, mQueue.size()); + assertEquals("after removing element2", "[1 3 4]", mQueue.toString()); + mQueue.remove(mElement4); + assertEquals("remove element4", 2, mQueue.size()); + assertEquals("after removing element4", "[1 3]", mQueue.toString()); + mQueue.add(mElement2); + assertEquals("add element2", 3, mQueue.size()); + assertEquals("after adding element2", "[1 3 2]", mQueue.toString()); + mQueue.remove(mElement4); + assertEquals("remove element4 again", 3, mQueue.size()); + assertEquals("after removing element4 again", "[1 3 2]", mQueue.toString()); + mQueue.remove(mElement1); + assertEquals("remove element1", 2, mQueue.size()); + assertEquals("after removing element4", "[3 2]", mQueue.toString()); + mQueue.add(mElement1); + assertEquals("add element1", 3, mQueue.size()); + assertEquals("after adding element1", "[3 2 1]", mQueue.toString()); + mQueue.remove(mElement3); + assertEquals("remove element3", 2, mQueue.size()); + assertEquals("after removing element3", "[2 1]", mQueue.toString()); + mQueue.remove(mElement1); + assertEquals("remove element1 again", 1, mQueue.size()); + assertEquals("after removing element1 again", "[2]", mQueue.toString()); + + assertEquals("after remove element1", + Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime); + assertEquals("after remove element2", + Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime); + assertEquals("after remove element3", + Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime); + assertEquals("after remove element4", + Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime); + } + + public void testReleaseAllPointers() { + mElement2.mIsModifier = true; + mQueue.add(mElement1); + mQueue.add(mElement2); + mQueue.add(mElement3); + mQueue.add(mElement4); + + final long eventTime = 123; + Element.sPhantomUpCount = 0; + mQueue.releaseAllPointers(eventTime); + assertEquals("after releaseAllPointers", 4, Element.sPhantomUpCount); + assertEquals("after releaseAllPointers", 0, mQueue.size()); + assertEquals("after releaseAllPointers", "[]", mQueue.toString()); + assertEquals("after releaseAllPointers element1", + eventTime + 1, mElement1.mPhantomUpEventTime); + assertEquals("after releaseAllPointers element2", + eventTime + 2, mElement2.mPhantomUpEventTime); + assertEquals("after releaseAllPointers element3", + eventTime + 3, mElement3.mPhantomUpEventTime); + assertEquals("after releaseAllPointers element4", + eventTime + 4, mElement4.mPhantomUpEventTime); + } + + public void testReleaseAllPointersOlderThan() { + mElement2.mIsModifier = true; + mQueue.add(mElement1); + mQueue.add(mElement2); + mQueue.add(mElement3); + mQueue.add(mElement4); + + final long eventTime = 123; + Element.sPhantomUpCount = 0; + mQueue.releaseAllPointersOlderThan(mElement4, eventTime); + assertEquals("after releaseAllPointersOlderThan", 2, Element.sPhantomUpCount); + assertEquals("after releaseAllPointersOlderThan", 2, mQueue.size()); + assertEquals("after releaseAllPointersOlderThan", "[2 4]", mQueue.toString()); + assertEquals("after releaseAllPointersOlderThan element1", + eventTime + 1, mElement1.mPhantomUpEventTime); + assertEquals("after releaseAllPointersOlderThan element2", + Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime); + assertEquals("after releaseAllPointersOlderThan element3", + eventTime + 2, mElement3.mPhantomUpEventTime); + assertEquals("after releaseAllPointersOlderThan element4", + Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime); + } + + public void testReleaseAllPointersOlderThanWithoutModifier() { + mQueue.add(mElement1); + mQueue.add(mElement2); + mQueue.add(mElement3); + mQueue.add(mElement4); + + final long eventTime = 123; + Element.sPhantomUpCount = 0; + mQueue.releaseAllPointersOlderThan(mElement4, eventTime); + assertEquals("after releaseAllPointersOlderThan without modifier", + 3, Element.sPhantomUpCount); + assertEquals("after releaseAllPointersOlderThan without modifier", 1, mQueue.size()); + assertEquals("after releaseAllPointersOlderThan without modifier", + "[4]", mQueue.toString()); + assertEquals("after releaseAllPointersOlderThan without modifier element1", + eventTime + 1, mElement1.mPhantomUpEventTime); + assertEquals("after releaseAllPointersOlderThan without modifier element2", + eventTime + 2, mElement2.mPhantomUpEventTime); + assertEquals("after releaseAllPointersOlderThan without modifier element3", + eventTime + 3, mElement3.mPhantomUpEventTime); + assertEquals("after releaseAllPointersOlderThan without modifier element4", + Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime); + } + + public void testReleaseAllPointersExcept() { + mElement2.mIsModifier = true; + mQueue.add(mElement1); + mQueue.add(mElement2); + mQueue.add(mElement3); + mQueue.add(mElement4); + + final long eventTime = 123; + Element.sPhantomUpCount = 0; + mQueue.releaseAllPointersExcept(mElement3, eventTime); + assertEquals("after releaseAllPointersExcept", 3, Element.sPhantomUpCount); + assertEquals("after releaseAllPointersExcept", 1, mQueue.size()); + assertEquals("after releaseAllPointersExcept", "[3]", mQueue.toString()); + assertEquals("after releaseAllPointersExcept element1", + eventTime + 1, mElement1.mPhantomUpEventTime); + assertEquals("after releaseAllPointersExcept element2", + eventTime + 2, mElement2.mPhantomUpEventTime); + assertEquals("after releaseAllPointersExcept element3", + Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime); + assertEquals("after releaseAllPointersExcept element4", + eventTime + 3, mElement4.mPhantomUpEventTime); + } + + public void testHasModifierKeyOlderThan() { + Element.sPhantomUpCount = 0; + assertFalse("hasModifierKeyOlderThan empty", mQueue.hasModifierKeyOlderThan(mElement1)); + + mQueue.add(mElement1); + mQueue.add(mElement2); + mQueue.add(mElement3); + mQueue.add(mElement4); + + assertFalse("hasModifierKeyOlderThan element1", mQueue.hasModifierKeyOlderThan(mElement1)); + assertFalse("hasModifierKeyOlderThan element2", mQueue.hasModifierKeyOlderThan(mElement2)); + assertFalse("hasModifierKeyOlderThan element3", mQueue.hasModifierKeyOlderThan(mElement3)); + assertFalse("hasModifierKeyOlderThan element4", mQueue.hasModifierKeyOlderThan(mElement4)); + + mElement2.mIsModifier = true; + assertFalse("hasModifierKeyOlderThan element1", mQueue.hasModifierKeyOlderThan(mElement1)); + assertFalse("hasModifierKeyOlderThan element2", mQueue.hasModifierKeyOlderThan(mElement2)); + assertTrue("hasModifierKeyOlderThan element3", mQueue.hasModifierKeyOlderThan(mElement3)); + assertTrue("hasModifierKeyOlderThan element4", mQueue.hasModifierKeyOlderThan(mElement4)); + + assertEquals("after hasModifierKeyOlderThan", 0, Element.sPhantomUpCount); + assertEquals("after hasModifierKeyOlderThan", 4, mQueue.size()); + assertEquals("after hasModifierKeyOlderThan", "[1 2 3 4]", mQueue.toString()); + assertEquals("after hasModifierKeyOlderThan element1", + Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime); + assertEquals("after hasModifierKeyOlderThan element2", + Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime); + assertEquals("after hasModifierKeyOlderThan element3", + Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime); + assertEquals("after hasModifierKeyOlderThan element4", + Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime); + } + + public void testIsAnyInSlidingKeyInput() { + Element.sPhantomUpCount = 0; + assertFalse("isAnyInSlidingKeyInput empty", mQueue.isAnyInSlidingKeyInput()); + + mQueue.add(mElement1); + mQueue.add(mElement2); + mQueue.add(mElement3); + mQueue.add(mElement4); + + assertFalse("isAnyInSlidingKeyInput element1", mQueue.isAnyInSlidingKeyInput()); + + mElement3.mIsInSlidingKeyInput = true; + assertTrue("isAnyInSlidingKeyInput element1", mQueue.isAnyInSlidingKeyInput()); + + assertEquals("after isAnyInSlidingKeyInput", 0, Element.sPhantomUpCount); + assertEquals("after isAnyInSlidingKeyInput", 4, mQueue.size()); + assertEquals("after isAnyInSlidingKeyInput", "[1 2 3 4]", mQueue.toString()); + assertEquals("after isAnyInSlidingKeyInput element1", + Element.NOT_HAPPENED, mElement1.mPhantomUpEventTime); + assertEquals("after isAnyInSlidingKeyInput element2", + Element.NOT_HAPPENED, mElement2.mPhantomUpEventTime); + assertEquals("after isAnyInSlidingKeyInput element3", + Element.NOT_HAPPENED, mElement3.mPhantomUpEventTime); + assertEquals("after isAnyInSlidingKeyInput element4", + Element.NOT_HAPPENED, mElement4.mPhantomUpEventTime); + } +} diff --git a/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java b/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java new file mode 100644 index 000000000..123959b4d --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java @@ -0,0 +1,48 @@ +/* + * 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; + +import android.test.AndroidTestCase; + +import com.android.inputmethod.latin.makedict.FusionDictionary; +import com.android.inputmethod.latin.makedict.FusionDictionary.Node; + +import java.util.HashMap; + +/** + * Unit test for FusionDictionary + */ +public class FusionDictionaryTests extends AndroidTestCase { + public void testFindWordInTree() { + FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions(new HashMap<String,String>(), false, false)); + + dict.add("abc", 10, null, false /* isNotAWord */); + assertNull(FusionDictionary.findWordInTree(dict.mRoot, "aaa")); + assertNotNull(FusionDictionary.findWordInTree(dict.mRoot, "abc")); + + dict.add("aa", 10, null, false /* isNotAWord */); + assertNull(FusionDictionary.findWordInTree(dict.mRoot, "aaa")); + assertNotNull(FusionDictionary.findWordInTree(dict.mRoot, "aa")); + + dict.add("babcd", 10, null, false /* isNotAWord */); + dict.add("bacde", 10, null, false /* isNotAWord */); + assertNull(FusionDictionary.findWordInTree(dict.mRoot, "ba")); + assertNotNull(FusionDictionary.findWordInTree(dict.mRoot, "babcd")); + assertNotNull(FusionDictionary.findWordInTree(dict.mRoot, "bacde")); + } +} diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTests.java b/tests/src/com/android/inputmethod/latin/InputLogicTests.java index f1ccfdd1d..38e57aaed 100644 --- a/tests/src/com/android/inputmethod/latin/InputLogicTests.java +++ b/tests/src/com/android/inputmethod/latin/InputLogicTests.java @@ -126,6 +126,26 @@ public class InputLogicTests extends InputTestsBase { mTextView.getText().toString()); } + public void testAutoCorrectWithSpaceThenRevert() { + final String STRING_TO_TYPE = "tgis "; + 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); + assertEquals("auto-correct with space then revert", EXPECTED_RESULT, + mTextView.getText().toString()); + } + + public void testAutoCorrectToSelfDoesNotRevert() { + final String STRING_TO_TYPE = "this "; + 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); + assertEquals("auto-correct with space does not revert", EXPECTED_RESULT, + mTextView.getText().toString()); + } + public void testDoubleSpace() { final String STRING_TO_TYPE = "this "; final String EXPECTED_RESULT = "this. "; @@ -196,6 +216,19 @@ public class InputLogicTests extends InputTestsBase { assertEquals("manual pick then separator", EXPECTED_RESULT, mTextView.getText().toString()); } + public void testManualPickThenStripperThenPick() { + final String WORD_TO_TYPE = "this"; + final String STRIPPER = "\n"; + final String EXPECTED_RESULT = "this\nthis"; + type(WORD_TO_TYPE); + pickSuggestionManually(0, WORD_TO_TYPE); + type(STRIPPER); + type(WORD_TO_TYPE); + pickSuggestionManually(0, WORD_TO_TYPE); + assertEquals("manual pick then \\n then manual pick", EXPECTED_RESULT, + mTextView.getText().toString()); + } + public void testManualPickThenSpaceThenType() { final String WORD1_TO_TYPE = "this"; final String WORD2_TO_TYPE = " is"; diff --git a/tests/src/com/android/inputmethod/latin/InputPointersTests.java b/tests/src/com/android/inputmethod/latin/InputPointersTests.java new file mode 100644 index 000000000..cc55076c0 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/InputPointersTests.java @@ -0,0 +1,235 @@ +/* + * 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; + +import android.test.AndroidTestCase; + +import java.util.Arrays; + +public class InputPointersTests extends AndroidTestCase { + private static final int DEFAULT_CAPACITY = 48; + + public void testNewInstance() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + assertEquals("new instance size", 0, src.getPointerSize()); + assertNotNull("new instance xCoordinates", src.getXCoordinates()); + assertNotNull("new instance yCoordinates", src.getYCoordinates()); + assertNotNull("new instance pointerIds", src.getPointerIds()); + assertNotNull("new instance times", src.getTimes()); + } + + public void testReset() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int[] xCoordinates = src.getXCoordinates(); + final int[] yCoordinates = src.getXCoordinates(); + final int[] pointerIds = src.getXCoordinates(); + final int[] times = src.getXCoordinates(); + + src.reset(); + assertEquals("size after reset", 0, src.getPointerSize()); + assertNotSame("xCoordinates after reset", xCoordinates, src.getXCoordinates()); + assertNotSame("yCoordinates after reset", yCoordinates, src.getYCoordinates()); + assertNotSame("pointerIds after reset", pointerIds, src.getPointerIds()); + assertNotSame("times after reset", times, src.getTimes()); + } + + public void testAdd() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = src.getXCoordinates().length * 2 + 10; + for (int i = 0; i < limit; i++) { + src.addPointer(i, i * 2, i * 3, i * 4); + assertEquals("size after add " + i, i + 1, src.getPointerSize()); + } + for (int i = 0; i < limit; i++) { + assertEquals("xCoordinates at " + i, i, src.getXCoordinates()[i]); + assertEquals("yCoordinates at " + i, i * 2, src.getYCoordinates()[i]); + assertEquals("pointerIds at " + i, i * 3, src.getPointerIds()[i]); + assertEquals("times at " + i, i * 4, src.getTimes()[i]); + } + } + + public void testAddAt() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = 1000, step = 100; + for (int i = 0; i < limit; i += step) { + src.addPointer(i, i, i * 2, i * 3, i * 4); + assertEquals("size after add at " + i, i + 1, src.getPointerSize()); + } + for (int i = 0; i < limit; i += step) { + assertEquals("xCoordinates at " + i, i, src.getXCoordinates()[i]); + assertEquals("yCoordinates at " + i, i * 2, src.getYCoordinates()[i]); + assertEquals("pointerIds at " + i, i * 3, src.getPointerIds()[i]); + assertEquals("times at " + i, i * 4, src.getTimes()[i]); + } + } + + public void testSet() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = src.getXCoordinates().length * 2 + 10; + for (int i = 0; i < limit; i++) { + src.addPointer(i, i * 2, i * 3, i * 4); + } + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + dst.set(src); + assertEquals("size after set", dst.getPointerSize(), src.getPointerSize()); + assertSame("xCoordinates after set", dst.getXCoordinates(), src.getXCoordinates()); + assertSame("yCoordinates after set", dst.getYCoordinates(), src.getYCoordinates()); + assertSame("pointerIds after set", dst.getPointerIds(), src.getPointerIds()); + assertSame("times after set", dst.getTimes(), src.getTimes()); + } + + public void testCopy() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = 100; + for (int i = 0; i < limit; i++) { + src.addPointer(i, i * 2, i * 3, i * 4); + } + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + dst.copy(src); + assertEquals("size after copy", dst.getPointerSize(), src.getPointerSize()); + assertNotSame("xCoordinates after copy", dst.getXCoordinates(), src.getXCoordinates()); + assertNotSame("yCoordinates after copy", dst.getYCoordinates(), src.getYCoordinates()); + assertNotSame("pointerIds after copy", dst.getPointerIds(), src.getPointerIds()); + assertNotSame("times after copy", dst.getTimes(), src.getTimes()); + final int size = dst.getPointerSize(); + assertArrayEquals("xCoordinates values after copy", + dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size); + assertArrayEquals("yCoordinates values after copy", + dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size); + assertArrayEquals("pointerIds values after copy", + dst.getPointerIds(), 0, src.getPointerIds(), 0, size); + assertArrayEquals("times values after copy", + dst.getTimes(), 0, src.getTimes(), 0, size); + } + + public void testAppend() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int srcLen = 100; + for (int i = 0; i < srcLen; i++) { + src.addPointer(i, i * 2, i * 3, i * 4); + } + final int dstLen = 50; + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + for (int i = 0; i < dstLen; i++) { + final int value = -i - 1; + dst.addPointer(value * 4, value * 3, value * 2, value); + } + final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY); + dstCopy.copy(dst); + + dst.append(src, 0, 0); + assertEquals("size after append zero", dstLen, dst.getPointerSize()); + assertArrayEquals("xCoordinates after append zero", + dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); + assertArrayEquals("yCoordinates after append zero", + dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); + assertArrayEquals("pointerIds after append zero", + dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); + assertArrayEquals("times after append zero", + dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); + + dst.append(src, 0, srcLen); + assertEquals("size after append", dstLen + srcLen, dst.getPointerSize()); + assertTrue("primitive length after append", + dst.getPointerIds().length >= dstLen + srcLen); + assertArrayEquals("original xCoordinates values after append", + dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); + assertArrayEquals("original yCoordinates values after append", + dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); + assertArrayEquals("original pointerIds values after append", + dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); + assertArrayEquals("original times values after append", + dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); + assertArrayEquals("appended xCoordinates values after append", + src.getXCoordinates(), 0, dst.getXCoordinates(), dstLen, srcLen); + assertArrayEquals("appended yCoordinates values after append", + src.getYCoordinates(), 0, dst.getYCoordinates(), dstLen, srcLen); + assertArrayEquals("appended pointerIds values after append", + src.getPointerIds(), 0, dst.getPointerIds(), dstLen, srcLen); + assertArrayEquals("appended times values after append", + src.getTimes(), 0, dst.getTimes(), dstLen, srcLen); + } + + public void testAppendResizableIntArray() { + final int srcLen = 100; + final int srcPointerId = 1; + final int[] srcPointerIds = new int[srcLen]; + Arrays.fill(srcPointerIds, srcPointerId); + final ResizableIntArray srcTimes = new ResizableIntArray(DEFAULT_CAPACITY); + final ResizableIntArray srcXCoords = new ResizableIntArray(DEFAULT_CAPACITY); + final ResizableIntArray srcYCoords= new ResizableIntArray(DEFAULT_CAPACITY); + for (int i = 0; i < srcLen; i++) { + srcTimes.add(i * 2); + srcXCoords.add(i * 3); + srcYCoords.add(i * 4); + } + final int dstLen = 50; + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + for (int i = 0; i < dstLen; i++) { + final int value = -i - 1; + dst.addPointer(value * 4, value * 3, value * 2, value); + } + final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY); + dstCopy.copy(dst); + + dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0); + assertEquals("size after append zero", dstLen, dst.getPointerSize()); + assertArrayEquals("xCoordinates after append zero", + dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); + assertArrayEquals("yCoordinates after append zero", + dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); + assertArrayEquals("pointerIds after append zero", + dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); + assertArrayEquals("times after append zero", + dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); + + dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLen); + assertEquals("size after append", dstLen + srcLen, dst.getPointerSize()); + assertTrue("primitive length after append", + dst.getPointerIds().length >= dstLen + srcLen); + assertArrayEquals("original xCoordinates values after append", + dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); + assertArrayEquals("original yCoordinates values after append", + dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); + assertArrayEquals("original pointerIds values after append", + dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); + assertArrayEquals("original times values after append", + dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); + assertArrayEquals("appended xCoordinates values after append", + srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLen, srcLen); + assertArrayEquals("appended yCoordinates values after append", + srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLen, srcLen); + assertArrayEquals("appended pointerIds values after append", + srcPointerIds, 0, dst.getPointerIds(), dstLen, srcLen); + assertArrayEquals("appended times values after append", + srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLen, srcLen); + } + + private static void assertArrayEquals(String message, int[] expecteds, int expectedPos, + int[] actuals, int actualPos, int length) { + if (expecteds == null && actuals == null) { + return; + } + if (expecteds == null || actuals == null) { + fail(message + ": expecteds=" + expecteds + " actuals=" + actuals); + } + for (int i = 0; i < length; i++) { + assertEquals(message + ": element at " + i, + expecteds[i + expectedPos], actuals[i + actualPos]); + } + } +} diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java index eb47fd517..fe58cb84e 100644 --- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java @@ -39,9 +39,9 @@ import android.widget.TextView; import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.KeyboardActionListener; import java.util.HashMap; +import java.util.Locale; public class InputTestsBase extends ServiceTestCase<LatinIME> { @@ -52,7 +52,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { protected LatinIME mLatinIME; protected Keyboard mKeyboard; - protected TextView mTextView; + protected MyTextView mTextView; protected InputConnection mInputConnection; private final HashMap<String, InputMethodSubtype> mSubtypeMap = new HashMap<String, InputMethodSubtype>(); @@ -87,6 +87,27 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { return (mSpan instanceof SuggestionSpan) && 0 != (SuggestionSpan.FLAG_AUTO_CORRECTION & ((SuggestionSpan)mSpan).getFlags()); } + public String[] getSuggestions() { + return ((SuggestionSpan)mSpan).getSuggestions(); + } + } + + // A helper class to increase control over the TextView + public static class MyTextView extends TextView { + public Locale mCurrentLocale; + public MyTextView(final Context c) { + super(c); + } + public void onAttachedToWindow() { + super.onAttachedToWindow(); + } + public Locale getTextServicesLocale() { + // This method is necessary because TextView is asking this method for the language + // to check the spell in. If we don't override this, the spell checker will run in + // whatever language the keyboard is currently set on the test device, ignoring any + // settings we do inside the tests. + return mCurrentLocale; + } } public InputTestsBase() { @@ -113,7 +134,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { @Override protected void setUp() throws Exception { super.setUp(); - mTextView = new TextView(getContext()); + mTextView = new MyTextView(getContext()); mTextView.setInputType(InputType.TYPE_CLASS_TEXT); mTextView.setEnabled(true); setupService(); @@ -130,13 +151,12 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); final ViewGroup vg = new FrameLayout(getContext()); final View inputView = inflater.inflate(R.layout.input_view, vg); + mLatinIME.onCreateInputMethodInterface().startInput(ic, ei); mLatinIME.setInputView(inputView); mLatinIME.onBindInput(); mLatinIME.onCreateInputView(); mLatinIME.onStartInputView(ei, false); - mLatinIME.onCreateInputMethodInterface().startInput(ic, ei); mInputConnection = ic; - mKeyboard = mLatinIME.mKeyboardSwitcher.getKeyboard(); changeLanguage("en_US"); } @@ -222,9 +242,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { return; } } - mLatinIME.onCodeInput(codePoint, - KeyboardActionListener.NOT_A_TOUCH_COORDINATE, - KeyboardActionListener.NOT_A_TOUCH_COORDINATE); + mLatinIME.onCodeInput(codePoint, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); //mLatinIME.onReleaseKey(codePoint, false); } @@ -252,17 +270,18 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { protected void changeLanguage(final String locale) { final InputMethodSubtype subtype = mSubtypeMap.get(locale); + mTextView.mCurrentLocale = LocaleUtils.constructLocaleFromString(locale); if (subtype == null) { fail("InputMethodSubtype for locale " + locale + " is not enabled"); } SubtypeSwitcher.getInstance().updateSubtype(subtype); + mLatinIME.loadKeyboard(); + mKeyboard = mLatinIME.mKeyboardSwitcher.getKeyboard(); waitForDictionaryToBeLoaded(); } protected void pickSuggestionManually(final int index, final CharSequence suggestion) { - mLatinIME.pickSuggestionManually(index, suggestion, - KeyboardActionListener.NOT_A_TOUCH_COORDINATE, - KeyboardActionListener.NOT_A_TOUCH_COORDINATE); + mLatinIME.pickSuggestionManually(index, suggestion); } // Helper to avoid writing the try{}catch block each time diff --git a/tests/src/com/android/inputmethod/latin/PunctuationTests.java b/tests/src/com/android/inputmethod/latin/PunctuationTests.java index e1d4c46f8..0eb3ba41a 100644 --- a/tests/src/com/android/inputmethod/latin/PunctuationTests.java +++ b/tests/src/com/android/inputmethod/latin/PunctuationTests.java @@ -27,7 +27,7 @@ public class PunctuationTests extends InputTestsBase { final String PUNCTUATION_FROM_STRIP = "!"; final String EXPECTED_RESULT = "this!! "; final boolean defaultNextWordPredictionOption = - mLatinIME.getResources().getBoolean(R.bool.config_default_next_word_suggestions); + mLatinIME.getResources().getBoolean(R.bool.config_default_next_word_prediction); final boolean previousNextWordPredictionOption = setBooleanPreference(NEXT_WORD_PREDICTION_OPTION, false, defaultNextWordPredictionOption); diff --git a/tests/src/com/android/inputmethod/latin/ResizableIntArrayTests.java b/tests/src/com/android/inputmethod/latin/ResizableIntArrayTests.java new file mode 100644 index 000000000..995fc14ea --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/ResizableIntArrayTests.java @@ -0,0 +1,333 @@ +/* + * 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; + +import android.test.AndroidTestCase; + +public class ResizableIntArrayTests extends AndroidTestCase { + private static final int DEFAULT_CAPACITY = 48; + + public void testNewInstance() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = src.getPrimitiveArray(); + assertEquals("new instance length", 0, src.getLength()); + assertNotNull("new instance array", array); + assertEquals("new instance array length", DEFAULT_CAPACITY, array.length); + } + + public void testAdd() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = src.getPrimitiveArray(); + int[] array2 = null, array3 = null; + final int limit = DEFAULT_CAPACITY * 2 + 10; + for (int i = 0; i < limit; i++) { + src.add(i); + assertEquals("length after add " + i, i + 1, src.getLength()); + if (i == DEFAULT_CAPACITY) { + array2 = src.getPrimitiveArray(); + } + if (i == DEFAULT_CAPACITY * 2) { + array3 = src.getPrimitiveArray(); + } + if (i < DEFAULT_CAPACITY) { + assertSame("array after add " + i, array, src.getPrimitiveArray()); + } else if (i < DEFAULT_CAPACITY * 2) { + assertSame("array after add " + i, array2, src.getPrimitiveArray()); + } else if (i < DEFAULT_CAPACITY * 3) { + assertSame("array after add " + i, array3, src.getPrimitiveArray()); + } + } + for (int i = 0; i < limit; i++) { + assertEquals("value at " + i, i, src.get(i)); + } + } + + public void testAddAt() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2; + for (int i = 0; i < limit; i += step) { + src.add(i, i); + assertEquals("length after add at " + i, i + 1, src.getLength()); + } + for (int i = 0; i < limit; i += step) { + assertEquals("value at " + i, i, src.get(i)); + } + } + + public void testGet() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + try { + final int value = src.get(0); + fail("get(0) shouldn't succeed"); + } catch (ArrayIndexOutOfBoundsException e) { + // success + } + try { + final int value = src.get(DEFAULT_CAPACITY); + fail("get(DEFAULT_CAPACITY) shouldn't succeed"); + } catch (ArrayIndexOutOfBoundsException e) { + // success + } + + final int index = DEFAULT_CAPACITY / 2; + src.add(index, 100); + assertEquals("legth after add at " + index, index + 1, src.getLength()); + assertEquals("value after add at " + index, 100, src.get(index)); + assertEquals("value after add at 0", 0, src.get(0)); + try { + final int value = src.get(src.getLength()); + fail("get(length) shouldn't succeed"); + } catch (ArrayIndexOutOfBoundsException e) { + // success + } + } + + public void testReset() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = src.getPrimitiveArray(); + for (int i = 0; i < DEFAULT_CAPACITY; i++) { + src.add(i); + assertEquals("length after add " + i, i + 1, src.getLength()); + } + + final int smallerLength = DEFAULT_CAPACITY / 2; + src.reset(smallerLength); + final int[] array2 = src.getPrimitiveArray(); + assertEquals("length after reset", 0, src.getLength()); + assertNotSame("array after reset", array, array2); + + int[] array3 = null; + for (int i = 0; i < DEFAULT_CAPACITY; i++) { + src.add(i); + assertEquals("length after add " + i, i + 1, src.getLength()); + if (i == smallerLength) { + array3 = src.getPrimitiveArray(); + } + if (i < smallerLength) { + assertSame("array after add " + i, array2, src.getPrimitiveArray()); + } else if (i < smallerLength * 2) { + assertSame("array after add " + i, array3, src.getPrimitiveArray()); + } + } + } + + public void testSetLength() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = src.getPrimitiveArray(); + for (int i = 0; i < DEFAULT_CAPACITY; i++) { + src.add(i); + assertEquals("length after add " + i, i + 1, src.getLength()); + } + + final int largerLength = DEFAULT_CAPACITY * 2; + src.setLength(largerLength); + final int[] array2 = src.getPrimitiveArray(); + assertEquals("length after larger setLength", largerLength, src.getLength()); + assertNotSame("array after larger setLength", array, array2); + assertEquals("array length after larger setLength", largerLength, array2.length); + for (int i = 0; i < largerLength; i++) { + final int v = src.get(i); + if (i < DEFAULT_CAPACITY) { + assertEquals("value at " + i, i, v); + } else { + assertEquals("value at " + i, 0, v); + } + } + + final int smallerLength = DEFAULT_CAPACITY / 2; + src.setLength(smallerLength); + final int[] array3 = src.getPrimitiveArray(); + assertEquals("length after smaller setLength", smallerLength, src.getLength()); + assertSame("array after smaller setLength", array2, array3); + assertEquals("array length after smaller setLength", largerLength, array3.length); + for (int i = 0; i < smallerLength; i++) { + assertEquals("value at " + i, i, src.get(i)); + } + } + + public void testSet() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int limit = DEFAULT_CAPACITY * 2 + 10; + for (int i = 0; i < limit; i++) { + src.add(i); + } + + final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); + dst.set(src); + assertEquals("length after set", dst.getLength(), src.getLength()); + assertSame("array after set", dst.getPrimitiveArray(), src.getPrimitiveArray()); + } + + public void testCopy() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + for (int i = 0; i < DEFAULT_CAPACITY; i++) { + src.add(i); + } + + final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = dst.getPrimitiveArray(); + dst.copy(src); + assertEquals("length after copy", dst.getLength(), src.getLength()); + assertSame("array after copy", array, dst.getPrimitiveArray()); + assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray()); + assertArrayEquals("values after copy", + dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength()); + + final int smallerLength = DEFAULT_CAPACITY / 2; + dst.reset(smallerLength); + final int[] array2 = dst.getPrimitiveArray(); + dst.copy(src); + final int[] array3 = dst.getPrimitiveArray(); + assertEquals("length after copy to smaller", dst.getLength(), src.getLength()); + assertNotSame("array after copy to smaller", array2, array3); + assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray()); + assertArrayEquals("values after copy to smaller", + dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength()); + } + + public void testAppend() { + final int srcLen = DEFAULT_CAPACITY; + final ResizableIntArray src = new ResizableIntArray(srcLen); + for (int i = 0; i < srcLen; i++) { + src.add(i); + } + final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2); + final int[] array = dst.getPrimitiveArray(); + final int dstLen = DEFAULT_CAPACITY / 2; + for (int i = 0; i < dstLen; i++) { + final int value = -i - 1; + dst.add(value); + } + final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength()); + dstCopy.copy(dst); + + dst.append(src, 0, 0); + assertEquals("length after append zero", dstLen, dst.getLength()); + assertSame("array after append zero", array, dst.getPrimitiveArray()); + assertArrayEquals("values after append zero", + dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); + + dst.append(src, 0, srcLen); + assertEquals("length after append", dstLen + srcLen, dst.getLength()); + assertSame("array after append", array, dst.getPrimitiveArray()); + assertTrue("primitive length after append", + dst.getPrimitiveArray().length >= dstLen + srcLen); + assertArrayEquals("original values after append", + dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); + assertArrayEquals("appended values after append", + src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen); + + dst.append(src, 0, srcLen); + assertEquals("length after 2nd append", dstLen + srcLen * 2, dst.getLength()); + assertNotSame("array after 2nd append", array, dst.getPrimitiveArray()); + assertTrue("primitive length after 2nd append", + dst.getPrimitiveArray().length >= dstLen + srcLen * 2); + assertArrayEquals("original values after 2nd append", + dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); + assertArrayEquals("appended values after 2nd append", + src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen); + assertArrayEquals("appended values after 2nd append", + src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen + srcLen, srcLen); + } + + public void testFill() { + final int srcLen = DEFAULT_CAPACITY; + final ResizableIntArray src = new ResizableIntArray(srcLen); + for (int i = 0; i < srcLen; i++) { + src.add(i); + } + final int[] array = src.getPrimitiveArray(); + + final int startPos = srcLen / 3; + final int length = srcLen / 3; + final int endPos = startPos + length; + assertTrue(startPos >= 1); + final int value = 123; + try { + src.fill(value, -1, length); + fail("fill from -1 shouldn't succeed"); + } catch (IllegalArgumentException e) { + // success + } + try { + src.fill(value, startPos, -1); + fail("fill negative length shouldn't succeed"); + } catch (IllegalArgumentException e) { + // success + } + + src.fill(value, startPos, length); + assertEquals("length after fill", srcLen, src.getLength()); + assertSame("array after fill", array, src.getPrimitiveArray()); + for (int i = 0; i < srcLen; i++) { + final int v = src.get(i); + if (i >= startPos && i < endPos) { + assertEquals("new values after fill at " + i, value, v); + } else { + assertEquals("unmodified values after fill at " + i, i, v); + } + } + + final int length2 = srcLen * 2 - startPos; + final int largeEnd = startPos + length2; + assertTrue(largeEnd > srcLen); + final int value2 = 456; + src.fill(value2, startPos, length2); + assertEquals("length after large fill", largeEnd, src.getLength()); + assertNotSame("array after large fill", array, src.getPrimitiveArray()); + for (int i = 0; i < largeEnd; i++) { + final int v = src.get(i); + if (i >= startPos && i < largeEnd) { + assertEquals("new values after large fill at " + i, value2, v); + } else { + assertEquals("unmodified values after large fill at " + i, i, v); + } + } + + final int startPos2 = largeEnd + length2; + final int endPos2 = startPos2 + length2; + final int value3 = 789; + src.fill(value3, startPos2, length2); + assertEquals("length after disjoint fill", endPos2, src.getLength()); + for (int i = 0; i < endPos2; i++) { + final int v = src.get(i); + if (i >= startPos2 && i < endPos2) { + assertEquals("new values after disjoint fill at " + i, value3, v); + } else if (i >= startPos && i < largeEnd) { + assertEquals("unmodified values after disjoint fill at " + i, value2, v); + } else if (i < startPos) { + assertEquals("unmodified values after disjoint fill at " + i, i, v); + } else { + assertEquals("gap values after disjoint fill at " + i, 0, v); + } + } + } + + private static void assertArrayEquals(String message, int[] expecteds, int expectedPos, + int[] actuals, int actualPos, int length) { + if (expecteds == null && actuals == null) { + return; + } + if (expecteds == null || actuals == null) { + fail(message + ": expecteds=" + expecteds + " actuals=" + actuals); + } + for (int i = 0; i < length; i++) { + assertEquals(message + ": element at " + i, + expecteds[i + expectedPos], actuals[i + actualPos]); + } + } +} diff --git a/tests/src/com/android/inputmethod/latin/RichInputConnectionTests.java b/tests/src/com/android/inputmethod/latin/RichInputConnectionTests.java new file mode 100644 index 000000000..ad9937940 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/RichInputConnectionTests.java @@ -0,0 +1,186 @@ +/* + * 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. + */ + +package com.android.inputmethod.latin; + +import android.inputmethodservice.InputMethodService; +import android.test.AndroidTestCase; +import android.view.inputmethod.ExtractedText; +import android.view.inputmethod.ExtractedTextRequest; +import android.view.inputmethod.InputConnection; +import android.view.inputmethod.InputConnectionWrapper; + +import com.android.inputmethod.latin.RichInputConnection.Range; + +public class RichInputConnectionTests extends AndroidTestCase { + + // The following is meant to be a reasonable default for + // the "word_separators" resource. + private static final String sSeparators = ".,:;!?-"; + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + private class MockConnection extends InputConnectionWrapper { + final String mTextBefore; + final String mTextAfter; + final ExtractedText mExtractedText; + + public MockConnection(String textBefore, String textAfter, ExtractedText extractedText) { + super(null, false); + mTextBefore = textBefore; + mTextAfter = textAfter; + mExtractedText = extractedText; + } + + /* (non-Javadoc) + * @see android.view.inputmethod.InputConnectionWrapper#getTextBeforeCursor(int, int) + */ + @Override + public CharSequence getTextBeforeCursor(int n, int flags) { + return mTextBefore; + } + + /* (non-Javadoc) + * @see android.view.inputmethod.InputConnectionWrapper#getTextAfterCursor(int, int) + */ + @Override + public CharSequence getTextAfterCursor(int n, int flags) { + return mTextAfter; + } + + /* (non-Javadoc) + * @see android.view.inputmethod.InputConnectionWrapper#getExtractedText( + * ExtractedTextRequest, int) + */ + @Override + public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { + return mExtractedText; + } + + @Override + public boolean beginBatchEdit() { + return true; + } + + @Override + public boolean endBatchEdit() { + return true; + } + } + + private class MockInputMethodService extends InputMethodService { + InputConnection mInputConnection; + public void setInputConnection(final InputConnection inputConnection) { + mInputConnection = inputConnection; + } + @Override + public InputConnection getCurrentInputConnection() { + return mInputConnection; + } + } + + /************************** Tests ************************/ + + /** + * Test for getting previous word (for bigram suggestions) + */ + public void testGetPreviousWord() { + // If one of the following cases breaks, the bigram suggestions won't work. + assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSeparators, 2), "abc"); + assertNull(RichInputConnection.getNthPreviousWord("abc", sSeparators, 2)); + assertNull(RichInputConnection.getNthPreviousWord("abc. def", sSeparators, 2)); + + // The following tests reflect the current behavior of the function + // RichInputConnection#getNthPreviousWord. + // TODO: However at this time, the code does never go + // into such a path, so it should be safe to change the behavior of + // this function if needed - especially since it does not seem very + // logical. These tests are just there to catch any unintentional + // changes in the behavior of the RichInputConnection#getPreviousWord method. + assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSeparators, 2), "abc"); + assertEquals(RichInputConnection.getNthPreviousWord("abc def.", sSeparators, 2), "abc"); + assertEquals(RichInputConnection.getNthPreviousWord("abc def .", sSeparators, 2), "def"); + assertNull(RichInputConnection.getNthPreviousWord("abc ", sSeparators, 2)); + + assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSeparators, 1), "def"); + assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSeparators, 1), "def"); + assertNull(RichInputConnection.getNthPreviousWord("abc def.", sSeparators, 1)); + assertNull(RichInputConnection.getNthPreviousWord("abc def .", sSeparators, 1)); + } + + /** + * Test logic in getting the word range at the cursor. + */ + public void testGetWordRangeAtCursor() { + ExtractedText et = new ExtractedText(); + final MockInputMethodService mockInputMethodService = new MockInputMethodService(); + final RichInputConnection ic = new RichInputConnection(mockInputMethodService); + mockInputMethodService.setInputConnection(new MockConnection("word wo", "rd", et)); + et.startOffset = 0; + et.selectionStart = 7; + Range r; + + ic.beginBatchEdit(); + // basic case + r = ic.getWordRangeAtCursor(" ", 0); + assertEquals("word", r.mWord); + + // more than one word + r = ic.getWordRangeAtCursor(" ", 1); + assertEquals("word word", r.mWord); + ic.endBatchEdit(); + + // tab character instead of space + mockInputMethodService.setInputConnection(new MockConnection("one\tword\two", "rd", et)); + ic.beginBatchEdit(); + r = ic.getWordRangeAtCursor("\t", 1); + ic.endBatchEdit(); + assertEquals("word\tword", r.mWord); + + // only one word doesn't go too far + mockInputMethodService.setInputConnection(new MockConnection("one\tword\two", "rd", et)); + ic.beginBatchEdit(); + r = ic.getWordRangeAtCursor("\t", 1); + ic.endBatchEdit(); + assertEquals("word\tword", r.mWord); + + // tab or space + mockInputMethodService.setInputConnection(new MockConnection("one word\two", "rd", et)); + ic.beginBatchEdit(); + r = ic.getWordRangeAtCursor(" \t", 1); + ic.endBatchEdit(); + assertEquals("word\tword", r.mWord); + + // tab or space multiword + mockInputMethodService.setInputConnection(new MockConnection("one word\two", "rd", et)); + ic.beginBatchEdit(); + r = ic.getWordRangeAtCursor(" \t", 2); + ic.endBatchEdit(); + assertEquals("one word\tword", r.mWord); + + // splitting on supplementary character + final String supplementaryChar = "\uD840\uDC8A"; + mockInputMethodService.setInputConnection( + new MockConnection("one word" + supplementaryChar + "wo", "rd", et)); + ic.beginBatchEdit(); + r = ic.getWordRangeAtCursor(supplementaryChar, 0); + ic.endBatchEdit(); + assertEquals("word", r.mWord); + } +} diff --git a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java index 5db06ef51..be3494dc7 100644 --- a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java @@ -17,6 +17,9 @@ package com.android.inputmethod.latin; import android.test.AndroidTestCase; +import android.text.TextUtils; + +import java.util.Locale; public class StringUtilsTests extends AndroidTestCase { public void testContainsInArray() { @@ -89,14 +92,57 @@ public class StringUtilsTests extends AndroidTestCase { StringUtils.removeFromCsvIfExists("key", "key1,key,key3,key,key5")); } - public void testHasUpperCase() { - assertTrue("single upper-case string", StringUtils.hasUpperCase("String")); - assertTrue("multi upper-case string", StringUtils.hasUpperCase("stRInG")); - assertTrue("all upper-case string", StringUtils.hasUpperCase("STRING")); - assertTrue("upper-case string with non-letters", StringUtils.hasUpperCase("He's")); + private void onePathForCaps(final CharSequence cs, final int expectedResult, final int mask, + final Locale l, final boolean hasSpaceBefore) { + int oneTimeResult = expectedResult & mask; + assertEquals("After >" + cs + "<", oneTimeResult, + StringUtils.getCapsMode(cs, mask, l, hasSpaceBefore)); + } + + private void allPathsForCaps(final CharSequence cs, final int expectedResult, final Locale l, + final boolean hasSpaceBefore) { + final int c = TextUtils.CAP_MODE_CHARACTERS; + final int w = TextUtils.CAP_MODE_WORDS; + final int s = TextUtils.CAP_MODE_SENTENCES; + onePathForCaps(cs, expectedResult, c | w | s, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, w | s, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, c | s, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, c | w, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, c, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, w, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, s, l, hasSpaceBefore); + } + + public void testGetCapsMode() { + final int c = TextUtils.CAP_MODE_CHARACTERS; + final int w = TextUtils.CAP_MODE_WORDS; + final int s = TextUtils.CAP_MODE_SENTENCES; + Locale l = Locale.ENGLISH; + allPathsForCaps("", c | w | s, l, false); + allPathsForCaps("Word", c, l, false); + allPathsForCaps("Word.", c, l, false); + allPathsForCaps("Word ", c | w, l, false); + allPathsForCaps("Word. ", c | w | s, l, false); + allPathsForCaps("Word..", c, l, false); + allPathsForCaps("Word.. ", c | w | s, l, false); + allPathsForCaps("Word... ", c | w | s, l, false); + allPathsForCaps("Word ... ", c | w | s, l, false); + allPathsForCaps("Word . ", c | w, l, false); + allPathsForCaps("In the U.S ", c | w, l, false); + allPathsForCaps("In the U.S. ", c | w, l, false); + allPathsForCaps("Some stuff (e.g. ", c | w, l, false); + allPathsForCaps("In the U.S.. ", c | w | s, l, false); + allPathsForCaps("\"Word.\" ", c | w | s, l, false); + allPathsForCaps("\"Word\". ", c | w | s, l, false); + allPathsForCaps("\"Word\" ", c | w, l, false); + + // Test for phantom space + allPathsForCaps("Word", c | w, l, true); + allPathsForCaps("Word.", c | w | s, l, true); - assertFalse("empty string", StringUtils.hasUpperCase("")); - assertFalse("lower-case string", StringUtils.hasUpperCase("string")); - assertFalse("lower-case string with non-letters", StringUtils.hasUpperCase("he's")); + l = Locale.FRENCH; + allPathsForCaps("\"Word.\" ", c | w, l, false); + allPathsForCaps("\"Word\". ", c | w | s, l, false); + allPathsForCaps("\"Word\" ", c | w, l, false); } } diff --git a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java index c70c2fde5..52a3745fa 100644 --- a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java +++ b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java @@ -28,7 +28,7 @@ import java.util.Locale; public class SubtypeLocaleTests extends AndroidTestCase { // Locale to subtypes list. - private final ArrayList<InputMethodSubtype> mSubtypesList = new ArrayList<InputMethodSubtype>(); + private final ArrayList<InputMethodSubtype> mSubtypesList = CollectionUtils.newArrayList(); private Resources mRes; diff --git a/tests/src/com/android/inputmethod/latin/UserHistoryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/UserHistoryDictIOUtilsTests.java new file mode 100644 index 000000000..70f916c1a --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/UserHistoryDictIOUtilsTests.java @@ -0,0 +1,252 @@ +/* + * 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; + +import com.android.inputmethod.latin.UserHistoryDictIOUtils.BigramDictionaryInterface; +import com.android.inputmethod.latin.UserHistoryDictIOUtils.OnAddWordListener; +import com.android.inputmethod.latin.makedict.BinaryDictInputOutput; +import com.android.inputmethod.latin.makedict.FormatSpec; +import com.android.inputmethod.latin.makedict.FusionDictionary; +import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; + +import android.content.Context; +import android.test.AndroidTestCase; +import android.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + +/** + * Unit tests for UserHistoryDictIOUtils + */ +public class UserHistoryDictIOUtilsTests extends AndroidTestCase + implements BigramDictionaryInterface { + + private static final String TAG = UserHistoryDictIOUtilsTests.class.getSimpleName(); + private static final int UNIGRAM_FREQUENCY = 50; + private static final int BIGRAM_FREQUENCY = 100; + private static final ArrayList<String> NOT_HAVE_BIGRAM = new ArrayList<String>(); + private static final FormatSpec.FormatOptions FORMAT_OPTIONS = new FormatSpec.FormatOptions(2); + + /** + * Return same frequency for all words and bigrams + */ + @Override + public int getFrequency(String word1, String word2) { + if (word1 == null) return UNIGRAM_FREQUENCY; + return BIGRAM_FREQUENCY; + } + + // Utilities for Testing + + private void addWord(final String word, + final HashMap<String, ArrayList<String> > addedWords) { + if (!addedWords.containsKey(word)) { + addedWords.put(word, new ArrayList<String>()); + } + } + + private void addBigram(final String word1, final String word2, + final HashMap<String, ArrayList<String> > addedWords) { + addWord(word1, addedWords); + addWord(word2, addedWords); + addedWords.get(word1).add(word2); + } + + private void addBigramToBigramList(final String word1, final String word2, + final HashMap<String, ArrayList<String> > addedWords, + final UserHistoryDictionaryBigramList bigramList) { + bigramList.addBigram(null, word1); + bigramList.addBigram(word1, word2); + + addBigram(word1, word2, addedWords); + } + + private void checkWordInFusionDict(final FusionDictionary dict, final String word, + final ArrayList<String> expectedBigrams) { + final CharGroup group = FusionDictionary.findWordInTree(dict.mRoot, word); + assertNotNull(group); + assertTrue(group.isTerminal()); + + for (final String bigram : expectedBigrams) { + assertNotNull(group.getBigram(bigram)); + } + } + + private void checkWordsInFusionDict(final FusionDictionary dict, + final HashMap<String, ArrayList<String> > bigrams) { + for (final String word : bigrams.keySet()) { + if (bigrams.containsKey(word)) { + checkWordInFusionDict(dict, word, bigrams.get(word)); + } else { + checkWordInFusionDict(dict, word, NOT_HAVE_BIGRAM); + } + } + } + + private void checkWordInBigramList( + final UserHistoryDictionaryBigramList bigramList, final String word, + final ArrayList<String> expectedBigrams) { + // check unigram + final HashMap<String,Byte> unigramMap = bigramList.getBigrams(null); + assertTrue(unigramMap.containsKey(word)); + + // check bigrams + final ArrayList<String> actualBigrams = new ArrayList<String>( + bigramList.getBigrams(word).keySet()); + + Collections.sort(expectedBigrams); + Collections.sort(actualBigrams); + assertEquals(expectedBigrams, actualBigrams); + } + + private void checkWordsInBigramList(final UserHistoryDictionaryBigramList bigramList, + final HashMap<String, ArrayList<String> > addedWords) { + for (final String word : addedWords.keySet()) { + if (addedWords.containsKey(word)) { + checkWordInBigramList(bigramList, word, addedWords.get(word)); + } else { + checkWordInBigramList(bigramList, word, NOT_HAVE_BIGRAM); + } + } + } + + private void writeDictToFile(final File file, + final UserHistoryDictionaryBigramList bigramList) { + try { + final FileOutputStream out = new FileOutputStream(file); + UserHistoryDictIOUtils.writeDictionaryBinary(out, this, bigramList, FORMAT_OPTIONS); + out.flush(); + out.close(); + } catch (IOException e) { + Log.e(TAG, "IO exception while writing file: " + e); + } + } + + private void readDictFromFile(final File file, final OnAddWordListener listener) { + FileInputStream inStream = null; + + try { + inStream = new FileInputStream(file); + final byte[] buffer = new byte[(int)file.length()]; + inStream.read(buffer); + + UserHistoryDictIOUtils.readDictionaryBinary( + new UserHistoryDictIOUtils.ByteArrayWrapper(buffer), listener); + } catch (FileNotFoundException e) { + Log.e(TAG, "file not found: " + e); + } catch (IOException e) { + Log.e(TAG, "IOException: " + e); + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (IOException e) { + // do nothing + } + } + } + } + + public void testGenerateFusionDictionary() { + final UserHistoryDictionaryBigramList originalList = new UserHistoryDictionaryBigramList(); + + final HashMap<String, ArrayList<String> > addedWords = + new HashMap<String, ArrayList<String>>(); + addBigramToBigramList("this", "is", addedWords, originalList); + addBigramToBigramList("this", "was", addedWords, originalList); + addBigramToBigramList("hello", "world", addedWords, originalList); + + final FusionDictionary fusionDict = + UserHistoryDictIOUtils.constructFusionDictionary(this, originalList); + + checkWordsInFusionDict(fusionDict, addedWords); + } + + public void testReadAndWrite() { + final Context context = getContext(); + + File file = null; + try { + file = File.createTempFile("testReadAndWrite", ".dict"); + } catch (IOException e) { + Log.d(TAG, "IOException while creating a temporary file: " + e); + } + assertNotNull(file); + + // make original dictionary + final UserHistoryDictionaryBigramList originalList = new UserHistoryDictionaryBigramList(); + final HashMap<String, ArrayList<String>> addedWords = CollectionUtils.newHashMap(); + addBigramToBigramList("this" , "is" , addedWords, originalList); + addBigramToBigramList("this" , "was" , addedWords, originalList); + addBigramToBigramList("is" , "not" , addedWords, originalList); + addBigramToBigramList("hello", "world", addedWords, originalList); + + // write to file + writeDictToFile(file, originalList); + + // make result dict. + final UserHistoryDictionaryBigramList resultList = new UserHistoryDictionaryBigramList(); + final OnAddWordListener listener = new OnAddWordListener() { + @Override + public void setUnigram(final String word, + final String shortcutTarget, final int frequency) { + Log.d(TAG, "in: setUnigram: " + word + "," + frequency); + resultList.addBigram(null, word, (byte)frequency); + } + @Override + public void setBigram(final String word1, final String word2, final int frequency) { + Log.d(TAG, "in: setBigram: " + word1 + "," + word2 + "," + frequency); + resultList.addBigram(word1, word2, (byte)frequency); + } + }; + + // load from file + readDictFromFile(file, listener); + checkWordsInBigramList(resultList, addedWords); + + // add new bigram + addBigramToBigramList("hello", "java", addedWords, resultList); + + // rewrite + writeDictToFile(file, resultList); + final UserHistoryDictionaryBigramList resultList2 = new UserHistoryDictionaryBigramList(); + final OnAddWordListener listener2 = new OnAddWordListener() { + @Override + public void setUnigram(final String word, + final String shortcutTarget, final int frequency) { + Log.d(TAG, "in: setUnigram: " + word + "," + frequency); + resultList2.addBigram(null, word, (byte)frequency); + } + @Override + public void setBigram(final String word1, final String word2, final int frequency) { + Log.d(TAG, "in: setBigram: " + word1 + "," + word2 + "," + frequency); + resultList2.addBigram(word1, word2, (byte)frequency); + } + }; + + // load from file + readDictFromFile(file, listener2); + checkWordsInBigramList(resultList2, addedWords); + } +} diff --git a/tests/src/com/android/inputmethod/latin/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/UserHistoryDictionaryTests.java new file mode 100644 index 000000000..f2a17d206 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/UserHistoryDictionaryTests.java @@ -0,0 +1,109 @@ +/* + * 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; + +import com.android.inputmethod.latin.UserHistoryDictionary; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.test.AndroidTestCase; +import android.util.Log; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Set; + +/** + * Unit tests for UserHistoryDictionary + */ +public class UserHistoryDictionaryTests extends AndroidTestCase { + private static final String TAG = UserHistoryDictionaryTests.class.getSimpleName(); + private SharedPreferences mPrefs; + + 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" + }; + + @Override + public void setUp() { + mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + } + + /** + * Generates a random word. + */ + 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; + } + return builder.toString(); + } + + private List<String> generateWords(final int number, final Random random) { + final Set<String> wordSet = CollectionUtils.newHashSet(); + while (wordSet.size() < number) { + wordSet.add(generateWord(random.nextInt())); + } + return new ArrayList<String>(wordSet); + } + + private void addToDict(final UserHistoryDictionary dict, final List<String> words) { + String prevWord = null; + for (String word : words) { + dict.forceAddWordForTest(prevWord, word, true); + prevWord = word; + } + } + + 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(); + + try { + Log.d(TAG, "waiting for writing ..."); + Thread.sleep(5000); + } catch (InterruptedException e) { + Log.d(TAG, "InterruptedException: " + e); + } + } +} diff --git a/tests/src/com/android/inputmethod/latin/UtilsTests.java b/tests/src/com/android/inputmethod/latin/UtilsTests.java deleted file mode 100644 index 2ef4e2ff5..000000000 --- a/tests/src/com/android/inputmethod/latin/UtilsTests.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2010,2011 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; - -import android.test.AndroidTestCase; - -public class UtilsTests extends AndroidTestCase { - - // The following is meant to be a reasonable default for - // the "word_separators" resource. - private static final String sSeparators = ".,:;!?-"; - - @Override - protected void setUp() throws Exception { - super.setUp(); - } - - /************************** Tests ************************/ - - /** - * Test for getting previous word (for bigram suggestions) - */ - public void testGetPreviousWord() { - // If one of the following cases breaks, the bigram suggestions won't work. - assertEquals(EditingUtils.getPreviousWord("abc def", sSeparators), "abc"); - assertNull(EditingUtils.getPreviousWord("abc", sSeparators)); - assertNull(EditingUtils.getPreviousWord("abc. def", sSeparators)); - - // The following tests reflect the current behavior of the function - // EditingUtils#getPreviousWord. - // TODO: However at this time, the code does never go - // into such a path, so it should be safe to change the behavior of - // this function if needed - especially since it does not seem very - // logical. These tests are just there to catch any unintentional - // changes in the behavior of the EditingUtils#getPreviousWord method. - assertEquals(EditingUtils.getPreviousWord("abc def ", sSeparators), "abc"); - assertEquals(EditingUtils.getPreviousWord("abc def.", sSeparators), "abc"); - assertEquals(EditingUtils.getPreviousWord("abc def .", sSeparators), "def"); - assertNull(EditingUtils.getPreviousWord("abc ", sSeparators)); - } - - /** - * Test for getting the word before the cursor (for bigram) - */ - public void testGetThisWord() { - assertEquals(EditingUtils.getThisWord("abc def", sSeparators), "def"); - assertEquals(EditingUtils.getThisWord("abc def ", sSeparators), "def"); - assertNull(EditingUtils.getThisWord("abc def.", sSeparators)); - assertNull(EditingUtils.getThisWord("abc def .", sSeparators)); - } -} diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java new file mode 100644 index 000000000..2f954318c --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java @@ -0,0 +1,594 @@ +/* + * 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.UserHistoryDictIOUtils; +import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface; +import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader; +import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; +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 android.util.SparseArray; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Random; +import java.util.Set; + +/** + * Unit tests for BinaryDictInputOutput + */ +public class BinaryDictIOTests extends AndroidTestCase { + private static final String TAG = BinaryDictIOTests.class.getSimpleName(); + private static final int MAX_UNIGRAMS = 1000; + private static final int UNIGRAM_FREQ = 10; + private static final int BIGRAM_FREQ = 50; + private static final int TOLERANCE_OF_BIGRAM_FREQ = 5; + + private static final int USE_BYTE_ARRAY = 1; + private static final int USE_BYTE_BUFFER = 2; + + private static final List<String> sWords = CollectionUtils.newArrayList(); + private static final SparseArray<List<Integer>> sEmptyBigrams = + CollectionUtils.newSparseArray(); + private static final SparseArray<List<Integer>> sStarBigrams = CollectionUtils.newSparseArray(); + private static final SparseArray<List<Integer>> sChainBigrams = + CollectionUtils.newSparseArray(); + + private static final FormatSpec.FormatOptions VERSION2 = new FormatSpec.FormatOptions(2); + private static final FormatSpec.FormatOptions VERSION3_WITHOUT_DYNAMIC_UPDATE = + new FormatSpec.FormatOptions(3, false /* supportsDynamicUpdate */); + private static final FormatSpec.FormatOptions VERSION3_WITH_DYNAMIC_UPDATE = + new FormatSpec.FormatOptions(3, true /* supportsDynamicUpdate */); + + 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" + }; + + public BinaryDictIOTests() { + super(); + + final Random random = new Random(123456); + sWords.clear(); + generateWords(MAX_UNIGRAMS, random); + + for (int i = 0; i < sWords.size(); ++i) { + sChainBigrams.put(i, new ArrayList<Integer>()); + if (i > 0) { + sChainBigrams.get(i - 1).add(i); + } + } + + sStarBigrams.put(0, new ArrayList<Integer>()); + for (int i = 1; i < sWords.size(); ++i) { + sStarBigrams.get(0).add(i); + } + } + + // Utilities for test + + /** + * Makes new buffer according to BUFFER_TYPE. + */ + private FusionDictionaryBufferInterface getBuffer(final File file, final int bufferType) { + FileInputStream inStream = null; + try { + inStream = new FileInputStream(file); + if (bufferType == USE_BYTE_ARRAY) { + final byte[] array = new byte[(int)file.length()]; + inStream.read(array); + return new UserHistoryDictIOUtils.ByteArrayWrapper(array); + } else if (bufferType == USE_BYTE_BUFFER){ + final ByteBuffer buffer = inStream.getChannel().map( + FileChannel.MapMode.READ_ONLY, 0, file.length()); + return new BinaryDictInputOutput.ByteBufferWrapper(buffer); + } + } catch (IOException e) { + Log.e(TAG, "IOException while making buffer: " + e); + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (IOException e) { + Log.e(TAG, "IOException while closing stream: " + e); + } + } + } + return null; + } + + /** + * Generates a random word. + */ + private String generateWord(final int value) { + final int lengthOfChars = CHARACTERS.length; + StringBuilder builder = new StringBuilder("a"); + long lvalue = Math.abs((long)value); + while (lvalue > 0) { + builder.append(CHARACTERS[(int)(lvalue % lengthOfChars)]); + lvalue /= lengthOfChars; + } + return builder.toString(); + } + + private void generateWords(final int number, final Random random) { + final Set<String> wordSet = CollectionUtils.newHashSet(); + while (wordSet.size() < number) { + wordSet.add(generateWord(random.nextInt())); + } + sWords.addAll(wordSet); + } + + /** + * Adds unigrams to the dictionary. + */ + private void addUnigrams(final int number, final FusionDictionary dict, + final List<String> words, final Map<String, List<String>> shortcutMap) { + for (int i = 0; i < number; ++i) { + final String word = words.get(i); + final ArrayList<WeightedString> shortcuts = CollectionUtils.newArrayList(); + if (shortcutMap != null && shortcutMap.containsKey(word)) { + for (final String shortcut : shortcutMap.get(word)) { + shortcuts.add(new WeightedString(shortcut, UNIGRAM_FREQ)); + } + } + dict.add(word, UNIGRAM_FREQ, (shortcutMap == null) ? null : shortcuts, + false /* isNotAWord */); + } + } + + private void addBigrams(final FusionDictionary dict, + final List<String> words, + final SparseArray<List<Integer>> bigrams) { + for (int i = 0; i < bigrams.size(); ++i) { + final int w1 = bigrams.keyAt(i); + for (int w2 : bigrams.valueAt(i)) { + dict.setBigram(words.get(w1), words.get(w2), BIGRAM_FREQ); + } + } + } + + private long timeWritingDictToFile(final File file, final FusionDictionary dict, + final FormatSpec.FormatOptions formatOptions) { + + long now = -1, diff = -1; + + try { + final FileOutputStream out = new FileOutputStream(file); + + now = System.currentTimeMillis(); + BinaryDictInputOutput.writeDictionaryBinary(out, dict, formatOptions); + diff = System.currentTimeMillis() - now; + + out.flush(); + out.close(); + } catch (IOException e) { + Log.e(TAG, "IO exception while writing file: " + e); + } catch (UnsupportedFormatException e) { + Log.e(TAG, "UnsupportedFormatException: " + e); + } + + return diff; + } + + private void checkDictionary(final FusionDictionary dict, final List<String> words, + final SparseArray<List<Integer>> bigrams, final Map<String, List<String>> shortcutMap) { + assertNotNull(dict); + + // check unigram + for (final String word : words) { + final CharGroup cg = FusionDictionary.findWordInTree(dict.mRoot, word); + assertNotNull(cg); + } + + // check bigram + for (int i = 0; i < bigrams.size(); ++i) { + final int w1 = bigrams.keyAt(i); + for (final int w2 : bigrams.valueAt(i)) { + final CharGroup cg = FusionDictionary.findWordInTree(dict.mRoot, words.get(w1)); + assertNotNull(words.get(w1) + "," + words.get(w2), cg.getBigram(words.get(w2))); + } + } + + // check shortcut + if (shortcutMap != null) { + for (final Map.Entry<String, List<String>> entry : shortcutMap.entrySet()) { + final CharGroup group = FusionDictionary.findWordInTree(dict.mRoot, entry.getKey()); + for (final String word : entry.getValue()) { + assertNotNull("shortcut not found: " + entry.getKey() + ", " + word, + group.getShortcut(word)); + } + } + } + } + + private String outputOptions(final int bufferType, + final FormatSpec.FormatOptions formatOptions) { + String result = " : buffer type = " + + ((bufferType == USE_BYTE_BUFFER) ? "byte buffer" : "byte array"); + result += " : version = " + formatOptions.mVersion; + return result + ", supportsDynamicUpdate = " + formatOptions.mSupportsDynamicUpdate; + } + + // Tests for readDictionaryBinary and writeDictionaryBinary + + private long timeReadingAndCheckDict(final File file, final List<String> words, + final SparseArray<List<Integer>> bigrams, final Map<String, List<String>> shortcutMap, + final int bufferType) { + long now, diff = -1; + final FusionDictionaryBufferInterface buffer = getBuffer(file, bufferType); + assertNotNull(buffer); + + FusionDictionary dict = null; + try { + now = System.currentTimeMillis(); + dict = BinaryDictInputOutput.readDictionaryBinary(buffer, null); + diff = System.currentTimeMillis() - now; + } catch (IOException e) { + Log.e(TAG, "IOException while reading dictionary: " + e); + } catch (UnsupportedFormatException e) { + Log.e(TAG, "Unsupported format: " + e); + } + + checkDictionary(dict, words, bigrams, shortcutMap); + return diff; + } + + // Tests for readDictionaryBinary and writeDictionaryBinary + private String runReadAndWrite(final List<String> words, + final SparseArray<List<Integer>> bigrams, final Map<String, List<String>> shortcuts, + final int bufferType, final FormatSpec.FormatOptions formatOptions, + final String message) { + File file = null; + try { + file = File.createTempFile("runReadAndWrite", ".dict"); + } catch (IOException e) { + Log.e(TAG, "IOException: " + e); + } + assertNotNull(file); + + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions(new HashMap<String,String>(), false, false)); + addUnigrams(words.size(), dict, words, shortcuts); + addBigrams(dict, words, bigrams); + checkDictionary(dict, words, bigrams, shortcuts); + + final long write = timeWritingDictToFile(file, dict, formatOptions); + final long read = timeReadingAndCheckDict(file, words, bigrams, shortcuts, bufferType); + + return "PROF: read=" + read + "ms, write=" + write + "ms :" + message + + " : " + outputOptions(bufferType, formatOptions); + } + + private void runReadAndWriteTests(final List<String> results, final int bufferType, + final FormatSpec.FormatOptions formatOptions) { + results.add(runReadAndWrite(sWords, sEmptyBigrams, null /* shortcuts */, bufferType, + formatOptions, "unigram")); + results.add(runReadAndWrite(sWords, sChainBigrams, null /* shortcuts */, bufferType, + formatOptions, "chain")); + results.add(runReadAndWrite(sWords, sStarBigrams, null /* shortcuts */, bufferType, + formatOptions, "star")); + } + + public void testReadAndWriteWithByteBuffer() { + final List<String> results = CollectionUtils.newArrayList(); + + runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION2); + runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_DYNAMIC_UPDATE); + runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITH_DYNAMIC_UPDATE); + + for (final String result : results) { + Log.d(TAG, result); + } + } + + public void testReadAndWriteWithByteArray() { + final List<String> results = CollectionUtils.newArrayList(); + + runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION2); + runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_DYNAMIC_UPDATE); + runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITH_DYNAMIC_UPDATE); + + for (final String result : results) { + Log.d(TAG, result); + } + } + + // Tests for readUnigramsAndBigramsBinary + + private void checkWordMap(final List<String> expectedWords, + final SparseArray<List<Integer>> expectedBigrams, + final Map<Integer, String> resultWords, + final Map<Integer, Integer> resultFrequencies, + final Map<Integer, ArrayList<PendingAttribute>> resultBigrams) { + // check unigrams + final Set<String> actualWordsSet = new HashSet<String>(resultWords.values()); + final Set<String> expectedWordsSet = new HashSet<String>(expectedWords); + assertEquals(actualWordsSet, expectedWordsSet); + + for (int freq : resultFrequencies.values()) { + assertEquals(freq, UNIGRAM_FREQ); + } + + // check bigrams + final Map<String, List<String>> expBigrams = new HashMap<String, List<String>>(); + for (int i = 0; i < expectedBigrams.size(); ++i) { + final String word1 = expectedWords.get(expectedBigrams.keyAt(i)); + for (int w2 : expectedBigrams.valueAt(i)) { + if (expBigrams.get(word1) == null) { + expBigrams.put(word1, new ArrayList<String>()); + } + expBigrams.get(word1).add(expectedWords.get(w2)); + } + } + + final Map<String, List<String>> actBigrams = new HashMap<String, List<String>>(); + for (Entry<Integer, ArrayList<PendingAttribute>> entry : resultBigrams.entrySet()) { + final String word1 = resultWords.get(entry.getKey()); + final int unigramFreq = resultFrequencies.get(entry.getKey()); + for (PendingAttribute attr : entry.getValue()) { + final String word2 = resultWords.get(attr.mAddress); + if (actBigrams.get(word1) == null) { + actBigrams.put(word1, new ArrayList<String>()); + } + actBigrams.get(word1).add(word2); + + final int bigramFreq = BinaryDictInputOutput.reconstructBigramFrequency( + unigramFreq, attr.mFrequency); + assertTrue(Math.abs(bigramFreq - BIGRAM_FREQ) < TOLERANCE_OF_BIGRAM_FREQ); + } + } + + assertEquals(actBigrams, expBigrams); + } + + private long timeAndCheckReadUnigramsAndBigramsBinary(final File file, final List<String> words, + final SparseArray<List<Integer>> bigrams, final int bufferType) { + FileInputStream inStream = null; + + final Map<Integer, String> resultWords = CollectionUtils.newTreeMap(); + final Map<Integer, ArrayList<PendingAttribute>> resultBigrams = + CollectionUtils.newTreeMap(); + final Map<Integer, Integer> resultFreqs = CollectionUtils.newTreeMap(); + + long now = -1, diff = -1; + final FusionDictionaryBufferInterface buffer = getBuffer(file, bufferType); + assertNotNull("Can't get buffer.", buffer); + try { + now = System.currentTimeMillis(); + BinaryDictIOUtils.readUnigramsAndBigramsBinary(buffer, resultWords, resultFreqs, + resultBigrams); + diff = System.currentTimeMillis() - now; + } catch (IOException e) { + Log.e(TAG, "IOException " + e); + } catch (UnsupportedFormatException e) { + Log.e(TAG, "UnsupportedFormatException: " + e); + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (IOException e) { + // do nothing + } + } + } + + checkWordMap(words, bigrams, resultWords, resultFreqs, resultBigrams); + return diff; + } + + private String runReadUnigramsAndBigramsBinary(final List<String> words, + final SparseArray<List<Integer>> bigrams, final int bufferType, + final FormatSpec.FormatOptions formatOptions, final String message) { + File file = null; + try { + file = File.createTempFile("runReadUnigrams", ".dict"); + } catch (IOException e) { + Log.e(TAG, "IOException: " + e); + } + assertNotNull(file); + + // making the dictionary from lists of words. + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions( + new HashMap<String, String>(), false, false)); + addUnigrams(words.size(), dict, words, null /* shortcutMap */); + addBigrams(dict, words, bigrams); + + timeWritingDictToFile(file, dict, formatOptions); + + long wordMap = timeAndCheckReadUnigramsAndBigramsBinary(file, words, bigrams, bufferType); + long fullReading = timeReadingAndCheckDict(file, words, bigrams, null /* shortcutMap */, + bufferType); + + return "readDictionaryBinary=" + fullReading + ", readUnigramsAndBigramsBinary=" + wordMap + + " : " + message + " : " + outputOptions(bufferType, formatOptions); + } + + private void runReadUnigramsAndBigramsTests(final List<String> results, final int bufferType, + final FormatSpec.FormatOptions formatOptions) { + results.add(runReadUnigramsAndBigramsBinary(sWords, sEmptyBigrams, bufferType, + formatOptions, "unigram")); + results.add(runReadUnigramsAndBigramsBinary(sWords, sChainBigrams, bufferType, + formatOptions, "chain")); + results.add(runReadUnigramsAndBigramsBinary(sWords, sChainBigrams, bufferType, + formatOptions, "star")); + } + + public void testReadUnigramsAndBigramsBinaryWithByteBuffer() { + final List<String> results = CollectionUtils.newArrayList(); + + runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION2); + runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_DYNAMIC_UPDATE); + runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITH_DYNAMIC_UPDATE); + + for (final String result : results) { + Log.d(TAG, result); + } + } + + public void testReadUnigramsAndBigramsBinaryWithByteArray() { + final List<String> results = CollectionUtils.newArrayList(); + + runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION2); + runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_DYNAMIC_UPDATE); + runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITH_DYNAMIC_UPDATE); + + for (final String result : results) { + Log.d(TAG, result); + } + } + + // Tests for getTerminalPosition + private String getWordFromBinary(final FusionDictionaryBufferInterface buffer, + final int address) { + if (buffer.position() != 0) buffer.position(0); + + FileHeader header = null; + try { + header = BinaryDictInputOutput.readHeader(buffer); + } catch (IOException e) { + return null; + } catch (UnsupportedFormatException e) { + return null; + } + if (header == null) return null; + return BinaryDictInputOutput.getWordAtAddress(buffer, header.mHeaderSize, + address - header.mHeaderSize, header.mFormatOptions); + } + + private long runGetTerminalPosition(final FusionDictionaryBufferInterface buffer, + final String word, int index, boolean contained) { + final int expectedFrequency = (UNIGRAM_FREQ + index) % 255; + long diff = -1; + int position = -1; + try { + final long now = System.nanoTime(); + position = BinaryDictIOUtils.getTerminalPosition(buffer, word); + diff = System.nanoTime() - now; + } catch (IOException e) { + Log.e(TAG, "IOException while getTerminalPosition: " + e); + } catch (UnsupportedFormatException e) { + Log.e(TAG, "UnsupportedFormatException while getTermianlPosition: " + e); + } + + assertEquals(FormatSpec.NOT_VALID_WORD != position, contained); + if (contained) assertEquals(getWordFromBinary(buffer, position), word); + return diff; + } + + public void testGetTerminalPosition() { + File file = null; + try { + file = File.createTempFile("testGetTerminalPosition", ".dict"); + } catch (IOException e) { + // do nothing + } + assertNotNull(file); + + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions( + new HashMap<String, String>(), false, false)); + addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */); + timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE); + + final FusionDictionaryBufferInterface buffer = getBuffer(file, USE_BYTE_ARRAY); + + try { + // too long word + final String longWord = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; + assertEquals(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, longWord)); + + // null + assertEquals(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, null)); + + // empty string + assertEquals(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, "")); + } catch (IOException e) { + } catch (UnsupportedFormatException e) { + } + + // Test a word that is contained within the dictionary. + long sum = 0; + for (int i = 0; i < sWords.size(); ++i) { + final long time = runGetTerminalPosition(buffer, sWords.get(i), i, true); + sum += time == -1 ? 0 : time; + } + Log.d(TAG, "per a search : " + (((double)sum) / sWords.size() / 1000000)); + + // Test a word that isn't contained within the dictionary. + final Random random = new Random((int)System.currentTimeMillis()); + for (int i = 0; i < 1000; ++i) { + final String word = generateWord(random.nextInt()); + if (sWords.indexOf(word) != -1) continue; + runGetTerminalPosition(buffer, word, i, false); + } + } + + public void testDeleteWord() { + File file = null; + try { + file = File.createTempFile("testDeleteWord", ".dict"); + } catch (IOException e) { + // do nothing + } + assertNotNull(file); + + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions( + new HashMap<String, String>(), false, false)); + addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */); + timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE); + + final FusionDictionaryBufferInterface buffer = getBuffer(file, USE_BYTE_ARRAY); + + try { + MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(0))); + BinaryDictIOUtils.deleteWord(buffer, sWords.get(0)); + assertEquals(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(0))); + + MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(5))); + BinaryDictIOUtils.deleteWord(buffer, sWords.get(5)); + assertEquals(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(5))); + } catch (IOException e) { + } catch (UnsupportedFormatException e) { + } + } +} diff --git a/tests/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerServiceTest.java b/tests/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerServiceTest.java new file mode 100644 index 000000000..21406d370 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerServiceTest.java @@ -0,0 +1,63 @@ +/* + * 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.spellcheck; + +import android.text.SpannableStringBuilder; +import android.text.style.CharacterStyle; +import android.text.style.SuggestionSpan; + +import com.android.inputmethod.latin.InputTestsBase; + +public class AndroidSpellCheckerServiceTest extends InputTestsBase { + public void testSpellchecker() { + mTextView.onAttachedToWindow(); + mTextView.setText("tgis"); + type(" "); + sleep(1000); + runMessages(); + sleep(1000); + + final SpanGetter span = new SpanGetter(mTextView.getText(), SuggestionSpan.class); + // If no span, the following will crash + final String[] suggestions = span.getSuggestions(); + // For this test we consider "tgis" should yield at least 2 suggestions (at this moment + // it yields 5). + assertTrue(suggestions.length >= 2); + // We also assume the top suggestion should be "this". + assertEquals("", "this", suggestions[0]); + } + + public void testRussianSpellchecker() { + changeLanguage("ru"); + mTextView.onAttachedToWindow(); + mTextView.setText("годп"); + type(" "); + sleep(1000); + runMessages(); + sleep(1000); + + final SpanGetter span = new SpanGetter(mTextView.getText(), SuggestionSpan.class); + // If no span, the following will crash + final String[] suggestions = span.getSuggestions(); + // For this test we consider "годп" should yield at least 2 suggestions (at this moment + // it yields 5). + assertTrue(suggestions.length >= 2); + // We also assume the top suggestion should be "года", which is the top word in the + // Russian dictionary. + assertEquals("", "года", suggestions[0]); + } +} |