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
|
|
|
|
|
|
|
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-11-06 11:29:29 +00:00
|
|
|
import com.android.inputmethod.latin.common.FileUtils;
|
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;
|
2014-08-13 06:51:16 +00:00
|
|
|
import java.io.FilenameFilter;
|
2012-08-20 10:29:20 +00:00
|
|
|
import java.util.ArrayList;
|
2014-05-23 11:18:17 +00:00
|
|
|
import java.util.HashSet;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-08-13 06:51:16 +00:00
|
|
|
private static final String TEST_LOCALE_PREFIX = "test_";
|
2012-08-20 10:29:20 +00:00
|
|
|
|
|
|
|
private static final String[] CHARACTERS = {
|
|
|
|
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
|
|
|
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
|
|
|
|
};
|
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
private int mCurrentTime = 0;
|
|
|
|
|
2014-08-13 06:51:16 +00:00
|
|
|
private void removeAllTestDictFiles() {
|
|
|
|
final Locale dummyLocale = new Locale(TEST_LOCALE_PREFIX);
|
|
|
|
final String dictName = ExpandableBinaryDictionary.getDictName(
|
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */);
|
|
|
|
final File dictFile = ExpandableBinaryDictionary.getDictFile(
|
|
|
|
mContext, dictName, null /* dictFile */);
|
|
|
|
final FilenameFilter filenameFilter = new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File dir, String filename) {
|
|
|
|
return filename.startsWith(UserHistoryDictionary.NAME + "." + TEST_LOCALE_PREFIX);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FileUtils.deleteFilteredFiles(dictFile.getParentFile(), filenameFilter);
|
|
|
|
}
|
|
|
|
|
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-20 05:48:56 +00:00
|
|
|
private static void checkExistenceAndRemoveDictFile(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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertTrue("check exisiting of " + dictFile, dictFile.exists());
|
|
|
|
FileUtils.deleteRecursively(dictFile);
|
|
|
|
}
|
|
|
|
|
2014-09-22 05:22:37 +00:00
|
|
|
private static Locale getDummyLocale(final String name) {
|
|
|
|
return new Locale(TEST_LOCALE_PREFIX + name + System.currentTimeMillis());
|
|
|
|
}
|
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
2014-02-20 12:23:57 +00:00
|
|
|
resetCurrentTimeForTestMode();
|
2014-08-13 06:51:16 +00:00
|
|
|
removeAllTestDictFiles();
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void tearDown() throws Exception {
|
2014-08-13 06:51:16 +00:00
|
|
|
removeAllTestDictFiles();
|
2014-02-20 09:36:46 +00:00
|
|
|
stopTestModeInNativeCode();
|
|
|
|
super.tearDown();
|
|
|
|
}
|
|
|
|
|
2014-02-20 12:23:57 +00:00
|
|
|
private void resetCurrentTimeForTestMode() {
|
|
|
|
mCurrentTime = 0;
|
|
|
|
setCurrentTimeForTestMode(mCurrentTime);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-20 10:29:20 +00:00
|
|
|
/**
|
|
|
|
* Generates a random word.
|
|
|
|
*/
|
2014-02-13 07:16:44 +00:00
|
|
|
private static String generateWord(final int value) {
|
2012-08-20 10:29:20 +00:00
|
|
|
final int lengthOfChars = CHARACTERS.length;
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
long lvalue = Math.abs((long)value);
|
|
|
|
while (lvalue > 0) {
|
|
|
|
builder.append(CHARACTERS[(int)(lvalue % lengthOfChars)]);
|
|
|
|
lvalue /= lengthOfChars;
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
2014-02-13 07:16:44 +00:00
|
|
|
private static List<String> generateWords(final int number, final Random random) {
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashSet<String> wordSet = new HashSet<>();
|
2012-08-20 10:29:20 +00:00
|
|
|
while (wordSet.size() < number) {
|
|
|
|
wordSet.add(generateWord(random.nextInt()));
|
|
|
|
}
|
2014-05-23 11:18:17 +00:00
|
|
|
return new ArrayList<>(wordSet);
|
2012-08-20 10:29:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 05:26:51 +00:00
|
|
|
private static void addToDict(final UserHistoryDictionary dict, final List<String> words,
|
|
|
|
final int timestamp) {
|
2014-09-29 01:52:18 +00:00
|
|
|
NgramContext ngramContext = NgramContext.EMPTY_PREV_WORDS_INFO;
|
2012-08-20 10:29:20 +00:00
|
|
|
for (String word : words) {
|
2014-09-29 05:26:51 +00:00
|
|
|
UserHistoryDictionary.addToDictionary(dict, ngramContext, word, true, timestamp,
|
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));
|
2012-08-20 10:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 13:12:52 +00:00
|
|
|
/**
|
2013-09-27 02:13:22 +00:00
|
|
|
* @param checkContents if true, checks whether written words are actually in the dictionary
|
2013-09-06 13:12:52 +00:00
|
|
|
* or not.
|
|
|
|
*/
|
2014-09-22 05:22:37 +00:00
|
|
|
private void addAndWriteRandomWords(final UserHistoryDictionary dict,
|
|
|
|
final int numberOfWords, final Random random, final boolean checkContents) {
|
2013-08-16 05:17:09 +00:00
|
|
|
final List<String> words = generateWords(numberOfWords, random);
|
|
|
|
// Add random words to the user history dictionary.
|
2014-09-29 05:26:51 +00:00
|
|
|
addToDict(dict, words, mCurrentTime);
|
2013-09-27 02:13:22 +00:00
|
|
|
if (checkContents) {
|
2013-12-13 08:09:16 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2013-09-27 02:13:22 +00:00
|
|
|
for (int i = 0; i < numberOfWords; ++i) {
|
2013-09-06 13:12:52 +00:00
|
|
|
final String word = words.get(i);
|
2014-06-09 02:04:28 +00:00
|
|
|
assertTrue(dict.isInDictionary(word));
|
2013-09-06 13:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-16 05:17:09 +00:00
|
|
|
// write to file.
|
|
|
|
dict.close();
|
|
|
|
}
|
|
|
|
|
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-09-22 05:22:37 +00:00
|
|
|
final Locale dummyLocale = getDummyLocale("random_words");
|
2014-02-13 07:16:44 +00:00
|
|
|
final String dictName = ExpandableBinaryDictionary.getDictName(
|
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */);
|
|
|
|
final File dictFile = ExpandableBinaryDictionary.getDictFile(
|
|
|
|
mContext, dictName, null /* dictFile */);
|
2014-09-22 05:22:37 +00:00
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
|
|
|
getContext(), dummyLocale);
|
2013-10-03 11:55:34 +00:00
|
|
|
|
2013-08-16 05:17:09 +00:00
|
|
|
final int numberOfWords = 1000;
|
|
|
|
final Random random = new Random(123456);
|
2014-09-22 08:11:26 +00:00
|
|
|
clearHistory(dict);
|
|
|
|
addAndWriteRandomWords(dict, numberOfWords, random, true /* checksContents */);
|
|
|
|
checkExistenceAndRemoveDictFile(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-09-22 05:22:37 +00:00
|
|
|
final Locale dummyLocale = getDummyLocale("switching_languages" + i);
|
2014-02-13 07:16:44 +00:00
|
|
|
final String dictName = ExpandableBinaryDictionary.getDictName(
|
2014-09-22 05:22:37 +00:00
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */);
|
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(),
|
|
|
|
dummyLocale);
|
|
|
|
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].
|
|
|
|
addAndWriteRandomWords(dicts[index], numberOfWordsInsertedForEachLanguageSwitch,
|
|
|
|
random, false /* checksContents */);
|
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-09-22 05:22:37 +00:00
|
|
|
checkExistenceAndRemoveDictFile(dicts[i], dictFiles[i]);
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 13:57:15 +00:00
|
|
|
|
|
|
|
public void testAddManyWords() {
|
2014-09-22 05:22:37 +00:00
|
|
|
final Locale dummyLocale = getDummyLocale("many_random_words");
|
2014-02-13 07:16:44 +00:00
|
|
|
final String dictName = ExpandableBinaryDictionary.getDictName(
|
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */);
|
|
|
|
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(
|
|
|
|
getContext(), dummyLocale);
|
|
|
|
clearHistory(dict);
|
2013-09-24 13:57:15 +00:00
|
|
|
try {
|
2014-09-22 05:22:37 +00:00
|
|
|
addAndWriteRandomWords(dict, numberOfWords, random, true /* checksContents */);
|
2013-09-24 13:57:15 +00:00
|
|
|
} finally {
|
2014-09-22 05:22:37 +00:00
|
|
|
checkExistenceAndRemoveDictFile(dict, dictFile);
|
2013-09-24 13:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-20 09:36:46 +00:00
|
|
|
|
|
|
|
public void testDecaying() {
|
2014-09-22 05:22:37 +00:00
|
|
|
final Locale dummyLocale = getDummyLocale("decaying");
|
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
|
|
|
getContext(), dummyLocale);
|
2014-02-20 09:36:46 +00:00
|
|
|
final int numberOfWords = 5000;
|
|
|
|
final Random random = new Random(123456);
|
2014-02-20 12:23:57 +00:00
|
|
|
resetCurrentTimeForTestMode();
|
2014-09-22 05:22:37 +00:00
|
|
|
clearHistory(dict);
|
2014-02-20 09:36:46 +00:00
|
|
|
final List<String> words = generateWords(numberOfWords, random);
|
2014-02-20 12:23:57 +00:00
|
|
|
dict.waitAllTasksForTests();
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2012-08-20 10:29:20 +00:00
|
|
|
}
|