am 36b8962a: Merge "Fix UserHistoryDictionaryTests"

* commit '36b8962a9ae063e51567ccae25d81b841cb322f0':
  Fix UserHistoryDictionaryTests
main
Tadashi G. Takaoka 2014-02-12 23:52:10 -08:00 committed by Android Git Automerger
commit 8cdc727346
3 changed files with 70 additions and 79 deletions

View File

@ -122,7 +122,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
new DictionaryUpdateController(); new DictionaryUpdateController();
/* A extension for a binary dictionary file. */ /* A extension for a binary dictionary file. */
public static final String DICT_FILE_EXTENSION = ".dict"; protected static final String DICT_FILE_EXTENSION = ".dict";
private final AtomicReference<Runnable> mUnfinishedFlushingTask = private final AtomicReference<Runnable> mUnfinishedFlushingTask =
new AtomicReference<Runnable>(); new AtomicReference<Runnable>();
@ -148,10 +148,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
return mBinaryDictionary.isValidDictionary(); return mBinaryDictionary.isValidDictionary();
} }
private File getDictFile() {
return mDictFile;
}
/** /**
* Gets the dictionary update controller for the given dictionary name. * Gets the dictionary update controller for the given dictionary name.
*/ */
@ -224,15 +220,20 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mContext = context; mContext = context;
mLocale = locale; mLocale = locale;
mIsUpdatable = isUpdatable; mIsUpdatable = isUpdatable;
mDictFile = (dictFile != null) ? dictFile mDictFile = getDictFile(context, dictName, dictFile);
: new File(context.getFilesDir(), dictName + DICT_FILE_EXTENSION);
mBinaryDictionary = null; mBinaryDictionary = null;
mDictNameDictionaryUpdateController = getDictionaryUpdateController(dictName); mDictNameDictionaryUpdateController = getDictionaryUpdateController(dictName);
// Currently, only dynamic personalization dictionary is updatable. // Currently, only dynamic personalization dictionary is updatable.
mDictionaryWriter = getDictionaryWriter(isUpdatable); mDictionaryWriter = getDictionaryWriter(isUpdatable);
} }
protected static String getDictName(final String name, final Locale locale, public static File getDictFile(final Context context, final String dictName,
final File dictFile) {
return (dictFile != null) ? dictFile
: new File(context.getFilesDir(), dictName + DICT_FILE_EXTENSION);
}
public static String getDictName(final String name, final Locale locale,
final File dictFile) { final File dictFile) {
return dictFile != null ? dictFile.getName() : name + "." + locale.toString(); return dictFile != null ? dictFile.getName() : name + "." + locale.toString();
} }
@ -276,6 +277,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
protected void clear() { protected void clear() {
final File dictFile = mDictFile;
getExecutor(mDictName).execute(new Runnable() { getExecutor(mDictName).execute(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -283,14 +285,13 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
if (mBinaryDictionary != null) { if (mBinaryDictionary != null) {
mBinaryDictionary.close(); mBinaryDictionary.close();
} }
final File file = getDictFile(); if (dictFile.exists() && !FileUtils.deleteRecursively(dictFile)) {
if (file.exists() && !FileUtils.deleteRecursively(file)) { Log.e(TAG, "Can't remove a file: " + dictFile.getName());
Log.e(TAG, "Can't remove a file: " + file.getName());
} }
BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(), BinaryDictionary.createEmptyDictFile(dictFile.getAbsolutePath(),
DICTIONARY_FORMAT_VERSION, mLocale, getHeaderAttributeMap()); DICTIONARY_FORMAT_VERSION, mLocale, getHeaderAttributeMap());
mBinaryDictionary = new BinaryDictionary( mBinaryDictionary = new BinaryDictionary(
file.getAbsolutePath(), 0 /* offset */, file.length(), dictFile.getAbsolutePath(), 0 /* offset */, dictFile.length(),
true /* useFullEditDistance */, mLocale, mDictType, mIsUpdatable); true /* useFullEditDistance */, mLocale, mDictType, mIsUpdatable);
} else { } else {
mDictionaryWriter.clear(); mDictionaryWriter.clear();
@ -541,9 +542,8 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
} }
final File file = getDictFile(); final String filename = mDictFile.getAbsolutePath();
final String filename = file.getAbsolutePath(); final long length = mDictFile.length();
final long length = file.length();
// Build the new binary dictionary // Build the new binary dictionary
final BinaryDictionary newBinaryDictionary = new BinaryDictionary(filename, 0 /* offset */, final BinaryDictionary newBinaryDictionary = new BinaryDictionary(filename, 0 /* offset */,
@ -582,17 +582,16 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
if (needsToReloadBeforeWriting()) { if (needsToReloadBeforeWriting()) {
mDictionaryWriter.clear(); mDictionaryWriter.clear();
loadDictionaryAsync(); loadDictionaryAsync();
mDictionaryWriter.write(getDictFile(), getHeaderAttributeMap()); mDictionaryWriter.write(mDictFile, getHeaderAttributeMap());
} else { } else {
if (mBinaryDictionary == null || !isValidDictionary() if (mBinaryDictionary == null || !isValidDictionary()
// TODO: remove the check below // TODO: remove the check below
|| !matchesExpectedBinaryDictFormatVersionForThisType( || !matchesExpectedBinaryDictFormatVersionForThisType(
mBinaryDictionary.getFormatVersion())) { mBinaryDictionary.getFormatVersion())) {
final File file = getDictFile(); if (mDictFile.exists() && !FileUtils.deleteRecursively(mDictFile)) {
if (file.exists() && !FileUtils.deleteRecursively(file)) { Log.e(TAG, "Can't remove a file: " + mDictFile.getName());
Log.e(TAG, "Can't remove a file: " + file.getName());
} }
BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(), BinaryDictionary.createEmptyDictFile(mDictFile.getAbsolutePath(),
DICTIONARY_FORMAT_VERSION, mLocale, getHeaderAttributeMap()); DICTIONARY_FORMAT_VERSION, mLocale, getHeaderAttributeMap());
} else { } else {
if (mBinaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) { if (mBinaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
@ -716,7 +715,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
// TODO: cache the file's existence so that we avoid doing a disk access each time. // TODO: cache the file's existence so that we avoid doing a disk access each time.
private boolean dictionaryFileExists() { private boolean dictionaryFileExists() {
return getDictFile().exists(); return mDictFile.exists();
} }
/** /**

View File

@ -51,11 +51,10 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
private final String mDictName; private final String mDictName;
/* package */ DecayingExpandableBinaryDictionaryBase(final Context context, protected DecayingExpandableBinaryDictionaryBase(final Context context,
final String dictName, final Locale locale, final String dictionaryType, final String dictName, final Locale locale, final String dictionaryType,
final File dictFile) { final File dictFile) {
super(context, getDictName(dictName, locale, dictFile), locale, dictionaryType, super(context, dictName, locale, dictionaryType, true /* isUpdatable */, dictFile);
true /* isUpdatable */, dictFile);
mLocale = locale; mLocale = locale;
mDictName = dictName; mDictName = dictName;
if (mLocale != null && mLocale.toString().length() > 1) { if (mLocale != null && mLocale.toString().length() > 1) {

View File

@ -22,6 +22,7 @@ import android.util.Log;
import com.android.inputmethod.latin.ExpandableBinaryDictionary; import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.FileUtils;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -46,7 +47,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
/** /**
* Generates a random word. * Generates a random word.
*/ */
private String generateWord(final int value) { private static String generateWord(final int value) {
final int lengthOfChars = CHARACTERS.length; final int lengthOfChars = CHARACTERS.length;
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
long lvalue = Math.abs((long)value); long lvalue = Math.abs((long)value);
@ -57,7 +58,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
return builder.toString(); return builder.toString();
} }
private List<String> generateWords(final int number, final Random random) { private static List<String> generateWords(final int number, final Random random) {
final Set<String> wordSet = CollectionUtils.newHashSet(); final Set<String> wordSet = CollectionUtils.newHashSet();
while (wordSet.size() < number) { while (wordSet.size() < number) {
wordSet.add(generateWord(random.nextInt())); wordSet.add(generateWord(random.nextInt()));
@ -65,7 +66,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
return new ArrayList<String>(wordSet); return new ArrayList<String>(wordSet);
} }
private void addToDict(final UserHistoryDictionary dict, final List<String> words) { private static void addToDict(final UserHistoryDictionary dict, final List<String> words) {
String prevWord = null; String prevWord = null;
for (String word : words) { for (String word : words) {
dict.addToDictionary(prevWord, word, true, dict.addToDictionary(prevWord, word, true,
@ -78,12 +79,11 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
* @param checkContents if true, checks whether written words are actually in the dictionary * @param checkContents if true, checks whether written words are actually in the dictionary
* or not. * or not.
*/ */
private void addAndWriteRandomWords(final String testFilenameSuffix, final int numberOfWords, private void addAndWriteRandomWords(final Locale locale, final int numberOfWords,
final Random random, final boolean checkContents) { final Random random, final boolean checkContents) {
final List<String> words = generateWords(numberOfWords, random); final List<String> words = generateWords(numberOfWords, random);
final UserHistoryDictionary dict = final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
PersonalizationHelper.getUserHistoryDictionary(getContext(), mContext, locale);
new Locale(testFilenameSuffix));
// Add random words to the user history dictionary. // Add random words to the user history dictionary.
addToDict(dict, words); addToDict(dict, words);
if (checkContents) { if (checkContents) {
@ -99,12 +99,11 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
/** /**
* Clear all entries in the user history dictionary. * Clear all entries in the user history dictionary.
* @param testFilenameSuffix file name suffix used for testing. * @param locale dummy locale for testing.
*/ */
private void clearHistory(final String testFilenameSuffix) { private void clearHistory(final Locale locale) {
final UserHistoryDictionary dict = final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
PersonalizationHelper.getUserHistoryDictionary(getContext(), mContext, locale);
new Locale(testFilenameSuffix));
dict.waitAllTasksForTests(); dict.waitAllTasksForTests();
dict.clearAndFlushDictionary(); dict.clearAndFlushDictionary();
dict.close(); dict.close();
@ -113,37 +112,35 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
/** /**
* Shut down executer and wait until all operations of user history are done. * Shut down executer and wait until all operations of user history are done.
* @param testFilenameSuffix file name suffix used for testing. * @param locale dummy locale for testing.
*/ */
private void waitForWriting(final String testFilenameSuffix) { private void waitForWriting(final Locale locale) {
final UserHistoryDictionary dict = final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
PersonalizationHelper.getUserHistoryDictionary(getContext(), mContext, locale);
new Locale(testFilenameSuffix));
dict.waitAllTasksForTests(); dict.waitAllTasksForTests();
} }
public void testRandomWords() { public void testRandomWords() {
Log.d(TAG, "This test can be used for profiling."); Log.d(TAG, "This test can be used for profiling.");
Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true."); Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true.");
final String testFilenameSuffix = "test_random_words" + System.currentTimeMillis(); final Locale dummyLocale = new Locale("test_random_words" + System.currentTimeMillis());
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix final String dictName = ExpandableBinaryDictionary.getDictName(
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION; UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */);
final File dictFile = ExpandableBinaryDictionary.getDictFile(
mContext, dictName, null /* dictFile */);
final int numberOfWords = 1000; final int numberOfWords = 1000;
final Random random = new Random(123456); final Random random = new Random(123456);
try { try {
clearHistory(testFilenameSuffix); clearHistory(dummyLocale);
addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random, addAndWriteRandomWords(dummyLocale, numberOfWords, random,
true /* checksContents */); true /* checksContents */);
} finally { } finally {
Log.d(TAG, "waiting for writing ..."); Log.d(TAG, "waiting for writing ...");
waitForWriting(testFilenameSuffix); waitForWriting(dummyLocale);
final File dictFile = new File(getContext().getFilesDir(), fileName); assertTrue("check exisiting of " + dictFile, dictFile.exists());
if (dictFile != null) { FileUtils.deleteRecursively(dictFile);
assertTrue(dictFile.exists());
dictFile.delete();
}
} }
} }
@ -153,17 +150,18 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
final int numberOfWordsInsertedForEachLanguageSwitch = 100; final int numberOfWordsInsertedForEachLanguageSwitch = 100;
final File dictFiles[] = new File[numberOfLanguages]; final File dictFiles[] = new File[numberOfLanguages];
final String testFilenameSuffixes[] = new String[numberOfLanguages]; final Locale dummyLocales[] = new Locale[numberOfLanguages];
try { try {
final Random random = new Random(123456); final Random random = new Random(123456);
// Create filename suffixes for this test. // Create filename suffixes for this test.
for (int i = 0; i < numberOfLanguages; i++) { for (int i = 0; i < numberOfLanguages; i++) {
testFilenameSuffixes[i] = "test_switching_languages" + i; dummyLocales[i] = new Locale("test_switching_languages" + i);
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffixes[i] final String dictName = ExpandableBinaryDictionary.getDictName(
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION; UserHistoryDictionary.NAME, dummyLocales[i], null /* dictFile */);
dictFiles[i] = new File(getContext().getFilesDir(), fileName); dictFiles[i] = ExpandableBinaryDictionary.getDictFile(
clearHistory(testFilenameSuffixes[i]); mContext, dictName, null /* dictFile */);
clearHistory(dummyLocales[i]);
} }
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
@ -171,7 +169,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
for (int i = 0; i < numberOfLanguageSwitching; i++) { for (int i = 0; i < numberOfLanguageSwitching; i++) {
final int index = i % numberOfLanguages; final int index = i % numberOfLanguages;
// Switch languages to testFilenameSuffixes[index]. // Switch languages to testFilenameSuffixes[index].
addAndWriteRandomWords(testFilenameSuffixes[index], addAndWriteRandomWords(dummyLocales[index],
numberOfWordsInsertedForEachLanguageSwitch, random, numberOfWordsInsertedForEachLanguageSwitch, random,
false /* checksContents */); false /* checksContents */);
} }
@ -182,36 +180,31 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
} finally { } finally {
Log.d(TAG, "waiting for writing ..."); Log.d(TAG, "waiting for writing ...");
for (int i = 0; i < numberOfLanguages; i++) { for (int i = 0; i < numberOfLanguages; i++) {
waitForWriting(testFilenameSuffixes[i]); waitForWriting(dummyLocales[i]);
} }
for (final File file : dictFiles) { for (final File dictFile : dictFiles) {
if (file != null) { assertTrue("check exisiting of " + dictFile, dictFile.exists());
assertTrue(file.exists()); FileUtils.deleteRecursively(dictFile);
file.delete();
}
} }
} }
} }
public void testAddManyWords() { public void testAddManyWords() {
final String testFilenameSuffix = "test_random_words" + System.currentTimeMillis(); 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 */);
final int numberOfWords = 10000; final int numberOfWords = 10000;
final Random random = new Random(123456); final Random random = new Random(123456);
clearHistory(testFilenameSuffix); clearHistory(dummyLocale);
try { try {
addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random, addAndWriteRandomWords(dummyLocale, numberOfWords, random, true /* checksContents */);
true /* checksContents */);
} finally { } finally {
Log.d(TAG, "waiting for writing ..."); Log.d(TAG, "waiting for writing ...");
waitForWriting(testFilenameSuffix); waitForWriting(dummyLocale);
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix assertTrue("check exisiting of " + dictFile, dictFile.exists());
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION; FileUtils.deleteRecursively(dictFile);
final File dictFile = new File(getContext().getFilesDir(), fileName);
if (dictFile != null) {
assertTrue(dictFile.exists());
dictFile.delete();
}
} }
} }
} }