PhoneCallDetailsHelperTest.java revision 6ecb732e22b271878d5f9215b7c821a1d97d888f
1/* 2 * Copyright (C) 2010 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; 18 19import com.android.contacts.calllog.CallTypeHelper; 20import com.android.contacts.calllog.PhoneNumberHelper; 21import com.android.contacts.util.LocaleTestUtils; 22import com.android.internal.telephony.CallerInfo; 23 24import android.content.Context; 25import android.content.res.Resources; 26import android.provider.CallLog.Calls; 27import android.test.AndroidTestCase; 28import android.view.View; 29 30import java.util.GregorianCalendar; 31import java.util.Locale; 32 33/** 34 * Unit tests for {@link PhoneCallDetailsHelper}. 35 */ 36public class PhoneCallDetailsHelperTest extends AndroidTestCase { 37 /** The number to be used to access the voicemail. */ 38 private static final String TEST_VOICEMAIL_NUMBER = "125"; 39 /** The date of the call log entry. */ 40 private static final long TEST_DATE = 1300000000; 41 /** A test duration value for phone calls. */ 42 private static final long TEST_DURATION = 62300; 43 /** The number of the caller/callee in the log entry. */ 44 private static final String TEST_NUMBER = "14125555555"; 45 /** The formatted version of {@link #TEST_NUMBER}. */ 46 private static final String TEST_FORMATTED_NUMBER = "1-412-255-5555"; 47 /** The country ISO name used in the tests. */ 48 private static final String TEST_COUNTRY_ISO = "US"; 49 50 /** The object under test. */ 51 private PhoneCallDetailsHelper mHelper; 52 /** The views to fill. */ 53 private PhoneCallDetailsViews mViews; 54 private PhoneNumberHelper mPhoneNumberHelper; 55 56 @Override 57 protected void setUp() throws Exception { 58 super.setUp(); 59 Context context = getContext(); 60 Resources resources = context.getResources(); 61 CallTypeHelper callTypeHelper = new CallTypeHelper(resources); 62 mPhoneNumberHelper = new PhoneNumberHelper(resources, TEST_VOICEMAIL_NUMBER); 63 mHelper = new PhoneCallDetailsHelper(resources, callTypeHelper, mPhoneNumberHelper); 64 mViews = PhoneCallDetailsViews.createForTest(context); 65 } 66 67 @Override 68 protected void tearDown() throws Exception { 69 mViews = null; 70 mHelper = null; 71 super.tearDown(); 72 } 73 74 public void testSetPhoneCallDetails_Unknown() { 75 setPhoneCallDetailsWithNumber(CallerInfo.UNKNOWN_NUMBER, CallerInfo.UNKNOWN_NUMBER); 76 assertNameEqualsResource(R.string.unknown); 77 } 78 79 public void testSetPhoneCallDetails_Private() { 80 setPhoneCallDetailsWithNumber(CallerInfo.PRIVATE_NUMBER, CallerInfo.PRIVATE_NUMBER); 81 assertNameEqualsResource(R.string.private_num); 82 } 83 84 public void testSetPhoneCallDetails_Payphone() { 85 setPhoneCallDetailsWithNumber(CallerInfo.PAYPHONE_NUMBER, CallerInfo.PAYPHONE_NUMBER); 86 assertNameEqualsResource(R.string.payphone); 87 } 88 89 public void testSetPhoneCallDetails_Voicemail() { 90 setPhoneCallDetailsWithNumber(TEST_VOICEMAIL_NUMBER, TEST_VOICEMAIL_NUMBER); 91 assertNameEqualsResource(R.string.voicemail); 92 } 93 94 public void testSetPhoneCallDetails_Normal() { 95 setPhoneCallDetailsWithNumber("14125551212", "1-412-555-1212"); 96 assertNameEquals("1-412-555-1212"); 97 } 98 99 public void testSetPhoneCallDetails_Date() { 100 LocaleTestUtils localeTestUtils = new LocaleTestUtils(getContext()); 101 localeTestUtils.setLocale(Locale.US); 102 try { 103 mHelper.setCurrentTimeForTest( 104 new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis()); 105 106 setPhoneCallDetailsWithDate( 107 new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis()); 108 assertDateEquals("0 mins ago"); 109 110 setPhoneCallDetailsWithDate( 111 new GregorianCalendar(2011, 5, 3, 12, 0, 0).getTimeInMillis()); 112 assertDateEquals("1 hour ago"); 113 114 setPhoneCallDetailsWithDate( 115 new GregorianCalendar(2011, 5, 2, 13, 0, 0).getTimeInMillis()); 116 assertDateEquals("yesterday"); 117 118 setPhoneCallDetailsWithDate( 119 new GregorianCalendar(2011, 5, 1, 13, 0, 0).getTimeInMillis()); 120 assertDateEquals("2 days ago"); 121 } finally { 122 localeTestUtils.restoreLocale(); 123 } 124 } 125 126 public void testSetPhoneCallDetails_CallTypeIcons() { 127 setPhoneCallDetailsWithCallTypeIcons(Calls.INCOMING_TYPE); 128 assertCallTypeIconsEquals(Calls.INCOMING_TYPE); 129 130 setPhoneCallDetailsWithCallTypeIcons(Calls.OUTGOING_TYPE); 131 assertCallTypeIconsEquals(Calls.OUTGOING_TYPE); 132 133 setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE); 134 assertCallTypeIconsEquals(Calls.MISSED_TYPE); 135 136 setPhoneCallDetailsWithCallTypeIcons(Calls.VOICEMAIL_TYPE); 137 assertCallTypeIconsEquals(Calls.VOICEMAIL_TYPE); 138 } 139 140 public void testSetPhoneCallDetails_MultipleCallTypeIcons() { 141 setPhoneCallDetailsWithCallTypeIcons(Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE); 142 assertCallTypeIconsEquals(Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE); 143 144 setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE, Calls.MISSED_TYPE); 145 assertCallTypeIconsEquals(Calls.MISSED_TYPE, Calls.MISSED_TYPE); 146 } 147 148 public void testSetPhoneCallDetails_MultipleCallTypeIconsLastOneDropped() { 149 setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE, Calls.MISSED_TYPE, 150 Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE); 151 assertCallTypeIconsEquals(Calls.MISSED_TYPE, Calls.MISSED_TYPE, Calls.INCOMING_TYPE); 152 } 153 154 public void testSetPhoneCallDetails_CallTypeText() { 155 LocaleTestUtils localeTestUtils = new LocaleTestUtils(getContext()); 156 localeTestUtils.setLocale(Locale.US); 157 try { 158 setPhoneCallDetailsWithCallTypeText(Calls.INCOMING_TYPE); 159 assertCallTypeTextEquals("Incoming call"); 160 161 setPhoneCallDetailsWithCallTypeText(Calls.OUTGOING_TYPE); 162 assertCallTypeTextEquals("Outgoing call"); 163 164 setPhoneCallDetailsWithCallTypeText(Calls.MISSED_TYPE); 165 assertCallTypeTextEquals("Missed call"); 166 167 setPhoneCallDetailsWithCallTypeText(Calls.VOICEMAIL_TYPE); 168 assertCallTypeTextEquals("Voicemail"); 169 } finally { 170 localeTestUtils.restoreLocale(); 171 } 172 } 173 174 public void testSetPhoneCallDetails_Geocode() { 175 LocaleTestUtils localeTestUtils = new LocaleTestUtils(getContext()); 176 localeTestUtils.setLocale(Locale.US); 177 try { 178 setPhoneCallDetailsWithNumber("+14125555555", "1-412-555-5555"); 179 assertNameEquals("1-412-555-5555"); // The phone number is shown as the name. 180 assertNumberEquals("Pennsylvania"); // The geocode is shown as the number. 181 } finally { 182 localeTestUtils.restoreLocale(); 183 } 184 } 185 186 public void testSetPhoneCallDetails_NoGeocode() { 187 LocaleTestUtils localeTestUtils = new LocaleTestUtils(getContext()); 188 localeTestUtils.setLocale(Locale.US); 189 try { 190 setPhoneCallDetailsWithNumber("+0", "+0"); 191 assertNameEquals("+0"); // The phone number is shown as the name. 192 assertNumberEquals("-"); // The empty geocode is shown as the number. 193 } finally { 194 localeTestUtils.restoreLocale(); 195 } 196 } 197 198 public void testSetPhoneCallDetails_NameOnly() { 199 setPhoneCallDetailsNameOnly(); 200 assertEquals(View.VISIBLE, mViews.nameView.getVisibility()); 201 assertEquals(View.GONE, mViews.numberView.getVisibility()); 202 assertEquals(View.GONE, mViews.callTypeView.getVisibility()); 203 } 204 205 /** Asserts that the name text field contains the value of the given string resource. */ 206 private void assertNameEqualsResource(int resId) { 207 assertNameEquals(getContext().getString(resId)); 208 } 209 210 /** Asserts that the name text field contains the given string value. */ 211 private void assertNameEquals(String text) { 212 assertEquals(text, mViews.nameView.getText().toString()); 213 } 214 215 /** Asserts that the number text field contains the given string value. */ 216 private void assertNumberEquals(String text) { 217 assertEquals(text, mViews.numberView.getText().toString()); 218 } 219 220 /** Asserts that the date text field contains the given string value. */ 221 private void assertDateEquals(String text) { 222 assertEquals(text, mViews.dateView.getText().toString()); 223 } 224 225 /** Asserts that the call type contains the images with the given drawables. */ 226 private void assertCallTypeIconsEquals(int... ids) { 227 assertEquals(ids.length, mViews.callTypeIcons.getCount()); 228 for (int index = 0; index < ids.length; ++index) { 229 int id = ids[index]; 230 assertEquals(id, mViews.callTypeIcons.getCallType(index)); 231 } 232 assertEquals(View.VISIBLE, mViews.callTypeIcons.getVisibility()); 233 assertEquals(View.GONE, mViews.callTypeText.getVisibility()); 234 assertEquals(View.GONE, mViews.callTypeSeparator.getVisibility()); 235 } 236 237 /** Asserts that the call type contains the given text. */ 238 private void assertCallTypeTextEquals(String text) { 239 assertEquals(text, mViews.callTypeText.getText().toString()); 240 assertEquals(View.GONE, mViews.callTypeIcons.getVisibility()); 241 assertEquals(View.VISIBLE, mViews.callTypeText.getVisibility()); 242 assertEquals(View.VISIBLE, mViews.callTypeSeparator.getVisibility()); 243 } 244 245 /** Sets the phone call details with default values and the given number. */ 246 private void setPhoneCallDetailsWithNumber(String number, String formattedNumber) { 247 mHelper.setPhoneCallDetails(mViews, 248 new PhoneCallDetails(number, formattedNumber, TEST_COUNTRY_ISO, 249 new int[]{ Calls.INCOMING_TYPE }, TEST_DATE, TEST_DURATION), 250 false, false, false); 251 } 252 253 /** Sets the phone call details with default values and the given date. */ 254 private void setPhoneCallDetailsWithDate(long date) { 255 mHelper.setPhoneCallDetails(mViews, 256 new PhoneCallDetails(TEST_NUMBER, TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, 257 new int[]{ Calls.INCOMING_TYPE }, date, TEST_DURATION), 258 false, false, false); 259 } 260 261 /** Sets the phone call details with default values and the given call types using icons. */ 262 private void setPhoneCallDetailsWithCallTypeIcons(int... callTypes) { 263 setPhoneCallDetailsWithCallTypes(true, callTypes); 264 } 265 266 /** Sets the phone call details with default values and the given call types using text. */ 267 private void setPhoneCallDetailsWithCallTypeText(int... callTypes) { 268 setPhoneCallDetailsWithCallTypes(false, callTypes); 269 } 270 271 private void setPhoneCallDetailsWithCallTypes(boolean useIcons, int... callTypes) { 272 mHelper.setPhoneCallDetails(mViews, 273 new PhoneCallDetails(TEST_NUMBER, TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, 274 callTypes, TEST_DATE, TEST_DURATION), 275 useIcons, false, false); 276 } 277 278 private void setPhoneCallDetailsNameOnly() { 279 mHelper.setPhoneCallDetails(mViews, 280 new PhoneCallDetails(TEST_NUMBER, TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, 281 new int[]{ Calls.INCOMING_TYPE }, TEST_DATE, TEST_DURATION), 282 true, false, true); 283 } 284} 285