diff options
Diffstat (limited to 'tests')
11 files changed, 737 insertions, 14 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 87501eeb2..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; diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java index 3dc4543c2..1346c00f4 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserCsvTests.java @@ -19,6 +19,8 @@ package com.android.inputmethod.keyboard.internal; 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; @@ -42,7 +44,7 @@ public class KeySpecParserCsvTests extends InstrumentationTestCase { } 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/latin/BinaryDictIOTests.java b/tests/src/com/android/inputmethod/latin/BinaryDictIOTests.java new file mode 100644 index 000000000..9b7f4a7f9 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/BinaryDictIOTests.java @@ -0,0 +1,371 @@ +/* + * 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.makedict.BinaryDictInputOutput; +import com.android.inputmethod.latin.makedict.FusionDictionary; +import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; +import com.android.inputmethod.latin.makedict.FusionDictionary.Node; +import com.android.inputmethod.latin.makedict.PendingAttribute; +import com.android.inputmethod.latin.makedict.UnsupportedFormatException; + +import android.test.AndroidTestCase; +import android.util.Log; +import android.util.SparseArray; + +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +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 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" + }; + + // Utilities for test + /** + * 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 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); + } + + /** + * Adds unigrams to the dictionary. + */ + private void addUnigrams(final int number, + final FusionDictionary dict, + final List<String> words) { + for (int i = 0; i < number; ++i) { + final String word = words.get(i); + dict.add(word, UNIGRAM_FREQ, null, 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) { + + long now = -1, diff = -1; + + try { + final FileOutputStream out = new FileOutputStream(file); + + now = System.currentTimeMillis(); + BinaryDictInputOutput.writeDictionaryBinary(out, dict, 2); + 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) { + 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))); + } + } + } + + // Tests for readDictionaryBinary and writeDictionaryBinary + + private long timeReadingAndCheckDict(final File file, final List<String> words, + final SparseArray<List<Integer>> bigrams) { + + long now, diff = -1; + + FileInputStream inStream = null; + try { + inStream = new FileInputStream(file); + final ByteBuffer buffer = inStream.getChannel().map( + FileChannel.MapMode.READ_ONLY, 0, file.length()); + + now = System.currentTimeMillis(); + + final FusionDictionary dict = + BinaryDictInputOutput.readDictionaryBinary(buffer, null); + + diff = System.currentTimeMillis() - now; + + checkDictionary(dict, words, bigrams); + return diff; + + } catch (IOException e) { + Log.e(TAG, "raise IOException while reading file " + e); + } catch (UnsupportedFormatException e) { + Log.e(TAG, "Unsupported format: " + e); + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (IOException e) { + // do nothing + } + } + } + + return diff; + } + + private String runReadAndWrite(final List<String> words, + final SparseArray<List<Integer>> bigrams, + final String message) { + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions( + new HashMap<String,String>(), false, false)); + + File file = null; + try { + file = File.createTempFile("runReadAndWrite", ".dict"); + } catch (IOException e) { + Log.e(TAG, "IOException: " + e); + } + + assertNotNull(file); + + addUnigrams(words.size(), dict, words); + addBigrams(dict, words, bigrams); + // check original dictionary + checkDictionary(dict, words, bigrams); + + final long write = timeWritingDictToFile(file, dict); + final long read = timeReadingAndCheckDict(file, words, bigrams); + + return "PROF: read=" + read + "ms, write=" + write + "ms :" + message; + } + + public void testReadAndWrite() { + final List<String> results = new ArrayList<String>(); + + final Random random = new Random(123456); + final List<String> words = generateWords(MAX_UNIGRAMS, random); + final SparseArray<List<Integer>> emptyArray = CollectionUtils.newSparseArray(); + + final SparseArray<List<Integer>> chain = CollectionUtils.newSparseArray(); + for (int i = 0; i < words.size(); ++i) chain.put(i, new ArrayList<Integer>()); + for (int i = 1; i < words.size(); ++i) chain.get(i-1).add(i); + + final SparseArray<List<Integer>> star = CollectionUtils.newSparseArray(); + final List<Integer> list0 = CollectionUtils.newArrayList(); + star.put(0, list0); + for (int i = 1; i < words.size(); ++i) star.get(0).add(i); + + results.add(runReadAndWrite(words, emptyArray, "only unigram")); + results.add(runReadAndWrite(words, chain, "chain")); + results.add(runReadAndWrite(words, star, "star")); + + 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) { + 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; + try { + inStream = new FileInputStream(file); + final ByteBuffer buffer = inStream.getChannel().map( + FileChannel.MapMode.READ_ONLY, 0, file.length()); + + now = System.currentTimeMillis(); + BinaryDictInputOutput.readUnigramsAndBigramsBinary( + new BinaryDictInputOutput.ByteBufferWrapper(buffer), resultWords, resultFreqs, + resultBigrams); + diff = System.currentTimeMillis() - now; + checkWordMap(words, bigrams, resultWords, resultFreqs, resultBigrams); + } 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 + } + } + } + + return diff; + } + + private void runReadUnigramsAndBigramsBinary(final List<String> words, + final SparseArray<List<Integer>> bigrams) { + + // making the dictionary from lists of words. + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions( + new HashMap<String, String>(), false, false)); + + File file = null; + try { + file = File.createTempFile("runReadUnigrams", ".dict"); + } catch (IOException e) { + Log.e(TAG, "IOException: " + e); + } + + assertNotNull(file); + + addUnigrams(words.size(), dict, words); + addBigrams(dict, words, bigrams); + timeWritingDictToFile(file, dict); + + long wordMap = timeAndCheckReadUnigramsAndBigramsBinary(file, words, bigrams); + long fullReading = timeReadingAndCheckDict(file, words, bigrams); + + Log.d(TAG, "read=" + fullReading + ", bytearray=" + wordMap); + } + + public void testReadUnigramsAndBigramsBinary() { + final List<String> results = new ArrayList<String>(); + + final Random random = new Random(123456); + final List<String> words = generateWords(MAX_UNIGRAMS, random); + final SparseArray<List<Integer>> emptyArray = CollectionUtils.newSparseArray(); + + runReadUnigramsAndBigramsBinary(words, emptyArray); + + final SparseArray<List<Integer>> star = CollectionUtils.newSparseArray(); + for (int i = 1; i < words.size(); ++i) { + star.put(i-1, new ArrayList<Integer>()); + star.get(i-1).add(i); + } + runReadUnigramsAndBigramsBinary(words, star); + } +} 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/InputPointersTests.java b/tests/src/com/android/inputmethod/latin/InputPointersTests.java index 6f04f3ebb..cc55076c0 100644 --- a/tests/src/com/android/inputmethod/latin/InputPointersTests.java +++ b/tests/src/com/android/inputmethod/latin/InputPointersTests.java @@ -18,6 +18,8 @@ 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; @@ -162,6 +164,61 @@ public class InputPointersTests extends AndroidTestCase { 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) { diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java index c672d5126..ffd95f57a 100644 --- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java @@ -39,7 +39,6 @@ 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; @@ -136,7 +135,6 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { mLatinIME.onCreateInputView(); mLatinIME.onStartInputView(ei, false); mInputConnection = ic; - mKeyboard = mLatinIME.mKeyboardSwitcher.getKeyboard(); changeLanguage("en_US"); } @@ -222,9 +220,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); } @@ -256,13 +252,13 @@ public class InputTestsBase extends ServiceTestCase<LatinIME> { 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/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..8f0551b4c --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/UserHistoryDictIOUtilsTests.java @@ -0,0 +1,249 @@ +/* + * 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.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>(); + + /** + * 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, 2); + 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); + } +} |