1/*
2 * Copyright (C) 2012 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.contacts.common.model;
18
19import static android.content.ContentProviderOperation.TYPE_INSERT;
20import static android.content.ContentProviderOperation.TYPE_UPDATE;
21
22import android.content.ContentProviderOperation.Builder;
23import android.content.ContentValues;
24import android.provider.ContactsContract.CommonDataKinds.Phone;
25import android.provider.ContactsContract.Data;
26import android.test.suitebuilder.annotation.SmallTest;
27
28import junit.framework.TestCase;
29
30/**
31 * Tests for  {@link ValuesDelta}. These tests
32 * focus on passing changes across {@link android.os.Parcel}, and verifying that they
33 * correctly build expected "diff" operations.
34 */
35@SmallTest
36public class ValuesDeltaTests extends TestCase {
37
38    public static final long TEST_PHONE_ID = 24;
39
40    public static final String TEST_PHONE_NUMBER_1 = "218-555-1111";
41    public static final String TEST_PHONE_NUMBER_2 = "218-555-2222";
42
43    public void testValuesDiffInsert() {
44        final ContentValues after = new ContentValues();
45        after.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
46
47        final ValuesDelta values = ValuesDelta.fromAfter(after);
48
49        // Should produce an insert action
50        final Builder builder = values.buildDiff(Data.CONTENT_URI);
51        final int type = builder.build().getType();
52        assertEquals("Didn't produce insert action", TYPE_INSERT, type);
53    }
54
55    /**
56     * Test that {@link ValuesDelta#buildDiff(android.net.Uri)} is correctly
57     * built for insert, update, and delete cases. Note this only tests behavior
58     * for individual {@link Data} rows.
59     */
60    public void testValuesDiffNone() {
61        final ContentValues before = new ContentValues();
62        before.put(Data._ID, TEST_PHONE_ID);
63        before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
64
65        final ValuesDelta values = ValuesDelta.fromBefore(before);
66
67        // None action shouldn't produce a builder
68        final Builder builder = values.buildDiff(Data.CONTENT_URI);
69        assertNull("None action produced a builder", builder);
70    }
71
72    public void testValuesDiffUpdate() {
73        final ContentValues before = new ContentValues();
74        before.put(Data._ID, TEST_PHONE_ID);
75        before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
76
77        final ValuesDelta values = ValuesDelta.fromBefore(before);
78        values.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
79
80        // Should produce an update action
81        final Builder builder = values.buildDiff(Data.CONTENT_URI);
82        final int type = builder.build().getType();
83        assertEquals("Didn't produce update action", TYPE_UPDATE, type);
84    }
85}
86