summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/providers/contacts/GroupsTest.java
blob: 18463217748caef975c287bdf9eebaa654a77dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.contacts;

import com.google.android.collect.Lists;

import android.accounts.Account;
import android.content.ContentProviderOperation;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.AggregationExceptions;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Groups;
import android.provider.ContactsContract.Settings;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;

import java.util.ArrayList;

/**
 * Unit tests for {@link Groups} and {@link GroupMembership}.
 *
 * Run the test like this:
 * <code>
 * adb shell am instrument -e class com.android.providers.contacts.GroupsTest -w \
 *         com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
 * </code>
 */
public class GroupsTest extends BaseContactsProvider2Test {

    private static final String GROUP_GREY = "Grey";
    private static final String GROUP_RED = "Red";
    private static final String GROUP_GREEN = "Green";
    private static final String GROUP_BLUE = "Blue";

    private static final String PERSON_ALPHA = "Alpha";
    private static final String PERSON_BRAVO = "Bravo";
    private static final String PERSON_CHARLIE = "Charlie";
    private static final String PERSON_DELTA = "Delta";

    private static final String PHONE_ALPHA = "555-1111";
    private static final String PHONE_BRAVO_1 = "555-2222";
    private static final String PHONE_BRAVO_2 = "555-3333";
    private static final String PHONE_CHARLIE_1 = "555-4444";
    private static final String PHONE_CHARLIE_2 = "555-5555";

    @LargeTest
    public void testGroupSummary() {

        // Clear any existing data before starting
        // TODO make the provider wipe data automatically
        ((SynchronousContactsProvider2)mActor.provider).wipeData();

        // Create a handful of groups
        long groupGrey = mActor.createGroup(GROUP_GREY);
        long groupRed = mActor.createGroup(GROUP_RED);
        long groupGreen = mActor.createGroup(GROUP_GREEN);
        long groupBlue = mActor.createGroup(GROUP_BLUE);

        // Create a handful of contacts
        long contactAlpha = mActor.createRawContact(false, PERSON_ALPHA);
        long contactBravo = mActor.createRawContact(false, PERSON_BRAVO);
        long contactCharlie = mActor.createRawContact(false, PERSON_CHARLIE);
        long contactCharlieDupe = mActor.createRawContact(false, PERSON_CHARLIE);
        setAggregationException(
                AggregationExceptions.TYPE_KEEP_TOGETHER, contactCharlie, contactCharlieDupe);
        long contactDelta = mActor.createRawContact(false, PERSON_DELTA);

        assertAggregated(contactCharlie, contactCharlieDupe);

        // Add phone numbers to specific contacts
        mActor.createPhone(contactAlpha, PHONE_ALPHA);
        mActor.createPhone(contactBravo, PHONE_BRAVO_1);
        mActor.createPhone(contactBravo, PHONE_BRAVO_2);
        mActor.createPhone(contactCharlie, PHONE_CHARLIE_1);
        mActor.createPhone(contactCharlieDupe, PHONE_CHARLIE_2);

        // Add contacts to various mixture of groups. Grey will have all
        // contacts, Red only with phone numbers, Green with no phones, and Blue
        // with no contacts at all.
        mActor.createGroupMembership(contactAlpha, groupGrey);
        mActor.createGroupMembership(contactBravo, groupGrey);
        mActor.createGroupMembership(contactCharlie, groupGrey);
        mActor.createGroupMembership(contactDelta, groupGrey);

        mActor.createGroupMembership(contactAlpha, groupRed);
        mActor.createGroupMembership(contactBravo, groupRed);
        mActor.createGroupMembership(contactCharlie, groupRed);

        mActor.createGroupMembership(contactDelta, groupGreen);

        // Walk across groups summary cursor and verify returned counts.
        final Cursor cursor = mActor.resolver.query(Groups.CONTENT_SUMMARY_URI,
                Projections.PROJ_SUMMARY, null, null, null);

        // Require that each group has a summary row
        assertTrue("Didn't return summary for all groups", (cursor.getCount() == 4));

        while (cursor.moveToNext()) {
            final long groupId = cursor.getLong(Projections.COL_ID);
            final int summaryCount = cursor.getInt(Projections.COL_SUMMARY_COUNT);
            final int summaryWithPhones = cursor.getInt(Projections.COL_SUMMARY_WITH_PHONES);

            if (groupId == groupGrey) {
                // Grey should have four aggregates, three with phones.
                assertEquals("Incorrect Grey count", 4, summaryCount);
                assertEquals("Incorrect Grey with phones count", 3, summaryWithPhones);
            } else if (groupId == groupRed) {
                // Red should have 3 aggregates, all with phones.
                assertEquals("Incorrect Red count", 3, summaryCount);
                assertEquals("Incorrect Red with phones count", 3, summaryWithPhones);
            } else if (groupId == groupGreen) {
                // Green should have 1 aggregate, none with phones.
                assertEquals("Incorrect Green count", 1, summaryCount);
                assertEquals("Incorrect Green with phones count", 0, summaryWithPhones);
            } else if (groupId == groupBlue) {
                // Blue should have no contacts.
                assertEquals("Incorrect Blue count", 0, summaryCount);
                assertEquals("Incorrect Blue with phones count", 0, summaryWithPhones);
            } else {
                fail("Unrecognized group in summary cursor");
            }
        }
        cursor.close();
    }

