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.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
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;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
|
|
|
|
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;
|
|
|
|
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 SharedPreferences mPrefs;
|
|
|
|
|
|
|
|
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"
|
|
|
|
};
|
|
|
|
|
2013-08-16 05:17:09 +00:00
|
|
|
private static final int MIN_USER_HISTORY_DICTIONARY_FILE_SIZE = 1000;
|
2013-09-13 08:32:51 +00:00
|
|
|
private static final int WAIT_TERMINATING_IN_MILLISECONDS = 100;
|
2013-08-16 05:17:09 +00:00
|
|
|
|
2012-08-20 10:29:20 +00:00
|
|
|
@Override
|
|
|
|
public void setUp() {
|
|
|
|
mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a random word.
|
|
|
|
*/
|
|
|
|
private String generateWord(final int value) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<String> generateWords(final int number, final Random random) {
|
|
|
|
final Set<String> wordSet = CollectionUtils.newHashSet();
|
|
|
|
while (wordSet.size() < number) {
|
|
|
|
wordSet.add(generateWord(random.nextInt()));
|
|
|
|
}
|
|
|
|
return new ArrayList<String>(wordSet);
|
|
|
|
}
|
|
|
|
|
2013-09-28 03:50:09 +00:00
|
|
|
private 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-09-28 03:50:09 +00:00
|
|
|
dict.addToDictionary(prevWord, word, true);
|
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.
|
|
|
|
*/
|
2013-08-19 01:17:02 +00:00
|
|
|
private void addAndWriteRandomWords(final String testFilenameSuffix, 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);
|
2013-09-28 03:50:09 +00:00
|
|
|
final UserHistoryDictionary dict =
|
|
|
|
PersonalizationHelper.getUserHistoryDictionary(getContext(),
|
2013-08-19 01:17:02 +00:00
|
|
|
testFilenameSuffix /* locale */, mPrefs);
|
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-09-06 13:12:52 +00:00
|
|
|
try {
|
|
|
|
Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS));
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
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);
|
|
|
|
assertTrue(dict.isInDictionaryForTests(word));
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 05:17:09 +00:00
|
|
|
// write to file.
|
|
|
|
dict.close();
|
|
|
|
}
|
|
|
|
|
2012-08-20 10:29:20 +00:00
|
|
|
public void testRandomWords() {
|
2012-10-05 06:12:50 +00:00
|
|
|
File dictFile = null;
|
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.");
|
2013-08-19 01:17:02 +00:00
|
|
|
final String testFilenameSuffix = "testRandomWords" + System.currentTimeMillis();
|
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 {
|
2013-09-06 13:12:52 +00:00
|
|
|
addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random,
|
|
|
|
true /* checksContents */);
|
2013-08-16 05:17:09 +00:00
|
|
|
} finally {
|
2012-10-05 06:12:50 +00:00
|
|
|
try {
|
2013-09-28 03:50:09 +00:00
|
|
|
final UserHistoryDictionary dict =
|
|
|
|
PersonalizationHelper.getUserHistoryDictionary(getContext(),
|
2013-09-13 08:32:51 +00:00
|
|
|
testFilenameSuffix, mPrefs);
|
2013-08-16 05:17:09 +00:00
|
|
|
Log.d(TAG, "waiting for writing ...");
|
2013-09-13 08:32:51 +00:00
|
|
|
dict.shutdownExecutorForTests();
|
|
|
|
while (!dict.isTerminatedForTests()) {
|
|
|
|
Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
|
|
|
|
}
|
2012-10-05 06:12:50 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.d(TAG, "InterruptedException: " + e);
|
|
|
|
}
|
|
|
|
|
2013-09-28 03:50:09 +00:00
|
|
|
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix
|
2013-08-19 01:17:02 +00:00
|
|
|
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
|
2013-08-16 05:17:09 +00:00
|
|
|
dictFile = new File(getContext().getFilesDir(), fileName);
|
2012-10-05 06:12:50 +00:00
|
|
|
|
|
|
|
if (dictFile != null) {
|
2013-08-16 05:17:09 +00:00
|
|
|
assertTrue(dictFile.exists());
|
|
|
|
assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
|
2012-10-05 06:12:50 +00:00
|
|
|
dictFile.delete();
|
|
|
|
}
|
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];
|
2013-09-13 08:32:51 +00:00
|
|
|
final String testFilenameSuffixes[] = new String[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++) {
|
2013-08-16 05:17:09 +00:00
|
|
|
testFilenameSuffixes[i] = "testSwitchingLanguages" + i;
|
2013-09-28 03:50:09 +00:00
|
|
|
final String fileName = UserHistoryDictionary.NAME + "." +
|
2013-08-19 01:17:02 +00:00
|
|
|
testFilenameSuffixes[i] + ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
|
2013-07-16 03:17:56 +00:00
|
|
|
dictFiles[i] = new File(getContext().getFilesDir(), fileName);
|
|
|
|
}
|
|
|
|
|
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].
|
|
|
|
addAndWriteRandomWords(testFilenameSuffixes[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-07-16 03:17:56 +00:00
|
|
|
try {
|
|
|
|
Log.d(TAG, "waiting for writing ...");
|
2013-09-13 08:32:51 +00:00
|
|
|
for (int i = 0; i < numberOfLanguages; i++) {
|
2013-09-28 03:50:09 +00:00
|
|
|
final UserHistoryDictionary dict =
|
|
|
|
PersonalizationHelper.getUserHistoryDictionary(getContext(),
|
2013-09-13 08:32:51 +00:00
|
|
|
testFilenameSuffixes[i], mPrefs);
|
|
|
|
dict.shutdownExecutorForTests();
|
|
|
|
while (!dict.isTerminatedForTests()) {
|
|
|
|
Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 03:17:56 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.d(TAG, "InterruptedException: " + e);
|
|
|
|
}
|
|
|
|
for (final File file : dictFiles) {
|
|
|
|
if (file != null) {
|
2013-08-16 05:17:09 +00:00
|
|
|
assertTrue(file.exists());
|
|
|
|
assertTrue(file.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
|
2013-07-16 03:17:56 +00:00
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 13:57:15 +00:00
|
|
|
|
|
|
|
public void testAddManyWords() {
|
|
|
|
File dictFile = null;
|
|
|
|
final String testFilenameSuffix = "testRandomWords" + System.currentTimeMillis();
|
|
|
|
final int numberOfWords =
|
|
|
|
ExpandableBinaryDictionary.ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE ?
|
|
|
|
10000 : 1000;
|
|
|
|
final Random random = new Random(123456);
|
|
|
|
|
2013-09-28 03:50:09 +00:00
|
|
|
UserHistoryDictionary dict =
|
|
|
|
PersonalizationHelper.getUserHistoryDictionary(getContext(),
|
2013-09-24 13:57:15 +00:00
|
|
|
testFilenameSuffix, mPrefs);
|
|
|
|
try {
|
|
|
|
addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random,
|
|
|
|
true /* checksContents */);
|
|
|
|
dict.close();
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
Log.d(TAG, "waiting for writing ...");
|
|
|
|
dict.shutdownExecutorForTests();
|
|
|
|
while (!dict.isTerminatedForTests()) {
|
|
|
|
Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.d(TAG, "InterruptedException: ", e);
|
|
|
|
}
|
2013-09-28 03:50:09 +00:00
|
|
|
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix
|
2013-09-24 13:57:15 +00:00
|
|
|
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
|
|
|
|
dictFile = new File(getContext().getFilesDir(), fileName);
|
|
|
|
if (dictFile != null) {
|
|
|
|
assertTrue(dictFile.exists());
|
|
|
|
assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 10:29:20 +00:00
|
|
|
}
|