diff options
Diffstat (limited to 'tests/src/com/android/inputmethod/latin')
5 files changed, 85 insertions, 169 deletions
diff --git a/tests/src/com/android/inputmethod/latin/EventRingBufferTests.java b/tests/src/com/android/inputmethod/latin/EventRingBufferTests.java deleted file mode 100644 index 869781f3d..000000000 --- a/tests/src/com/android/inputmethod/latin/EventRingBufferTests.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (C) 2010 Google Inc. - * - * 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.keyboard.SwipeTracker.EventRingBuffer; - -import android.test.AndroidTestCase; - -public class EventRingBufferTests extends AndroidTestCase { - - @Override - protected void setUp() throws Exception { - super.setUp(); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - - private static float X_BASE = 1000f; - - private static float Y_BASE = 2000f; - - private static long TIME_BASE = 3000l; - - private static float x(int id) { - return X_BASE + id; - } - - private static float y(int id) { - return Y_BASE + id; - } - - private static long time(int id) { - return TIME_BASE + id; - } - - private static void addEvent(EventRingBuffer buf, int id) { - buf.add(x(id), y(id), time(id)); - } - - private static void assertEventSize(EventRingBuffer buf, int size) { - assertEquals(size, buf.size()); - } - - private static void assertEvent(EventRingBuffer buf, int pos, int id) { - assertEquals(x(id), buf.getX(pos), 0f); - assertEquals(y(id), buf.getY(pos), 0f); - assertEquals(time(id), buf.getTime(pos)); - } - - public void testClearBuffer() { - EventRingBuffer buf = new EventRingBuffer(4); - assertEventSize(buf, 0); - - addEvent(buf, 0); - addEvent(buf, 1); - addEvent(buf, 2); - addEvent(buf, 3); - addEvent(buf, 4); - assertEventSize(buf, 4); - - buf.clear(); - assertEventSize(buf, 0); - } - - public void testRingBuffer() { - EventRingBuffer buf = new EventRingBuffer(4); - assertEventSize(buf, 0); // [0] - - addEvent(buf, 0); - assertEventSize(buf, 1); // [1] 0 - assertEvent(buf, 0, 0); - - addEvent(buf, 1); - addEvent(buf, 2); - assertEventSize(buf, 3); // [3] 2 1 0 - assertEvent(buf, 0, 0); - assertEvent(buf, 1, 1); - assertEvent(buf, 2, 2); - - addEvent(buf, 3); - assertEventSize(buf, 4); // [4] 3 2 1 0 - assertEvent(buf, 0, 0); - assertEvent(buf, 1, 1); - assertEvent(buf, 2, 2); - assertEvent(buf, 3, 3); - - addEvent(buf, 4); - addEvent(buf, 5); - assertEventSize(buf, 4); // [4] 5 4|3 2(1 0) - assertEvent(buf, 0, 2); - assertEvent(buf, 1, 3); - assertEvent(buf, 2, 4); - assertEvent(buf, 3, 5); - - addEvent(buf, 6); - addEvent(buf, 7); - addEvent(buf, 8); - assertEventSize(buf, 4); // [4] 8 7 6 5|(4 3 2)1|0 - assertEvent(buf, 0, 5); - assertEvent(buf, 1, 6); - assertEvent(buf, 2, 7); - assertEvent(buf, 3, 8); - } - - public void testDropOldest() { - EventRingBuffer buf = new EventRingBuffer(4); - - addEvent(buf, 0); - assertEventSize(buf, 1); // [1] 0 - assertEvent(buf, 0, 0); - - buf.dropOldest(); - assertEventSize(buf, 0); // [0] (0) - - addEvent(buf, 1); - addEvent(buf, 2); - addEvent(buf, 3); - addEvent(buf, 4); - assertEventSize(buf, 4); // [4] 4|3 2 1(0) - assertEvent(buf, 0, 1); - - buf.dropOldest(); - assertEventSize(buf, 3); // [3] 4|3 2(1)0 - assertEvent(buf, 0, 2); - - buf.dropOldest(); - assertEventSize(buf, 2); // [2] 4|3(2)10 - assertEvent(buf, 0, 3); - - buf.dropOldest(); - assertEventSize(buf, 1); // [1] 4|(3)210 - assertEvent(buf, 0, 4); - - buf.dropOldest(); - assertEventSize(buf, 0); // [0] (4)|3210 - } -} diff --git a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java index d128cb3fa..d102aa4d1 100644 --- a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java +++ b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java @@ -16,6 +16,8 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.latin.Utils; + import android.content.Context; import android.content.res.Resources; import android.test.AndroidTestCase; @@ -75,7 +77,7 @@ public class SubtypeLocaleTests extends AndroidTestCase { int failedCount = 0; for (final InputMethodSubtype subtype : mKeyboardSubtypes) { final String localeCode = subtype.getLocale(); - final Locale locale = new Locale(localeCode); + final Locale locale = Utils.constructLocaleFromString(localeCode); // The locale name which will be displayed on spacebar. For example 'English (US)' or // 'Francais (Canada)'. (c=\u008d) final String displayName = SubtypeLocale.getFullDisplayName(locale); diff --git a/tests/src/com/android/inputmethod/latin/SuggestHelper.java b/tests/src/com/android/inputmethod/latin/SuggestHelper.java index 5930ea36e..07d0e5d75 100644 --- a/tests/src/com/android/inputmethod/latin/SuggestHelper.java +++ b/tests/src/com/android/inputmethod/latin/SuggestHelper.java @@ -20,7 +20,6 @@ import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.KeyDetector; import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.keyboard.LatinKeyboard; -import com.android.inputmethod.keyboard.ProximityKeyDetector; import android.content.Context; import android.text.TextUtils; @@ -34,17 +33,19 @@ public class SuggestHelper { private final KeyDetector mKeyDetector; public SuggestHelper(Context context, int dictionaryId, KeyboardId keyboardId) { - mSuggest = new Suggest(context, dictionaryId); - mKeyboard = new LatinKeyboard(context, keyboardId); - mKeyDetector = new ProximityKeyDetector(); + // Use null as the locale for Suggest so as to force it to use the internal dictionary + // (and not try to find a dictionary provider for a specified locale) + mSuggest = new Suggest(context, dictionaryId, null); + mKeyboard = new LatinKeyboard(context, keyboardId, keyboardId.mWidth); + mKeyDetector = new KeyDetector(0); init(); } protected SuggestHelper(Context context, File dictionaryPath, long startOffset, long length, KeyboardId keyboardId) { - mSuggest = new Suggest(dictionaryPath, startOffset, length); - mKeyboard = new LatinKeyboard(context, keyboardId); - mKeyDetector = new ProximityKeyDetector(); + mSuggest = new Suggest(context, dictionaryPath, startOffset, length, null); + mKeyboard = new LatinKeyboard(context, keyboardId, keyboardId.mWidth); + mKeyDetector = new KeyDetector(0); init(); } @@ -53,7 +54,7 @@ public class SuggestHelper { mSuggest.setCorrectionMode(Suggest.CORRECTION_FULL); mKeyDetector.setKeyboard(mKeyboard, 0, 0); mKeyDetector.setProximityCorrectionEnabled(true); - mKeyDetector.setProximityThreshold(KeyDetector.getMostCommonKeyWidth(mKeyboard)); + mKeyDetector.setProximityThreshold(mKeyboard.getMostCommonKeyWidth()); } public void setCorrectionMode(int correctionMode) { diff --git a/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java b/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java index 48d169f6a..8aadee42e 100644 --- a/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java @@ -16,15 +16,14 @@ package com.android.inputmethod.latin; -import com.android.inputmethod.keyboard.KeyboardId; -import com.android.inputmethod.keyboard.KeyboardView; - import android.content.res.AssetFileDescriptor; import android.content.res.Configuration; import android.test.AndroidTestCase; import android.text.TextUtils; import android.view.inputmethod.EditorInfo; +import com.android.inputmethod.keyboard.KeyboardId; + import java.io.File; import java.io.InputStream; import java.util.Locale; @@ -38,10 +37,11 @@ public class SuggestTestsBase extends AndroidTestCase { mTestPackageFile = new File(getTestContext().getApplicationInfo().sourceDir); } - protected static KeyboardId createKeyboardId(Locale locale) { + protected KeyboardId createKeyboardId(Locale locale) { + final int displayWidth = getContext().getResources().getDisplayMetrics().widthPixels; return new KeyboardId(locale.toString() + " keyboard", - com.android.inputmethod.latin.R.xml.kbd_qwerty, KeyboardView.COLOR_SCHEME_WHITE, - locale, Configuration.ORIENTATION_LANDSCAPE, KeyboardId.MODE_TEXT, + com.android.inputmethod.latin.R.xml.kbd_qwerty, locale, + Configuration.ORIENTATION_LANDSCAPE, displayWidth, KeyboardId.MODE_TEXT, new EditorInfo(), false, KeyboardId.F2KEY_MODE_NONE, false, false, false, false); } diff --git a/tests/src/com/android/inputmethod/latin/UtilsTests.java b/tests/src/com/android/inputmethod/latin/UtilsTests.java new file mode 100644 index 000000000..5c0b03a0a --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/UtilsTests.java @@ -0,0 +1,67 @@ +/* + * 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; + +import com.android.inputmethod.latin.tests.R; + +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)); + } +} |