2012-08-20 10:29:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 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.
|
|
|
|
*/
|
|
|
|
|
2013-07-26 03:35:11 +00:00
|
|
|
package com.android.inputmethod.latin.personalization;
|
2012-08-20 10:29:20 +00:00
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.test.InstrumentationRegistry;
|
|
|
|
import android.support.test.filters.LargeTest;
|
|
|
|
import android.support.test.runner.AndroidJUnit4;
|
2012-08-20 10:29:20 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2013-08-19 01:17:02 +00:00
|
|
|
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
|
2014-03-05 09:19:34 +00:00
|
|
|
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
|
2013-06-23 16:11:32 +00:00
|
|
|
|
2012-10-05 06:12:50 +00:00
|
|
|
import java.io.File;
|
2013-12-13 08:09:16 +00:00
|
|
|
import java.util.Locale;
|
2012-08-20 10:29:20 +00:00
|
|
|
import java.util.Random;
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
2012-08-20 10:29:20 +00:00
|
|
|
/**
|
|
|
|
* Unit tests for UserHistoryDictionary
|
|
|
|
*/
|
2013-02-04 23:25:24 +00:00
|
|
|
@LargeTest
|
2018-07-06 17:10:54 +00:00
|
|
|
@RunWith(AndroidJUnit4.class)
|
|
|
|
public class UserHistoryDictionaryTests {
|
2012-08-20 10:29:20 +00:00
|
|
|
private static final String TAG = UserHistoryDictionaryTests.class.getSimpleName();
|
2014-08-12 13:29:02 +00:00
|
|
|
private static final int WAIT_FOR_WRITING_FILE_IN_MILLISECONDS = 3000;
|
2014-10-24 00:50:35 +00:00
|
|
|
private static final String TEST_ACCOUNT = "account@example.com";
|
2012-08-20 10:29:20 +00:00
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
private int mCurrentTime = 0;
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
private Context getContext() {
|
|
|
|
return InstrumentationRegistry.getTargetContext();
|
|
|
|
}
|
|
|
|
|
2014-10-20 05:48:56 +00:00
|
|
|
private static void printAllFiles(final File dir) {
|
2014-08-13 06:51:34 +00:00
|
|
|
Log.d(TAG, dir.getAbsolutePath());
|
|
|
|
for (final File file : dir.listFiles()) {
|
|
|
|
Log.d(TAG, " " + file.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 18:33:39 +00:00
|
|
|
private static void assertDictionaryExists(final UserHistoryDictionary dict,
|
2014-09-22 05:22:37 +00:00
|
|
|
final File dictFile) {
|
2014-08-15 08:55:27 +00:00
|
|
|
Log.d(TAG, "waiting for writing ...");
|
2014-09-22 05:22:37 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-08-15 08:55:27 +00:00
|
|
|
if (!dictFile.exists()) {
|
|
|
|
try {
|
|
|
|
Log.d(TAG, dictFile + " is not existing. Wait "
|
|
|
|
+ WAIT_FOR_WRITING_FILE_IN_MILLISECONDS + " ms for writing.");
|
|
|
|
printAllFiles(dictFile.getParentFile());
|
|
|
|
Thread.sleep(WAIT_FOR_WRITING_FILE_IN_MILLISECONDS);
|
|
|
|
} catch (final InterruptedException e) {
|
|
|
|
Log.e(TAG, "Interrupted during waiting for writing the dict file.");
|
|
|
|
}
|
|
|
|
}
|
2014-10-30 18:33:39 +00:00
|
|
|
assertTrue("Following dictionary file doesn't exist: " + dictFile, dictFile.exists());
|
2014-09-22 05:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
2014-02-20 12:23:57 +00:00
|
|
|
resetCurrentTimeForTestMode();
|
2014-10-30 18:33:39 +00:00
|
|
|
UserHistoryDictionaryTestsHelper.removeAllTestDictFiles(
|
2018-07-06 17:10:54 +00:00
|
|
|
UserHistoryDictionaryTestsHelper.TEST_LOCALE_PREFIX, getContext());
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@After
|
|
|
|
public void tearDown() throws Exception {
|
2014-10-30 18:33:39 +00:00
|
|
|
UserHistoryDictionaryTestsHelper.removeAllTestDictFiles(
|
2018-07-06 17:10:54 +00:00
|
|
|
UserHistoryDictionaryTestsHelper.TEST_LOCALE_PREFIX, getContext());
|
2014-02-20 09:36:46 +00:00
|
|
|
stopTestModeInNativeCode();
|
|
|
|
}
|
|
|
|
|
2014-02-20 12:23:57 +00:00
|
|
|
private void resetCurrentTimeForTestMode() {
|
|
|
|
mCurrentTime = 0;
|
|
|
|
setCurrentTimeForTestMode(mCurrentTime);
|
|
|
|
}
|
|
|
|
|
2014-02-20 09:36:46 +00:00
|
|
|
private static int setCurrentTimeForTestMode(final int currentTime) {
|
2014-03-05 09:19:34 +00:00
|
|
|
return BinaryDictionaryUtils.setCurrentTimeForTest(currentTime);
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int stopTestModeInNativeCode() {
|
2014-03-05 09:19:34 +00:00
|
|
|
return BinaryDictionaryUtils.setCurrentTimeForTest(-1);
|
2014-02-20 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 11:55:34 +00:00
|
|
|
/**
|
|
|
|
* Clear all entries in the user history dictionary.
|
2014-09-22 05:22:37 +00:00
|
|
|
* @param dict the user history dictionary.
|
2013-10-03 11:55:34 +00:00
|
|
|
*/
|
2014-10-20 05:48:56 +00:00
|
|
|
private static void clearHistory(final UserHistoryDictionary dict) {
|
2014-01-15 02:18:39 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2014-04-24 18:25:47 +00:00
|
|
|
dict.clear();
|
2013-10-03 11:55:34 +00:00
|
|
|
dict.close();
|
2014-01-15 02:18:39 +00:00
|
|
|
dict.waitAllTasksForTests();
|
2013-10-03 11:55:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 21:48:16 +00:00
|
|
|
private void doTestRandomWords(final String testAccount) {
|
2013-08-16 05:17:09 +00:00
|
|
|
Log.d(TAG, "This test can be used for profiling.");
|
|
|
|
Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true.");
|
2014-10-30 18:33:39 +00:00
|
|
|
final Locale dummyLocale = UserHistoryDictionaryTestsHelper.getDummyLocale("random_words");
|
2014-10-24 00:50:35 +00:00
|
|
|
final String dictName = UserHistoryDictionary.getUserHistoryDictName(
|
2014-11-24 21:48:16 +00:00
|
|
|
UserHistoryDictionary.NAME, dummyLocale,
|
|
|
|
null /* dictFile */,
|
|
|
|
testAccount /* account */);
|
2014-02-13 07:16:44 +00:00
|
|
|
final File dictFile = ExpandableBinaryDictionary.getDictFile(
|
2018-07-06 17:10:54 +00:00
|
|
|
getContext(), dictName, null /* dictFile */);
|
2014-09-22 05:22:37 +00:00
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
2014-11-24 21:48:16 +00:00
|
|
|
getContext(), dummyLocale, testAccount);
|
2014-10-30 18:33:39 +00:00
|
|
|
clearHistory(dict);
|
2014-11-24 21:48:16 +00:00
|
|
|
|
2013-08-16 05:17:09 +00:00
|
|
|
final int numberOfWords = 1000;
|
|
|
|
final Random random = new Random(123456);
|
2014-10-30 18:33:39 +00:00
|
|
|
assertTrue(UserHistoryDictionaryTestsHelper.addAndWriteRandomWords(
|
|
|
|
dict, numberOfWords, random, true /* checksContents */, mCurrentTime));
|
|
|
|
assertDictionaryExists(dict, dictFile);
|
2012-08-20 10:29:20 +00:00
|
|
|
}
|
2013-07-16 03:17:56 +00:00
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2014-11-24 21:48:16 +00:00
|
|
|
public void testRandomWords_NullAccount() {
|
|
|
|
doTestRandomWords(null /* testAccount */);
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2014-11-24 21:48:16 +00:00
|
|
|
public void testRandomWords() {
|
|
|
|
doTestRandomWords(TEST_ACCOUNT);
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2013-07-16 03:17:56 +00:00
|
|
|
public void testStressTestForSwitchingLanguagesAndAddingWords() {
|
2014-11-24 21:48:16 +00:00
|
|
|
doTestStressTestForSwitchingLanguagesAndAddingWords(TEST_ACCOUNT);
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2014-11-24 21:48:16 +00:00
|
|
|
public void testStressTestForSwitchingLanguagesAndAddingWords_NullAccount() {
|
|
|
|
doTestStressTestForSwitchingLanguagesAndAddingWords(null /* testAccount */);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doTestStressTestForSwitchingLanguagesAndAddingWords(final String testAccount) {
|
2013-07-16 03:17:56 +00:00
|
|
|
final int numberOfLanguages = 2;
|
2013-08-16 05:17:09 +00:00
|
|
|
final int numberOfLanguageSwitching = 80;
|
|
|
|
final int numberOfWordsInsertedForEachLanguageSwitch = 100;
|
2013-07-16 03:17:56 +00:00
|
|
|
|
|
|
|
final File dictFiles[] = new File[numberOfLanguages];
|
2014-09-22 05:22:37 +00:00
|
|
|
final UserHistoryDictionary dicts[] = new UserHistoryDictionary[numberOfLanguages];
|
|
|
|
|
2013-07-16 03:17:56 +00:00
|
|
|
try {
|
|
|
|
final Random random = new Random(123456);
|
|
|
|
|
2013-08-16 05:17:09 +00:00
|
|
|
// Create filename suffixes for this test.
|
2013-07-16 03:17:56 +00:00
|
|
|
for (int i = 0; i < numberOfLanguages; i++) {
|
2014-10-30 18:33:39 +00:00
|
|
|
final Locale dummyLocale =
|
|
|
|
UserHistoryDictionaryTestsHelper.getDummyLocale("switching_languages" + i);
|
2014-10-24 00:50:35 +00:00
|
|
|
final String dictName = UserHistoryDictionary.getUserHistoryDictName(
|
2014-11-24 21:48:16 +00:00
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */,
|
|
|
|
testAccount /* account */);
|
2014-02-13 07:16:44 +00:00
|
|
|
dictFiles[i] = ExpandableBinaryDictionary.getDictFile(
|
2018-07-06 17:10:54 +00:00
|
|
|
getContext(), dictName, null /* dictFile */);
|
2014-09-22 05:22:37 +00:00
|
|
|
dicts[i] = PersonalizationHelper.getUserHistoryDictionary(getContext(),
|
2014-11-24 21:48:16 +00:00
|
|
|
dummyLocale, testAccount);
|
2014-09-22 05:22:37 +00:00
|
|
|
clearHistory(dicts[i]);
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 05:17:09 +00:00
|
|
|
final long start = System.currentTimeMillis();
|
2013-07-16 03:17:56 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < numberOfLanguageSwitching; i++) {
|
|
|
|
final int index = i % numberOfLanguages;
|
2014-09-22 05:22:37 +00:00
|
|
|
// Switch to dicts[index].
|
2014-10-30 18:33:39 +00:00
|
|
|
assertTrue(UserHistoryDictionaryTestsHelper.addAndWriteRandomWords(dicts[index],
|
|
|
|
numberOfWordsInsertedForEachLanguageSwitch,
|
|
|
|
random,
|
|
|
|
false /* checksContents */,
|
|
|
|
mCurrentTime));
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final long end = System.currentTimeMillis();
|
|
|
|
Log.d(TAG, "testStressTestForSwitchingLanguageAndAddingWords took "
|
2013-08-16 05:17:09 +00:00
|
|
|
+ (end - start) + " ms");
|
|
|
|
} finally {
|
2013-10-03 11:55:34 +00:00
|
|
|
for (int i = 0; i < numberOfLanguages; i++) {
|
2014-10-30 18:33:39 +00:00
|
|
|
assertDictionaryExists(dicts[i], dictFiles[i]);
|
2013-07-16 03:17:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 13:57:15 +00:00
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2013-09-24 13:57:15 +00:00
|
|
|
public void testAddManyWords() {
|
2014-11-24 21:48:16 +00:00
|
|
|
doTestAddManyWords(TEST_ACCOUNT);
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2014-11-24 21:48:16 +00:00
|
|
|
public void testAddManyWords_NullAccount() {
|
|
|
|
doTestAddManyWords(null /* testAccount */);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doTestAddManyWords(final String testAccount) {
|
2014-10-30 18:33:39 +00:00
|
|
|
final Locale dummyLocale =
|
|
|
|
UserHistoryDictionaryTestsHelper.getDummyLocale("many_random_words");
|
2014-10-24 00:50:35 +00:00
|
|
|
final String dictName = UserHistoryDictionary.getUserHistoryDictName(
|
2014-11-24 21:48:16 +00:00
|
|
|
UserHistoryDictionary.NAME, dummyLocale, null /* dictFile */, testAccount);
|
2014-02-13 07:16:44 +00:00
|
|
|
final File dictFile = ExpandableBinaryDictionary.getDictFile(
|
2018-07-06 17:10:54 +00:00
|
|
|
getContext(), dictName, null /* dictFile */);
|
2013-12-13 08:09:16 +00:00
|
|
|
final int numberOfWords = 10000;
|
2013-09-24 13:57:15 +00:00
|
|
|
final Random random = new Random(123456);
|
2014-09-22 05:22:37 +00:00
|
|
|
final UserHistoryDictionary dict = PersonalizationHelper.getUserHistoryDictionary(
|
2014-11-24 21:48:16 +00:00
|
|
|
getContext(), dummyLocale, testAccount);
|
2014-09-22 05:22:37 +00:00
|
|
|
clearHistory(dict);
|
2014-10-30 18:33:39 +00:00
|
|
|
assertTrue(UserHistoryDictionaryTestsHelper.addAndWriteRandomWords(dict,
|
|
|
|
numberOfWords, random, true /* checksContents */, mCurrentTime));
|
|
|
|
assertDictionaryExists(dict, dictFile);
|
2013-09-24 13:57:15 +00:00
|
|
|
}
|
2014-10-24 00:50:35 +00:00
|
|
|
}
|