1 /* 2 * Copyright (C) 2014 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 17 package com.android.inputmethod.latin; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.database.MatrixCursor; 23 import android.net.Uri; 24 import android.provider.ContactsContract; 25 import android.provider.ContactsContract.Contacts; 26 import android.test.AndroidTestCase; 27 import android.test.RenamingDelegatingContext; 28 import android.test.mock.MockContentProvider; 29 import android.test.mock.MockContentResolver; 30 import android.test.suitebuilder.annotation.SmallTest; 31 32 import com.android.inputmethod.latin.ContactsDictionaryConstants; 33 import com.android.inputmethod.latin.ContactsManager; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 38 import java.util.ArrayList; 39 import java.util.HashMap; 40 import java.util.concurrent.TimeUnit; 41 42 /** 43 * Tests for {@link ContactsManager} 44 */ 45 @SmallTest 46 public class ContactsManagerTest extends AndroidTestCase { 47 48 private ContactsManager mManager; 49 private FakeContactsContentProvider mFakeContactsContentProvider; 50 private MatrixCursor mMatrixCursor; 51 52 @Before 53 @Override setUp()54 public void setUp() throws Exception { 55 // Fake content provider 56 mFakeContactsContentProvider = new FakeContactsContentProvider(); 57 mMatrixCursor = new MatrixCursor(ContactsDictionaryConstants.PROJECTION); 58 // Add the fake content provider to fake content resolver. 59 final MockContentResolver contentResolver = new MockContentResolver(); 60 contentResolver.addProvider(ContactsContract.AUTHORITY, mFakeContactsContentProvider); 61 // Add the fake content resolver to a fake context. 62 final ContextWithMockContentResolver context = new ContextWithMockContentResolver(mContext); 63 context.setContentResolver(contentResolver); 64 65 mManager = new ContactsManager(context); 66 } 67 68 @Test testGetValidNames()69 public void testGetValidNames() { 70 final String contactName1 = "firstname last-name"; 71 final String contactName2 = "larry"; 72 mMatrixCursor.addRow(new Object[] { 1, contactName1, 0, 0, 0 }); 73 mMatrixCursor.addRow(new Object[] { 2, null /* null name */, 0, 0, 0 }); 74 mMatrixCursor.addRow(new Object[] { 3, contactName2, 0, 0, 0 }); 75 mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */, 0, 0, 0 }); 76 mMatrixCursor.addRow(new Object[] { 5, "news-group" /* invalid name */, 0, 0, 0 }); 77 mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor); 78 79 final ArrayList<String> validNames = mManager.getValidNames(Contacts.CONTENT_URI); 80 assertEquals(2, validNames.size()); 81 assertEquals(contactName1, validNames.get(0)); 82 assertEquals(contactName2, validNames.get(1)); 83 } 84 85 @Test testGetValidNamesAffinity()86 public void testGetValidNamesAffinity() { 87 final long now = System.currentTimeMillis(); 88 final long month_ago = now - TimeUnit.MILLISECONDS.convert(31, TimeUnit.DAYS); 89 for (int i = 0; i < ContactsManager.MAX_CONTACT_NAMES + 10; ++i) { 90 mMatrixCursor.addRow(new Object[] { i, "name" + i, i, now, 1 }); 91 } 92 mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor); 93 94 final ArrayList<String> validNames = mManager.getValidNames(Contacts.CONTENT_URI); 95 assertEquals(ContactsManager.MAX_CONTACT_NAMES, validNames.size()); 96 for (int i = 0; i < 10; ++i) { 97 assertFalse(validNames.contains("name" + i)); 98 } 99 for (int i = 10; i < ContactsManager.MAX_CONTACT_NAMES + 10; ++i) { 100 assertTrue(validNames.contains("name" + i)); 101 } 102 } 103 104 @Test testComputeAffinity()105 public void testComputeAffinity() { 106 final long now = System.currentTimeMillis(); 107 final long month_ago = now - TimeUnit.MILLISECONDS.convert(31, TimeUnit.DAYS); 108 mMatrixCursor.addRow(new Object[] { 1, "name", 1, month_ago, 1 }); 109 mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor); 110 111 Cursor cursor = mFakeContactsContentProvider.query(Contacts.CONTENT_URI, 112 ContactsDictionaryConstants.PROJECTION_ID_ONLY, null, null, null); 113 cursor.moveToFirst(); 114 ContactsManager.RankedContact contact = new ContactsManager.RankedContact(cursor); 115 contact.computeAffinity(1, month_ago); 116 assertEquals(contact.getAffinity(), 1.0f); 117 contact.computeAffinity(2, now); 118 assertEquals(contact.getAffinity(), (2.0f/3.0f + (float)Math.pow(0.5, 3) + 1.0f) / 3); 119 } 120 121 @Test testGetCount()122 public void testGetCount() { 123 mMatrixCursor.addRow(new Object[] { 1, "firstname", 0, 0, 0 }); 124 mMatrixCursor.addRow(new Object[] { 2, null /* null name */, 0, 0, 0 }); 125 mMatrixCursor.addRow(new Object[] { 3, "larry", 0, 0, 0 }); 126 mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */, 0, 0, 0 }); 127 mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor); 128 assertEquals(4, mManager.getContactCount()); 129 } 130 131 132 static class ContextWithMockContentResolver extends RenamingDelegatingContext { 133 private ContentResolver contentResolver; 134 setContentResolver(final ContentResolver contentResolver)135 public void setContentResolver(final ContentResolver contentResolver) { 136 this.contentResolver = contentResolver; 137 } 138 ContextWithMockContentResolver(final Context targetContext)139 public ContextWithMockContentResolver(final Context targetContext) { 140 super(targetContext, "test"); 141 } 142 143 @Override getContentResolver()144 public ContentResolver getContentResolver() { 145 return contentResolver; 146 } 147 } 148 149 static class FakeContactsContentProvider extends MockContentProvider { 150 private final HashMap<String, MatrixCursor> mQueryCursorMapForTestExpectations = 151 new HashMap<>(); 152 153 @Override query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder)154 public Cursor query(final Uri uri, final String[] projection, final String selection, 155 final String[] selectionArgs, final String sortOrder) { 156 return mQueryCursorMapForTestExpectations.get(uri.toString()); 157 } 158 reset()159 public void reset() { 160 mQueryCursorMapForTestExpectations.clear(); 161 } 162 addQueryResult(final Uri uri, final MatrixCursor cursor)163 public void addQueryResult(final Uri uri, final MatrixCursor cursor) { 164 mQueryCursorMapForTestExpectations.put(uri.toString(), cursor); 165 } 166 } 167 } 168