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.model;
18
19import android.content.ContentProviderOperation.Builder;
20import android.content.ContentValues;
21import android.os.Build;
22import android.provider.ContactsContract.CommonDataKinds.Phone;
23import android.provider.ContactsContract.Data;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import com.android.contacts.compat.CompatUtils;
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 BuilderWrapper builderWrapper = values.buildDiffWrapper(Data.CONTENT_URI);
51        final boolean isInsert = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
52                ? builderWrapper.getBuilder().build().isInsert()
53                : builderWrapper.getType() == CompatUtils.TYPE_INSERT;
54        assertTrue("Didn't produce insert action", isInsert);
55    }
56
57    /**
58     * Test that {@link ValuesDelta#buildDiff(android.net.Uri)} is correctly
59     * built for insert, update, and delete cases. Note this only tests behavior
60     * for individual {@link Data} rows.
61     */
62    public void testValuesDiffNone() {
63        final ContentValues before = new ContentValues();
64        before.put(Data._ID, TEST_PHONE_ID);
65        before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
66
67        final ValuesDelta values = ValuesDelta.fromBefore(before);
68
69        // None action shouldn't produce a builder
70        final Builder builder = values.buildDiff(Data.CONTENT_URI);
71        assertNull("None action produced a builder", builder);
72    }
73
74    public void testValuesDiffUpdate() {
75        final ContentValues before = new ContentValues();
76        before.put(Data._ID, TEST_PHONE_ID);
77        before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
78
79        final ValuesDelta values = ValuesDelta.fromBefore(before);
80        values.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
81
82        // Should produce an update action
83        final BuilderWrapper builderWrapper = values.buildDiffWrapper(Data.CONTENT_URI);
84        final boolean isUpdate = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
85                ? builderWrapper.getBuilder().build().isUpdate()
86                : builderWrapper.getType() == CompatUtils.TYPE_UPDATE;
87        assertTrue("Didn't produce update action", isUpdate);
88    }
89}
90