GroupsTest.java revision 6bc46c9f22aaa9e68f344b171426fc686d3b536a
1/* 2 * Copyright (C) 2009 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 17package com.android.providers.contacts; 18 19import android.content.ContentUris; 20import android.content.ContentValues; 21import android.database.Cursor; 22import android.net.Uri; 23import android.provider.ContactsContract.Groups; 24import android.provider.ContactsContract.CommonDataKinds.GroupMembership; 25import android.test.suitebuilder.annotation.LargeTest; 26 27/** 28 * Unit tests for {@link Groups} and {@link GroupMembership}. 29 * 30 * Run the test like this: 31 * <code> 32 * adb shell am instrument -w \ 33 * com.android.providers.contacts.tests/android.test.InstrumentationTestRunner 34 * </code> 35 */ 36@LargeTest 37public class GroupsTest extends BaseContactsProvider2Test { 38 39 private static final String GROUP_GREY = "Grey"; 40 private static final String GROUP_RED = "Red"; 41 private static final String GROUP_GREEN = "Green"; 42 private static final String GROUP_BLUE = "Blue"; 43 44 private static final String PERSON_ALPHA = "Alpha"; 45 private static final String PERSON_BRAVO = "Bravo"; 46 private static final String PERSON_CHARLIE = "Charlie"; 47 private static final String PERSON_DELTA = "Delta"; 48 49 private static final String PHONE_ALPHA = "555-1111"; 50 private static final String PHONE_BRAVO_1 = "555-2222"; 51 private static final String PHONE_BRAVO_2 = "555-3333"; 52 private static final String PHONE_CHARLIE_1 = "555-4444"; 53 private static final String PHONE_CHARLIE_2 = "555-5555"; 54 55 public void testGroupSummary() { 56 57 // Clear any existing data before starting 58 // TODO make the provider wipe data automatically 59 ((SynchronousContactsProvider2)mActor.provider).wipeData(); 60 61 // Create a handful of groups 62 long groupGrey = mActor.createGroup(GROUP_GREY); 63 long groupRed = mActor.createGroup(GROUP_RED); 64 long groupGreen = mActor.createGroup(GROUP_GREEN); 65 long groupBlue = mActor.createGroup(GROUP_BLUE); 66 67 // Create a handful of contacts 68 long contactAlpha = mActor.createContact(false, PERSON_ALPHA); 69 long contactBravo = mActor.createContact(false, PERSON_BRAVO); 70 long contactCharlie = mActor.createContact(false, PERSON_CHARLIE); 71 long contactCharlieDupe = mActor.createContact(false, PERSON_CHARLIE); 72 long contactDelta = mActor.createContact(false, PERSON_DELTA); 73 74 assertAggregated(contactCharlie, contactCharlieDupe); 75 76 // Add phone numbers to specific contacts 77 mActor.createPhone(contactAlpha, PHONE_ALPHA); 78 mActor.createPhone(contactBravo, PHONE_BRAVO_1); 79 mActor.createPhone(contactBravo, PHONE_BRAVO_2); 80 mActor.createPhone(contactCharlie, PHONE_CHARLIE_1); 81 mActor.createPhone(contactCharlieDupe, PHONE_CHARLIE_2); 82 83 // Add contacts to various mixture of groups. Grey will have all 84 // contacts, Red only with phone numbers, Green with no phones, and Blue 85 // with no contacts at all. 86 mActor.createGroupMembership(contactAlpha, groupGrey); 87 mActor.createGroupMembership(contactBravo, groupGrey); 88 mActor.createGroupMembership(contactCharlie, groupGrey); 89 mActor.createGroupMembership(contactDelta, groupGrey); 90 91 mActor.createGroupMembership(contactAlpha, groupRed); 92 mActor.createGroupMembership(contactBravo, groupRed); 93 mActor.createGroupMembership(contactCharlie, groupRed); 94 95 mActor.createGroupMembership(contactDelta, groupGreen); 96 97 // Walk across groups summary cursor and verify returned counts. 98 final Cursor cursor = mActor.resolver.query(Groups.CONTENT_SUMMARY_URI, 99 Projections.PROJ_SUMMARY, null, null, null); 100 101 // Require that each group has a summary row 102 assertTrue("Didn't return summary for all groups", (cursor.getCount() == 4)); 103 104 while (cursor.moveToNext()) { 105 final long groupId = cursor.getLong(Projections.COL_ID); 106 final int summaryCount = cursor.getInt(Projections.COL_SUMMARY_COUNT); 107 final int summaryWithPhones = cursor.getInt(Projections.COL_SUMMARY_WITH_PHONES); 108 109 if (groupId == groupGrey) { 110 // Grey should have four aggregates, three with phones. 111 assertEquals("Incorrect Grey count", 4, summaryCount); 112 assertEquals("Incorrect Grey with phones count", 3, summaryWithPhones); 113 } else if (groupId == groupRed) { 114 // Red should have 3 aggregates, all with phones. 115 assertEquals("Incorrect Red count", 3, summaryCount); 116 assertEquals("Incorrect Red with phones count", 3, summaryWithPhones); 117 } else if (groupId == groupGreen) { 118 // Green should have 1 aggregate, none with phones. 119 assertEquals("Incorrect Green count", 1, summaryCount); 120 assertEquals("Incorrect Green with phones count", 0, summaryWithPhones); 121 } else if (groupId == groupBlue) { 122 // Blue should have no contacts. 123 assertEquals("Incorrect Blue count", 0, summaryCount); 124 assertEquals("Incorrect Blue with phones count", 0, summaryWithPhones); 125 } else { 126 fail("Unrecognized group in summary cursor"); 127 } 128 } 129 130 } 131 132 public void testGroupDirtySetOnChange() { 133 Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, 134 createGroup(mAccount, "gsid1", "title1")); 135 assertDirty(uri, true); 136 final Uri updateUri = uri.buildUpon().appendQueryParameter(Groups.MARK_AS_DIRTY, "0").build(); 137 clearDirty(updateUri); 138 assertDirty(uri, false); 139 } 140 141 public void testMarkAsDirtyParameter() { 142 Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, 143 createGroup(mAccount, "gsid1", "title1")); 144 145 final Uri updateUri = uri.buildUpon().appendQueryParameter(Groups.MARK_AS_DIRTY, "0").build(); 146 clearDirty(updateUri); 147 148 ContentValues values = new ContentValues(); 149 values.put(Groups.NOTES, "New notes"); 150 mResolver.update(updateUri, values, null, null); 151 assertDirty(uri, false); 152 } 153 154 public void testGroupDirtyClearedWhenSetExplicitly() { 155 Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, 156 createGroup(mAccount, "gsid1", "title1")); 157 assertDirty(uri, true); 158 159 final Uri updateUri = uri.buildUpon().appendQueryParameter(Groups.MARK_AS_DIRTY, "0").build(); 160 ContentValues values = new ContentValues(); 161 values.put(Groups.DIRTY, 0); 162 values.put(Groups.NOTES, "other notes"); 163 assertEquals(1, mResolver.update(updateUri, values, null, null)); 164 165 assertDirty(uri, false); 166 } 167 168 public void testGroupDeletion1() { 169 long groupId = createGroup(mAccount, "g1", "gt1"); 170 Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); 171 172 assertEquals(1, getCount(uri, null, null)); 173 mResolver.delete(uri, null, null); 174 assertEquals(1, getCount(uri, null, null)); 175 assertStoredValue(uri, Groups.DELETED, "1"); 176 177 Uri permanentDeletionUri = 178 uri.buildUpon().appendQueryParameter(Groups.DELETE_PERMANENTLY, "true").build(); 179 mResolver.delete(permanentDeletionUri, null, null); 180 assertEquals(0, getCount(uri, null, null)); 181 } 182 183 public void testGroupDeletion2() { 184 long groupId = createGroup(mAccount, "g1", "gt1"); 185 Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); 186 187 assertEquals(1, getCount(uri, null, null)); 188 Uri permanentDeletionUri = 189 uri.buildUpon().appendQueryParameter(Groups.DELETE_PERMANENTLY, "true").build(); 190 mResolver.delete(permanentDeletionUri, null, null); 191 assertEquals(0, getCount(uri, null, null)); 192 } 193 194 public void testGroupVersionUpdates() { 195 Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, 196 createGroup(mAccount, "gsid1", "title1")); 197 long version = getVersion(uri); 198 ContentValues values = new ContentValues(); 199 values.put(Groups.TITLE, "title2"); 200 mResolver.update(uri, values, null, null); 201 assertEquals(version + 1, getVersion(uri)); 202 } 203 204 private interface Projections { 205 public static final String[] PROJ_SUMMARY = new String[] { 206 Groups._ID, 207 Groups.SUMMARY_COUNT, 208 Groups.SUMMARY_WITH_PHONES, 209 }; 210 211 public static final int COL_ID = 0; 212 public static final int COL_SUMMARY_COUNT = 1; 213 public static final int COL_SUMMARY_WITH_PHONES = 2; 214 } 215 216} 217