summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/contacts/common/util/NameConverterTests.java
blob: c4f67c3bec33eb7ba2ce9e5b99c32d06621562bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package src.com.android.contacts.common.util;

import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.TextUtils;

import com.android.contacts.common.util.NameConverter;

import java.util.HashMap;
import java.util.Map;

/**
 * Tests for {@link NameConverter}.
 */
@SmallTest
public class NameConverterTests extends AndroidTestCase {

    public void testStructuredNameToDisplayName() {
        Map<String, String> structuredName = new HashMap<String, String>();
        structuredName.put(StructuredName.PREFIX, "Mr.");
        structuredName.put(StructuredName.GIVEN_NAME, "John");
        structuredName.put(StructuredName.MIDDLE_NAME, "Quincy");
        structuredName.put(StructuredName.FAMILY_NAME, "Adams");
        structuredName.put(StructuredName.SUFFIX, "Esquire");

        assertEquals("Mr. John Quincy Adams, Esquire",
                NameConverter.structuredNameToDisplayName(mContext, structuredName));

        structuredName.remove(StructuredName.SUFFIX);
        assertEquals("Mr. John Quincy Adams",
                NameConverter.structuredNameToDisplayName(mContext, structuredName));

        structuredName.remove(StructuredName.MIDDLE_NAME);
        assertEquals("Mr. John Adams",
                NameConverter.structuredNameToDisplayName(mContext, structuredName));
    }

    public void testDisplayNameToStructuredName() {
        assertStructuredName("Mr. John Quincy Adams, Esquire",
                "Mr.", "John", "Quincy", "Adams", "Esquire");
        assertStructuredName("John Doe", null, "John", null, "Doe", null);
        assertStructuredName("Ms. Jane Eyre", "Ms.", "Jane", null, "Eyre", null);
        assertStructuredName("Dr Leo Spaceman, PhD", "Dr", "Leo", null, "Spaceman", "PhD");
    }

    /**
     * Helper method to check whether a given display name parses out to the other parameters.
     * @param displayName Display name to break into a structured name.
     * @param prefix Expected prefix (null if not expected).
     * @param givenName Expected given name (null if not expected).
     * @param middleName Expected middle name (null if not expected).
     * @param familyName Expected family name (null if not expected).
     * @param suffix Expected suffix (null if not expected).
     */
    private void assertStructuredName(String displayName, String prefix,
            String givenName, String middleName, String familyName, String suffix) {
        Map<String, String> structuredName = NameConverter.displayNameToStructuredName(mContext,
                displayName);
        checkNameComponent(StructuredName.PREFIX, prefix, structuredName);
        checkNameComponent(StructuredName.GIVEN_NAME, givenName, structuredName);
        checkNameComponent(StructuredName.MIDDLE_NAME, middleName, structuredName);
        checkNameComponent(StructuredName.FAMILY_NAME, familyName, structuredName);
        checkNameComponent(StructuredName.SUFFIX, suffix, structuredName);
        assertEquals(0, structuredName.size());
    }

    /**
     * Checks that the given field and value are present in the structured name map (or not present
     * if the given value is null).  If the value is present and matches, the key is removed from
     * the map - once all components of the name are checked, the map should be empty.
     * @param field Field to check.
     * @param value Expected value for the field (null if it is not expected to be populated).
     * @param structuredName The map of structured field names to values.
     */
    private void checkNameComponent(String field, String value,
            Map<String, String> structuredName) {
        if (TextUtils.isEmpty(value)) {
            assertNull(structuredName.get(field));
        } else {
            assertEquals(value, structuredName.get(field));
        }
        structuredName.remove(field);
    }
}