1 /* 2 * Copyright (C) 2010 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 android.provider.cts; 18 19 20 import android.content.ContentProviderClient; 21 import android.content.ContentResolver; 22 import android.content.ContentValues; 23 import android.net.Uri; 24 import android.provider.ContactsContract; 25 import android.provider.ContactsContract.CommonDataKinds.StructuredName; 26 import android.provider.ContactsContract.RawContacts; 27 import android.provider.cts.ContactsContract_TestDataBuilder.TestContact; 28 import android.provider.cts.ContactsContract_TestDataBuilder.TestRawContact; 29 import android.provider.cts.contacts.CommonDatabaseUtils; 30 import android.provider.cts.contacts.ContactUtil; 31 import android.provider.cts.contacts.DatabaseAsserts; 32 import android.provider.cts.contacts.RawContactUtil; 33 import android.provider.cts.contacts.account.StaticAccountAuthenticator; 34 import android.test.AndroidTestCase; 35 import android.test.MoreAsserts; 36 37 public class ContactsContract_RawContactsTest extends AndroidTestCase { 38 private ContentResolver mResolver; 39 private ContactsContract_TestDataBuilder mBuilder; 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 mResolver = getContext().getContentResolver(); 45 ContentProviderClient provider = 46 mResolver.acquireContentProviderClient(ContactsContract.AUTHORITY); 47 mBuilder = new ContactsContract_TestDataBuilder(provider); 48 } 49 50 @Override 51 protected void tearDown() throws Exception { 52 super.tearDown(); 53 mBuilder.cleanup(); 54 } 55 56 public void testGetLookupUriBySourceId() throws Exception { 57 TestRawContact rawContact = mBuilder.newRawContact() 58 .with(RawContacts.ACCOUNT_TYPE, "test_type") 59 .with(RawContacts.ACCOUNT_NAME, "test_name") 60 .with(RawContacts.SOURCE_ID, "source_id") 61 .insert(); 62 63 // TODO remove this. The method under test is currently broken: it will not 64 // work without at least one data row in the raw contact. 65 rawContact.newDataRow(StructuredName.CONTENT_ITEM_TYPE) 66 .with(StructuredName.DISPLAY_NAME, "test name") 67 .insert(); 68 69 Uri lookupUri = RawContacts.getContactLookupUri(mResolver, rawContact.getUri()); 70 assertNotNull("Could not produce a lookup URI", lookupUri); 71 72 TestContact lookupContact = mBuilder.newContact().setUri(lookupUri).load(); 73 assertEquals("Lookup URI matched the wrong contact", 74 lookupContact.getId(), rawContact.load().getContactId()); 75 } 76 77 public void testGetLookupUriByDisplayName() throws Exception { 78 TestRawContact rawContact = mBuilder.newRawContact() 79 .with(RawContacts.ACCOUNT_TYPE, "test_type") 80 .with(RawContacts.ACCOUNT_NAME, "test_name") 81 .insert(); 82 rawContact.newDataRow(StructuredName.CONTENT_ITEM_TYPE) 83 .with(StructuredName.DISPLAY_NAME, "test name") 84 .insert(); 85 86 Uri lookupUri = RawContacts.getContactLookupUri(mResolver, rawContact.getUri()); 87 assertNotNull("Could not produce a lookup URI", lookupUri); 88 89 TestContact lookupContact = mBuilder.newContact().setUri(lookupUri).load(); 90 assertEquals("Lookup URI matched the wrong contact", 91 lookupContact.getId(), rawContact.load().getContactId()); 92 } 93 94 public void testRawContactDelete_setsDeleteFlag() { 95 long rawContactid = RawContactUtil.insertRawContact(mResolver, 96 StaticAccountAuthenticator.ACCOUNT_1); 97 98 assertTrue(RawContactUtil.rawContactExistsById(mResolver, rawContactid)); 99 100 RawContactUtil.delete(mResolver, rawContactid, false); 101 102 String[] projection = new String[]{ 103 ContactsContract.RawContacts.CONTACT_ID, 104 ContactsContract.RawContacts.DELETED 105 }; 106 String[] result = RawContactUtil.queryByRawContactId(mResolver, rawContactid, 107 projection); 108 109 // Contact id should be null 110 assertNull(result[0]); 111 // Record should be marked deleted. 112 assertEquals("1", result[1]); 113 } 114 115 public void testRawContactDelete_removesRecord() { 116 long rawContactid = RawContactUtil.insertRawContact(mResolver, 117 StaticAccountAuthenticator.ACCOUNT_1); 118 assertTrue(RawContactUtil.rawContactExistsById(mResolver, rawContactid)); 119 120 RawContactUtil.delete(mResolver, rawContactid, true); 121 122 assertFalse(RawContactUtil.rawContactExistsById(mResolver, rawContactid)); 123 124 // already clean 125 } 126 127 128 // This implicitly tests the Contact create case. 129 public void testRawContactCreate_updatesContactUpdatedTimestamp() { 130 long startTime = System.currentTimeMillis(); 131 132 DatabaseAsserts.ContactIdPair ids = DatabaseAsserts.assertAndCreateContact(mResolver); 133 long lastUpdated = getContactLastUpdatedTimestampByRawContactId(mResolver, 134 ids.mRawContactId); 135 136 assertTrue(lastUpdated > startTime); 137 138 // Clean up 139 RawContactUtil.delete(mResolver, ids.mRawContactId, true); 140 } 141 142 public void testRawContactUpdate_updatesContactUpdatedTimestamp() { 143 DatabaseAsserts.ContactIdPair ids = DatabaseAsserts.assertAndCreateContact(mResolver); 144 145 long baseTime = ContactUtil.queryContactLastUpdatedTimestamp(mResolver, ids.mContactId); 146 147 ContentValues values = new ContentValues(); 148 values.put(ContactsContract.RawContacts.STARRED, 1); 149 RawContactUtil.update(mResolver, ids.mRawContactId, values); 150 151 long newTime = ContactUtil.queryContactLastUpdatedTimestamp(mResolver, ids.mContactId); 152 assertTrue(newTime > baseTime); 153 154 // Clean up 155 RawContactUtil.delete(mResolver, ids.mRawContactId, true); 156 } 157 158 public void testRawContactPsuedoDelete_hasDeleteLogForContact() { 159 DatabaseAsserts.ContactIdPair ids = DatabaseAsserts.assertAndCreateContact(mResolver); 160 161 long baseTime = ContactUtil.queryContactLastUpdatedTimestamp(mResolver, ids.mContactId); 162 163 RawContactUtil.delete(mResolver, ids.mRawContactId, false); 164 165 DatabaseAsserts.assertHasDeleteLogGreaterThan(mResolver, ids.mContactId, baseTime); 166 167 // clean up 168 RawContactUtil.delete(mResolver, ids.mRawContactId, true); 169 } 170 171 public void testRawContactDelete_hasDeleteLogForContact() { 172 DatabaseAsserts.ContactIdPair ids = DatabaseAsserts.assertAndCreateContact(mResolver); 173 174 long baseTime = ContactUtil.queryContactLastUpdatedTimestamp(mResolver, ids.mContactId); 175 176 RawContactUtil.delete(mResolver, ids.mRawContactId, true); 177 178 DatabaseAsserts.assertHasDeleteLogGreaterThan(mResolver, ids.mContactId, baseTime); 179 180 // already clean 181 } 182 183 private long getContactLastUpdatedTimestampByRawContactId(ContentResolver resolver, 184 long rawContactId) { 185 long contactId = RawContactUtil.queryContactIdByRawContactId(mResolver, rawContactId); 186 MoreAsserts.assertNotEqual(CommonDatabaseUtils.NOT_FOUND, contactId); 187 188 return ContactUtil.queryContactLastUpdatedTimestamp(mResolver, contactId); 189 } 190 } 191