Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [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 | |
Walter Jang | b219848 | 2016-08-12 10:37:54 -0700 | [diff] [blame] | 17 | package com.android.contacts.common.model; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.ContentValues; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Walter Jang | 7e79b52 | 2016-08-23 09:33:41 -0700 | [diff] [blame] | 23 | import android.test.suitebuilder.annotation.SmallTest; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 24 | |
| 25 | import com.android.contacts.common.model.RawContact; |
| 26 | |
| 27 | import junit.framework.TestCase; |
| 28 | |
| 29 | /** |
| 30 | * Unit test for {@link RawContact}. |
| 31 | */ |
Walter Jang | 7e79b52 | 2016-08-23 09:33:41 -0700 | [diff] [blame] | 32 | @SmallTest |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 33 | public class RawContactTest extends TestCase { |
| 34 | |
| 35 | private RawContact buildRawContact() { |
| 36 | final ContentValues values = new ContentValues(); |
| 37 | values.put("key1", "value1"); |
| 38 | values.put("key2", "value2"); |
| 39 | |
| 40 | final ContentValues dataItem = new ContentValues(); |
| 41 | dataItem.put("key3", "value3"); |
| 42 | dataItem.put("key4", "value4"); |
| 43 | |
| 44 | final RawContact contact = new RawContact(values); |
| 45 | contact.addDataItemValues(dataItem); |
| 46 | |
| 47 | return contact; |
| 48 | } |
| 49 | |
| 50 | private RawContact buildRawContact2() { |
| 51 | final ContentValues values = new ContentValues(); |
| 52 | values.put("key11", "value11"); |
| 53 | values.put("key22", "value22"); |
| 54 | |
| 55 | final ContentValues dataItem = new ContentValues(); |
| 56 | dataItem.put("key33", "value33"); |
| 57 | dataItem.put("key44", "value44"); |
| 58 | |
| 59 | final RawContact contact = new RawContact(values); |
| 60 | contact.addDataItemValues(dataItem); |
| 61 | |
| 62 | return contact; |
| 63 | } |
| 64 | |
| 65 | public void testNotEquals() { |
| 66 | final RawContact one = buildRawContact(); |
| 67 | final RawContact two = buildRawContact2(); |
| 68 | assertFalse(one.equals(two)); |
| 69 | } |
| 70 | |
| 71 | public void testEquals() { |
| 72 | assertEquals(buildRawContact(), buildRawContact()); |
| 73 | } |
| 74 | |
| 75 | public void testParcelable() { |
| 76 | assertParcelableEquals(buildRawContact()); |
| 77 | } |
| 78 | |
| 79 | private RawContact.NamedDataItem buildNamedDataItem() { |
| 80 | final ContentValues values = new ContentValues(); |
| 81 | values.put("key1", "value1"); |
| 82 | values.put("key2", "value2"); |
| 83 | final Uri uri = Uri.fromParts("content:", "ssp", "fragment"); |
| 84 | |
| 85 | return new RawContact.NamedDataItem(uri, values); |
| 86 | } |
| 87 | |
| 88 | private RawContact.NamedDataItem buildNamedDataItem2() { |
| 89 | final ContentValues values = new ContentValues(); |
| 90 | values.put("key11", "value11"); |
| 91 | values.put("key22", "value22"); |
| 92 | final Uri uri = Uri.fromParts("content:", "blah", "blah"); |
| 93 | |
| 94 | return new RawContact.NamedDataItem(uri, values); |
| 95 | } |
| 96 | |
| 97 | public void testNamedDataItemEquals() { |
| 98 | assertEquals(buildNamedDataItem(), buildNamedDataItem()); |
| 99 | } |
| 100 | |
| 101 | public void testNamedDataItemNotEquals() { |
| 102 | assertFalse(buildNamedDataItem().equals(buildNamedDataItem2())); |
| 103 | } |
| 104 | |
| 105 | public void testNamedDataItemParcelable() { |
| 106 | assertParcelableEquals(buildNamedDataItem()); |
| 107 | } |
| 108 | |
| 109 | private void assertParcelableEquals(Parcelable parcelable) { |
| 110 | final Parcel parcel = Parcel.obtain(); |
| 111 | try { |
| 112 | parcel.writeParcelable(parcelable, 0); |
| 113 | parcel.setDataPosition(0); |
| 114 | |
| 115 | Parcelable out = parcel.readParcelable(parcelable.getClass().getClassLoader()); |
| 116 | assertEquals(parcelable, out); |
| 117 | } finally { |
| 118 | parcel.recycle(); |
| 119 | } |
| 120 | } |
| 121 | } |