Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0bc66daa authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi
Browse files

Add user history dictionary decaying test.

Bug: 10667710

Change-Id: Ib2be57d8c4cbbb34f64555d84ea6fd571cfdd247
parent bb173540
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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) {
+62 −0
Original line number Diff line number Diff line
@@ -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));
        }
    }
}