blob: b4cd1ca889d0e9f4aa04367e03763f5794ccf0e0 [file] [log] [blame]
Chiao Cheng0b24d792012-10-29 18:13:52 -07001/*
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
17package com.android.contacts.common.util;
18
19import static android.provider.ContactsContract.CommonDataKinds.Phone;
20
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23
24import com.android.contacts.common.R;
Brandon Maxwellc1e02722015-10-30 13:43:39 -070025import com.android.contacts.common.preference.ContactsPreferences;
Chiao Cheng0b24d792012-10-29 18:13:52 -070026
Brandon Maxwell7edf9832016-02-04 20:07:20 -080027import org.mockito.Mock;
28import org.mockito.Mockito;
29import org.mockito.MockitoAnnotations;
30
Chiao Cheng0b24d792012-10-29 18:13:52 -070031/**
32 * Unit tests for (@link ContactDisplayUtils}
33 */
34@SmallTest
35public class ContactDisplayUtilTests extends AndroidTestCase {
36
Brandon Maxwellc1e02722015-10-30 13:43:39 -070037 private static final String NAME_PRIMARY = "Name Primary";
38 private static final String NAME_ALTERNATIVE = "Name Alternative";
39
Brandon Maxwell7edf9832016-02-04 20:07:20 -080040 @Mock private ContactsPreferences mContactsPreferences;
41
42 @Override
43 public void setUp() throws Exception {
44 super.setUp();
45 MockitoAnnotations.initMocks(this);
46 }
47
Chiao Cheng0b24d792012-10-29 18:13:52 -070048 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
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800111 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);
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700133 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY, "",
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800134 mContactsPreferences));
Brandon Maxwellc1e02722015-10-30 13:43:39 -0700135 }
136
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800137 public void testGetPreferredDisplayName_InvalidPreference() {
138 Mockito.when(mContactsPreferences.getDisplayOrder()).thenReturn(-1);
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700139 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY,
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800140 NAME_ALTERNATIVE, mContactsPreferences));
Brandon Maxwellc1e02722015-10-30 13:43:39 -0700141 }
142
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800143 public void testGetPreferredDisplayName_Primary() {
144 Mockito.when(mContactsPreferences.getDisplayOrder())
145 .thenReturn(ContactsPreferences.DISPLAY_ORDER_PRIMARY);
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700146 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY,
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800147 NAME_ALTERNATIVE, mContactsPreferences));
Brandon Maxwellc1e02722015-10-30 13:43:39 -0700148 }
149
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800150 public void testGetPreferredDisplayName_Alternative() {
151 Mockito.when(mContactsPreferences.getDisplayOrder())
152 .thenReturn(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE);
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700153 assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredDisplayName(NAME_PRIMARY,
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800154 NAME_ALTERNATIVE, mContactsPreferences));
Brandon Maxwellc1e02722015-10-30 13:43:39 -0700155 }
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700156
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800157 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);
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700179 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY, "",
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800180 mContactsPreferences));
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700181 }
182
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800183 public void testGetPreferredSortName_InvalidPreference() {
184 Mockito.when(mContactsPreferences.getSortOrder()).thenReturn(-1);
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700185 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY,
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800186 NAME_ALTERNATIVE, mContactsPreferences));
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700187 }
188
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800189 public void testGetPreferredSortName_Primary() {
190 Mockito.when(mContactsPreferences.getSortOrder())
191 .thenReturn(ContactsPreferences.SORT_ORDER_PRIMARY);
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700192 assertEquals(NAME_PRIMARY, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY,
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800193 NAME_ALTERNATIVE, mContactsPreferences));
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700194 }
195
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800196 public void testGetPreferredSortName_Alternative() {
197 Mockito.when(mContactsPreferences.getSortOrder())
198 .thenReturn(ContactsPreferences.SORT_ORDER_ALTERNATIVE);
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700199 assertEquals(NAME_ALTERNATIVE, ContactDisplayUtils.getPreferredSortName(NAME_PRIMARY,
Brandon Maxwell7edf9832016-02-04 20:07:20 -0800200 NAME_ALTERNATIVE, mContactsPreferences));
Brandon Maxwell3e2799f2015-10-30 16:18:43 -0700201 }
Chiao Cheng0b24d792012-10-29 18:13:52 -0700202}