diff options
Diffstat (limited to 'tests/src/com/android/inputmethod/latin/personalization')
-rw-r--r-- | tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java | 104 |
1 files changed, 51 insertions, 53 deletions
diff --git a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java index 06c427193..7c1decb71 100644 --- a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java +++ b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java @@ -75,10 +75,10 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { return new ArrayList<String>(wordSet); } - private void addToDict(final UserHistoryPredictionDictionary dict, final List<String> words) { + private void addToDict(final UserHistoryDictionary dict, final List<String> words) { String prevWord = null; for (String word : words) { - dict.addToPersonalizationPredictionDictionary(prevWord, word, true); + dict.addToDictionary(prevWord, word, true); prevWord = word; } } @@ -90,8 +90,8 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { private void addAndWriteRandomWords(final String testFilenameSuffix, final int numberOfWords, final Random random, final boolean checkContents) { final List<String> words = generateWords(numberOfWords, random); - final UserHistoryPredictionDictionary dict = - PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), + final UserHistoryDictionary dict = + PersonalizationHelper.getUserHistoryDictionary(getContext(), testFilenameSuffix /* locale */, mPrefs); // Add random words to the user history dictionary. addToDict(dict, words); @@ -109,35 +109,54 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { dict.close(); } + /** + * Clear all entries in the user history dictionary. + * @param testFilenameSuffix file name suffix used for testing. + */ + private void clearHistory(final String testFilenameSuffix) { + final UserHistoryDictionary dict = + PersonalizationHelper.getUserHistoryDictionary(getContext(), + testFilenameSuffix /* locale */, mPrefs); + dict.clearAndFlushDictionary(); + dict.close(); + } + + /** + * Shut down executer and wait until all operations of user history are done. + * @param testFilenameSuffix file name suffix used for testing. + */ + private void waitForWriting(final String testFilenameSuffix) { + try { + final UserHistoryDictionary dict = + PersonalizationHelper.getUserHistoryDictionary(getContext(), + testFilenameSuffix, mPrefs); + dict.shutdownExecutorForTests(); + while (!dict.isTerminatedForTests()) { + Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS); + } + } catch (InterruptedException e) { + Log.d(TAG, "InterruptedException: ", e); + } + } + public void testRandomWords() { - File dictFile = null; Log.d(TAG, "This test can be used for profiling."); Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true."); final String testFilenameSuffix = "testRandomWords" + System.currentTimeMillis(); + final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix + + ExpandableBinaryDictionary.DICT_FILE_EXTENSION; + final int numberOfWords = 1000; final Random random = new Random(123456); try { + clearHistory(testFilenameSuffix); addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random, true /* checksContents */); } finally { - try { - final UserHistoryPredictionDictionary dict = - PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), - testFilenameSuffix, mPrefs); - 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); - + Log.d(TAG, "waiting for writing ..."); + waitForWriting(testFilenameSuffix); + final File dictFile = new File(getContext().getFilesDir(), fileName); if (dictFile != null) { assertTrue(dictFile.exists()); assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE); @@ -159,9 +178,10 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { // Create filename suffixes for this test. for (int i = 0; i < numberOfLanguages; i++) { testFilenameSuffixes[i] = "testSwitchingLanguages" + i; - final String fileName = UserHistoryPredictionDictionary.NAME + "." + + final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffixes[i] + ExpandableBinaryDictionary.DICT_FILE_EXTENSION; dictFiles[i] = new File(getContext().getFilesDir(), fileName); + clearHistory(testFilenameSuffixes[i]); } final long start = System.currentTimeMillis(); @@ -178,19 +198,9 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { Log.d(TAG, "testStressTestForSwitchingLanguageAndAddingWords took " + (end - start) + " ms"); } finally { - try { - Log.d(TAG, "waiting for writing ..."); - 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); + Log.d(TAG, "waiting for writing ..."); + for (int i = 0; i < numberOfLanguages; i++) { + waitForWriting(testFilenameSuffixes[i]); } for (final File file : dictFiles) { if (file != null) { @@ -203,33 +213,21 @@ 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); + clearHistory(testFilenameSuffix); 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 + Log.d(TAG, "waiting for writing ..."); + waitForWriting(testFilenameSuffix); + final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix + ExpandableBinaryDictionary.DICT_FILE_EXTENSION; - dictFile = new File(getContext().getFilesDir(), fileName); + final File dictFile = new File(getContext().getFilesDir(), fileName); if (dictFile != null) { assertTrue(dictFile.exists()); assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE); |