1 /* 2 * Copyright (C) 2013 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.dialer.database; 18 19 import static com.android.dialer.database.DatabaseTestUtils.*; 20 21 import android.database.MatrixCursor; 22 import android.database.sqlite.SQLiteDatabase; 23 import android.test.suitebuilder.annotation.SmallTest; 24 import android.test.suitebuilder.annotation.Suppress; 25 import android.test.AndroidTestCase; 26 27 import com.android.dialer.database.DialerDatabaseHelper; 28 import com.android.dialer.database.DialerDatabaseHelper.ContactNumber; 29 import com.android.dialer.dialpad.SmartDialNameMatcher; 30 import com.android.dialer.dialpad.SmartDialPrefix; 31 32 import java.lang.Exception; 33 import java.lang.Override; 34 import java.util.ArrayList; 35 36 /** 37 * Validates the behavior of the smart dial database helper with regards to contact updates and 38 * deletes. 39 * To run this test, use the command: 40 * adb shell am instrument -w -e class com.android.dialer.database.DialerDatabaseHelperTest / 41 * com.android.dialer.tests/android.test.InstrumentationTestRunner 42 */ 43 @SmallTest 44 public class DialerDatabaseHelperTest extends AndroidTestCase { 45 46 private DialerDatabaseHelper mTestHelper; 47 private SQLiteDatabase mDb; 48 49 @Override setUp()50 protected void setUp() { 51 mTestHelper = DialerDatabaseHelper.getNewInstanceForTest(getContext()); 52 mDb = mTestHelper.getWritableDatabase(); 53 } 54 55 @Override tearDown()56 protected void tearDown() throws Exception { 57 final SQLiteDatabase db = mTestHelper.getWritableDatabase(); 58 mTestHelper.removeAllContacts(db); 59 super.tearDown(); 60 } 61 62 /** 63 * Verifies that a new contact added into the database is a match after the update. 64 */ testForNewContacts()65 public void testForNewContacts() { 66 final MatrixCursor nameCursor = constructNewNameCursor(); 67 final MatrixCursor contactCursor = constructNewContactCursor(); 68 69 mTestHelper.insertUpdatedContactsAndNumberPrefix(mDb, contactCursor, 0L); 70 mTestHelper.insertNamePrefixes(mDb, nameCursor); 71 assertEquals(0, getMatchesFromDb("5105272357").size()); 72 73 // Insert new contact 74 constructNewContactWithDummyIds(contactCursor, nameCursor, 75 "510-527-2357", 0, "James"); 76 mTestHelper.insertUpdatedContactsAndNumberPrefix(mDb, contactCursor, 1L); 77 mTestHelper.insertNamePrefixes(mDb, nameCursor); 78 assertEquals(1, getMatchesFromDb("5105272357").size()); 79 } 80 81 /** 82 * Verifies that a contact that has its phone number changed is a match after the update. 83 */ testForUpdatedContacts()84 public void testForUpdatedContacts() { 85 final MatrixCursor nameCursor = constructNewNameCursor(); 86 final MatrixCursor contactCursor = constructNewContactCursor(); 87 constructNewContactWithDummyIds(contactCursor, nameCursor, 88 "510-527-2357", 0, "James"); 89 mTestHelper.insertUpdatedContactsAndNumberPrefix(mDb, contactCursor, 0L); 90 mTestHelper.insertNamePrefixes(mDb, nameCursor); 91 assertEquals(1, getMatchesFromDb("5105272357").size()); 92 assertEquals(0, getMatchesFromDb("6501234567").size()); 93 94 // Update the database with the new contact information 95 final MatrixCursor nameCursor2 = constructNewNameCursor(); 96 final MatrixCursor contactCursor2 = constructNewContactCursor(); 97 constructNewContactWithDummyIds(contactCursor2, nameCursor2, 98 "650-123-4567", 0, "James"); 99 mTestHelper.removeUpdatedContacts(mDb, contactCursor2); 100 mTestHelper.insertUpdatedContactsAndNumberPrefix(mDb, contactCursor2, 1L); 101 mTestHelper.insertNamePrefixes(mDb, nameCursor2); 102 103 // Now verify the matches are correct based on the new information 104 assertEquals(0, getMatchesFromDb("5105272357").size()); 105 assertEquals(1, getMatchesFromDb("6501234567").size()); 106 } 107 108 /** 109 * Verifies that a contact that is deleted from CP2 is similarly deleted from the database 110 */ testForDeletedContacts()111 public void testForDeletedContacts() { 112 final MatrixCursor nameCursor = constructNewNameCursor(); 113 final MatrixCursor contactCursor = constructNewContactCursor(); 114 constructNewContactWithDummyIds(contactCursor, nameCursor, 115 "510-527-2357", 0, "James"); 116 mTestHelper.insertUpdatedContactsAndNumberPrefix(mDb, contactCursor, 0L); 117 mTestHelper.insertNamePrefixes(mDb, nameCursor); 118 assertEquals(1, getMatchesFromDb("5105272357").size()); 119 120 // Delete the contact and update its projection. 121 final MatrixCursor deletedCursor = 122 new MatrixCursor(DialerDatabaseHelper.DeleteContactQuery.PROJECTION); 123 deletedCursor.addRow(new Object[] {0, 1L}); 124 mTestHelper.removeDeletedContacts(mDb, deletedCursor); 125 assertEquals(0, getMatchesFromDb("5105272357").size()); 126 } 127 128 /** 129 * Verifies that when a contact's number is deleted (but not the entire contact), the 130 * number is correctly deleted from the database. 131 */ testForDeletedNumber()132 public void testForDeletedNumber() { 133 final MatrixCursor nameCursor = constructNewNameCursor(); 134 final MatrixCursor contactCursor = constructNewContactCursor(); 135 constructNewContactWithDummyIds(contactCursor, nameCursor, 136 "510-527-2357", 0, "James"); 137 mTestHelper.insertUpdatedContactsAndNumberPrefix(mDb, contactCursor, 0L); 138 mTestHelper.insertNamePrefixes(mDb, nameCursor); 139 assertEquals(1, getMatchesFromDb("5105272357").size()); 140 141 // Match no longer exists after number was deleted from contact 142 final MatrixCursor updatedContactCursor = 143 new MatrixCursor(DialerDatabaseHelper.UpdatedContactQuery.PROJECTION); 144 updatedContactCursor.addRow(new Object[] {0}); 145 mTestHelper.removeUpdatedContacts(mDb, updatedContactCursor); 146 assertEquals(0, getMatchesFromDb("5105272357").size()); 147 } 148 getMatchesFromDb(String query)149 private ArrayList<ContactNumber> getMatchesFromDb(String query) { 150 final SmartDialNameMatcher nameMatcher = new SmartDialNameMatcher(query, 151 SmartDialPrefix.getMap()); 152 return mTestHelper.getLooseMatches(query, nameMatcher); 153 } 154 } 155