VCardEntryTests.java revision 48dd8e86a81d2ab40eb762975c8211c225002bf0
1/*
2 * Copyright (C) 2011 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 */
16package com.android.vcard.tests;
17
18import com.android.vcard.VCardConstants;
19import com.android.vcard.VCardEntry;
20import com.android.vcard.VCardEntryConstructor;
21import com.android.vcard.VCardEntryHandler;
22import com.android.vcard.VCardInterpreter;
23
24import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.test.AndroidTestCase;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.List;
31
32public class VCardEntryTests extends AndroidTestCase {
33    private class MockVCardEntryHandler implements VCardEntryHandler {
34        private List<VCardEntry> mEntries = new ArrayList<VCardEntry>();
35        private boolean mOnStartCalled;
36        private boolean mOnEndCalled;
37
38        @Override
39        public void onStart() {
40            assertFalse(mOnStartCalled);
41            mOnStartCalled = true;
42        }
43
44        @Override
45        public void onEntryCreated(VCardEntry entry) {
46            assertTrue(mOnStartCalled);
47            assertFalse(mOnEndCalled);
48            mEntries.add(entry);
49        }
50
51        @Override
52        public void onEnd() {
53            assertTrue(mOnStartCalled);
54            assertFalse(mOnEndCalled);
55            mOnEndCalled = true;
56        }
57
58        public List<VCardEntry> getEntries() {
59            return mEntries;
60        }
61    }
62
63    /**
64     * Tests VCardEntry and related clasess can handle nested classes given
65     * {@link VCardInterpreter} is called appropriately.
66     *
67     * This test manually calls VCardInterpreter's callback mechanism and checks
68     * {@link VCardEntryConstructor} constructs {@link VCardEntry} per given calls.
69     *
70     * Intended vCard is as follows:
71     * <code>
72     * BEGIN:VCARD
73     * N:test1
74     * BEGIN:VCARD
75     * N:test2
76     * END:VCARD
77     * TEL:1
78     * END:VCARD
79     * </code>
80     */
81    public void testNestHandling() {
82        VCardEntryConstructor entryConstructor = new VCardEntryConstructor();
83        MockVCardEntryHandler entryHandler = new MockVCardEntryHandler();
84        entryConstructor.addEntryHandler(entryHandler);
85
86        entryConstructor.start();
87        entryConstructor.startEntry();
88        entryConstructor.startProperty();
89        entryConstructor.propertyName(VCardConstants.PROPERTY_N);
90        entryConstructor.propertyValues(Arrays.asList("test1"));
91        entryConstructor.endProperty();
92
93        entryConstructor.startEntry();  // begin nest
94        entryConstructor.startProperty();
95        entryConstructor.propertyName(VCardConstants.PROPERTY_N);
96        entryConstructor.propertyValues(Arrays.asList("test2"));
97        entryConstructor.endProperty();
98        entryConstructor.endEntry();  // end nest
99
100        entryConstructor.startProperty();
101        entryConstructor.propertyName(VCardConstants.PROPERTY_TEL);
102        entryConstructor.propertyValues(Arrays.asList("1"));
103        entryConstructor.endProperty();
104        entryConstructor.endEntry();
105        entryConstructor.end();
106
107        List<VCardEntry> entries = entryHandler.getEntries();
108        assertEquals(2, entries.size());
109        VCardEntry parent = entries.get(1);
110        VCardEntry child = entries.get(0);
111        assertEquals("test1", parent.getDisplayName());
112        assertEquals("test2", child.getDisplayName());
113        List<VCardEntry.PhoneData> phoneList = parent.getPhoneList();
114        assertNotNull(phoneList);
115        assertEquals(1, phoneList.size());
116        assertEquals("1", phoneList.get(0).data);
117    }
118
119    /**
120     * Tests that VCardEntry emits correct insert operation for name field.
121     */
122    public void testConstructInsertOperationsInsertName() {
123        VCardEntry entry = new VCardEntry();
124        VCardEntry.Property property = new VCardEntry.Property();
125        property.setPropertyName("N");
126        property.addPropertyValue("Family", "Given", "Middle", "Prefix", "Suffix");
127        entry.addProperty(property);
128        entry.consolidateFields();
129
130        assertEquals("Family", entry.getFamilyName());
131        assertEquals("Given", entry.getGivenName());
132        assertEquals("Middle", entry.getMiddleName());
133        assertEquals("Prefix", entry.getPrefix());
134        assertEquals("Suffix", entry.getSuffix());
135
136        ContentResolver resolver = getContext().getContentResolver();
137        ArrayList<ContentProviderOperation> operationList =
138                new ArrayList<ContentProviderOperation>();
139        entry.constructInsertOperations(resolver, operationList);
140
141        // Need too many details for testing these. Just check basics.
142        // TODO: introduce nice-to-test mechanism here.
143        assertEquals(2, operationList.size());
144        assertEquals(ContentProviderOperation.TYPE_INSERT, operationList.get(0).getType());
145        assertEquals(ContentProviderOperation.TYPE_INSERT, operationList.get(1).getType());
146    }
147
148    /**
149     * Tests that VCardEntry refrains from emitting unnecessary insert operation.
150     */
151    public void testConstructInsertOperationsEmptyData() {
152        VCardEntry entry = new VCardEntry();
153        ContentResolver resolver = getContext().getContentResolver();
154        ArrayList<ContentProviderOperation> operationList =
155                new ArrayList<ContentProviderOperation>();
156        entry.constructInsertOperations(resolver, operationList);
157        assertEquals(0, operationList.size());
158    }
159
160    // TODO: add bunch of test for constructInsertOperations..
161}