UserHistoryDictionaryTests.java revision e507d92aa3ee4ae43124c5452f20aa8ed0ecef4c
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.inputmethod.latin.personalization; 18 19import android.test.AndroidTestCase; 20import android.test.suitebuilder.annotation.LargeTest; 21import android.util.Log; 22 23import com.android.inputmethod.latin.ExpandableBinaryDictionary; 24import com.android.inputmethod.latin.PrevWordsInfo; 25import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; 26import com.android.inputmethod.latin.utils.CollectionUtils; 27import com.android.inputmethod.latin.utils.FileUtils; 28 29import java.io.File; 30import java.util.ArrayList; 31import java.util.List; 32import java.util.Locale; 33import java.util.Random; 34import java.util.Set; 35import java.util.concurrent.TimeUnit; 36 37/** 38 * Unit tests for UserHistoryDictionary 39 */ 40@LargeTest 41public class UserHistoryDictionaryTests extends AndroidTestCase { 42 private static final String TAG = UserHistoryDictionaryTests.class.getSimpleName(); 43 44 private static final String[] CHARACTERS = { 45 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 46 "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" 47 }; 48 49 private int mCurrentTime = 0; 50 51 @Override 52 protected void setUp() throws Exception { 53 super.setUp(); 54 resetCurrentTimeForTestMode(); 55 } 56 57 @Override 58 protected void tearDown() throws Exception { 59 stopTestModeInNativeCode(); 60 super.tearDown(); 61 } 62 63 private void resetCurrentTimeForTestMode() { 64 mCurrentTime = 0; 65 setCurrentTimeForTestMode(mCurrentTime); 66 } 67 68 private void forcePassingShortTime() { 69 // 3 days. 70 final int timeToElapse = (int)TimeUnit.DAYS.toSeconds(3); 71 mCurrentTime += timeToElapse; 72 setCurrentTimeForTestMode(mCurrentTime); 73 } 74 75 private void forcePassingLongTime() { 76 // 365 days. 77 final int timeToElapse = (int)TimeUnit.DAYS.toSeconds(365); 78 mCurrentTime += timeToElapse; 79 setCurrentTimeForTestMode(mCurrentTime); 80 } 81 82 private static int setCurrentTimeForTestMode(final int currentTime) { 83 return BinaryDictionaryUtils.setCurrentTimeForTest(currentTime); 84 } 85 86 private static int stopTestModeInNativeCode() { 87 return BinaryDictionaryUtils.setCurrentTimeForTest(-1); 88 } 89 90 /** 91 * Generates a random word. 92 */ 93 private static String generateWord(final int value) { 94 final int lengthOfChars = CHARACTERS.length; 95 StringBuilder builder = new StringBuilder(); 96 long lvalue = Math.abs((long)value); 97 while (lvalue > 0) { 98 builder.append(CHARACTERS[(int)(lvalue % lengthOfChars)]); 99 lvalue /= lengthOfChars; 100 } 101 return builder.toString(); 102 } 103 104 private static List<String> generateWords(final int number, final Random random) { 105 final Set<String> wordSet = CollectionUtils.newHashSet(); 106 while (wordSet.size() < number) { 107 wordSet.add(generateWord(random.nextInt())); 108 } 109 return new ArrayList<String>(wordSet); 110 } 111 112 private static void addToDict(final UserHistoryDictionary dict, final List<String> words) { 113 PrevWordsInfo prevWordsInfo = new PrevWordsInfo(null); 114 for (String word : words) { 115 UserHistoryDictionary.addToDictionary(dict, prevWordsInfo, word, true, 116 (int)TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())); 117 prevWordsInfo = new PrevWordsInfo(word); 118 } 119 } 120 121 /** 122 * @param checkContents if true, checks whether written words are actually in the dictionary 123 * or not. 124 */ 125 private void addAndWriteRandomWords(final Locale locale, final int numberOfWords, 126 final Random random, final boolean checkContents) { 127 final List<String> words = generateWords(numberOfWords, random); 128 final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary( 129 mContext, locale); 130 // Add random words to the user history dictionary. 131 addToDict(dict, words); 132 if (checkContents) { 133 dict.waitAllTasksForTests(); 134 for (int i = 0; i < numberOfWords; ++i) { 135 final String word = words.get(i); 136 assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word)); 137 } 138 } 139 // write to file. 140 dict.close(); 141 } 142 143 /** 144 * Clear all entries in the user history dictionary. 145 * @param locale dummy locale for testing. 146 */ 147 private void clearHistory(final Locale locale) { 148 final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary( 149 mContext, locale); 150 dict.waitAllTasksForTests(); 151 dict.clear(); 152 dict.close(); 153 dict.waitAllTasksForTests(); 154 } 155 156 /** 157 * Shut down executer and wait until all operations of user history are done. 158 * @param locale dummy locale for testing. 159 */ 160 private void waitForWriting(final Locale locale) { 161 final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary( 162 mContext, locale); 163 dict.waitAllTasksForTests(); 164 } 165 166 public void testRandomWords() { 167 Log.d(TAG, "This test can be used for profiling."); 168 Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true."); 169 final Locale dummyLocale = new Locale("test_random_words" + System.currentTimeMillis()); 170 final String dictName = ExpandableBinaryDictionary.getDictName( 171 UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */); 172 final File dictFile = ExpandableBinaryDictionary.getDictFile( 173 mContext, dictName, null /* dictFile */); 174 175 final int numberOfWords = 1000; 176 final Random random = new Random(123456); 177 178 try { 179 clearHistory(dummyLocale); 180 addAndWriteRandomWords(dummyLocale, numberOfWords, random, 181 true /* checksContents */); 182 } finally { 183 Log.d(TAG, "waiting for writing ..."); 184 waitForWriting(dummyLocale); 185 assertTrue("check exisiting of " + dictFile, dictFile.exists()); 186 FileUtils.deleteRecursively(dictFile); 187 } 188 } 189 190 public void testStressTestForSwitchingLanguagesAndAddingWords() { 191 final int numberOfLanguages = 2; 192 final int numberOfLanguageSwitching = 80; 193 final int numberOfWordsInsertedForEachLanguageSwitch = 100; 194 195 final File dictFiles[] = new File[numberOfLanguages]; 196 final Locale dummyLocales[] = new Locale[numberOfLanguages]; 197 try { 198 final Random random = new Random(123456); 199 200 // Create filename suffixes for this test. 201 for (int i = 0; i < numberOfLanguages; i++) { 202 dummyLocales[i] = new Locale("test_switching_languages" + i); 203 final String dictName = ExpandableBinaryDictionary.getDictName( 204 UserHistoryDictionary.NAME, dummyLocales[i], null /* dictFile */); 205 dictFiles[i] = ExpandableBinaryDictionary.getDictFile( 206 mContext, dictName, null /* dictFile */); 207 clearHistory(dummyLocales[i]); 208 } 209 210 final long start = System.currentTimeMillis(); 211 212 for (int i = 0; i < numberOfLanguageSwitching; i++) { 213 final int index = i % numberOfLanguages; 214 // Switch languages to testFilenameSuffixes[index]. 215 addAndWriteRandomWords(dummyLocales[index], 216 numberOfWordsInsertedForEachLanguageSwitch, random, 217 false /* checksContents */); 218 } 219 220 final long end = System.currentTimeMillis(); 221 Log.d(TAG, "testStressTestForSwitchingLanguageAndAddingWords took " 222 + (end - start) + " ms"); 223 } finally { 224 Log.d(TAG, "waiting for writing ..."); 225 for (int i = 0; i < numberOfLanguages; i++) { 226 waitForWriting(dummyLocales[i]); 227 } 228 for (final File dictFile : dictFiles) { 229 assertTrue("check exisiting of " + dictFile, dictFile.exists()); 230 FileUtils.deleteRecursively(dictFile); 231 } 232 } 233 } 234 235 public void testAddManyWords() { 236 final Locale dummyLocale = new Locale("test_random_words" + System.currentTimeMillis()); 237 final String dictName = ExpandableBinaryDictionary.getDictName( 238 UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */); 239 final File dictFile = ExpandableBinaryDictionary.getDictFile( 240 mContext, dictName, null /* dictFile */); 241 final int numberOfWords = 10000; 242 final Random random = new Random(123456); 243 clearHistory(dummyLocale); 244 try { 245 addAndWriteRandomWords(dummyLocale, numberOfWords, random, true /* checksContents */); 246 } finally { 247 Log.d(TAG, "waiting for writing ..."); 248 waitForWriting(dummyLocale); 249 assertTrue("check exisiting of " + dictFile, dictFile.exists()); 250 FileUtils.deleteRecursively(dictFile); 251 } 252 } 253 254 public void testDecaying() { 255 final Locale dummyLocale = new Locale("test_decaying" + System.currentTimeMillis()); 256 final int numberOfWords = 5000; 257 final Random random = new Random(123456); 258 resetCurrentTimeForTestMode(); 259 clearHistory(dummyLocale); 260 final List<String> words = generateWords(numberOfWords, random); 261 final UserHistoryDictionary dict = 262 PersonalizationHelper.getUserHistoryDictionary(getContext(), dummyLocale); 263 dict.waitAllTasksForTests(); 264 PrevWordsInfo prevWordsInfo = new PrevWordsInfo(null); 265 for (final String word : words) { 266 UserHistoryDictionary.addToDictionary(dict, prevWordsInfo, word, true, mCurrentTime); 267 prevWordsInfo = new PrevWordsInfo(word); 268 dict.waitAllTasksForTests(); 269 assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word)); 270 } 271 forcePassingShortTime(); 272 dict.runGCIfRequired(); 273 dict.waitAllTasksForTests(); 274 for (final String word : words) { 275 assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word)); 276 } 277 forcePassingLongTime(); 278 dict.runGCIfRequired(); 279 dict.waitAllTasksForTests(); 280 for (final String word : words) { 281 assertFalse(dict.isInUnderlyingBinaryDictionaryForTests(word)); 282 } 283 } 284} 285