diff options
Diffstat (limited to 'tests/src/com/android/inputmethod/latin/personalization')
-rw-r--r-- | tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java | 89 |
1 files changed, 81 insertions, 8 deletions
diff --git a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java index 99ccb1a75..d605cdb84 100644 --- a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java +++ b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java @@ -46,6 +46,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { }; private static final int MIN_USER_HISTORY_DICTIONARY_FILE_SIZE = 1000; + private static final int WAIT_TERMINATING_IN_MILLISECONDS = 100; @Override public void setUp() { @@ -77,19 +78,38 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { private void addToDict(final UserHistoryPredictionDictionary dict, final List<String> words) { String prevWord = null; for (String word : words) { - dict.forceAddWordForTest(prevWord, word, true); + dict.addToPersonalizationPredictionDictionary(prevWord, word, true); prevWord = word; } } + /** + * @param checksContents if true, checks whether written words are actually in the dictionary + * or not. + */ private void addAndWriteRandomWords(final String testFilenameSuffix, final int numberOfWords, - final Random random) { + final Random random, final boolean checksContents) { final List<String> words = generateWords(numberOfWords, random); final UserHistoryPredictionDictionary dict = - PersonalizationDictionaryHelper.getUserHistoryPredictionDictionary(getContext(), + PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), testFilenameSuffix /* locale */, mPrefs); // Add random words to the user history dictionary. addToDict(dict, words); + if (checksContents) { + try { + Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS)); + } catch (InterruptedException e) { + } + // Limit word count to check when using a Java on memory dictionary. + final int wordCountToCheck = + ExpandableBinaryDictionary.ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE ? + numberOfWords : 10; + for (int i = 0; i < wordCountToCheck; ++i) { + final String word = words.get(i); + // This may fail as long as we use tryLock on inserting the bigram words + assertTrue(dict.isInDictionaryForTests(word)); + } + } // write to file. dict.close(); } @@ -103,11 +123,18 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { final Random random = new Random(123456); try { - addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random); + addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random, + true /* checksContents */); } finally { try { + final UserHistoryPredictionDictionary dict = + PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), + testFilenameSuffix, mPrefs); Log.d(TAG, "waiting for writing ..."); - Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS)); + dict.shutdownExecutorForTests(); + while (!dict.isTerminatedForTests()) { + Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS); + } } catch (InterruptedException e) { Log.d(TAG, "InterruptedException: " + e); } @@ -130,11 +157,11 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { final int numberOfWordsInsertedForEachLanguageSwitch = 100; final File dictFiles[] = new File[numberOfLanguages]; + final String testFilenameSuffixes[] = new String[numberOfLanguages]; try { final Random random = new Random(123456); // Create filename suffixes for this test. - String testFilenameSuffixes[] = new String[numberOfLanguages]; for (int i = 0; i < numberOfLanguages; i++) { testFilenameSuffixes[i] = "testSwitchingLanguages" + i; final String fileName = UserHistoryPredictionDictionary.NAME + "." + @@ -148,7 +175,8 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { final int index = i % numberOfLanguages; // Switch languages to testFilenameSuffixes[index]. addAndWriteRandomWords(testFilenameSuffixes[index], - numberOfWordsInsertedForEachLanguageSwitch, random); + numberOfWordsInsertedForEachLanguageSwitch, random, + false /* checksContents */); } final long end = System.currentTimeMillis(); @@ -157,7 +185,15 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { } finally { try { Log.d(TAG, "waiting for writing ..."); - Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS)); + for (int i = 0; i < numberOfLanguages; i++) { + final UserHistoryPredictionDictionary dict = + PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), + testFilenameSuffixes[i], mPrefs); + dict.shutdownExecutorForTests(); + while (!dict.isTerminatedForTests()) { + Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS); + } + } } catch (InterruptedException e) { Log.d(TAG, "InterruptedException: " + e); } @@ -170,4 +206,41 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { } } } + + public void testAddManyWords() { + File dictFile = null; + final String testFilenameSuffix = "testRandomWords" + System.currentTimeMillis(); + final int numberOfWords = + ExpandableBinaryDictionary.ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE ? + 10000 : 1000; + final Random random = new Random(123456); + + UserHistoryPredictionDictionary dict = + PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), + testFilenameSuffix, mPrefs); + try { + addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random, + true /* checksContents */); + dict.close(); + } finally { + try { + Log.d(TAG, "waiting for writing ..."); + dict.shutdownExecutorForTests(); + while (!dict.isTerminatedForTests()) { + Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS); + } + } catch (InterruptedException e) { + Log.d(TAG, "InterruptedException: ", e); + } + final String fileName = UserHistoryPredictionDictionary.NAME + "." + testFilenameSuffix + + ExpandableBinaryDictionary.DICT_FILE_EXTENSION; + dictFile = new File(getContext().getFilesDir(), fileName); + if (dictFile != null) { + assertTrue(dictFile.exists()); + assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE); + dictFile.delete(); + } + } + } + } |