Chiao Cheng | 429b911 | 2012-11-30 12:04:14 -0800 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.model; |
Chiao Cheng | 429b911 | 2012-11-30 12:04:14 -0800 | [diff] [blame] | 18 | |
Chiao Cheng | 429b911 | 2012-11-30 12:04:14 -0800 | [diff] [blame] | 19 | import android.content.ContentProviderOperation.Builder; |
| 20 | import android.content.ContentValues; |
Wenyi Wang | f46a619 | 2016-02-18 16:34:37 -0800 | [diff] [blame] | 21 | import android.os.Build; |
Chiao Cheng | 429b911 | 2012-11-30 12:04:14 -0800 | [diff] [blame] | 22 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 23 | import android.provider.ContactsContract.Data; |
| 24 | import android.test.suitebuilder.annotation.SmallTest; |
| 25 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 26 | import com.android.contacts.compat.CompatUtils; |
Wenyi Wang | f46a619 | 2016-02-18 16:34:37 -0800 | [diff] [blame] | 27 | |
Chiao Cheng | 429b911 | 2012-11-30 12:04:14 -0800 | [diff] [blame] | 28 | import 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 |
| 36 | public 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 Wang | f46a619 | 2016-02-18 16:34:37 -0800 | [diff] [blame] | 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; |
Jay Shrauner | 7bae063 | 2015-08-27 16:23:44 -0700 | [diff] [blame] | 54 | assertTrue("Didn't produce insert action", isInsert); |
Chiao Cheng | 429b911 | 2012-11-30 12:04:14 -0800 | [diff] [blame] | 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 |
Wenyi Wang | f46a619 | 2016-02-18 16:34:37 -0800 | [diff] [blame] | 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; |
Jay Shrauner | 7bae063 | 2015-08-27 16:23:44 -0700 | [diff] [blame] | 87 | assertTrue("Didn't produce update action", isUpdate); |
Chiao Cheng | 429b911 | 2012-11-30 12:04:14 -0800 | [diff] [blame] | 88 | } |
| 89 | } |