diff options
author | Fred Quintana <fredq@google.com> | 2010-04-13 14:52:56 -0700 |
---|---|---|
committer | Fred Quintana <fredq@google.com> | 2010-04-27 12:20:47 -0700 |
commit | dd5c25c65f09ada246c826fb6d04f0b6d4cf4388 (patch) | |
tree | 93dd1e2b5f0eb20c6773025afc69b9b759a02572 /tests | |
parent | 633d446a21eaceecc0948893769f82163f1cb1e5 (diff) | |
download | packages_providers_ContactsProvider-dd5c25c65f09ada246c826fb6d04f0b6d4cf4388.tar.gz packages_providers_ContactsProvider-dd5c25c65f09ada246c826fb6d04f0b6d4cf4388.tar.bz2 packages_providers_ContactsProvider-dd5c25c65f09ada246c826fb6d04f0b6d4cf4388.zip |
move favorites and my contacts group handling to the provider and sync adapter
Change-Id: Ia2aa3b7d5ede8ce00adbd957a2cae5d89051cf4c
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java | 48 | ||||
-rw-r--r-- | tests/src/com/android/providers/contacts/ContactsProvider2Test.java | 298 |
2 files changed, 340 insertions, 6 deletions
diff --git a/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java b/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java index de410a9e..718880e2 100644 --- a/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java +++ b/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java @@ -171,24 +171,46 @@ public abstract class BaseContactsProvider2Test extends AndroidTestCase { protected long createRawContact(Account account, String... extras) { ContentValues values = new ContentValues(); + extrasVarArgsToValues(values, extras); + final Uri uri = maybeAddAccountQueryParameters(RawContacts.CONTENT_URI, account); + Uri contactUri = mResolver.insert(uri, values); + return ContentUris.parseId(contactUri); + } + + protected int updateItem(Uri uri, long id, String... extras) { + Uri itemUri = ContentUris.withAppendedId(uri, id); + return updateItem(itemUri, extras); + } + + protected int updateItem(Uri uri, String... extras) { + ContentValues values = new ContentValues(); + extrasVarArgsToValues(values, extras); + return mResolver.update(uri, values, null, null); + } + + private static void extrasVarArgsToValues(ContentValues values, String... extras) { for (int i = 0; i < extras.length; ) { values.put(extras[i], extras[i + 1]); i += 2; } - final Uri uri = maybeAddAccountQueryParameters(RawContacts.CONTENT_URI, account); - Uri contactUri = mResolver.insert(uri, values); - return ContentUris.parseId(contactUri); } protected long createGroup(Account account, String sourceId, String title) { - return createGroup(account, sourceId, title, 1); + return createGroup(account, sourceId, title, 1, false, false); } protected long createGroup(Account account, String sourceId, String title, int visible) { + return createGroup(account, sourceId, title, visible, false, false); + } + + protected long createGroup(Account account, String sourceId, String title, + int visible, boolean autoAdd, boolean favorite) { ContentValues values = new ContentValues(); values.put(Groups.SOURCE_ID, sourceId); values.put(Groups.TITLE, title); values.put(Groups.GROUP_VISIBLE, visible); + values.put(Groups.AUTO_ADD, autoAdd ? 1 : 0); + values.put(Groups.FAVORITES, favorite ? 1 : 0); final Uri uri = maybeAddAccountQueryParameters(Groups.CONTENT_URI, account); return ContentUris.parseId(mResolver.insert(uri, values)); } @@ -419,6 +441,16 @@ public abstract class BaseContactsProvider2Test extends AndroidTestCase { return photoId; } + protected boolean queryRawContactIsStarred(long rawContactId) { + Cursor c = queryRawContact(rawContactId); + try { + assertTrue(c.moveToFirst()); + return c.getLong(c.getColumnIndex(RawContacts.STARRED)) != 0; + } finally { + c.close(); + } + } + protected String queryDisplayName(long contactId) { Cursor c = queryContact(contactId); assertTrue(c.moveToFirst()); @@ -577,6 +609,14 @@ public abstract class BaseContactsProvider2Test extends AndroidTestCase { } } + protected void assertNoRowsAndClose(Cursor c) { + try { + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + } + protected static class IdComparator implements Comparator<ContentValues> { public int compare(ContentValues o1, ContentValues o2) { long id1 = o1.getAsLong(ContactsContract.Data._ID); diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java index a8beec0a..2e23e0ec 100644 --- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java +++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java @@ -29,6 +29,7 @@ import android.content.Entity; import android.content.EntityIterator; import android.content.res.AssetFileDescriptor; import android.database.Cursor; +import android.database.DatabaseUtils; import android.net.Uri; import android.os.RemoteException; import android.provider.ContactsContract; @@ -59,6 +60,7 @@ import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; import android.test.MoreAsserts; import android.test.suitebuilder.annotation.LargeTest; +import android.util.Log; import java.io.FileInputStream; import java.io.IOException; @@ -1738,8 +1740,8 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { ContactsProvider2 cp = (ContactsProvider2) getProvider(); cp.onAccountsUpdated(new Account[]{mAccount, mAccountTwo}); assertEquals(1, getCount(RawContacts.CONTENT_URI, null, null)); - assertStoredValue(rawContact3, RawContacts.ACCOUNT_NAME, "account1"); - assertStoredValue(rawContact3, RawContacts.ACCOUNT_TYPE, "account type1"); + assertStoredValue(rawContact3, RawContacts.ACCOUNT_NAME, null); + assertStoredValue(rawContact3, RawContacts.ACCOUNT_TYPE, null); long rawContactId1 = createRawContact(mAccount); insertEmail(rawContactId1, "account1@email.com"); @@ -2409,6 +2411,298 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { } } + public void testAutoGroupMembership() { + long g1 = createGroup(mAccount, "g1", "t1", 0, true /* autoAdd */, false /* favorite */); + long g2 = createGroup(mAccount, "g2", "t2", 0, false /* autoAdd */, false /* favorite */); + long g3 = createGroup(mAccountTwo, "g3", "t3", 0, true /* autoAdd */, false /* favorite */); + long g4 = createGroup(mAccountTwo, "g4", "t4", 0, false /* autoAdd */, false/* favorite */); + long r1 = createRawContact(mAccount); + long r2 = createRawContact(mAccountTwo); + long r3 = createRawContact(null); + + Cursor c = queryGroupMemberships(mAccount); + try { + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r1, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + + c = queryGroupMemberships(mAccountTwo); + try { + assertTrue(c.moveToNext()); + assertEquals(g3, c.getLong(0)); + assertEquals(r2, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + } + + public void testNoAutoAddMembershipAfterGroupCreation() { + long r1 = createRawContact(mAccount); + long r2 = createRawContact(mAccount); + long r3 = createRawContact(mAccount); + long r4 = createRawContact(mAccountTwo); + long r5 = createRawContact(mAccountTwo); + long r6 = createRawContact(null); + + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + + long g1 = createGroup(mAccount, "g1", "t1", 0, true /* autoAdd */, false /* favorite */); + long g2 = createGroup(mAccount, "g2", "t2", 0, false /* autoAdd */, false /* favorite */); + long g3 = createGroup(mAccountTwo, "g3", "t3", 0, true /* autoAdd */, false/* favorite */); + + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + } + + // create some starred and non-starred contacts, some associated with account, some not + // favorites group created + // the starred contacts should be added to group + // favorites group removed + // no change to starred status + public void testFavoritesMembershipAfterGroupCreation() { + long r1 = createRawContact(mAccount, RawContacts.STARRED, "1"); + long r2 = createRawContact(mAccount); + long r3 = createRawContact(mAccount, RawContacts.STARRED, "1"); + long r4 = createRawContact(mAccountTwo, RawContacts.STARRED, "1"); + long r5 = createRawContact(mAccountTwo); + long r6 = createRawContact(null, RawContacts.STARRED, "1"); + long r7 = createRawContact(null); + + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + + long g1 = createGroup(mAccount, "g1", "t1", 0, false /* autoAdd */, true /* favorite */); + long g2 = createGroup(mAccount, "g2", "t2", 0, false /* autoAdd */, false /* favorite */); + long g3 = createGroup(mAccountTwo, "g3", "t3", 0, false /* autoAdd */, false/* favorite */); + + assertTrue(queryRawContactIsStarred(r1)); + assertFalse(queryRawContactIsStarred(r2)); + assertTrue(queryRawContactIsStarred(r3)); + assertTrue(queryRawContactIsStarred(r4)); + assertFalse(queryRawContactIsStarred(r5)); + assertTrue(queryRawContactIsStarred(r6)); + assertFalse(queryRawContactIsStarred(r7)); + + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + Cursor c = queryGroupMemberships(mAccount); + try { + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r1, c.getLong(1)); + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r3, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + + updateItem(RawContacts.CONTENT_URI, r6, + RawContacts.ACCOUNT_NAME, mAccount.name, + RawContacts.ACCOUNT_TYPE, mAccount.type); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + c = queryGroupMemberships(mAccount); + try { + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r1, c.getLong(1)); + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r3, c.getLong(1)); + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r6, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + + mResolver.delete(ContentUris.withAppendedId(Groups.CONTENT_URI, g1), null, null); + + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + + assertTrue(queryRawContactIsStarred(r1)); + assertFalse(queryRawContactIsStarred(r2)); + assertTrue(queryRawContactIsStarred(r3)); + assertTrue(queryRawContactIsStarred(r4)); + assertFalse(queryRawContactIsStarred(r5)); + assertTrue(queryRawContactIsStarred(r6)); + assertFalse(queryRawContactIsStarred(r7)); + } + + public void testFavoritesGroupMembershipChangeAfterStarChange() { + long g1 = createGroup(mAccount, "g1", "t1", 0, false /* autoAdd */, true /* favorite */); + long g2 = createGroup(mAccount, "g2", "t2", 0, false /* autoAdd */, false/* favorite */); + long g4 = createGroup(mAccountTwo, "g4", "t4", 0, false /* autoAdd */, true /* favorite */); + long g5 = createGroup(mAccountTwo, "g5", "t5", 0, false /* autoAdd */, false/* favorite */); + long r1 = createRawContact(mAccount, RawContacts.STARRED, "1"); + long r2 = createRawContact(mAccount); + long r3 = createRawContact(mAccountTwo); + + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + Cursor c = queryGroupMemberships(mAccount); + try { + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r1, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + + // remove the star from r1 + assertEquals(1, updateItem(RawContacts.CONTENT_URI, r1, RawContacts.STARRED, "0")); + + // Since no raw contacts are starred, there should be no group memberships. + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + + // mark r1 as starred + assertEquals(1, updateItem(RawContacts.CONTENT_URI, r1, RawContacts.STARRED, "1")); + // Now that r1 is starred it should have a membership in the one groups from mAccount + // that is marked as a favorite. + // There should be no memberships in mAccountTwo since it has no starred raw contacts. + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + c = queryGroupMemberships(mAccount); + try { + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r1, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + + // remove the star from r1 + assertEquals(1, updateItem(RawContacts.CONTENT_URI, r1, RawContacts.STARRED, "0")); + // Since no raw contacts are starred, there should be no group memberships. + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + + Uri contactUri = findContactUriByRawContactUri( + ContentUris.withAppendedId(RawContacts.CONTENT_URI, r1)); + assertNotNull(contactUri); + + // mark r1 as starred via its contact lookup uri + assertEquals(1, updateItem(contactUri, Contacts.STARRED, "1")); + // Now that r1 is starred it should have a membership in the one groups from mAccount + // that is marked as a favorite. + // There should be no memberships in mAccountTwo since it has no starred raw contacts. + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + c = queryGroupMemberships(mAccount); + try { + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r1, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + + // remove the star from r1 + updateItem(contactUri, Contacts.STARRED, "0"); + // Since no raw contacts are starred, there should be no group memberships. + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + } + + private Uri findContactUriByRawContactUri(Uri uri) { + Cursor c = mResolver.query(uri, + new String[]{RawContacts.CONTACT_ID}, + null, + null, + null); + if (c.moveToNext()) { + return ContentUris.withAppendedId(Contacts.CONTENT_URI, c.getLong(0)); + } + return null; + } + + public void testStarChangedAfterGroupMembershipChange() { + long g1 = createGroup(mAccount, "g1", "t1", 0, false /* autoAdd */, true /* favorite */); + long g2 = createGroup(mAccount, "g2", "t2", 0, false /* autoAdd */, false/* favorite */); + long g4 = createGroup(mAccountTwo, "g4", "t4", 0, false /* autoAdd */, true /* favorite */); + long g5 = createGroup(mAccountTwo, "g5", "t5", 0, false /* autoAdd */, false/* favorite */); + long r1 = createRawContact(mAccount); + long r2 = createRawContact(mAccount); + long r3 = createRawContact(mAccountTwo); + + assertFalse(queryRawContactIsStarred(r1)); + assertFalse(queryRawContactIsStarred(r2)); + assertFalse(queryRawContactIsStarred(r3)); + + Cursor c; + + // add r1 to one favorites group + // r1's star should automatically be set + // r1 should automatically be added to the other favorites group + Uri urir1g1 = insertGroupMembership(r1, g1); + assertTrue(queryRawContactIsStarred(r1)); + assertFalse(queryRawContactIsStarred(r2)); + assertFalse(queryRawContactIsStarred(r3)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + c = queryGroupMemberships(mAccount); + try { + assertTrue(c.moveToNext()); + assertEquals(g1, c.getLong(0)); + assertEquals(r1, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + + // remove r1 from one favorites group + mResolver.delete(urir1g1, null, null); + // r1's star should no longer be set + assertFalse(queryRawContactIsStarred(r1)); + assertFalse(queryRawContactIsStarred(r2)); + assertFalse(queryRawContactIsStarred(r3)); + // there should be no membership rows + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + + // add r3 to the one favorites group for that account + // r3's star should automatically be set + Uri urir3g4 = insertGroupMembership(r3, g4); + assertFalse(queryRawContactIsStarred(r1)); + assertFalse(queryRawContactIsStarred(r2)); + assertTrue(queryRawContactIsStarred(r3)); + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + c = queryGroupMemberships(mAccountTwo); + try { + assertTrue(c.moveToNext()); + assertEquals(g4, c.getLong(0)); + assertEquals(r3, c.getLong(1)); + assertFalse(c.moveToNext()); + } finally { + c.close(); + } + + // remove r3 from the favorites group + mResolver.delete(urir3g4, null, null); + // r3's star should automatically be cleared + assertFalse(queryRawContactIsStarred(r1)); + assertFalse(queryRawContactIsStarred(r2)); + assertFalse(queryRawContactIsStarred(r3)); + assertNoRowsAndClose(queryGroupMemberships(mAccount)); + assertNoRowsAndClose(queryGroupMemberships(mAccountTwo)); + } + + private Cursor queryGroupMemberships(Account account) { + Cursor c = mResolver.query(maybeAddAccountQueryParameters(Data.CONTENT_URI, account), + new String[]{GroupMembership.GROUP_ROW_ID, GroupMembership.RAW_CONTACT_ID}, + Data.MIMETYPE + "=?", new String[]{GroupMembership.CONTENT_ITEM_TYPE}, + GroupMembership.GROUP_SOURCE_ID); + return c; + } + private String readToEnd(FileInputStream inputStream) { try { int ch; |