ContactsDatabaseHelperTest.java revision 9d990d339c9e3a9e03f6fe13c260d36665f00e61
1/*
2 * Copyright (C) 2011 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 com.android.providers.contacts.ContactsDatabaseHelper.Tables;
20import com.google.android.collect.Sets;
21
22import android.test.suitebuilder.annotation.SmallTest;
23
24import java.util.Set;
25
26@SmallTest
27public class ContactsDatabaseHelperTest extends BaseContactsProvider2Test {
28    private ContactsDatabaseHelper mDbHelper;
29
30    @Override
31    protected void setUp() throws Exception {
32        super.setUp();
33        mDbHelper = getContactsProvider().getDatabaseHelper(getContext());
34    }
35
36    public void testGetOrCreateAccountId() {
37        final AccountWithDataSet a1 = null;
38        final AccountWithDataSet a2 = new AccountWithDataSet("a", null, null);
39        final AccountWithDataSet a3 = new AccountWithDataSet(null, "b", null);
40        final AccountWithDataSet a4 = new AccountWithDataSet(null, null, "c");
41        final AccountWithDataSet a5 = new AccountWithDataSet("a", "b", "c");
42
43        // First, there's no accounts.  getAccountIdOrNull() always returns null.
44        assertNull(mDbHelper.getAccountIdOrNull(a1));
45        assertNull(mDbHelper.getAccountIdOrNull(a2));
46        assertNull(mDbHelper.getAccountIdOrNull(a3));
47        assertNull(mDbHelper.getAccountIdOrNull(a4));
48        assertNull(mDbHelper.getAccountIdOrNull(a5));
49
50        // getOrCreateAccountId should create accounts.
51        final long a1id = mDbHelper.getOrCreateAccountIdInTransaction(a1);
52        final long a2id = mDbHelper.getOrCreateAccountIdInTransaction(a2);
53        final long a3id = mDbHelper.getOrCreateAccountIdInTransaction(a3);
54        final long a4id = mDbHelper.getOrCreateAccountIdInTransaction(a4);
55        final long a5id = mDbHelper.getOrCreateAccountIdInTransaction(a5);
56
57        // The IDs should be all positive and unique.
58        assertTrue(a1id > 0);
59        assertTrue(a2id > 0);
60        assertTrue(a3id > 0);
61        assertTrue(a4id > 0);
62        assertTrue(a5id > 0);
63
64        final Set<Long> ids = Sets.newHashSet();
65        ids.add(a1id);
66        ids.add(a2id);
67        ids.add(a3id);
68        ids.add(a4id);
69        ids.add(a5id);
70        assertEquals(5, ids.size());
71
72        // Second call: This time getOrCreateAccountId will return the existing IDs.
73        assertEquals(a1id, mDbHelper.getOrCreateAccountIdInTransaction(a1));
74        assertEquals(a2id, mDbHelper.getOrCreateAccountIdInTransaction(a2));
75        assertEquals(a3id, mDbHelper.getOrCreateAccountIdInTransaction(a3));
76        assertEquals(a4id, mDbHelper.getOrCreateAccountIdInTransaction(a4));
77        assertEquals(a5id, mDbHelper.getOrCreateAccountIdInTransaction(a5));
78
79        // Now getAccountIdOrNull() returns IDs too.
80        assertEquals((Long) a1id, mDbHelper.getAccountIdOrNull(a1));
81        assertEquals((Long) a2id, mDbHelper.getAccountIdOrNull(a2));
82        assertEquals((Long) a3id, mDbHelper.getAccountIdOrNull(a3));
83        assertEquals((Long) a4id, mDbHelper.getAccountIdOrNull(a4));
84        assertEquals((Long) a5id, mDbHelper.getAccountIdOrNull(a5));
85
86        // null and AccountWithDataSet.NULL should be treated as the same thing.
87        assertEquals(a1id, mDbHelper.getOrCreateAccountIdInTransaction(AccountWithDataSet.LOCAL));
88        assertEquals((Long) a1id, mDbHelper.getAccountIdOrNull(AccountWithDataSet.LOCAL));
89
90        // Clear the table, but until we invalidate the cache getAccountIdOrNull() keeps returning
91        // cached values.
92        mDbHelper.getWritableDatabase().execSQL("delete from " + Tables.ACCOUNTS);
93        assertEquals((Long) a1id, mDbHelper.getAccountIdOrNull(AccountWithDataSet.LOCAL));
94
95        // invalidateAccountCacheInTransaction() clears the cache.
96        mDbHelper.invalidateAccountCacheInTransaction();
97        assertNull(mDbHelper.getAccountIdOrNull(AccountWithDataSet.LOCAL));
98        assertNull(mDbHelper.getAccountIdOrNull(a1));
99        assertNull(mDbHelper.getAccountIdOrNull(a2));
100        assertNull(mDbHelper.getAccountIdOrNull(a3));
101        assertNull(mDbHelper.getAccountIdOrNull(a4));
102        assertNull(mDbHelper.getAccountIdOrNull(a5));
103
104        // Logically same as a5, but physically different object.
105        final AccountWithDataSet a5b = new AccountWithDataSet("a", "b", "c");
106        // a5 and a5b should have the same ID.
107        assertEquals(
108                mDbHelper.getOrCreateAccountIdInTransaction(a5),
109                mDbHelper.getOrCreateAccountIdInTransaction(a5b));
110    }
111}
112