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;
|
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
import com.android.inputmethod.latin.BinaryDictionary;
|
2013-08-19 01:17:02 +00:00
|
|
|
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
2014-02-13 07:16:44 +00:00
|
|
|
import com.android.inputmethod.latin.utils.FileUtils;
|
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.ArrayList;
|
|
|
|
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;
|
|
|
|
import java.util.Set;
|
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();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
2014-02-20 12:23:57 +00:00
|
|
|
resetCurrentTimeForTestMode();
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void tearDown() throws Exception {
|
|
|
|
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() {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-08-20 10:29:20 +00:00
|
|
|
final Set<String> wordSet = CollectionUtils.newHashSet();
|
|
|
|
while (wordSet.size() < number) {
|
|
|
|
wordSet.add(generateWord(random.nextInt()));
|
|
|
|
}
|
|
|
|
return new ArrayList<String>(wordSet);
|
|
|
|
}
|
|
|
|
|
2014-02-13 07:16:44 +00:00
|
|
|
private static void addToDict(final UserHistoryDictionary dict, final List<String> words) {
|
2012-08-20 10:29:20 +00:00
|
|
|
String prevWord = null;
|
|
|
|
for (String word : words) {
|
2013-12-13 08:09:16 +00:00
|
|
|
dict.addToDictionary(prevWord, word, true,
|
|
|
|
(int)TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
|
2012-08-20 10:29:20 +00:00
|
|
|
prevWord = word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-13 07:16:44 +00:00
|
|
|
private void addAndWriteRandomWords(final Locale locale, final int numberOfWords,
|
2013-09-27 02:13:22 +00:00
|
|
|
final Random random, final boolean checkContents) {
|
2013-08-16 05:17:09 +00:00
|
|
|
final List<String> words = generateWords(numberOfWords, random);
|
2014-02-13 07:16:44 +00:00
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
|
|
|
mContext, locale);
|
2013-08-16 05:17:09 +00:00
|
|
|
// Add random words to the user history dictionary.
|
|
|
|
addToDict(dict, words);
|
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-02-05 08:00:51 +00:00
|
|
|
assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(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-02-13 07:16:44 +00:00
|
|
|
* @param locale dummy locale for testing.
|
2013-10-03 11:55:34 +00:00
|
|
|
*/
|
2014-02-13 07:16:44 +00:00
|
|
|
private void clearHistory(final Locale locale) {
|
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
|
|
|
mContext, locale);
|
2014-01-15 02:18:39 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2013-10-03 11:55:34 +00:00
|
|
|
dict.clearAndFlushDictionary();
|
|
|
|
dict.close();
|
2014-01-15 02:18:39 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2013-10-03 11:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shut down executer and wait until all operations of user history are done.
|
2014-02-13 07:16:44 +00:00
|
|
|
* @param locale dummy locale for testing.
|
2013-10-03 11:55:34 +00:00
|
|
|
*/
|
2014-02-13 07:16:44 +00:00
|
|
|
private void waitForWriting(final Locale locale) {
|
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
|
|
|
mContext, locale);
|
2013-12-13 08:09:16 +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-02-13 07:16:44 +00:00
|
|
|
final Locale dummyLocale = new Locale("test_random_words" + System.currentTimeMillis());
|
|
|
|
final String dictName = ExpandableBinaryDictionary.getDictName(
|
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */);
|
|
|
|
final File dictFile = ExpandableBinaryDictionary.getDictFile(
|
|
|
|
mContext, dictName, null /* dictFile */);
|
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);
|
2012-10-05 06:12:50 +00:00
|
|
|
|
2013-08-16 05:17:09 +00:00
|
|
|
try {
|
2014-02-13 07:16:44 +00:00
|
|
|
clearHistory(dummyLocale);
|
|
|
|
addAndWriteRandomWords(dummyLocale, numberOfWords, random,
|
2013-09-06 13:12:52 +00:00
|
|
|
true /* checksContents */);
|
2013-08-16 05:17:09 +00:00
|
|
|
} finally {
|
2013-10-03 11:55:34 +00:00
|
|
|
Log.d(TAG, "waiting for writing ...");
|
2014-02-13 07:16:44 +00:00
|
|
|
waitForWriting(dummyLocale);
|
|
|
|
assertTrue("check exisiting of " + dictFile, dictFile.exists());
|
|
|
|
FileUtils.deleteRecursively(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-02-13 07:16:44 +00:00
|
|
|
final Locale dummyLocales[] = new Locale[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-02-13 07:16:44 +00:00
|
|
|
dummyLocales[i] = new Locale("test_switching_languages" + i);
|
|
|
|
final String dictName = ExpandableBinaryDictionary.getDictName(
|
|
|
|
UserHistoryDictionary.NAME, dummyLocales[i], null /* dictFile */);
|
|
|
|
dictFiles[i] = ExpandableBinaryDictionary.getDictFile(
|
|
|
|
mContext, dictName, null /* dictFile */);
|
|
|
|
clearHistory(dummyLocales[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;
|
2013-08-16 05:17:09 +00:00
|
|
|
// Switch languages to testFilenameSuffixes[index].
|
2014-02-13 07:16:44 +00:00
|
|
|
addAndWriteRandomWords(dummyLocales[index],
|
2013-09-06 13:12:52 +00:00
|
|
|
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
|
|
|
Log.d(TAG, "waiting for writing ...");
|
|
|
|
for (int i = 0; i < numberOfLanguages; i++) {
|
2014-02-13 07:16:44 +00:00
|
|
|
waitForWriting(dummyLocales[i]);
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
2014-02-13 07:16:44 +00:00
|
|
|
for (final File dictFile : dictFiles) {
|
|
|
|
assertTrue("check exisiting of " + dictFile, dictFile.exists());
|
|
|
|
FileUtils.deleteRecursively(dictFile);
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 13:57:15 +00:00
|
|
|
|
|
|
|
public void testAddManyWords() {
|
2014-02-13 07:16:44 +00:00
|
|
|
final Locale dummyLocale = new Locale("test_random_words" + System.currentTimeMillis());
|
|
|
|
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-02-13 07:16:44 +00:00
|
|
|
clearHistory(dummyLocale);
|
2013-09-24 13:57:15 +00:00
|
|
|
try {
|
2014-02-13 07:16:44 +00:00
|
|
|
addAndWriteRandomWords(dummyLocale, numberOfWords, random, true /* checksContents */);
|
2013-09-24 13:57:15 +00:00
|
|
|
} finally {
|
2013-10-03 11:55:34 +00:00
|
|
|
Log.d(TAG, "waiting for writing ...");
|
2014-02-13 07:16:44 +00:00
|
|
|
waitForWriting(dummyLocale);
|
|
|
|
assertTrue("check exisiting of " + dictFile, dictFile.exists());
|
|
|
|
FileUtils.deleteRecursively(dictFile);
|
2013-09-24 13:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-20 09:36:46 +00:00
|
|
|
|
|
|
|
public void testDecaying() {
|
|
|
|
final Locale dummyLocale = new Locale("test_decaying" + System.currentTimeMillis());
|
|
|
|
final int numberOfWords = 5000;
|
|
|
|
final Random random = new Random(123456);
|
2014-02-20 12:23:57 +00:00
|
|
|
resetCurrentTimeForTestMode();
|
2014-02-20 09:36:46 +00:00
|
|
|
clearHistory(dummyLocale);
|
|
|
|
final List<String> words = generateWords(numberOfWords, random);
|
|
|
|
final UserHistoryDictionary dict =
|
|
|
|
PersonalizationHelper.getUserHistoryDictionary(getContext(), dummyLocale);
|
2014-02-20 12:23:57 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-02-20 09:36:46 +00:00
|
|
|
String prevWord = null;
|
|
|
|
for (final String word : words) {
|
|
|
|
dict.addToDictionary(prevWord, word, true, mCurrentTime);
|
|
|
|
prevWord = word;
|
|
|
|
assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word));
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word));
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
assertFalse(dict.isInUnderlyingBinaryDictionaryForTests(word));
|
|
|
|
}
|
|
|
|
}
|
2012-08-20 10:29:20 +00:00
|
|
|
}
|