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 17 package com.android.contacts.common.model; 18 19 import android.content.ContentProviderOperation.Builder; 20 import android.content.ContentValues; 21 import android.os.Build; 22 import android.provider.ContactsContract.CommonDataKinds.Phone; 23 import android.provider.ContactsContract.Data; 24 import android.test.suitebuilder.annotation.SmallTest; 25 26 import com.android.contacts.common.compat.CompatUtils; 27 import com.android.contacts.common.model.BuilderWrapper; 28 29 import 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 37 public 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 51 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; 55 assertTrue("Didn't produce insert action", isInsert); 56 } 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 84 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; 88 assertTrue("Didn't produce update action", isUpdate); 89 } 90 } 91