summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/providers/contacts/ContactsProvider2TransactionTest.java
blob: 6a82bf9fc821f4af6b3aed275b86c11918401890 (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
/*
 * Copyright (C) 2012 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 static com.android.providers.contacts.TestUtils.cv;

import com.google.android.collect.Lists;

import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.Profile;
import android.provider.ContactsContract.RawContacts;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;

import java.util.ArrayList;

/**
 * Tests to make sure we're handling DB transactions properly in regard to two databases,
 * the profile db and the contacts db.
 */
@LargeTest
public class ContactsProvider2TransactionTest extends BaseContactsProvider2Test {
    private SynchronousContactsProvider2 mProvider;

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

        mProvider = (SynchronousContactsProvider2) getProvider();
    }

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

        mProvider = null;
    }

    /**
     * Make sure we start/finish transactions on the right databases for insert.
     */
    public void testTransactionCallback_insert() {

        final ContentValues values = cv(RawContacts.LAST_TIME_CONTACTED, 12345);

        // Insert a raw contact.
        mProvider.resetTrasactionCallbackCalledFlags();
        mResolver.insert(RawContacts.CONTENT_URI, values);

        // Make sure we only COMMIT on the contacts DB, but there was no transaction on the
        // profile db.
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertNoTransactionsForProfileMode();


        // Insert a profile raw contact.
        mProvider.resetTrasactionCallbackCalledFlags();
        mResolver.insert(Profile.CONTENT_RAW_CONTACTS_URI, values);

        // Even though we only touched the profile DB, we also start and finish a transaction
        // on the contacts db.  AbstractContactsProvider does that to avoid deadlocks.
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertCommitTransactionCalledForProfileMode();
    }

    /**
     * Make sure we start/finish transactions on the right databases for update.
     */
    public void testTransactionCallback_update() {

        final ContentValues values = cv(RawContacts.LAST_TIME_CONTACTED, 12345);

        // Make sure to create a raw contact and a profile raw contact.
        mResolver.insert(RawContacts.CONTENT_URI, values);
        mResolver.insert(Profile.CONTENT_RAW_CONTACTS_URI, values);

        values.clear();
        values.put(RawContacts.LAST_TIME_CONTACTED, 99999);

        // Update all raw contacts.
        mProvider.resetTrasactionCallbackCalledFlags();
        assertTrue(mResolver.update(RawContacts.CONTENT_URI, values, null, null) > 0);

        // Make sure we only COMMIT on the contacts DB, but there was no transaction on the
        // profile db.
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertNoTransactionsForProfileMode();


        // Update all profile raw contacts.
        mProvider.resetTrasactionCallbackCalledFlags();
        assertTrue(mResolver.update(Profile.CONTENT_RAW_CONTACTS_URI, values, null, null) > 0);

        // Even though we only touched the profile DB, we also start and finish a transaction
        // on the contacts db.  AbstractContactsProvider does that to avoid deadlocks.
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertCommitTransactionCalledForProfileMode();
    }

    /**
     * Make sure we start/finish transactions on the right databases for delete.
     */
    public void testTransactionCallback_delete() {

        final ContentValues values = cv(RawContacts.LAST_TIME_CONTACTED, 12345);

        // Make sure to create a raw contact and a profile raw contact.
        mResolver.insert(RawContacts.CONTENT_URI, values);
        mResolver.insert(Profile.CONTENT_RAW_CONTACTS_URI, values);

        // Delete all raw contacts.
        mProvider.resetTrasactionCallbackCalledFlags();
        assertTrue(mResolver.delete(RawContacts.CONTENT_URI, null, null) > 0);

        // Make sure we only COMMIT on the contacts DB, but there was no transaction on the
        // profile db.
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertNoTransactionsForProfileMode();

        // Delete all profile raw contact.
        mProvider.resetTrasactionCallbackCalledFlags();
        assertTrue(mResolver.delete(Profile.CONTENT_RAW_CONTACTS_URI, null, null) > 0);

        // Even though we only touched the profile DB, we also start and finish a transaction
        // on the contacts db.  AbstractContactsProvider does that to avoid deadlocks.
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertCommitTransactionCalledForProfileMode();
    }
    /**
     * Make sure we start/finish transactions on the right databases for bulk insert.
     */
    public void testTransactionCallback_bulkInsert() {

        final ContentValues values = cv(RawContacts.LAST_TIME_CONTACTED, 12345);

        // Insert a raw contact.
        mProvider.resetTrasactionCallbackCalledFlags();
        mResolver.bulkInsert(RawContacts.CONTENT_URI, new ContentValues[] {values});

        // Make sure we only COMMIT on the contacts DB, but there was no transaction on the
        // profile db.
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertNoTransactionsForProfileMode();


        // Insert a profile raw contact.
        mProvider.resetTrasactionCallbackCalledFlags();
        mResolver.bulkInsert(Profile.CONTENT_RAW_CONTACTS_URI, new ContentValues[] {values});

        // Even though we only touched the profile DB, we also start and finish a transaction
        // on the contacts db.  AbstractContactsProvider does that to avoid deadlocks.
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertCommitTransactionCalledForProfileMode();
    }

    /**
     * Add an operation to create a raw contact.
     */
    private static void addInsertContactOperations(ArrayList<ContentProviderOperation> ops) {
        ContentProviderOperation.Builder b;
        b = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
        b.withValue(RawContacts.STARRED, 1);
        b.withValue(RawContacts.TIMES_CONTACTED, 200001);
        ops.add(b.build());

        b = ContentProviderOperation.newInsert(Data.CONTENT_URI);
        b.withValueBackReference(Data.RAW_CONTACT_ID, ops.size() - 1);
        b.withValue(StructuredName.DISPLAY_NAME, "Regular Contact");
        b.withValue(StructuredName.GIVEN_NAME, "Regular");
        b.withValue(StructuredName.FAMILY_NAME, "Contact");
        b.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
        ops.add(b.build());
    }

    /**
     * Check for a contact created that'll be created for {@link #addInsertContactOperations}.
     */
    private void checkStoredContact() {
        assertStoredValues(Contacts.CONTENT_URI, cv(
                Contacts.DISPLAY_NAME, "Regular Contact",
                RawContacts.TIMES_CONTACTED, 200001
                ));
    }

    /**
     * Add an operation to create a profile raw contact.
     */
    private static void addInsertProfileOperations(ArrayList<ContentProviderOperation> ops) {
        ContentProviderOperation.Builder b;
        b = ContentProviderOperation.newInsert(Profile.CONTENT_RAW_CONTACTS_URI);
        b.withValue(RawContacts.STARRED, 1);
        b.withValue(RawContacts.TIMES_CONTACTED, 100001);
        ops.add(b.build());

        b = ContentProviderOperation.newInsert(Data.CONTENT_URI);
        b.withValueBackReference(Data.RAW_CONTACT_ID, ops.size() - 1);
        b.withValue(StructuredName.DISPLAY_NAME, "Profile Contact");
        b.withValue(StructuredName.GIVEN_NAME, "Profile");
        b.withValue(StructuredName.FAMILY_NAME, "Contact");
        b.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
        ops.add(b.build());
    }

    /**
     * Check for a profile contact created that'll be created for
     * {@link #addInsertProfileOperations}.
     */
    private void checkStoredProfile() {
        assertStoredValues(Profile.CONTENT_URI, cv(
                Contacts.DISPLAY_NAME, "Profile Contact",
                RawContacts.TIMES_CONTACTED, 100001
                ));
    }

    public void testTransactionCallback_contactBatch() throws Exception {
        final ArrayList<ContentProviderOperation> ops = Lists.newArrayList();

        addInsertContactOperations(ops);

        mProvider.resetTrasactionCallbackCalledFlags();

        // Execute the operations.
        mResolver.applyBatch(ContactsContract.AUTHORITY, ops);

        // Check the result
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertNoTransactionsForProfileMode();

        checkStoredContact();
    }

    public void testTransactionCallback_profileBatch() throws Exception {
        final ArrayList<ContentProviderOperation> ops = Lists.newArrayList();

        addInsertProfileOperations(ops);

        mProvider.resetTrasactionCallbackCalledFlags();

        // Execute the operations.
        mResolver.applyBatch(ContactsContract.AUTHORITY, ops);

        // Check the result
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertCommitTransactionCalledForProfileMode();

        checkStoredProfile();
    }

    public void testTransactionCallback_mixedBatch() throws Exception {
        final ArrayList<ContentProviderOperation> ops = Lists.newArrayList();

        // Create a raw contact and a profile raw contact in a single batch.

        addInsertContactOperations(ops);
        addInsertProfileOperations(ops);

        mProvider.resetTrasactionCallbackCalledFlags();

        // Execute the operations.
        mResolver.applyBatch(ContactsContract.AUTHORITY, ops);

        // Check the result
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertCommitTransactionCalledForProfileMode();

        checkStoredProfile();
        checkStoredContact();
    }

    public void testTransactionCallback_mixedBatchReversed() throws Exception {
        final ArrayList<ContentProviderOperation> ops = Lists.newArrayList();

        // Create a profile raw contact and a raw contact in a single batch.

        addInsertProfileOperations(ops);
        addInsertContactOperations(ops);

        mProvider.resetTrasactionCallbackCalledFlags();

        // Execute the operations.
        mResolver.applyBatch(ContactsContract.AUTHORITY, ops);

        // Check the result
        mProvider.assertCommitTransactionCalledForContactMode();
        mProvider.assertCommitTransactionCalledForProfileMode();

        checkStoredProfile();
        checkStoredContact();
    }
}