From 49b5fadae159d3de6a40a918d4b9db18e5463dc9 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 26 Dec 2013 18:26:28 +0900 Subject: [PATCH] Add DictionaryFacilitatorForSuggest constructor for testing This change must be checked in together with I00b6e79d6b. Change-Id: Id5f87a8d0286eb44958381fefd428f16896dc0d4 --- .../latin/ContactsBinaryDictionary.java | 5 +- .../DictionaryFacilitatorForSuggest.java | 55 ++++++++++++++++--- .../latin/UserBinaryDictionary.java | 2 + 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java index ae41e74e3..a787ef153 100644 --- a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java @@ -16,8 +16,6 @@ package com.android.inputmethod.latin; -import com.android.inputmethod.latin.personalization.AccountUtils; - import android.content.ContentResolver; import android.content.Context; import android.database.ContentObserver; @@ -31,6 +29,8 @@ import android.provider.ContactsContract.Contacts; import android.text.TextUtils; import android.util.Log; +import com.android.inputmethod.annotations.UsedForTesting; +import com.android.inputmethod.latin.personalization.AccountUtils; import com.android.inputmethod.latin.utils.StringUtils; import java.io.File; @@ -85,6 +85,7 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary { } // Dummy constructor for tests. + @UsedForTesting public ContactsBinaryDictionary(final Context context, final Locale locale, final File file) { this(context, locale); } diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java index c7f055393..c9bcfe369 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java @@ -29,6 +29,9 @@ import com.android.inputmethod.latin.personalization.UserHistoryDictionary; import com.android.inputmethod.latin.settings.SettingsValues; import com.android.inputmethod.latin.utils.CollectionUtils; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.Locale; import java.util.Set; @@ -103,13 +106,49 @@ public class DictionaryFacilitatorForSuggest { } @UsedForTesting - DictionaryFacilitatorForSuggest(final Context context, final AssetFileAddress[] dictionaryList, - final Locale locale) { - final Dictionary mainDict = DictionaryFactory.createDictionaryForTest(dictionaryList, - false /* useFullEditDistance */, locale); + public DictionaryFacilitatorForSuggest(final Context context, final Locale locale, + final ArrayList dictionaryTypes, final HashMap dictionaryFiles) { mContext = context; mLocale = locale; - setMainDictionary(mainDict); + for (final String dictType : dictionaryTypes) { + if (dictType.equals(Dictionary.TYPE_MAIN)) { + final DictionaryCollection mainDictionary = + DictionaryFactory.createMainDictionaryFromManager(context, locale); + setMainDictionary(mainDictionary); + } else if (dictType.equals(Dictionary.TYPE_USER_HISTORY)) { + final UserHistoryDictionary userHistoryDictionary = + PersonalizationHelper.getUserHistoryDictionary(context, locale); + // Staring with an empty user history dictionary for testing. + // Testing program may populate this dictionary before actual testing. + userHistoryDictionary.reloadDictionaryIfRequired(); + userHistoryDictionary.waitAllTasksForTests(); + setUserHistoryDictionary(userHistoryDictionary); + } else if (dictType.equals(Dictionary.TYPE_PERSONALIZATION)) { + final PersonalizationDictionary personalizationDictionary = + PersonalizationHelper.getPersonalizationDictionary(context, locale); + // Staring with an empty personalization dictionary for testing. + // Testing program may populate this dictionary before actual testing. + personalizationDictionary.reloadDictionaryIfRequired(); + personalizationDictionary.waitAllTasksForTests(); + setPersonalizationDictionary(personalizationDictionary); + } else if (dictType.equals(Dictionary.TYPE_USER)) { + final File file = dictionaryFiles.get(dictType); + final UserBinaryDictionary userDictionary = new UserBinaryDictionary( + context, locale, file); + userDictionary.reloadDictionaryIfRequired(); + userDictionary.waitAllTasksForTests(); + setUserDictionary(userDictionary); + } else if (dictType.equals(Dictionary.TYPE_CONTACTS)) { + final File file = dictionaryFiles.get(dictType); + final ContactsBinaryDictionary contactsDictionary = new ContactsBinaryDictionary( + context, locale, file); + contactsDictionary.reloadDictionaryIfRequired(); + contactsDictionary.waitAllTasksForTests(); + setContactsDictionary(contactsDictionary); + } else { + throw new RuntimeException("Unknown dictionary type: " + dictType); + } + } } // initialize a debug flag for the personalization @@ -169,8 +208,7 @@ public class DictionaryFacilitatorForSuggest { * Sets an optional user dictionary resource to be loaded. The user dictionary is consulted * before the main dictionary, if set. This refers to the system-managed user dictionary. */ - @UsedForTesting - public void setUserDictionary(final UserBinaryDictionary userDictionary) { + private void setUserDictionary(final UserBinaryDictionary userDictionary) { mUserDictionary = userDictionary; addOrReplaceDictionary(Dictionary.TYPE_USER, userDictionary); } @@ -180,8 +218,7 @@ public class DictionaryFacilitatorForSuggest { * the contacts dictionary by passing null to this method. In this case no contacts dictionary * won't be used. */ - @UsedForTesting - public void setContactsDictionary(final ContactsBinaryDictionary contactsDictionary) { + private void setContactsDictionary(final ContactsBinaryDictionary contactsDictionary) { mContactsDictionary = contactsDictionary; addOrReplaceDictionary(Dictionary.TYPE_CONTACTS, contactsDictionary); } diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java index 68648dd62..8011247c6 100644 --- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java @@ -28,6 +28,7 @@ import android.provider.UserDictionary.Words; import android.text.TextUtils; import android.util.Log; +import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.compat.UserDictionaryCompatUtils; import com.android.inputmethod.latin.utils.LocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; @@ -81,6 +82,7 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { } // Dummy constructor for tests. + @UsedForTesting public UserBinaryDictionary(final Context context, final Locale locale, final File file) { this(context, locale); }