aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTestsHelper.java
diff options
context:
space:
mode:
authorJatin Matani <jatinm@google.com>2014-12-08 20:00:09 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2014-12-08 20:00:10 +0000
commit2bdd5290a979f6412a91a7f0a4a97b07e5955dd2 (patch)
tree91ba46889b76d2ce707525bf229fd6d1960ddd9d /tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTestsHelper.java
parent639306faf1c0a4df9a73f462a2c10c6e473affd6 (diff)
parent12d63820d459edd71a46fa2495fea98b3c785f2d (diff)
downloadlatinime-2bdd5290a979f6412a91a7f0a4a97b07e5955dd2.tar.gz
latinime-2bdd5290a979f6412a91a7f0a4a97b07e5955dd2.tar.xz
latinime-2bdd5290a979f6412a91a7f0a4a97b07e5955dd2.zip
Merge "Hook for fetching sync content from UserHistoryDict"
Diffstat (limited to 'tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTestsHelper.java')
-rw-r--r--tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTestsHelper.java144
1 files changed, 144 insertions, 0 deletions
diff --git a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTestsHelper.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTestsHelper.java
new file mode 100644
index 000000000..d394c0faa
--- /dev/null
+++ b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTestsHelper.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+package com.android.inputmethod.latin.personalization;
+
+import android.content.Context;
+
+import com.android.inputmethod.latin.NgramContext;
+import com.android.inputmethod.latin.NgramContext.WordInfo;
+import com.android.inputmethod.latin.common.FileUtils;
+import com.android.inputmethod.latin.utils.DistracterFilter;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Random;
+
+/**
+ * Utility class for helping while running tests involving {@link UserHistoryDictionary}.
+ */
+public class UserHistoryDictionaryTestsHelper {
+
+ /**
+ * Locale prefix for generating dummy locales for tests.
+ */
+ public static final String TEST_LOCALE_PREFIX = "test-";
+
+ /**
+ * Characters for generating random words.
+ */
+ 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"
+ };
+
+ /**
+ * Remove all the test dictionary files created for the given locale.
+ */
+ public static void removeAllTestDictFiles(final String filter, final Context context) {
+ final FilenameFilter filenameFilter = new FilenameFilter() {
+ @Override
+ public boolean accept(final File dir, final String filename) {
+ return filename.startsWith(UserHistoryDictionary.NAME + "." + filter);
+ }
+ };
+ FileUtils.deleteFilteredFiles(context.getFilesDir(), filenameFilter);
+ }
+
+ /**
+ * Generates and writes random words to dictionary. Caller can be assured
+ * that the write tasks would be finished; and its success would be reflected
+ * in the returned boolean.
+ *
+ * @param dict {@link UserHistoryDictionary} to which words should be added.
+ * @param numberOfWords number of words to be added.
+ * @param random helps generate random words.
+ * @param checkContents if true, checks whether written words are actually in the dictionary.
+ * @param currentTime timestamp that would be used for adding the words.
+ * @returns true if all words have been written to dictionary successfully.
+ */
+ public static boolean addAndWriteRandomWords(final UserHistoryDictionary dict,
+ final int numberOfWords, final Random random, final boolean checkContents,
+ final int currentTime) {
+ final List<String> words = generateWords(numberOfWords, random);
+ // Add random words to the user history dictionary.
+ addWordsToDictionary(dict, words, currentTime);
+ boolean success = true;
+ if (checkContents) {
+ dict.waitAllTasksForTests();
+ for (int i = 0; i < numberOfWords; ++i) {
+ final String word = words.get(i);
+ if (!dict.isInDictionary(word)) {
+ success = false;
+ break;
+ }
+ }
+ }
+ // write to file.
+ dict.close();
+ dict.waitAllTasksForTests();
+ return success;
+ }
+
+ private static void addWordsToDictionary(final UserHistoryDictionary dict,
+ final List<String> words, final int timestamp) {
+ NgramContext ngramContext = NgramContext.EMPTY_PREV_WORDS_INFO;
+ for (final String word : words) {
+ UserHistoryDictionary.addToDictionary(dict, ngramContext, word, true, timestamp,
+ DistracterFilter.EMPTY_DISTRACTER_FILTER);
+ ngramContext = ngramContext.getNextNgramContext(new WordInfo(word));
+ }
+ }
+
+ /**
+ * Creates unique test locale for using within tests.
+ */
+ public static Locale getDummyLocale(final String name) {
+ return new Locale(TEST_LOCALE_PREFIX + name + System.currentTimeMillis());
+ }
+
+ /**
+ * Generates random words.
+ *
+ * @param numberOfWords number of words to generate.
+ * @param random salt used for generating random words.
+ */
+ public static List<String> generateWords(final int numberOfWords, final Random random) {
+ final HashSet<String> wordSet = new HashSet<>();
+ while (wordSet.size() < numberOfWords) {
+ wordSet.add(generateWord(random.nextInt()));
+ }
+ return new ArrayList<>(wordSet);
+ }
+
+ /**
+ * Generates a random word.
+ */
+ private static String generateWord(final int value) {
+ final int lengthOfChars = CHARACTERS.length;
+ final StringBuilder builder = new StringBuilder();
+ long lvalue = Math.abs((long)value);
+ while (lvalue > 0) {
+ builder.append(CHARACTERS[(int)(lvalue % lengthOfChars)]);
+ lvalue /= lengthOfChars;
+ }
+ return builder.toString();
+ }
+}