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