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 17 package com.android.contacts.common.util; 18 19 import static android.provider.ContactsContract.CommonDataKinds.Phone; 20 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 import com.android.contacts.common.R; 25 import com.android.contacts.common.preference.ContactsPreferences; 26 27 import org.mockito.Mock; 28 import org.mockito.Mockito; 29 import org.mockito.MockitoAnnotations; 30 31 /** 32 * Unit tests for (@link ContactDisplayUtils} 33 */ 34 @SmallTest 35 public class ContactDisplayUtilTests extends AndroidTestCase { 36 37 private static final String NAME_PRIMARY = "Name Primary"; 38 private static final String NAME_ALTERNATIVE = "Name Alternative"; 39 40 @Mock private ContactsPreferences mContactsPreferences; 41 42 @Override 43 public void setUp() throws Exception { 44 super.setUp(); 45 MockitoAnnotations.initMocks(this); 46 } 47 48 public void testIsCustomPhoneTypeReturnsTrue() { 49 assertTrue(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_CUSTOM)); 50 assertTrue(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_ASSISTANT)); 51 } 52 53 public void testIsCustomPhoneTypeReturnsFalse() { 54 assertFalse(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_HOME)); 55 assertFalse(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_FAX_WORK)); 56 assertFalse(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_MOBILE)); 57 assertFalse(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_OTHER)); 58 } 59 60 public void testGetLabelForCallOrSmsReturnsCustomLabel() { 61 final CharSequence smsResult = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_CUSTOM, 62 "expected sms label", ContactDisplayUtils.INTERACTION_SMS, getContext()); 63 assertEquals("expected sms label", smsResult); 64 65 final CharSequence callResult = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_CUSTOM, 66 "expected call label", ContactDisplayUtils.INTERACTION_CALL, getContext()); 67 assertEquals("expected call label", callResult); 68 } 69 70 public void testGetLabelForCallOrSmsReturnsCallLabels() { 71 CharSequence result = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_HOME, "", 72 ContactDisplayUtils.INTERACTION_CALL, getContext()); 73 CharSequence expected = getContext().getResources().getText(R.string.call_home); 74 assertEquals(expected, result); 75 76 result = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_MOBILE, "", 77 ContactDisplayUtils.INTERACTION_CALL, getContext()); 78 expected = getContext().getResources().getText(R.string.call_mobile); 79 assertEquals(expected, result); 80 } 81 82 public void testGetLabelForCallOrSmsReturnsSmsLabels() { 83 CharSequence result = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_HOME, "", 84 ContactDisplayUtils.INTERACTION_SMS, getContext()); 85 CharSequence expected = getContext().getResources().getText(R.string.sms_home); 86 assertEquals(expected, result); 87 88 result = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_MOBILE, "", 89 ContactDisplayUtils.INTERACTION_SMS, getContext()); 90 expected = getContext().getResources().getText(R.string.sms_mobile); 91 assertEquals(expected, result); 92 } 93 94 public void testGetPhoneLabelResourceIdReturnsOther() { 95 assertEquals(R.string.call_other, ContactDisplayUtils.getPhoneLabelResourceId(null)); 96 } 97 98 public void testGetPhoneLabelResourceIdReturnsMatchHome() { 99 assertEquals(R.string.call_home, ContactDisplayUtils.getPhoneLabelResourceId( 100 Phone.TYPE_HOME)); 101 } 102 103 public void testGetSmsLabelResourceIdReturnsOther() { 104 assertEquals(R.string.sms_other, ContactDisplayUtils.getSmsLabelResourceId(null)); 105 } 106 107 public void testGetSmsLabelResourceIdReturnsMatchHome() { 108 assertEquals(R.string.sms_home, ContactDisplayUtils.getSmsLabelResourceId(Phone.TYPE_HOME)); 109 } 110 111 public void testGetPreferredDisplayName_NullContactsPreferences() { 112 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, 113 NAME_ALTERNATIVE, null)); 114 } 115 116 public void testGetPreferredDisplayName_NullContactsPreferences_NullAlternative() { 117 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, null, 118 null)); 119 } 120 121 public void testGetPreferredDisplayName_NullContactsPreferences_NullPrimary() { 122 assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredDisplayName(null, 123 NAME_ALTERNATIVE, null)); 124 } 125 126 public void testGetPreferredDisplayName_NullContactsPreferences_BothNull() { 127 assertNull(ContactDisplayUtils.getPreferredDisplayName(null, null, null)); 128 } 129 130 public void testGetPreferredDisplayName_EmptyAlternative() { 131 Mockito.when(mContactsPreferences.getDisplayOrder()) 132 .thenReturn(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE); 133 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, "", 134 mContactsPreferences)); 135 } 136 137 public void testGetPreferredDisplayName_InvalidPreference() { 138 Mockito.when(mContactsPreferences.getDisplayOrder()).thenReturn(-1); 139 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, 140 NAME_ALTERNATIVE, mContactsPreferences)); 141 } 142 143 public void testGetPreferredDisplayName_Primary() { 144 Mockito.when(mContactsPreferences.getDisplayOrder()) 145 .thenReturn(ContactsPreferences.DISPLAY_ORDER_PRIMARY); 146 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, 147 NAME_ALTERNATIVE, mContactsPreferences)); 148 } 149 150 public void testGetPreferredDisplayName_Alternative() { 151 Mockito.when(mContactsPreferences.getDisplayOrder()) 152 .thenReturn(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE); 153 assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, 154 NAME_ALTERNATIVE, mContactsPreferences)); 155 } 156 157 public void testGetPreferredSortName_NullContactsPreferences() { 158 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, 159 NAME_ALTERNATIVE, null)); 160 } 161 162 public void testGetPreferredSortName_NullContactsPreferences_NullAlternative() { 163 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, null, 164 null)); 165 } 166 167 public void testGetPreferredSortName_NullContactsPreferences_NullPrimary() { 168 assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredSortName(null, 169 NAME_ALTERNATIVE, null)); 170 } 171 172 public void testGetPreferredSortName_NullContactsPreferences_BothNull() { 173 assertNull(ContactDisplayUtils.getPreferredSortName(null, null, null)); 174 } 175 176 public void testGetPreferredSortName_EmptyAlternative() { 177 Mockito.when(mContactsPreferences.getSortOrder()) 178 .thenReturn(ContactsPreferences.SORT_ORDER_ALTERNATIVE); 179 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, "", 180 mContactsPreferences)); 181 } 182 183 public void testGetPreferredSortName_InvalidPreference() { 184 Mockito.when(mContactsPreferences.getSortOrder()).thenReturn(-1); 185 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, 186 NAME_ALTERNATIVE, mContactsPreferences)); 187 } 188 189 public void testGetPreferredSortName_Primary() { 190 Mockito.when(mContactsPreferences.getSortOrder()) 191 .thenReturn(ContactsPreferences.SORT_ORDER_PRIMARY); 192 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, 193 NAME_ALTERNATIVE, mContactsPreferences)); 194 } 195 196 public void testGetPreferredSortName_Alternative() { 197 Mockito.when(mContactsPreferences.getSortOrder()) 198 .thenReturn(ContactsPreferences.SORT_ORDER_ALTERNATIVE); 199 assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, 200 NAME_ALTERNATIVE, mContactsPreferences)); 201 } 202 } 203