2012-08-20 10:29:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-07-26 03:35:11 +00:00
|
|
|
package com.android.inputmethod.latin.personalization;
|
2012-08-20 10:29:20 +00:00
|
|
|
|
2014-10-24 00:50:35 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
2012-08-20 10:29:20 +00:00
|
|
|
import android.test.AndroidTestCase;
|
2013-02-04 23:25:24 +00:00
|
|
|
import android.test.suitebuilder.annotation.LargeTest;
|
2012-08-20 10:29:20 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2013-08-19 01:17:02 +00:00
|
|
|
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
|
2014-09-29 01:52:18 +00:00
|
|
|
import com.android.inputmethod.latin.NgramContext;
|
|
|
|
import com.android.inputmethod.latin.NgramContext.WordInfo;
|
2014-10-24 00:50:35 +00:00
|
|
|
import com.android.inputmethod.latin.settings.LocalSettingsConstants;
|
2014-03-05 09:19:34 +00:00
|
|
|
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
|
2014-05-26 08:28:27 +00:00
|
|
|
import com.android.inputmethod.latin.utils.DistracterFilter;
|
2013-06-23 16:11:32 +00:00
|
|
|
|
2012-10-05 06:12:50 +00:00
|
|
|
import java.io.File;
|
2012-08-20 10:29:20 +00:00
|
|
|
import java.util.List;
|
2013-12-13 08:09:16 +00:00
|
|
|
import java.util.Locale;
|
2012-08-20 10:29:20 +00:00
|
|
|
import java.util.Random;
|
2013-08-16 05:17:09 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2012-08-20 10:29:20 +00:00
|
|
|
|
2014-10-24 00:50:35 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2012-08-20 10:29:20 +00:00
|
|
|
/**
|
|
|
|
* Unit tests for UserHistoryDictionary
|
|
|
|
*/
|
2013-02-04 23:25:24 +00:00
|
|
|
@LargeTest
|
2012-08-20 10:29:20 +00:00
|
|
|
public class UserHistoryDictionaryTests extends AndroidTestCase {
|
|
|
|
private static final String TAG = UserHistoryDictionaryTests.class.getSimpleName();
|
2014-08-12 13:29:02 +00:00
|
|
|
private static final int WAIT_FOR_WRITING_FILE_IN_MILLISECONDS = 3000;
|
2014-10-24 00:50:35 +00:00
|
|
|
private static final String TEST_ACCOUNT = "account@example.com";
|
2012-08-20 10:29:20 +00:00
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
private int mCurrentTime = 0;
|
|
|
|
|
2014-10-24 00:50:35 +00:00
|
|
|
private SharedPreferences mPrefs;
|
|
|
|
private String mLastKnownAccount = null;
|
|
|
|
|
2014-10-20 05:48:56 +00:00
|
|
|
private static void printAllFiles(final File dir) {
|
2014-08-13 06:51:34 +00:00
|
|
|
Log.d(TAG, dir.getAbsolutePath());
|
|
|
|
for (final File file : dir.listFiles()) {
|
|
|
|
Log.d(TAG, " " + file.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 18:33:39 +00:00
|
|
|
private static void assertDictionaryExists(final UserHistoryDictionary dict,
|
2014-09-22 05:22:37 +00:00
|
|
|
final File dictFile) {
|
2014-08-15 08:55:27 +00:00
|
|
|
Log.d(TAG, "waiting for writing ...");
|
2014-09-22 05:22:37 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-08-15 08:55:27 +00:00
|
|
|
if (!dictFile.exists()) {
|
|
|
|
try {
|
|
|
|
Log.d(TAG, dictFile + " is not existing. Wait "
|
|
|
|
+ WAIT_FOR_WRITING_FILE_IN_MILLISECONDS + " ms for writing.");
|
|
|
|
printAllFiles(dictFile.getParentFile());
|
|
|
|
Thread.sleep(WAIT_FOR_WRITING_FILE_IN_MILLISECONDS);
|
|
|
|
} catch (final InterruptedException e) {
|
|
|
|
Log.e(TAG, "Interrupted during waiting for writing the dict file.");
|
|
|
|
}
|
|
|
|
}
|
2014-10-30 18:33:39 +00:00
|
|
|
assertTrue("Following dictionary file doesn't exist: " + dictFile, dictFile.exists());
|
2014-09-22 05:22:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
2014-10-24 00:50:35 +00:00
|
|
|
|
|
|
|
mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
// Keep track of the current account so that we restore it when the test finishes.
|
|
|
|
mLastKnownAccount = mPrefs.getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null);
|
|
|
|
updateAccountName(TEST_ACCOUNT);
|
|
|
|
|
2014-02-20 12:23:57 +00:00
|
|
|
resetCurrentTimeForTestMode();
|
2014-10-30 18:33:39 +00:00
|
|
|
UserHistoryDictionaryTestsHelper.removeAllTestDictFiles(
|
|
|
|
UserHistoryDictionaryTestsHelper.TEST_LOCALE_PREFIX, mContext);
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void tearDown() throws Exception {
|
2014-10-30 18:33:39 +00:00
|
|
|
UserHistoryDictionaryTestsHelper.removeAllTestDictFiles(
|
|
|
|
UserHistoryDictionaryTestsHelper.TEST_LOCALE_PREFIX, mContext);
|
2014-02-20 09:36:46 +00:00
|
|
|
stopTestModeInNativeCode();
|
2014-10-24 00:50:35 +00:00
|
|
|
|
|
|
|
// Restore the account that was present before running the test.
|
|
|
|
updateAccountName(mLastKnownAccount);
|
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
super.tearDown();
|
|
|
|
}
|
|
|
|
|
2014-02-20 12:23:57 +00:00
|
|
|
private void resetCurrentTimeForTestMode() {
|
|
|
|
mCurrentTime = 0;
|
|
|
|
setCurrentTimeForTestMode(mCurrentTime);
|
|
|
|
}
|
|
|
|
|
2014-10-24 00:50:35 +00:00
|
|
|
private void updateAccountName(@Nullable final String accountName) {
|
|
|
|
if (accountName == null) {
|
|
|
|
mPrefs.edit().remove(LocalSettingsConstants.PREF_ACCOUNT_NAME).apply();
|
|
|
|
} else {
|
|
|
|
mPrefs.edit().putString(LocalSettingsConstants.PREF_ACCOUNT_NAME, accountName).apply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
private void forcePassingShortTime() {
|
2014-02-20 12:23:57 +00:00
|
|
|
// 3 days.
|
|
|
|
final int timeToElapse = (int)TimeUnit.DAYS.toSeconds(3);
|
2014-02-20 09:36:46 +00:00
|
|
|
mCurrentTime += timeToElapse;
|
|
|
|
setCurrentTimeForTestMode(mCurrentTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void forcePassingLongTime() {
|
2014-03-14 03:23:24 +00:00
|
|
|
// 365 days.
|
|
|
|
final int timeToElapse = (int)TimeUnit.DAYS.toSeconds(365);
|
2014-02-20 09:36:46 +00:00
|
|
|
mCurrentTime += timeToElapse;
|
|
|
|
setCurrentTimeForTestMode(mCurrentTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int setCurrentTimeForTestMode(final int currentTime) {
|
2014-03-05 09:19:34 +00:00
|
|
|
return BinaryDictionaryUtils.setCurrentTimeForTest(currentTime);
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int stopTestModeInNativeCode() {
|
2014-03-05 09:19:34 +00:00
|
|
|
return BinaryDictionaryUtils.setCurrentTimeForTest(-1);
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 11:55:34 +00:00
|
|
|
/**
|
|
|
|
* Clear all entries in the user history dictionary.
|
2014-09-22 05:22:37 +00:00
|
|
|
* @param dict the user history dictionary.
|
2013-10-03 11:55:34 +00:00
|
|
|
*/
|
2014-10-20 05:48:56 +00:00
|
|
|
private static void clearHistory(final UserHistoryDictionary dict) {
|
2014-01-15 02:18:39 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-04-24 18:25:47 +00:00
|
|
|
dict.clear();
|
2013-10-03 11:55:34 +00:00
|
|
|
dict.close();
|
2014-01-15 02:18:39 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2013-10-03 11:55:34 +00:00
|
|
|
}
|
|
|
|
|
2012-08-20 10:29:20 +00:00
|
|
|
public void testRandomWords() {
|
2013-08-16 05:17:09 +00:00
|
|
|
Log.d(TAG, "This test can be used for profiling.");
|
|
|
|
Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true.");
|
2014-10-30 18:33:39 +00:00
|
|
|
final Locale dummyLocale = UserHistoryDictionaryTestsHelper.getDummyLocale("random_words");
|
2014-10-24 00:50:35 +00:00
|
|
|
final String dictName = UserHistoryDictionary.getUserHistoryDictName(
|
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */, getContext());
|
2014-02-13 07:16:44 +00:00
|
|
|
final File dictFile = ExpandableBinaryDictionary.getDictFile(
|
|
|
|
mContext, dictName, null /* dictFile */);
|
2014-09-22 05:22:37 +00:00
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
2014-10-24 00:50:35 +00:00
|
|
|
getContext(), dummyLocale, TEST_ACCOUNT);
|
2014-10-30 18:33:39 +00:00
|
|
|
clearHistory(dict);
|
2013-08-16 05:17:09 +00:00
|
|
|
final int numberOfWords = 1000;
|
|
|
|
final Random random = new Random(123456);
|
2014-10-30 18:33:39 +00:00
|
|
|
assertTrue(UserHistoryDictionaryTestsHelper.addAndWriteRandomWords(
|
|
|
|
dict, numberOfWords, random, true /* checksContents */, mCurrentTime));
|
|
|
|
assertDictionaryExists(dict, dictFile);
|
2012-08-20 10:29:20 +00:00
|
|
|
}
|
2013-07-16 03:17:56 +00:00
|
|
|
|
|
|
|
public void testStressTestForSwitchingLanguagesAndAddingWords() {
|
|
|
|
final int numberOfLanguages = 2;
|
2013-08-16 05:17:09 +00:00
|
|
|
final int numberOfLanguageSwitching = 80;
|
|
|
|
final int numberOfWordsInsertedForEachLanguageSwitch = 100;
|
2013-07-16 03:17:56 +00:00
|
|
|
|
|
|
|
final File dictFiles[] = new File[numberOfLanguages];
|
2014-09-22 05:22:37 +00:00
|
|
|
final UserHistoryDictionary dicts[] = new UserHistoryDictionary[numberOfLanguages];
|
|
|
|
|
2013-07-16 03:17:56 +00:00
|
|
|
try {
|
|
|
|
final Random random = new Random(123456);
|
|
|
|
|
2013-08-16 05:17:09 +00:00
|
|
|
// Create filename suffixes for this test.
|
2013-07-16 03:17:56 +00:00
|
|
|
for (int i = 0; i < numberOfLanguages; i++) {
|
2014-10-30 18:33:39 +00:00
|
|
|
final Locale dummyLocale =
|
|
|
|
UserHistoryDictionaryTestsHelper.getDummyLocale("switching_languages" + i);
|
2014-10-24 00:50:35 +00:00
|
|
|
final String dictName = UserHistoryDictionary.getUserHistoryDictName(
|
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */, getContext());
|
2014-02-13 07:16:44 +00:00
|
|
|
dictFiles[i] = ExpandableBinaryDictionary.getDictFile(
|
|
|
|
mContext, dictName, null /* dictFile */);
|
2014-09-22 05:22:37 +00:00
|
|
|
dicts[i] = PersonalizationHelper.getUserHistoryDictionary(getContext(),
|
2014-10-24 00:50:35 +00:00
|
|
|
dummyLocale, TEST_ACCOUNT);
|
2014-09-22 05:22:37 +00:00
|
|
|
clearHistory(dicts[i]);
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 05:17:09 +00:00
|
|
|
final long start = System.currentTimeMillis();
|
2013-07-16 03:17:56 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < numberOfLanguageSwitching; i++) {
|
|
|
|
final int index = i % numberOfLanguages;
|
2014-09-22 05:22:37 +00:00
|
|
|
// Switch to dicts[index].
|
2014-10-30 18:33:39 +00:00
|
|
|
assertTrue(UserHistoryDictionaryTestsHelper.addAndWriteRandomWords(dicts[index],
|
|
|
|
numberOfWordsInsertedForEachLanguageSwitch,
|
|
|
|
random,
|
|
|
|
false /* checksContents */,
|
|
|
|
mCurrentTime));
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final long end = System.currentTimeMillis();
|
|
|
|
Log.d(TAG, "testStressTestForSwitchingLanguageAndAddingWords took "
|
2013-08-16 05:17:09 +00:00
|
|
|
+ (end - start) + " ms");
|
|
|
|
} finally {
|
2013-10-03 11:55:34 +00:00
|
|
|
for (int i = 0; i < numberOfLanguages; i++) {
|
2014-10-30 18:33:39 +00:00
|
|
|
assertDictionaryExists(dicts[i], dictFiles[i]);
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 13:57:15 +00:00
|
|
|
|
|
|
|
public void testAddManyWords() {
|
2014-10-30 18:33:39 +00:00
|
|
|
final Locale dummyLocale =
|
|
|
|
UserHistoryDictionaryTestsHelper.getDummyLocale("many_random_words");
|
2014-10-24 00:50:35 +00:00
|
|
|
final String dictName = UserHistoryDictionary.getUserHistoryDictName(
|
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */, getContext());
|
2014-02-13 07:16:44 +00:00
|
|
|
final File dictFile = ExpandableBinaryDictionary.getDictFile(
|
|
|
|
mContext, dictName, null /* dictFile */);
|
2013-12-13 08:09:16 +00:00
|
|
|
final int numberOfWords = 10000;
|
2013-09-24 13:57:15 +00:00
|
|
|
final Random random = new Random(123456);
|
2014-09-22 05:22:37 +00:00
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
2014-10-24 00:50:35 +00:00
|
|
|
getContext(), dummyLocale, TEST_ACCOUNT);
|
2014-09-22 05:22:37 +00:00
|
|
|
clearHistory(dict);
|
2014-10-30 18:33:39 +00:00
|
|
|
assertTrue(UserHistoryDictionaryTestsHelper.addAndWriteRandomWords(dict,
|
|
|
|
numberOfWords, random, true /* checksContents */, mCurrentTime));
|
|
|
|
assertDictionaryExists(dict, dictFile);
|
2013-09-24 13:57:15 +00:00
|
|
|
}
|
2014-02-20 09:36:46 +00:00
|
|
|
|
|
|
|
public void testDecaying() {
|
2014-10-30 18:33:39 +00:00
|
|
|
final Locale dummyLocale = UserHistoryDictionaryTestsHelper.getDummyLocale("decaying");
|
2014-09-22 05:22:37 +00:00
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
2014-10-24 00:50:35 +00:00
|
|
|
getContext(), dummyLocale, TEST_ACCOUNT);
|
2014-02-20 12:23:57 +00:00
|
|
|
resetCurrentTimeForTestMode();
|
2014-09-22 05:22:37 +00:00
|
|
|
clearHistory(dict);
|
2014-02-20 12:23:57 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-10-30 18:33:39 +00:00
|
|
|
|
|
|
|
final int numberOfWords = 5000;
|
|
|
|
final Random random = new Random(123456);
|
|
|
|
final List<String> words = UserHistoryDictionaryTestsHelper.generateWords(numberOfWords,
|
|
|
|
random);
|
2014-09-29 01:52:18 +00:00
|
|
|
NgramContext ngramContext = NgramContext.EMPTY_PREV_WORDS_INFO;
|
2014-02-20 09:36:46 +00:00
|
|
|
for (final String word : words) {
|
2014-09-29 01:52:18 +00:00
|
|
|
UserHistoryDictionary.addToDictionary(dict, ngramContext, word, true, mCurrentTime,
|
2014-05-26 08:28:27 +00:00
|
|
|
DistracterFilter.EMPTY_DISTRACTER_FILTER);
|
2014-09-29 01:52:18 +00:00
|
|
|
ngramContext = ngramContext.getNextNgramContext(new WordInfo(word));
|
2014-04-28 11:56:01 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-06-09 02:04:28 +00:00
|
|
|
assertTrue(dict.isInDictionary(word));
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
forcePassingShortTime();
|
2014-02-28 09:17:09 +00:00
|
|
|
dict.runGCIfRequired();
|
2014-02-20 12:23:57 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-02-20 09:36:46 +00:00
|
|
|
for (final String word : words) {
|
2014-06-09 02:04:28 +00:00
|
|
|
assertTrue(dict.isInDictionary(word));
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
2014-10-30 18:33:39 +00:00
|
|
|
// Long term decay results in words removed from the dictionary.
|
2014-02-20 09:36:46 +00:00
|
|
|
forcePassingLongTime();
|
2014-02-28 09:17:09 +00:00
|
|
|
dict.runGCIfRequired();
|
2014-02-20 12:23:57 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-02-20 09:36:46 +00:00
|
|
|
for (final String word : words) {
|
2014-06-09 02:04:28 +00:00
|
|
|
assertFalse(dict.isInDictionary(word));
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-24 00:50:35 +00:00
|
|
|
|
|
|
|
public void testRandomWords_NullAccount() {
|
|
|
|
updateAccountName(null);
|
|
|
|
testRandomWords();
|
|
|
|
}
|
|
|
|
}
|