    @MediumTest
    public void testGroupDirtySetOnChange() {
        Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI,
                createGroup(mAccount, "gsid1", "title1"));
        assertDirty(uri, true);
        clearDirty(uri);
        assertDirty(uri, false);
    }

    @MediumTest
    public void testMarkAsDirtyParameter() {
        Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI,
                createGroup(mAccount, "gsid1", "title1"));
        clearDirty(uri);
        Uri updateUri = setCallerIsSyncAdapter(uri, mAccount);

        ContentValues values = new ContentValues();
        values.put(Groups.NOTES, "New notes");
        mResolver.update(updateUri, values, null, null);
        assertDirty(uri, false);
    }

    @MediumTest
    public void testGroupDirtyClearedWhenSetExplicitly() {
        Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI,
                createGroup(mAccount, "gsid1", "title1"));
        assertDirty(uri, true);

        ContentValues values = new ContentValues();
        values.put(Groups.DIRTY, 0);
        values.put(Groups.NOTES, "other notes");
        assertEquals(1, mResolver.update(uri, values, null, null));

        assertDirty(uri, false);
    }

    @MediumTest
    public void testGroupDeletion1() {
        long groupId = createGroup(mAccount, "g1", "gt1");
        Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId);

        assertEquals(1, getCount(uri, null, null));
        mResolver.delete(uri, null, null);
        assertEquals(1, getCount(uri, null, null));
        assertStoredValue(uri, Groups.DELETED, "1");

        Uri permanentDeletionUri = setCallerIsSyncAdapter(uri, mAccount);
        mResolver.delete(permanentDeletionUri, null, null);
        assertEquals(0, getCount(uri, null, null));
    }

    @MediumTest
    public void testGroupDeletion2() {
        long groupId = createGroup(mAccount, "g1", "gt1");
        Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId);

        assertEquals(1, getCount(uri, null, null));
        Uri permanentDeletionUri = setCallerIsSyncAdapter(uri, mAccount);
        mResolver.delete(permanentDeletionUri, null, null);
        assertEquals(0, getCount(uri, null, null));
    }

    @MediumTest
    public void testGroupVersionUpdates() {
        Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI,
                createGroup(mAccount, "gsid1", "title1"));
        long version = getVersion(uri);
        ContentValues values = new ContentValues();
        values.put(Groups.TITLE, "title2");
        mResolver.update(uri, values, null, null);
        assertEquals(version + 1, getVersion(uri));
    }

    private interface Projections {
        public static final String[] PROJ_SUMMARY = new String[] {
            Groups._ID,
            Groups.SUMMARY_COUNT,
            Groups.SUMMARY_WITH_PHONES,
        };

        public static final int COL_ID = 0;
        public static final int COL_SUMMARY_COUNT = 1;
        public static final int COL_SUMMARY_WITH_PHONES = 2;
    }

    private static final Account sTestAccount = new Account("user@example.com", "com.example");
    private static final Account sSecondAccount = new Account("other@example.net", "net.example");
    private static final String GROUP_ID = "testgroup";

    public void assertRawContactVisible(long rawContactId, boolean expected) {
        final long contactId = this.queryContactId(rawContactId);
        assertContactVisible(contactId, expected);
    }

    public void assertContactVisible(long contactId, boolean expected) {
        final Cursor cursor = mResolver.query(Contacts.CONTENT_URI, new String[] {
            Contacts.IN_VISIBLE_GROUP
        }, Contacts._ID + "=" + contactId, null, null);
        assertTrue("Contact not found", cursor.moveToFirst());
        final boolean actual = (cursor.getInt(0) != 0);
        cursor.close();
        assertEquals("Unexpected visibility", expected, actual);
    }

    public ContentProviderOperation buildVisibleAssert(long contactId, boolean visible) {
        return ContentProviderOperation.newAssertQuery(Contacts.CONTENT_URI).withSelection(
                Contacts._ID + "=" + contactId + " AND " + Contacts.IN_VISIBLE_GROUP + "="
                        + (visible ? 1 : 0), null).withExpectedCount(1).build();
    }

    @LargeTest
    public void testDelayVisibleTransaction() throws RemoteException, OperationApplicationException {
        final ContentValues values = new ContentValues();

        final long groupId = this.createGroup(sTestAccount, GROUP_ID, GROUP_ID, 1);
        final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId);

        // Create contact with specific membership
        final long rawContactId = this.createRawContact(sTestAccount);
        final long contactId = this.queryContactId(rawContactId);
        final Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);

        this.insertGroupMembership(rawContactId, groupId);

        final ArrayList<ContentProviderOperation> oper = Lists.newArrayList();

        // Update visibility inside a transaction and assert that inside the
        // transaction it hasn't been updated yet.
        oper.add(buildVisibleAssert(contactId, true));
        oper.add(ContentProviderOperation.newUpdate(groupUri).withValue(Groups.GROUP_VISIBLE, 0)
                .build());
        oper.add(buildVisibleAssert(contactId, true));
        mResolver.applyBatch(ContactsContract.AUTHORITY, oper);

        // After previous transaction finished, visibility should be updated
        oper.clear();
        oper.add(buildVisibleAssert(contactId, false));
        mResolver.applyBatch(ContactsContract.AUTHORITY, oper);
    }

    public void testLocalSingleVisible() {
        final long rawContactId = this.createRawContact();

        // Single, local contacts should always be visible
        assertRawContactVisible(rawContactId, true);
    }

    public void testLocalMixedVisible() {
        // Aggregate, when mixed with local, should become visible
        final long rawContactId1 = this.createRawContact();
        final long rawContactId2 = this.createRawContact(sTestAccount);

        final long groupId = this.createGroup(sTestAccount, GROUP_ID, GROUP_ID, 0);
        this.insertGroupMembership(rawContactId2, groupId);

        // Make sure they are still apart
        assertNotAggregated(rawContactId1, rawContactId2);
        assertRawContactVisible(rawContactId1, true);
        assertRawContactVisible(rawContactId2, false);

        // Force together and see what happens
        final ContentValues values = new ContentValues();
        values.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);
        values.put(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1);
        values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2);
        mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);

        assertRawContactVisible(rawContactId1, true);
        assertRawContactVisible(rawContactId2, true);
    }

    public void testUngroupedVisible() {
        final long rawContactId = this.createRawContact(sTestAccount);

        final ContentValues values = new ContentValues();
        values.put(Settings.ACCOUNT_NAME, sTestAccount.name);
        values.put(Settings.ACCOUNT_TYPE, sTestAccount.type);
        values.put(Settings.UNGROUPED_VISIBLE, 0);
        mResolver.insert(Settings.CONTENT_URI, values);

        assertRawContactVisible(rawContactId, false);

        values.clear();
        values.put(Settings.UNGROUPED_VISIBLE, 1);
        mResolver.update(Settings.CONTENT_URI, values, Settings.ACCOUNT_NAME + "=? AND "
                + Settings.ACCOUNT_TYPE + "=?", new String[] {
                sTestAccount.name, sTestAccount.type
        });

        assertRawContactVisible(rawContactId, true);
    }

    public void testMultipleSourcesVisible() {
        final long rawContactId1 = this.createRawContact(sTestAccount);
        final long rawContactId2 = this.createRawContact(sSecondAccount);

        final long groupId = this.createGroup(sTestAccount, GROUP_ID, GROUP_ID, 0);
        this.insertGroupMembership(rawContactId1, groupId);

        // Make sure still invisible
        assertRawContactVisible(rawContactId1, false);
        assertRawContactVisible(rawContactId2, false);

        // Make group visible
        final ContentValues values = new ContentValues();
        values.put(Groups.GROUP_VISIBLE, 1);
        mResolver.update(Groups.CONTENT_URI, values, Groups._ID + "=" + groupId, null);

        assertRawContactVisible(rawContactId1, true);
        assertRawContactVisible(rawContactId2, false);

        // Force them together
        values.clear();
        values.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);
        values.put(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1);
        values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2);
        mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);

        assertRawContactVisible(rawContactId1, true);
        assertRawContactVisible(rawContactId2, true);

        // Make group invisible
        values.clear();
        values.put(Groups.GROUP_VISIBLE, 0);
        mResolver.update(Groups.CONTENT_URI, values, Groups._ID + "=" + groupId, null);

        assertRawContactVisible(rawContactId1, false);
        assertRawContactVisible(rawContactId2, false);

        // Turn on ungrouped for first
        values.clear();
        values.put(Settings.ACCOUNT_NAME, sTestAccount.name);
        values.put(Settings.ACCOUNT_TYPE, sTestAccount.type);
        values.put(Settings.UNGROUPED_VISIBLE, 1);
        mResolver.insert(Settings.CONTENT_URI, values);

        assertRawContactVisible(rawContactId1, false);
        assertRawContactVisible(rawContactId2, false);

        // Turn on ungrouped for second account
        values.clear();
        values.put(Settings.ACCOUNT_NAME, sSecondAccount.name);
        values.put(Settings.ACCOUNT_TYPE, sSecondAccount.type);
        values.put(Settings.UNGROUPED_VISIBLE, 1);
        mResolver.insert(Settings.CONTENT_URI, values);

        assertRawContactVisible(rawContactId1, true);
        assertRawContactVisible(rawContactId2, true);
    }
}