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.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.R;
25 import com.android.contacts.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
setUp()43     public void setUp() throws Exception {
44         super.setUp();
45         MockitoAnnotations.initMocks(this);
46     }
47 
testIsCustomPhoneTypeReturnsTrue()48     public void testIsCustomPhoneTypeReturnsTrue() {
49         assertTrue(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_CUSTOM));
50         assertTrue(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_ASSISTANT));
51     }
52 
testIsCustomPhoneTypeReturnsFalse()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 
testGetPhoneLabelResourceIdReturnsOther()60     public void testGetPhoneLabelResourceIdReturnsOther() {
61         assertEquals(R.string.call_other, ContactDisplayUtils.getPhoneLabelResourceId(null));
62     }
63 
testGetPhoneLabelResourceIdReturnsMatchHome()64     public void testGetPhoneLabelResourceIdReturnsMatchHome() {
65         assertEquals(R.string.call_home, ContactDisplayUtils.getPhoneLabelResourceId(
66                 Phone.TYPE_HOME));
67     }
68 
testGetSmsLabelResourceIdReturnsOther()69     public void testGetSmsLabelResourceIdReturnsOther() {
70         assertEquals(R.string.sms_other, ContactDisplayUtils.getSmsLabelResourceId(null));
71     }
72 
testGetSmsLabelResourceIdReturnsMatchHome()73     public void testGetSmsLabelResourceIdReturnsMatchHome() {
74         assertEquals(R.string.sms_home, ContactDisplayUtils.getSmsLabelResourceId(Phone.TYPE_HOME));
75     }
76 
testGetPreferredDisplayName_NullContactsPreferences()77     public void testGetPreferredDisplayName_NullContactsPreferences() {
78         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY,
79                 NAME_ALTERNATIVE, null));
80     }
81 
testGetPreferredDisplayName_NullContactsPreferences_NullAlternative()82     public void testGetPreferredDisplayName_NullContactsPreferences_NullAlternative() {
83         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, null,
84                 null));
85     }
86 
testGetPreferredDisplayName_NullContactsPreferences_NullPrimary()87     public void testGetPreferredDisplayName_NullContactsPreferences_NullPrimary() {
88         assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredDisplayName(null,
89                 NAME_ALTERNATIVE, null));
90     }
91 
testGetPreferredDisplayName_NullContactsPreferences_BothNull()92     public void testGetPreferredDisplayName_NullContactsPreferences_BothNull() {
93         assertNull(ContactDisplayUtils.getPreferredDisplayName(null, null, null));
94     }
95 
testGetPreferredDisplayName_EmptyAlternative()96     public void testGetPreferredDisplayName_EmptyAlternative() {
97         Mockito.when(mContactsPreferences.getDisplayOrder())
98                 .thenReturn(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE);
99         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, "",
100                 mContactsPreferences));
101     }
102 
testGetPreferredDisplayName_InvalidPreference()103     public void testGetPreferredDisplayName_InvalidPreference() {
104         Mockito.when(mContactsPreferences.getDisplayOrder()).thenReturn(-1);
105         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY,
106                 NAME_ALTERNATIVE, mContactsPreferences));
107     }
108 
testGetPreferredDisplayName_Primary()109     public void testGetPreferredDisplayName_Primary() {
110         Mockito.when(mContactsPreferences.getDisplayOrder())
111                 .thenReturn(ContactsPreferences.DISPLAY_ORDER_PRIMARY);
112         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY,
113                 NAME_ALTERNATIVE, mContactsPreferences));
114     }
115 
testGetPreferredDisplayName_Alternative()116     public void testGetPreferredDisplayName_Alternative() {
117         Mockito.when(mContactsPreferences.getDisplayOrder())
118                 .thenReturn(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE);
119         assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY,
120                 NAME_ALTERNATIVE, mContactsPreferences));
121     }
122 
testGetPreferredSortName_NullContactsPreferences()123     public void testGetPreferredSortName_NullContactsPreferences() {
124         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY,
125                 NAME_ALTERNATIVE, null));
126     }
127 
testGetPreferredSortName_NullContactsPreferences_NullAlternative()128     public void testGetPreferredSortName_NullContactsPreferences_NullAlternative() {
129         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, null,
130                 null));
131     }
132 
testGetPreferredSortName_NullContactsPreferences_NullPrimary()133     public void testGetPreferredSortName_NullContactsPreferences_NullPrimary() {
134         assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredSortName(null,
135                 NAME_ALTERNATIVE, null));
136     }
137 
testGetPreferredSortName_NullContactsPreferences_BothNull()138     public void testGetPreferredSortName_NullContactsPreferences_BothNull() {
139         assertNull(ContactDisplayUtils.getPreferredSortName(null, null, null));
140     }
141 
testGetPreferredSortName_EmptyAlternative()142     public void testGetPreferredSortName_EmptyAlternative() {
143         Mockito.when(mContactsPreferences.getSortOrder())
144                 .thenReturn(ContactsPreferences.SORT_ORDER_ALTERNATIVE);
145         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, "",
146                 mContactsPreferences));
147     }
148 
testGetPreferredSortName_InvalidPreference()149     public void testGetPreferredSortName_InvalidPreference() {
150         Mockito.when(mContactsPreferences.getSortOrder()).thenReturn(-1);
151         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY,
152                 NAME_ALTERNATIVE, mContactsPreferences));
153     }
154 
testGetPreferredSortName_Primary()155     public void testGetPreferredSortName_Primary() {
156         Mockito.when(mContactsPreferences.getSortOrder())
157                 .thenReturn(ContactsPreferences.SORT_ORDER_PRIMARY);
158         assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY,
159                 NAME_ALTERNATIVE, mContactsPreferences));
160     }
161 
testGetPreferredSortName_Alternative()162     public void testGetPreferredSortName_Alternative() {
163         Mockito.when(mContactsPreferences.getSortOrder())
164                 .thenReturn(ContactsPreferences.SORT_ORDER_ALTERNATIVE);
165         assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY,
166                 NAME_ALTERNATIVE, mContactsPreferences));
167     }
168 }
169