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