aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/inputmethod/latin
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src/com/android/inputmethod/latin')
-rw-r--r--tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java8
-rw-r--r--tests/src/com/android/inputmethod/latin/WordComposerTests.java93
-rw-r--r--tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java69
-rw-r--r--tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java15
4 files changed, 152 insertions, 33 deletions
diff --git a/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java b/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java
index 0e077bbbe..c0dd9933c 100644
--- a/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java
+++ b/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java
@@ -16,6 +16,8 @@
package com.android.inputmethod.latin;
+import com.android.inputmethod.latin.utils.TextRange;
+
import android.inputmethodservice.InputMethodService;
import android.os.Parcel;
import android.test.AndroidTestCase;
@@ -30,8 +32,6 @@ import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
-import com.android.inputmethod.latin.RichInputConnection.Range;
-
import java.util.Locale;
@SmallTest
@@ -169,7 +169,7 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
mockInputMethodService.setInputConnection(new MockConnection("word wo", "rd", et));
et.startOffset = 0;
et.selectionStart = 7;
- Range r;
+ TextRange r;
ic.beginBatchEdit();
// basic case
@@ -241,7 +241,7 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS1, 0 /* flags */),
10 /* start */, 16 /* end */, 0 /* flags */);
mockInputMethodService.setInputConnection(new MockConnection(text, cursorPos));
- Range r;
+ TextRange r;
SuggestionSpan[] suggestions;
r = ic.getWordRangeAtCursor(" ", 0);
diff --git a/tests/src/com/android/inputmethod/latin/WordComposerTests.java b/tests/src/com/android/inputmethod/latin/WordComposerTests.java
new file mode 100644
index 000000000..1434c6b63
--- /dev/null
+++ b/tests/src/com/android/inputmethod/latin/WordComposerTests.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2013 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 android.test.suitebuilder.annotation.SmallTest;
+
+/**
+ * Unit tests for WordComposer.
+ */
+@SmallTest
+public class WordComposerTests extends AndroidTestCase {
+ public void testMoveCursor() {
+ final WordComposer wc = new WordComposer();
+ final String STR_WITHIN_BMP = "abcdef";
+ wc.setComposingWord(STR_WITHIN_BMP, null);
+ assertEquals(wc.size(),
+ STR_WITHIN_BMP.codePointCount(0, STR_WITHIN_BMP.length()));
+ assertFalse(wc.isCursorFrontOrMiddleOfComposingWord());
+ wc.setCursorPositionWithinWord(2);
+ assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
+ // Move the cursor to after the 'd'
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(2));
+ assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
+ // Move the cursor to after the 'e'
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(1));
+ assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
+ assertEquals(wc.size(), 6);
+ // Move the cursor to after the 'f'
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(1));
+ assertFalse(wc.isCursorFrontOrMiddleOfComposingWord());
+ // Move the cursor past the end of the word
+ assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(1));
+ assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(15));
+
+ // \uD861\uDED7 is 𨛗, a character outside the BMP
+ final String STR_WITH_SUPPLEMENTARY_CHAR = "abcde\uD861\uDED7fgh";
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ assertEquals(wc.size(), STR_WITH_SUPPLEMENTARY_CHAR.codePointCount(0,
+ STR_WITH_SUPPLEMENTARY_CHAR.length()));
+ assertFalse(wc.isCursorFrontOrMiddleOfComposingWord());
+ wc.setCursorPositionWithinWord(3);
+ assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(6));
+ assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(1));
+ assertFalse(wc.isCursorFrontOrMiddleOfComposingWord());
+
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ wc.setCursorPositionWithinWord(3);
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(7));
+
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ wc.setCursorPositionWithinWord(3);
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(7));
+
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ wc.setCursorPositionWithinWord(3);
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(-3));
+ assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(-1));
+
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ wc.setCursorPositionWithinWord(3);
+ assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(-9));
+
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(-10));
+
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(-11));
+
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(0));
+
+ wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
+ wc.setCursorPositionWithinWord(2);
+ assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(0));
+ }
+}
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java
index cca81a0e0..55f163255 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java
@@ -51,7 +51,8 @@ import java.util.Set;
@LargeTest
public class BinaryDictIOTests extends AndroidTestCase {
private static final String TAG = BinaryDictIOTests.class.getSimpleName();
- private static final int MAX_UNIGRAMS = 100;
+ private static final int DEFAULT_MAX_UNIGRAMS = 100;
+ private static final int DEFAULT_CODE_POINT_SET_SIZE = 50;
private static final int UNIGRAM_FREQ = 10;
private static final int BIGRAM_FREQ = 50;
private static final int TOLERANCE_OF_BIGRAM_FREQ = 5;
@@ -73,13 +74,16 @@ public class BinaryDictIOTests extends AndroidTestCase {
new FormatSpec.FormatOptions(3, true /* supportsDynamicUpdate */);
public BinaryDictIOTests() {
- super();
+ this(System.currentTimeMillis(), DEFAULT_MAX_UNIGRAMS);
+ }
- final long time = System.currentTimeMillis();
- Log.e(TAG, "Testing dictionary: seed is " + time);
- final Random random = new Random(time);
+ public BinaryDictIOTests(final long seed, final int maxUnigrams) {
+ super();
+ Log.e(TAG, "Testing dictionary: seed is " + seed);
+ final Random random = new Random(seed);
sWords.clear();
- generateWords(MAX_UNIGRAMS, random);
+ final int[] codePointSet = generateCodePointSet(DEFAULT_CODE_POINT_SET_SIZE, random);
+ generateWords(maxUnigrams, random, codePointSet);
for (int i = 0; i < sWords.size(); ++i) {
sChainBigrams.put(i, new ArrayList<Integer>());
@@ -94,6 +98,23 @@ public class BinaryDictIOTests extends AndroidTestCase {
}
}
+ private int[] generateCodePointSet(final int codePointSetSize, final Random random) {
+ final int[] codePointSet = new int[codePointSetSize];
+ for (int i = codePointSet.length - 1; i >= 0; ) {
+ final int r = Math.abs(random.nextInt());
+ if (r < 0) continue;
+ // Don't insert 0~0x20, but insert any other code point.
+ // Code points are in the range 0~0x10FFFF.
+ final int candidateCodePoint = (int)(0x20 + r % (Character.MAX_CODE_POINT - 0x20));
+ // Code points between MIN_ and MAX_SURROGATE are not valid on their own.
+ if (candidateCodePoint >= Character.MIN_SURROGATE
+ && candidateCodePoint <= Character.MAX_SURROGATE) continue;
+ codePointSet[i] = candidateCodePoint;
+ --i;
+ }
+ return codePointSet;
+ }
+
// Utilities for test
/**
@@ -129,28 +150,29 @@ public class BinaryDictIOTests extends AndroidTestCase {
/**
* Generates a random word.
*/
- private String generateWord(final Random random) {
- StringBuilder builder = new StringBuilder("a");
- int count = random.nextInt() % 30; // Arbitrarily 30 chars max
- while (count > 0) {
- final long r = Math.abs(random.nextInt());
- if (r < 0) continue;
- // Don't insert 0~0x20, but insert any other code point.
- // Code points are in the range 0~0x10FFFF.
- final int candidateCodePoint = (int)(0x20 + r % (Character.MAX_CODE_POINT - 0x20));
- // Code points between MIN_ and MAX_SURROGATE are not valid on their own.
- if (candidateCodePoint >= Character.MIN_SURROGATE
- && candidateCodePoint <= Character.MAX_SURROGATE) continue;
- builder.appendCodePoint(candidateCodePoint);
- --count;
+ private String generateWord(final Random random, final int[] codePointSet) {
+ StringBuilder builder = new StringBuilder();
+ // 8 * 4 = 32 chars max, but we do it the following way so as to bias the random toward
+ // longer words. This should be closer to natural language, and more importantly, it will
+ // exercise the algorithms in dicttool much more.
+ final int count = 1 + (Math.abs(random.nextInt()) % 5)
+ + (Math.abs(random.nextInt()) % 5)
+ + (Math.abs(random.nextInt()) % 5)
+ + (Math.abs(random.nextInt()) % 5)
+ + (Math.abs(random.nextInt()) % 5)
+ + (Math.abs(random.nextInt()) % 5)
+ + (Math.abs(random.nextInt()) % 5)
+ + (Math.abs(random.nextInt()) % 5);
+ while (builder.length() < count) {
+ builder.appendCodePoint(codePointSet[Math.abs(random.nextInt()) % codePointSet.length]);
}
return builder.toString();
}
- private void generateWords(final int number, final Random random) {
+ private void generateWords(final int number, final Random random, final int[] codePointSet) {
final Set<String> wordSet = CollectionUtils.newHashSet();
while (wordSet.size() < number) {
- wordSet.add(generateWord(random));
+ wordSet.add(generateWord(random, codePointSet));
}
sWords.addAll(wordSet);
}
@@ -558,8 +580,9 @@ public class BinaryDictIOTests extends AndroidTestCase {
// Test a word that isn't contained within the dictionary.
final Random random = new Random((int)System.currentTimeMillis());
+ final int[] codePointSet = generateCodePointSet(DEFAULT_CODE_POINT_SET_SIZE, random);
for (int i = 0; i < 1000; ++i) {
- final String word = generateWord(random);
+ final String word = generateWord(random, codePointSet);
if (sWords.indexOf(word) != -1) continue;
runGetTerminalPosition(buffer, word, i, false);
}
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
index d33142ce0..9331da44b 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
@@ -44,9 +44,10 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
private static final String TAG = BinaryDictIOUtilsTests.class.getSimpleName();
private static final FormatSpec.FormatOptions FORMAT_OPTIONS =
new FormatSpec.FormatOptions(3, true);
- private static final int MAX_UNIGRAMS = 1500;
private static final ArrayList<String> sWords = CollectionUtils.newArrayList();
+ public static final int DEFAULT_MAX_UNIGRAMS = 1500;
+ private final int mMaxUnigrams;
private static final String[] CHARACTERS = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
@@ -57,15 +58,17 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
};
public BinaryDictIOUtilsTests() {
- this(System.currentTimeMillis());
+ // 1500 is the default max unigrams
+ this(System.currentTimeMillis(), DEFAULT_MAX_UNIGRAMS);
}
- public BinaryDictIOUtilsTests(final long seed) {
+ public BinaryDictIOUtilsTests(final long seed, final int maxUnigrams) {
super();
- Log.d(TAG, "Seed for test is " + seed);
+ Log.d(TAG, "Seed for test is " + seed + ", maxUnigrams is " + maxUnigrams);
+ mMaxUnigrams = maxUnigrams;
final Random random = new Random(seed);
sWords.clear();
- for (int i = 0; i < MAX_UNIGRAMS; ++i) {
+ for (int i = 0; i < maxUnigrams; ++i) {
sWords.add(generateWord(random.nextInt()));
}
}
@@ -395,6 +398,6 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
Log.d(TAG, "max = " + ((double)maxTimeToInsert/1000000) + " ms.");
Log.d(TAG, "min = " + ((double)minTimeToInsert/1000000) + " ms.");
- Log.d(TAG, "avg = " + ((double)sum/MAX_UNIGRAMS/1000000) + " ms.");
+ Log.d(TAG, "avg = " + ((double)sum/mMaxUnigrams/1000000) + " ms.");
}
}