summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTom Ouyang <ouyang@google.com>2015-04-13 17:49:17 -0700
committerTom Ouyang <ouyang@google.com>2015-04-16 11:33:54 -0700
commit44a175732dc4b872515f978b986ef7b357fe2f00 (patch)
treebe4c2e92514d7f5ac8306149ba469607ffb0f0e7 /tests
parent40f0f61bb365b5073f1d9fdb56a393c5df5ef4b0 (diff)
downloadandroid_packages_inputmethods_LatinIME-44a175732dc4b872515f978b986ef7b357fe2f00.tar.gz
android_packages_inputmethods_LatinIME-44a175732dc4b872515f978b986ef7b357fe2f00.tar.bz2
android_packages_inputmethods_LatinIME-44a175732dc4b872515f978b986ef7b357fe2f00.zip
Add affinity model for contact names.
This allows us to: 1. Rank contacts and only add the top N names to the keyboard LM. 2. Avoid adding duplicate names. Note: The affinity calcualuation is limited by the fact that some apps currently do not update the TIMES_CONTACTED counter. To better handle this case, the new measure also takes into account whether or not a name is in the visible contacts group. Bug: 20053274 Change-Id: I2741cb8958667d4a294aba8c437a45cec4b42dc7
Diffstat (limited to 'tests')
-rw-r--r--tests/src/com/android/inputmethod/latin/ContactsManagerTest.java60
1 files changed, 50 insertions, 10 deletions
diff --git a/tests/src/com/android/inputmethod/latin/ContactsManagerTest.java b/tests/src/com/android/inputmethod/latin/ContactsManagerTest.java
index 6326b3b0f..f987e0c05 100644
--- a/tests/src/com/android/inputmethod/latin/ContactsManagerTest.java
+++ b/tests/src/com/android/inputmethod/latin/ContactsManagerTest.java
@@ -29,11 +29,15 @@ import android.test.mock.MockContentProvider;
import android.test.mock.MockContentResolver;
import android.test.suitebuilder.annotation.SmallTest;
+import com.android.inputmethod.latin.ContactsDictionaryConstants;
+import com.android.inputmethod.latin.ContactsManager;
+
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.concurrent.TimeUnit;
/**
* Tests for {@link ContactsManager}
@@ -63,12 +67,13 @@ public class ContactsManagerTest extends AndroidTestCase {
@Test
public void testGetValidNames() {
- final String contactName1 = "firstname lastname";
+ final String contactName1 = "firstname last-name";
final String contactName2 = "larry";
- mMatrixCursor.addRow(new Object[] { 1, contactName1 });
- mMatrixCursor.addRow(new Object[] { 2, null /* null name */ });
- mMatrixCursor.addRow(new Object[] { 3, contactName2 });
- mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */ });
+ mMatrixCursor.addRow(new Object[] { 1, contactName1, 0, 0, 0 });
+ mMatrixCursor.addRow(new Object[] { 2, null /* null name */, 0, 0, 0 });
+ mMatrixCursor.addRow(new Object[] { 3, contactName2, 0, 0, 0 });
+ mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */, 0, 0, 0 });
+ mMatrixCursor.addRow(new Object[] { 5, "news-group" /* invalid name */, 0, 0, 0 });
mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor);
final ArrayList<String> validNames = mManager.getValidNames(Contacts.CONTENT_URI);
@@ -78,13 +83,48 @@ public class ContactsManagerTest extends AndroidTestCase {
}
@Test
- public void testGetCount() {
- mMatrixCursor.addRow(new Object[] { 1, "firstname" });
- mMatrixCursor.addRow(new Object[] { 2, null /* null name */ });
- mMatrixCursor.addRow(new Object[] { 3, "larry" });
- mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */ });
+ public void testGetValidNamesAffinity() {
+ final long now = System.currentTimeMillis();
+ final long month_ago = now - TimeUnit.MILLISECONDS.convert(31, TimeUnit.DAYS);
+ for (int i = 0; i < ContactsManager.MAX_CONTACT_NAMES + 10; ++i) {
+ mMatrixCursor.addRow(new Object[] { i, "name" + i, i, now, 1 });
+ }
+ mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor);
+
+ final ArrayList<String> validNames = mManager.getValidNames(Contacts.CONTENT_URI);
+ assertEquals(ContactsManager.MAX_CONTACT_NAMES, validNames.size());
+ for (int i = 0; i < 10; ++i) {
+ assertFalse(validNames.contains("name" + i));
+ }
+ for (int i = 10; i < ContactsManager.MAX_CONTACT_NAMES + 10; ++i) {
+ assertTrue(validNames.contains("name" + i));
+ }
+ }
+
+ @Test
+ public void testComputeAffinity() {
+ final long now = System.currentTimeMillis();
+ final long month_ago = now - TimeUnit.MILLISECONDS.convert(31, TimeUnit.DAYS);
+ mMatrixCursor.addRow(new Object[] { 1, "name", 1, month_ago, 1 });
mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor);
+ Cursor cursor = mFakeContactsContentProvider.query(Contacts.CONTENT_URI,
+ ContactsDictionaryConstants.PROJECTION_ID_ONLY, null, null, null);
+ cursor.moveToFirst();
+ ContactsManager.RankedContact contact = new ContactsManager.RankedContact(cursor);
+ contact.computeAffinity(1, month_ago);
+ assertEquals(contact.getAffinity(), 1.0f);
+ contact.computeAffinity(2, now);
+ assertEquals(contact.getAffinity(), (2.0f/3.0f + (float)Math.pow(0.5, 3) + 1.0f) / 3);
+ }
+
+ @Test
+ public void testGetCount() {
+ mMatrixCursor.addRow(new Object[] { 1, "firstname", 0, 0, 0 });
+ mMatrixCursor.addRow(new Object[] { 2, null /* null name */, 0, 0, 0 });
+ mMatrixCursor.addRow(new Object[] { 3, "larry", 0, 0, 0 });
+ mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */, 0, 0, 0 });
+ mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor);
assertEquals(4, mManager.getContactCount());
}