blob: 77bf4563f365fa2c41acef79fc7de7ed192aaf64 [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
17package com.android.contacts.common.model;
18
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
Wenyi Wangf46a6192016-02-18 16:34:37 -080026import com.android.contacts.common.compat.CompatUtils;
27import com.android.contacts.common.model.BuilderWrapper;
28
Chiao Cheng429b9112012-11-30 12:04:14 -080029import junit.framework.TestCase;
30
31/**
32 * Tests for {@link ValuesDelta}. These tests
33 * focus on passing changes across {@link android.os.Parcel}, and verifying that they
34 * correctly build expected "diff" operations.
35 */
36@SmallTest
37public class ValuesDeltaTests extends TestCase {
38
39 public static final long TEST_PHONE_ID = 24;
40
41 public static final String TEST_PHONE_NUMBER_1 = "218-555-1111";
42 public static final String TEST_PHONE_NUMBER_2 = "218-555-2222";
43
44 public void testValuesDiffInsert() {
45 final ContentValues after = new ContentValues();
46 after.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
47
48 final ValuesDelta values = ValuesDelta.fromAfter(after);
49
50 // Should produce an insert action
Wenyi Wangf46a6192016-02-18 16:34:37 -080051 final BuilderWrapper builderWrapper = values.buildDiffWrapper(Data.CONTENT_URI);
52 final boolean isInsert = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
53 ? builderWrapper.getBuilder().build().isInsert()
54 : builderWrapper.getType() == CompatUtils.TYPE_INSERT;
Jay Shrauner7bae0632015-08-27 16:23:44 -070055 assertTrue("Didn't produce insert action", isInsert);
Chiao Cheng429b9112012-11-30 12:04:14 -080056 }
57
58 /**
59 * Test that {@link ValuesDelta#buildDiff(android.net.Uri)} is correctly
60 * built for insert, update, and delete cases. Note this only tests behavior
61 * for individual {@link Data} rows.
62 */
63 public void testValuesDiffNone() {
64 final ContentValues before = new ContentValues();
65 before.put(Data._ID, TEST_PHONE_ID);
66 before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
67
68 final ValuesDelta values = ValuesDelta.fromBefore(before);
69
70 // None action shouldn't produce a builder
71 final Builder builder = values.buildDiff(Data.CONTENT_URI);
72 assertNull("None action produced a builder", builder);
73 }
74
75 public void testValuesDiffUpdate() {
76 final ContentValues before = new ContentValues();
77 before.put(Data._ID, TEST_PHONE_ID);
78 before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
79
80 final ValuesDelta values = ValuesDelta.fromBefore(before);
81 values.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
82
83 // Should produce an update action
Wenyi Wangf46a6192016-02-18 16:34:37 -080084 final BuilderWrapper builderWrapper = values.buildDiffWrapper(Data.CONTENT_URI);
85 final boolean isUpdate = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
86 ? builderWrapper.getBuilder().build().isUpdate()
87 : builderWrapper.getType() == CompatUtils.TYPE_UPDATE;
Jay Shrauner7bae0632015-08-27 16:23:44 -070088 assertTrue("Didn't produce update action", isUpdate);
Chiao Cheng429b9112012-11-30 12:04:14 -080089 }
90}