blob: b7161d785953d4b7e9d9cec8e82a727a1aa12594 [file] [log] [blame]
Chiao Cheng429b9112012-11-30 12:04:14 -08001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.model;
Chiao Cheng429b9112012-11-30 12:04:14 -080018
Chiao Cheng429b9112012-11-30 12:04:14 -080019import android.content.ContentProviderOperation.Builder;
20import android.content.ContentValues;
Wenyi Wangf46a6192016-02-18 16:34:37 -080021import android.os.Build;
Chiao Cheng429b9112012-11-30 12:04:14 -080022import android.provider.ContactsContract.CommonDataKinds.Phone;
23import android.provider.ContactsContract.Data;
24import android.test.suitebuilder.annotation.SmallTest;
25
Gary Mai69c182a2016-12-05 13:07:03 -080026import com.android.contacts.compat.CompatUtils;
Wenyi Wangf46a6192016-02-18 16:34:37 -080027
Chiao Cheng429b9112012-11-30 12:04:14 -080028import 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
Wenyi Wangf46a6192016-02-18 16:34:37 -080050 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;
Jay Shrauner7bae0632015-08-27 16:23:44 -070054 assertTrue("Didn't produce insert action", isInsert);
Chiao Cheng429b9112012-11-30 12:04:14 -080055 }
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
Wenyi Wangf46a6192016-02-18 16:34:37 -080083 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;
Jay Shrauner7bae0632015-08-27 16:23:44 -070087 assertTrue("Didn't produce update action", isUpdate);
Chiao Cheng429b9112012-11-30 12:04:14 -080088 }
89}