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 */
16
17package src.com.android.contacts.common.util;
18
19import android.provider.ContactsContract.CommonDataKinds.StructuredName;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22import android.text.TextUtils;
23
24import com.android.contacts.common.util.NameConverter;
25
26import java.util.HashMap;
27import java.util.Map;
28
29/**
30 * Tests for {@link NameConverter}.
31 */
32@SmallTest
33public class NameConverterTests extends AndroidTestCase {
34
35    public void testStructuredNameToDisplayName() {
36        Map<String, String> structuredName = new HashMap<String, String>();
37        structuredName.put(StructuredName.PREFIX, "Mr.");
38        structuredName.put(StructuredName.GIVEN_NAME, "John");
39        structuredName.put(StructuredName.MIDDLE_NAME, "Quincy");
40        structuredName.put(StructuredName.FAMILY_NAME, "Adams");
41        structuredName.put(StructuredName.SUFFIX, "Esquire");
42
43        assertEquals("Mr. John Quincy Adams, Esquire",
44                NameConverter.structuredNameToDisplayName(mContext, structuredName));
45
46        structuredName.remove(StructuredName.SUFFIX);
47        assertEquals("Mr. John Quincy Adams",
48                NameConverter.structuredNameToDisplayName(mContext, structuredName));
49
50        structuredName.remove(StructuredName.MIDDLE_NAME);
51        assertEquals("Mr. John Adams",
52                NameConverter.structuredNameToDisplayName(mContext, structuredName));
53    }
54
55    public void testDisplayNameToStructuredName() {
56        assertStructuredName("Mr. John Quincy Adams, Esquire",
57                "Mr.", "John", "Quincy", "Adams", "Esquire");
58        assertStructuredName("John Doe", null, "John", null, "Doe", null);
59        assertStructuredName("Ms. Jane Eyre", "Ms.", "Jane", null, "Eyre", null);
60        assertStructuredName("Dr Leo Spaceman, PhD", "Dr", "Leo", null, "Spaceman", "PhD");
61    }
62
63    /**
64     * Helper method to check whether a given display name parses out to the other parameters.
65     * @param displayName Display name to break into a structured name.
66     * @param prefix Expected prefix (null if not expected).
67     * @param givenName Expected given name (null if not expected).
68     * @param middleName Expected middle name (null if not expected).
69     * @param familyName Expected family name (null if not expected).
70     * @param suffix Expected suffix (null if not expected).
71     */
72    private void assertStructuredName(String displayName, String prefix,
73            String givenName, String middleName, String familyName, String suffix) {
74        Map<String, String> structuredName = NameConverter.displayNameToStructuredName(mContext,
75                displayName);
76        checkNameComponent(StructuredName.PREFIX, prefix, structuredName);
77        checkNameComponent(StructuredName.GIVEN_NAME, givenName, structuredName);
78        checkNameComponent(StructuredName.MIDDLE_NAME, middleName, structuredName);
79        checkNameComponent(StructuredName.FAMILY_NAME, familyName, structuredName);
80        checkNameComponent(StructuredName.SUFFIX, suffix, structuredName);
81        assertEquals(0, structuredName.size());
82    }
83
84    /**
85     * Checks that the given field and value are present in the structured name map (or not present
86     * if the given value is null).  If the value is present and matches, the key is removed from
87     * the map - once all components of the name are checked, the map should be empty.
88     * @param field Field to check.
89     * @param value Expected value for the field (null if it is not expected to be populated).
90     * @param structuredName The map of structured field names to values.
91     */
92    private void checkNameComponent(String field, String value,
93            Map<String, String> structuredName) {
94        if (TextUtils.isEmpty(value)) {
95            assertNull(structuredName.get(field));
96        } else {
97            assertEquals(value, structuredName.get(field));
98        }
99        structuredName.remove(field);
100    }
101}
102