diff options
Diffstat (limited to 'tests/src')
10 files changed, 863 insertions, 241 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/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 deleted file mode 100644 index 0094db8a7..000000000 --- a/tests/src/com/android/inputmethod/latin/BinaryDictIOTests.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * 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.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.List; -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 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" - }; - - /** - * 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); - } - - 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); - } - } - - private void addBigrams(final FusionDictionary dict, - final List<String> words, - final SparseArray<List<Integer>> sparseArray) { - for (int i = 0; i < sparseArray.size(); ++i) { - final int w1 = sparseArray.keyAt(i); - for (int w2 : sparseArray.valueAt(i)) { - dict.setBigram(words.get(w1), words.get(w2), BIGRAM_FREQ); - } - } - } - - private long timeWritingDictToFile(final String fileName, - final FusionDictionary dict) { - - final File file = new File(getContext().getFilesDir(), fileName); - 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))); - } - } - } - - private long timeReadingAndCheckDict(final String fileName, - final List<String> words, - final SparseArray<List<Integer>> bigrams) { - - long now, diff = -1; - - try { - final File file = new File(getContext().getFilesDir(), fileName); - final FileInputStream 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); - } - - 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)); - - final String fileName = generateWord((int)System.currentTimeMillis()) + ".dict"; - - addUnigrams(words.size(), dict, words); - addBigrams(dict, words, bigrams); - // check original dictionary - checkDictionary(dict, words, bigrams); - - final long write = timeWritingDictToFile(fileName, dict); - final long read = timeReadingAndCheckDict(fileName, words, bigrams); - deleteFile(fileName); - - return "PROF: read=" + read + "ms, write=" + write + "ms :" + message; - } - - private void deleteFile(final String fileName) { - final File file = new File(getContext().getFilesDir(), fileName); - file.delete(); - } - - 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); - } - } -} diff --git a/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java b/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java index 8ecdcc366..123959b4d 100644 --- a/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java +++ b/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java @@ -31,16 +31,16 @@ public class FusionDictionaryTests extends AndroidTestCase { FusionDictionary dict = new FusionDictionary(new Node(), new FusionDictionary.DictionaryOptions(new HashMap<String,String>(), false, false)); - dict.add("abc", 10, null); + dict.add("abc", 10, null, false /* isNotAWord */); assertNull(FusionDictionary.findWordInTree(dict.mRoot, "aaa")); assertNotNull(FusionDictionary.findWordInTree(dict.mRoot, "abc")); - dict.add("aa", 10, null); + dict.add("aa", 10, null, false /* isNotAWord */); assertNull(FusionDictionary.findWordInTree(dict.mRoot, "aaa")); assertNotNull(FusionDictionary.findWordInTree(dict.mRoot, "aa")); - dict.add("babcd", 10, null); - dict.add("bacde", 10, null); + 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/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java index ffd95f57a..fe58cb84e 100644 --- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java @@ -41,6 +41,7 @@ import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; import java.util.HashMap; +import java.util.Locale; public class InputTestsBase extends ServiceTestCase<LatinIME> { @@ -51,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>(); @@ -86,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() { @@ -112,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(); @@ -248,6 +270,7 @@ 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"); } diff --git a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java index 5db06ef51..00cca9d3b 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,50 @@ 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) { + int oneTimeResult = expectedResult & mask; + assertEquals("After >" + cs + "<", oneTimeResult, StringUtils.getCapsMode(cs, mask, l)); + } + + private void allPathsForCaps(final CharSequence cs, final int expectedResult, final Locale l) { + 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); + onePathForCaps(cs, expectedResult, w | s, l); + onePathForCaps(cs, expectedResult, c | s, l); + onePathForCaps(cs, expectedResult, c | w, l); + onePathForCaps(cs, expectedResult, c, l); + onePathForCaps(cs, expectedResult, w, l); + onePathForCaps(cs, expectedResult, s, l); + } - assertFalse("empty string", StringUtils.hasUpperCase("")); - assertFalse("lower-case string", StringUtils.hasUpperCase("string")); - assertFalse("lower-case string with non-letters", StringUtils.hasUpperCase("he's")); + 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); + allPathsForCaps("Word", c, l); + allPathsForCaps("Word.", c, l); + allPathsForCaps("Word ", c | w, l); + allPathsForCaps("Word. ", c | w | s, l); + allPathsForCaps("Word..", c, l); + allPathsForCaps("Word.. ", c | w | s, l); + allPathsForCaps("Word... ", c | w | s, l); + allPathsForCaps("Word ... ", c | w | s, l); + allPathsForCaps("Word . ", c | w, l); + allPathsForCaps("In the U.S ", c | w, l); + allPathsForCaps("In the U.S. ", c | w, l); + allPathsForCaps("Some stuff (e.g. ", c | w, l); + allPathsForCaps("In the U.S.. ", c | w | s, l); + allPathsForCaps("\"Word.\" ", c | w | s, l); + allPathsForCaps("\"Word\". ", c | w | s, l); + allPathsForCaps("\"Word\" ", c | w, l); + l = Locale.FRENCH; + allPathsForCaps("\"Word.\" ", c | w, l); + allPathsForCaps("\"Word\". ", c | w | s, l); + allPathsForCaps("\"Word\" ", c | w, l); } } 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/makedict/BinaryDictIOTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java new file mode 100644 index 000000000..328784b1a --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java @@ -0,0 +1,470 @@ +/* + * 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; +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.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_PARENTADDRESS = + new FormatSpec.FormatOptions(3, false); + private static final FormatSpec.FormatOptions VERSION3_WITH_PARENTADDRESS = + new FormatSpec.FormatOptions(3, true); + + 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 + ", hasParentAddress = " + formatOptions.mHasParentAddress; + } + + // 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_PARENTADDRESS); + runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITH_PARENTADDRESS); + + 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_PARENTADDRESS); + runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITH_PARENTADDRESS); + + 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(); + BinaryDictInputOutput.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_PARENTADDRESS); + runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITH_PARENTADDRESS); + + 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_PARENTADDRESS); + runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITH_PARENTADDRESS); + + for (final String result : results) { + Log.d(TAG, result); + } + } +} 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]); + } +} |