From 9bd6dac4708ad94fd0257c53e977df62b152e20c Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Thu, 12 Dec 2013 20:14:06 +0900 Subject: [PATCH] Add a constructor for testing to non-main dictionaries. The new constructor is used for creating dictionary instance that uses a given file. Bug: 11956652 Change-Id: Ifbf420a1a768eb95a5c3a58eeb1be5936b306d35 --- .../latin/ExpandableBinaryDictionary.java | 14 +++++++++++++- .../DecayingExpandableBinaryDictionaryBase.java | 13 +++++++++++++ .../personalization/PersonalizationDictionary.java | 10 ++++++++++ .../personalization/UserHistoryDictionary.java | 10 ++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java index ed80a9629..9f5cd162f 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java @@ -105,6 +105,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { /** Whether to support dynamically updating the dictionary */ private final boolean mIsUpdatable; + /** Dictionary file */ + private final File mDictFile; + // TODO: remove, once dynamic operations is serialized /** Controls updating the shared binary dictionary file across multiple instances. */ private final DictionaryUpdateController mDictNameDictionaryUpdateController; @@ -146,7 +149,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { } private File getDictFile() { - return new File(mContext.getFilesDir(), mDictName + DICT_FILE_EXTENSION); + return mDictFile; } /** @@ -200,11 +203,20 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { */ public ExpandableBinaryDictionary(final Context context, final String dictName, final Locale locale, final String dictType, final boolean isUpdatable) { + this(context, dictName, locale, dictType, isUpdatable, + new File(context.getFilesDir(), dictName + DICT_FILE_EXTENSION)); + } + + // Creates an instance that uses a given dictionary file. + public ExpandableBinaryDictionary(final Context context, final String dictName, + final Locale locale, final String dictType, final boolean isUpdatable, + final File dictFile) { super(dictType); mDictName = dictName; mContext = context; mLocale = locale; mIsUpdatable = isUpdatable; + mDictFile = dictFile; mBinaryDictionary = null; mDictNameDictionaryUpdateController = getDictionaryUpdateController(dictName); // Currently, only dynamic personalization dictionary is updatable. diff --git a/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java b/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java index ceec3e1cb..296733fad 100644 --- a/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java +++ b/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java @@ -70,6 +70,19 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB } } + // Creates an instance that uses a given dictionary file for testing. + @UsedForTesting + /* package */ DecayingExpandableBinaryDictionaryBase(final Context context, + final Locale locale, final String dictionaryType, final String dictName, + final File dictFile) { + super(context, dictName, locale, dictionaryType, true, dictFile); + mLocale = locale; + mDictName = dictName; + if (mLocale != null && mLocale.toString().length() > 1) { + reloadDictionaryIfRequired(); + } + } + @Override public void close() { if (DBG_DUMP_ON_CLOSE) { diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java index b1ec76f28..596562f1d 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java @@ -16,9 +16,11 @@ package com.android.inputmethod.latin.personalization; +import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.utils.CollectionUtils; +import java.io.File; import java.util.ArrayList; import java.util.Locale; @@ -35,6 +37,14 @@ public class PersonalizationDictionary extends DecayingExpandableBinaryDictionar getDictNameWithLocale(NAME, locale)); } + // Creates an instance that uses a given dictionary file for testing. + @UsedForTesting + public PersonalizationDictionary(final Context context, final Locale locale, + final File dictFile) { + super(context, locale, Dictionary.TYPE_PERSONALIZATION, getDictNameWithLocale(NAME, locale), + dictFile); + } + public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) { session.setPredictionDictionary(this); mSessions.add(session); diff --git a/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java index 3f03de0d4..868f21cbc 100644 --- a/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java +++ b/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java @@ -16,8 +16,10 @@ package com.android.inputmethod.latin.personalization; +import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.latin.Dictionary; +import java.io.File; import java.util.Locale; import android.content.Context; @@ -33,6 +35,14 @@ public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBas super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictNameWithLocale(NAME, locale)); } + // Creates an instance that uses a given dictionary file for testing. + @UsedForTesting + public UserHistoryDictionary(final Context context, final Locale locale, + final File dictFile) { + super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictNameWithLocale(NAME, locale), + dictFile); + } + public void cancelAddingUserHistory(final String word0, final String word1) { removeBigramDynamically(word0, word1); }