diff options
Diffstat (limited to 'tests')
7 files changed, 387 insertions, 71 deletions
diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java index fa067f4c8..3dc4543c2 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java @@ -16,25 +16,27 @@ package com.android.inputmethod.keyboard.internal; -import android.test.AndroidTestCase; +import android.app.Instrumentation; +import android.test.InstrumentationTestCase; 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); } diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTests.java b/tests/src/com/android/inputmethod/latin/InputLogicTests.java index f1ccfdd1d..7790299b0 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. "; 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..b60c2df58 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/InputPointersTests.java @@ -0,0 +1,173 @@ +/* + * 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 InputPointersTests extends AndroidTestCase { + public void testNewInstance() { + final InputPointers src = new InputPointers(); + assertEquals("newInstance 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(); + final int[] xCoordinates = src.getXCoordinates(); + final int[] yCoordinates = src.getXCoordinates(); + final int[] pointerIds = src.getXCoordinates(); + final int[] times = src.getXCoordinates(); + + src.reset(); + assertEquals("after reset size", 0, src.getPointerSize()); + assertNotSame("after reset xCoordinates", xCoordinates, src.getXCoordinates()); + assertNotSame("after reset yCoordinates", yCoordinates, src.getYCoordinates()); + assertNotSame("after reset pointerIds", pointerIds, src.getPointerIds()); + assertNotSame("after reset times", times, src.getTimes()); + } + + public void testAdd() { + final InputPointers src = new InputPointers(); + 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("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(); + 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("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(); + 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(); + dst.set(src); + assertEquals("after set size", dst.getPointerSize(), src.getPointerSize()); + assertSame("after set xCoordinates", dst.getXCoordinates(), src.getXCoordinates()); + assertSame("after set yCoordinates", dst.getYCoordinates(), src.getYCoordinates()); + assertSame("after set pointerIds", dst.getPointerIds(), src.getPointerIds()); + assertSame("after set times", dst.getTimes(), src.getTimes()); + } + + public void testCopy() { + final InputPointers src = new InputPointers(); + 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(); + dst.copy(src); + assertEquals("after copy size", dst.getPointerSize(), src.getPointerSize()); + assertNotSame("after copy xCoordinates", dst.getXCoordinates(), src.getXCoordinates()); + assertNotSame("after copy yCoordinates", dst.getYCoordinates(), src.getYCoordinates()); + assertNotSame("after copy pointerIds", dst.getPointerIds(), src.getPointerIds()); + assertNotSame("after copy times", dst.getTimes(), src.getTimes()); + final int size = dst.getPointerSize(); + assertArrayEquals("after copy xCoordinates values", + dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size); + assertArrayEquals("after copy yCoordinates values", + dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size); + assertArrayEquals("after copy pointerIds values", + dst.getPointerIds(), 0, src.getPointerIds(), 0, size); + assertArrayEquals("after copy times values", + dst.getTimes(), 0, src.getTimes(), 0, size); + } + + public void testAppend() { + final InputPointers src = new InputPointers(); + 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(); + for (int i = 0; i < limit; i++) { + final int value = limit - i; + dst.addPointer(value * 4, value * 3, value * 2, value); + } + final InputPointers dstCopy = new InputPointers(); + dstCopy.copy(dst); + + dst.append(src, 0, 0); + assertEquals("after append zero size", limit, dst.getPointerSize()); + assertArrayEquals("affer append zero xCoordinates", dstCopy.getXCoordinates(), 0, + dst.getXCoordinates(), 0, limit); + assertArrayEquals("affer append zero yCoordinates", dstCopy.getYCoordinates(), 0, + dst.getYCoordinates(), 0, limit); + assertArrayEquals("affer append zero pointerIds", dstCopy.getPointerIds(), 0, + dst.getPointerIds(), 0, limit); + assertArrayEquals("affer append zero times", dstCopy.getTimes(), 0, + dst.getTimes(), 0, limit); + + dst.append(src, 0, src.getPointerSize()); + assertEquals("after append size", limit * 2, dst.getPointerSize() + src.getPointerSize()); + assertArrayEquals("affer append xCoordinates", dstCopy.getXCoordinates(), 0, + dst.getXCoordinates(), 0, limit); + assertArrayEquals("affer append yCoordinates", dstCopy.getYCoordinates(), 0, + dst.getYCoordinates(), 0, limit); + assertArrayEquals("affer append pointerIds", dstCopy.getPointerIds(), 0, + dst.getPointerIds(), 0, limit); + assertArrayEquals("affer append times", dstCopy.getTimes(), 0, + dst.getTimes(), 0, limit); + assertArrayEquals("after append xCoordinates", dst.getXCoordinates(), limit, + src.getXCoordinates(), 0, limit); + assertArrayEquals("after append yCoordinates", dst.getYCoordinates(), limit, + src.getYCoordinates(), 0, limit); + assertArrayEquals("after append pointerIds", dst.getPointerIds(), limit, + src.getPointerIds(), 0, limit); + assertArrayEquals("after append times", dst.getTimes(), limit, + src.getTimes(), 0, limit); + } + + 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..c672d5126 100644 --- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java @@ -130,11 +130,11 @@ 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"); 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/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/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)); - } -} |