1/*
2 * Copyright (C) 2013 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 android.test.suitebuilder.annotation.SmallTest;
20
21import junit.framework.TestCase;
22
23import java.util.Map;
24import java.util.Set;
25
26/**
27 * Unit tests for TransactionContext.
28 */
29@SmallTest
30public class TransactionContextTest extends TestCase {
31
32    public void testClearExceptSearchIndexUpdates_returnsNewSets() {
33        TransactionContext context = new TransactionContext(false);
34        context.markRawContactDirtyAndChanged(1L, false);
35        context.rawContactUpdated(1L);
36        context.rawContactInserted(1L, 1L);
37        context.syncStateUpdated(1L, new Object());
38
39        context.clearExceptSearchIndexUpdates();
40
41        Set<Long> newDirty = context.getDirtyRawContactIds();
42        Set<Long> newChanged = context.getChangedRawContactIds();
43        Set<Long> newInserted = context.getInsertedRawContactIds();
44        Set<Long> newUpdated = context.getUpdatedRawContactIds();
45        Set<Map.Entry<Long, Object>> newSync = context.getUpdatedSyncStates();
46
47        assertTrue(newDirty.isEmpty());
48        assertTrue(newChanged.isEmpty());
49        assertTrue(newInserted.isEmpty());
50        assertTrue(newUpdated.isEmpty());
51        assertTrue(newSync.isEmpty());
52    }
53
54    public void testMarkDirtyAndChanged_onlyUpdatesChanged() {
55        TransactionContext context = new TransactionContext(false);
56
57        context.markRawContactDirtyAndChanged(1L, true /* isSyncAdapter */);
58
59        assertEquals(1, context.getChangedRawContactIds().size());
60        assertEquals(0, context.getDirtyRawContactIds().size());
61    }
62
63    public void testMarkDirtyAndChanged_onlyUpdatesDirtyAndChanged() {
64        TransactionContext context = new TransactionContext(false);
65
66        context.markRawContactDirtyAndChanged(1L, false /* isSyncAdapter */);
67
68        assertEquals(1, context.getChangedRawContactIds().size());
69        assertEquals(1, context.getDirtyRawContactIds().size());
70    }
71
72    public void testRawContactInserted_affectsChangedContacts() {
73        TransactionContext context = new TransactionContext(false);
74        assertTrue(context.getChangedRawContactIds().isEmpty());
75
76        context.rawContactInserted(1L, 2L);
77        assertEquals(1, context.getChangedRawContactIds().size());
78        assertTrue(context.getChangedRawContactIds().contains(1L));
79
80        context.rawContactInserted(5L, 10L);
81        assertEquals(2, context.getChangedRawContactIds().size());
82        assertTrue(context.getChangedRawContactIds().contains(5L));
83    }
84
85    public void testMarkRawContactChangedOrDeletedOrInserted_affectsChangedContacts() {
86        TransactionContext context = new TransactionContext(false);
87        assertTrue(context.getChangedRawContactIds().isEmpty());
88
89        context.markRawContactChangedOrDeletedOrInserted(1L);
90        assertEquals(1, context.getChangedRawContactIds().size());
91        assertTrue(context.getChangedRawContactIds().contains(1L));
92
93        context.rawContactInserted(5L, 10L);
94        assertEquals(2, context.getChangedRawContactIds().size());
95        assertTrue(context.getChangedRawContactIds().contains(5L));
96    }
97}
98