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