aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/src/com/android/inputmethod/latin/InputTestsBase.java9
-rw-r--r--tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java72
2 files changed, 77 insertions, 4 deletions
diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java
index 4b157e700..690c559e8 100644
--- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java
+++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java
@@ -275,9 +275,9 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> {
}
}
- protected void waitForDictionaryToBeLoaded() {
+ protected void waitForDictionariesToBeLoaded() {
try {
- mLatinIME.waitForMainDictionary(
+ mLatinIME.waitForLoadingDictionaries(
TIMEOUT_TO_WAIT_FOR_LOADING_MAIN_DICTIONARY_IN_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted during waiting for loading main dictionary.", e);
@@ -286,7 +286,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> {
protected void changeLanguage(final String locale) {
changeLanguageWithoutWait(locale);
- waitForDictionaryToBeLoaded();
+ waitForDictionariesToBeLoaded();
}
protected void changeLanguageWithoutWait(final String locale) {
@@ -314,6 +314,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> {
mLatinIME.loadKeyboard();
runMessages();
mKeyboard = mLatinIME.mKeyboardSwitcher.getKeyboard();
+ mLatinIME.clearPersonalizedDictionariesForTest();
}
protected void changeKeyboardLocaleAndDictLocale(final String keyboardLocale,
@@ -322,7 +323,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> {
if (!keyboardLocale.equals(dictLocale)) {
mLatinIME.replaceDictionariesForTest(LocaleUtils.constructLocaleFromString(dictLocale));
}
- waitForDictionaryToBeLoaded();
+ waitForDictionariesToBeLoaded();
}
protected void pickSuggestionManually(final int index, final String suggestion) {
diff --git a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
index dff5a77d7..b1239f0af 100644
--- a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
+++ b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
@@ -20,6 +20,7 @@ import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
+import com.android.inputmethod.latin.BinaryDictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.FileUtils;
@@ -44,6 +45,47 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
+ private int mCurrentTime = 0;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ resetCurrentTimeForTestMode();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ stopTestModeInNativeCode();
+ super.tearDown();
+ }
+
+ private void resetCurrentTimeForTestMode() {
+ mCurrentTime = 0;
+ setCurrentTimeForTestMode(mCurrentTime);
+ }
+
+ private void forcePassingShortTime() {
+ // 3 days.
+ final int timeToElapse = (int)TimeUnit.DAYS.toSeconds(3);
+ mCurrentTime += timeToElapse;
+ setCurrentTimeForTestMode(mCurrentTime);
+ }
+
+ private void forcePassingLongTime() {
+ // 60 days.
+ final int timeToElapse = (int)TimeUnit.DAYS.toSeconds(60);
+ mCurrentTime += timeToElapse;
+ setCurrentTimeForTestMode(mCurrentTime);
+ }
+
+ private static int setCurrentTimeForTestMode(final int currentTime) {
+ return BinaryDictionary.setCurrentTimeForTest(currentTime);
+ }
+
+ private static int stopTestModeInNativeCode() {
+ return BinaryDictionary.setCurrentTimeForTest(-1);
+ }
+
/**
* Generates a random word.
*/
@@ -207,4 +249,34 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
FileUtils.deleteRecursively(dictFile);
}
}
+
+ public void testDecaying() {
+ final Locale dummyLocale = new Locale("test_decaying" + System.currentTimeMillis());
+ final int numberOfWords = 5000;
+ final Random random = new Random(123456);
+ resetCurrentTimeForTestMode();
+ clearHistory(dummyLocale);
+ final List<String> words = generateWords(numberOfWords, random);
+ final UserHistoryDictionary dict =
+ PersonalizationHelper.getUserHistoryDictionary(getContext(), dummyLocale);
+ dict.waitAllTasksForTests();
+ String prevWord = null;
+ for (final String word : words) {
+ dict.addToDictionary(prevWord, word, true, mCurrentTime);
+ prevWord = word;
+ assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word));
+ }
+ forcePassingShortTime();
+ dict.decayIfNeeded();
+ dict.waitAllTasksForTests();
+ for (final String word : words) {
+ assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word));
+ }
+ forcePassingLongTime();
+ dict.decayIfNeeded();
+ dict.waitAllTasksForTests();
+ for (final String word : words) {
+ assertFalse(dict.isInUnderlyingBinaryDictionaryForTests(word));
+ }
+ }
}