summaryrefslogtreecommitdiff
path: root/tests/src/com/android/contacts/GroupsDaoIntegrationTests.java
blob: 4057ee8211d9015766ee3fd1b1c1102802b9c36d (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
/*
 * Copyright (C) 2016 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.contacts;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract.Data;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.MediumTest;

import com.android.contacts.model.account.AccountWithDataSet;

import java.util.ArrayList;
import java.util.List;

/**
 * Tests of GroupsDaoImpl that perform DB operations directly against CP2
 */
@MediumTest
public class GroupsDaoIntegrationTests extends InstrumentationTestCase {

    private ContentResolver mResolver;
    private List<Uri> mTestRecords;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mTestRecords = new ArrayList<>();
        mResolver = getContext().getContentResolver();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();

        // Cleanup anything leftover by the tests.
        cleanupTestRecords();
        mTestRecords.clear();
    }

    public void test_createGroup_createsGroupWithCorrectTitle() throws Exception {
        final ContactSaveService.GroupsDao sut = createDao();
        final Uri uri = sut.create("Test Create Group", getLocalAccount());

        assertNotNull(uri);
        assertGroupHasTitle(uri, "Test Create Group");
    }

    public void test_deleteEmptyGroup_marksRowDeleted() throws Exception {
        final ContactSaveService.GroupsDao sut = createDao();
        final Uri uri = sut.create("Test Delete Group", getLocalAccount());

        assertEquals(1, sut.delete(uri));

        final Cursor cursor = mResolver.query(uri, null, null, null, null, null);
        try {
            cursor.moveToFirst();
            assertEquals(1, cursor.getInt(cursor.getColumnIndexOrThrow(
                    ContactsContract.Groups.DELETED)));
        } finally {
            cursor.close();
        }
    }

    public void test_undoDeleteEmptyGroup_createsGroupWithMatchingTitle() throws Exception {
        final ContactSaveService.GroupsDao sut = createDao();
        final Uri uri = sut.create("Test Undo Delete Empty Group", getLocalAccount());

        final Bundle undoData = sut.captureDeletionUndoData(uri);

        assertEquals(1, sut.delete(uri));

        final Uri groupUri = sut.undoDeletion(undoData);

        assertGroupHasTitle(groupUri, "Test Undo Delete Empty Group");
    }

    public void test_deleteNonEmptyGroup_removesGroupAndMembers() throws Exception {
        final ContactSaveService.GroupsDao sut = createDao();
        final Uri groupUri = sut.create("Test delete non-empty group", getLocalAccount());

        final long groupId = ContentUris.parseId(groupUri);
        addMemberToGroup(ContentUris.parseId(createRawContact()), groupId);
        addMemberToGroup(ContentUris.parseId(createRawContact()), groupId);

        assertEquals(1, sut.delete(groupUri));

        final Cursor cursor = mResolver.query(Data.CONTENT_URI, null,
                Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?",
                new String[] { GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId) },
                null, null);

        try {
            cursor.moveToFirst();
            // This is more of a characterization test since our code isn't manually deleting
            // the membership rows just the group but this still helps document the expected
            // behavior.
            assertEquals(0, cursor.getCount());
        } finally {
            cursor.close();
        }
    }

    public void test_undoDeleteNonEmptyGroup_restoresGroupAndMembers() throws Exception {
        final ContactSaveService.GroupsDao sut = createDao();
        final Uri groupUri = sut.create("Test undo delete non-empty group", getLocalAccount());

        final long groupId = ContentUris.parseId(groupUri);
        addMemberToGroup(ContentUris.parseId(createRawContact()), groupId);
        addMemberToGroup(ContentUris.parseId(createRawContact()), groupId);

        final Bundle undoData = sut.captureDeletionUndoData(groupUri);

        sut.delete(groupUri);

        final Uri recreatedGroup = sut.undoDeletion(undoData);

        final long newGroupId = ContentUris.parseId(recreatedGroup);

        final Cursor cursor = mResolver.query(Data.CONTENT_URI, null,
                Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?",
                new String[] { GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(newGroupId) },
                null, null);

        try {
            assertEquals(2, cursor.getCount());
        } finally {
            cursor.close();
        }
    }

    public void test_captureUndoDataForDeletedGroup_returnsEmptyBundle() {
        final ContactSaveService.GroupsDao sut = createDao();

        final Uri uri = sut.create("a deleted group", getLocalAccount());
        sut.delete(uri);

        final Bundle undoData = sut.captureDeletionUndoData(uri);

        assertTrue(undoData.isEmpty());
    }

    public void test_captureUndoDataForNonExistentGroup_returnsEmptyBundle() {
        final ContactSaveService.GroupsDao sut = createDao();

        // This test could potentially be flaky if this ID exists for some reason. 10 is subtracted
        // to reduce the likelihood of this happening; some other test may use Integer.MAX_VALUE
        // or nearby values  to cover some special case or boundary condition.
        final long nonExistentId = Integer.MAX_VALUE - 10;

        final Bundle undoData = sut.captureDeletionUndoData(ContentUris
                .withAppendedId(ContactsContract.Groups.CONTENT_URI, nonExistentId));

        assertTrue(undoData.isEmpty());
    }

    public void test_undoWithEmptyBundle_doesNothing() {
        final ContactSaveService.GroupsDao sut = createDao();

        final Uri uri = sut.undoDeletion(new Bundle());

        assertNull(uri);
    }

    public void test_undoDeleteEmptyGroupWithMissingMembersKey_shouldRecreateGroup() {
        final ContactSaveService.GroupsDao sut = createDao();
        final Uri groupUri = sut.create("Test undo delete null memberIds", getLocalAccount());

        final Bundle undoData = sut.captureDeletionUndoData(groupUri);
        undoData.remove(ContactSaveService.GroupsDaoImpl.KEY_GROUP_MEMBERS);
        sut.delete(groupUri);

        sut.undoDeletion(undoData);

        assertGroupWithTitleExists("Test undo delete null memberIds");
    }

    private void assertGroupHasTitle(Uri groupUri, String title) {
        final Cursor cursor = mResolver.query(groupUri,
                new String[] { ContactsContract.Groups.TITLE },
                ContactsContract.Groups.DELETED + "=?",
                new String[] { "0" }, null, null);
        try {
            assertTrue("Group does not have title \"" + title + "\"",
                    cursor.getCount() == 1 && cursor.moveToFirst() &&
                            title.equals(cursor.getString(0)));
        } finally {
            cursor.close();
        }
    }

    private void assertGroupWithTitleExists(String title) {
        final Cursor cursor = mResolver.query(ContactsContract.Groups.CONTENT_URI, null,
                ContactsContract.Groups.TITLE + "=? AND " +
                        ContactsContract.Groups.DELETED + "=?",
                new String[] { title, "0" }, null, null);
        try {
            assertTrue("No group exists with title \"" + title + "\"", cursor.getCount() > 0);
        } finally {
            cursor.close();
        }
    }

    public ContactSaveService.GroupsDao createDao() {
        return new GroupsDaoWrapper(new ContactSaveService.GroupsDaoImpl(getContext()));
    }

    private Uri createRawContact() {
        final ContentValues values = new ContentValues();
        values.putNull(ContactsContract.RawContacts.ACCOUNT_NAME);
        values.putNull(ContactsContract.RawContacts.ACCOUNT_TYPE);
        final Uri result = mResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values);
        mTestRecords.add(result);
        return result;
    }

    private Uri addMemberToGroup(long rawContactId, long groupId) {
        final ContentValues values = new ContentValues();
        values.put(Data.RAW_CONTACT_ID, rawContactId);
        values.put(Data.MIMETYPE,
                GroupMembership.CONTENT_ITEM_TYPE);
        values.put(GroupMembership.GROUP_ROW_ID, groupId);

        // Dont' need to add to testRecords because it will be cleaned up when parent raw_contact
        // is deleted.
        return mResolver.insert(Data.CONTENT_URI, values);
    }

    private Context getContext() {
        return getInstrumentation().getTargetContext();
    }

    private AccountWithDataSet getLocalAccount() {
        return new AccountWithDataSet(null, null, null);
    }

    private void cleanupTestRecords() throws RemoteException, OperationApplicationException {
        final ArrayList<ContentProviderOperation> ops = new ArrayList<>();
        for (Uri uri : mTestRecords) {
            if (uri == null) continue;
            ops.add(ContentProviderOperation
                    .newDelete(uri.buildUpon()
                            .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
                            .build())
                    .build());
        }
        mResolver.applyBatch(ContactsContract.AUTHORITY, ops);
    }

    private class GroupsDaoWrapper implements ContactSaveService.GroupsDao {
        private final ContactSaveService.GroupsDao mDelegate;

        public GroupsDaoWrapper(ContactSaveService.GroupsDao delegate) {
            mDelegate = delegate;
        }

        @Override
        public Uri create(String title, AccountWithDataSet account) {
            final Uri result = mDelegate.create(title, account);
            mTestRecords.add(result);
            return result;
        }

        @Override
        public int delete(Uri groupUri) {
            return mDelegate.delete(groupUri);
        }

        @Override
        public Bundle captureDeletionUndoData(Uri groupUri) {
            return mDelegate.captureDeletionUndoData(groupUri);
        }

        @Override
        public Uri undoDeletion(Bundle undoData) {
            final Uri result = mDelegate.undoDeletion(undoData);
            mTestRecords.add(result);
            return result;
        }
    }
}