Merge "Add user history dictionary decaying test."

This commit is contained in:
Keisuke Kuroyanagi 2014-02-20 10:05:19 +00:00 committed by Android (Google) Code Review
commit afdb6f011b
2 changed files with 64 additions and 2 deletions

View file

@ -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<Boolean> holder = new AsyncResultHolder<Boolean>();
getExecutor(mDictName).executePrioritized(new Runnable() {
getExecutor(mDictName).execute(new Runnable() {
@Override
public void run() {
if (mDictType == Dictionary.TYPE_USER_HISTORY) {

View file

@ -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<String> 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));
}
}
}