From 0bc66daae36ef7a1f2db1e2fd5c22abfe1b20163 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Thu, 20 Feb 2014 18:36:46 +0900 Subject: [PATCH] Add user history dictionary decaying test. Bug: 10667710 Change-Id: Ib2be57d8c4cbbb34f64555d84ea6fd571cfdd247 --- .../latin/ExpandableBinaryDictionary.java | 4 +- .../UserHistoryDictionaryTests.java | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java index 3b9be4395..230739d6f 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java @@ -62,7 +62,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { private static final boolean DBG_STRESS_TEST = false; private static final int TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS = 100; - private static final int TIMEOUT_FOR_READ_OPS_FOR_TESTS_IN_MILLISECONDS = 1000; + private static final int TIMEOUT_FOR_READ_OPS_FOR_TESTS_IN_MILLISECONDS = 10000; /** * The maximum length of a word in this dictionary. @@ -750,7 +750,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { @UsedForTesting public boolean isInUnderlyingBinaryDictionaryForTests(final String word) { final AsyncResultHolder holder = new AsyncResultHolder(); - getExecutor(mDictName).executePrioritized(new Runnable() { + getExecutor(mDictName).execute(new Runnable() { @Override public void run() { if (mDictType == Dictionary.TYPE_USER_HISTORY) { diff --git a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java index dff5a77d7..449030cec 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,43 @@ 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(); + mCurrentTime = 0; + setCurrentTimeForTestMode(mCurrentTime); + } + + @Override + protected void tearDown() throws Exception { + stopTestModeInNativeCode(); + super.tearDown(); + } + + private void forcePassingShortTime() { + // 4 days. + final int timeToElapse = (int)TimeUnit.DAYS.toSeconds(4); + 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 +245,28 @@ 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); + clearHistory(dummyLocale); + final List words = generateWords(numberOfWords, random); + final UserHistoryDictionary dict = + PersonalizationHelper.getUserHistoryDictionary(getContext(), dummyLocale); + String prevWord = null; + for (final String word : words) { + dict.addToDictionary(prevWord, word, true, mCurrentTime); + prevWord = word; + assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word)); + } + forcePassingShortTime(); + for (final String word : words) { + assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word)); + } + forcePassingLongTime(); + for (final String word : words) { + assertFalse(dict.isInUnderlyingBinaryDictionaryForTests(word)); + } + } }