PhoneCallDetailsHelperTest.java revision fb585079cc4c522c27f6dd6bf03fd296535960f3
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.dialer; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.provider.CallLog.Calls; 22import android.test.AndroidTestCase; 23import android.text.Html; 24import android.text.Spanned; 25import android.view.View; 26import android.widget.TextView; 27 28import com.android.dialer.calllog.CallTypeHelper; 29import com.android.dialer.calllog.PhoneNumberHelper; 30import com.android.dialer.calllog.TestPhoneNumberHelper; 31import com.android.dialer.util.LocaleTestUtils; 32 33import java.util.GregorianCalendar; 34import java.util.Locale; 35 36/** 37 * Unit tests for {@link PhoneCallDetailsHelper}. 38 */ 39public class PhoneCallDetailsHelperTest extends AndroidTestCase { 40 /** The number to be used to access the voicemail. */ 41 private static final String TEST_VOICEMAIL_NUMBER = "125"; 42 /** The date of the call log entry. */ 43 private static final long TEST_DATE = 44 new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis(); 45 /** A test duration value for phone calls. */ 46 private static final long TEST_DURATION = 62300; 47 /** The number of the caller/callee in the log entry. */ 48 private static final String TEST_NUMBER = "14125555555"; 49 /** The formatted version of {@link #TEST_NUMBER}. */ 50 private static final String TEST_FORMATTED_NUMBER = "1-412-255-5555"; 51 /** The country ISO name used in the tests. */ 52 private static final String TEST_COUNTRY_ISO = "US"; 53 /** The geocoded location used in the tests. */ 54 private static final String TEST_GEOCODE = "United States"; 55 56 /** The object under test. */ 57 private PhoneCallDetailsHelper mHelper; 58 /** The views to fill. */ 59 private PhoneCallDetailsViews mViews; 60 private TextView mNameView; 61 private PhoneNumberHelper mPhoneNumberHelper; 62 private LocaleTestUtils mLocaleTestUtils; 63 64 @Override 65 protected void setUp() throws Exception { 66 super.setUp(); 67 Context context = getContext(); 68 Resources resources = context.getResources(); 69 CallTypeHelper callTypeHelper = new CallTypeHelper(resources); 70 mPhoneNumberHelper = new TestPhoneNumberHelper(resources, TEST_VOICEMAIL_NUMBER); 71 mHelper = new PhoneCallDetailsHelper(resources, callTypeHelper, mPhoneNumberHelper); 72 mHelper.setCurrentTimeForTest( 73 new GregorianCalendar(2011, 5, 4, 13, 0, 0).getTimeInMillis()); 74 mViews = PhoneCallDetailsViews.createForTest(context); 75 mNameView = new TextView(context); 76 mLocaleTestUtils = new LocaleTestUtils(getContext()); 77 mLocaleTestUtils.setLocale(Locale.US); 78 } 79 80 @Override 81 protected void tearDown() throws Exception { 82 mLocaleTestUtils.restoreLocale(); 83 mNameView = null; 84 mViews = null; 85 mHelper = null; 86 mPhoneNumberHelper = null; 87 super.tearDown(); 88 } 89 90 public void testSetPhoneCallDetails_Unknown() { 91 setPhoneCallDetailsWithNumber("", Calls.PRESENTATION_UNKNOWN, ""); 92 assertNameEqualsResource(R.string.unknown); 93 } 94 95 public void testSetPhoneCallDetails_Private() { 96 setPhoneCallDetailsWithNumber("", Calls.PRESENTATION_RESTRICTED, ""); 97 assertNameEqualsResource(R.string.private_num); 98 } 99 100 public void testSetPhoneCallDetails_Payphone() { 101 setPhoneCallDetailsWithNumber("", Calls.PRESENTATION_PAYPHONE, ""); 102 assertNameEqualsResource(R.string.payphone); 103 } 104 105 public void testSetPhoneCallDetails_Voicemail() { 106 setPhoneCallDetailsWithNumber(TEST_VOICEMAIL_NUMBER, 107 Calls.PRESENTATION_ALLOWED, TEST_VOICEMAIL_NUMBER); 108 assertNameEqualsResource(R.string.voicemail); 109 } 110 111 public void testSetPhoneCallDetails_Normal() { 112 setPhoneCallDetailsWithNumber("14125551212", 113 Calls.PRESENTATION_ALLOWED, "1-412-555-1212"); 114 assertEquals("Yesterday", mViews.callTypeAndDate.getText().toString()); 115 assertEqualsHtml("<font color='#33b5e5'><b>Yesterday</b></font>", 116 mViews.callTypeAndDate.getText()); 117 } 118 119 /** Asserts that a char sequence is actually a Spanned corresponding to the expected HTML. */ 120 private void assertEqualsHtml(String expectedHtml, CharSequence actualText) { 121 // In order to contain HTML, the text should actually be a Spanned. 122 assertTrue(actualText instanceof Spanned); 123 Spanned actualSpanned = (Spanned) actualText; 124 // Convert from and to HTML to take care of alternative formatting of HTML. 125 assertEquals(Html.toHtml(Html.fromHtml(expectedHtml)), Html.toHtml(actualSpanned)); 126 127 } 128 129 public void testSetPhoneCallDetails_Date() { 130 mHelper.setCurrentTimeForTest( 131 new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis()); 132 133 setPhoneCallDetailsWithDate( 134 new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis()); 135 assertDateEquals("0 mins ago"); 136 137 setPhoneCallDetailsWithDate( 138 new GregorianCalendar(2011, 5, 3, 12, 0, 0).getTimeInMillis()); 139 assertDateEquals("1 hour ago"); 140 141 setPhoneCallDetailsWithDate( 142 new GregorianCalendar(2011, 5, 2, 13, 0, 0).getTimeInMillis()); 143 assertDateEquals("Yesterday"); 144 145 setPhoneCallDetailsWithDate( 146 new GregorianCalendar(2011, 5, 1, 13, 0, 0).getTimeInMillis()); 147 assertDateEquals("2 days ago"); 148 } 149 150 public void testSetPhoneCallDetails_CallTypeIcons() { 151 setPhoneCallDetailsWithCallTypeIcons(Calls.INCOMING_TYPE); 152 assertCallTypeIconsEquals(Calls.INCOMING_TYPE); 153 154 setPhoneCallDetailsWithCallTypeIcons(Calls.OUTGOING_TYPE); 155 assertCallTypeIconsEquals(Calls.OUTGOING_TYPE); 156 157 setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE); 158 assertCallTypeIconsEquals(Calls.MISSED_TYPE); 159 160 setPhoneCallDetailsWithCallTypeIcons(Calls.VOICEMAIL_TYPE); 161 assertCallTypeIconsEquals(Calls.VOICEMAIL_TYPE); 162 } 163 164 public void testSetPhoneCallDetails_MultipleCallTypeIcons() { 165 setPhoneCallDetailsWithCallTypeIcons(Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE); 166 assertCallTypeIconsEquals(Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE); 167 168 setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE, Calls.MISSED_TYPE); 169 assertCallTypeIconsEquals(Calls.MISSED_TYPE, Calls.MISSED_TYPE); 170 } 171 172 public void testSetPhoneCallDetails_MultipleCallTypeIconsLastOneDropped() { 173 setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE, Calls.MISSED_TYPE, 174 Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE); 175 assertCallTypeIconsEqualsPlusOverflow("(4)", 176 Calls.MISSED_TYPE, Calls.MISSED_TYPE, Calls.INCOMING_TYPE); 177 } 178 179 public void testSetPhoneCallDetails_Geocode() { 180 setPhoneCallDetailsWithNumberAndGeocode("+14125555555", "1-412-555-5555", "Pennsylvania"); 181 assertNameEquals("1-412-555-5555"); // The phone number is shown as the name. 182 assertLabelEquals("Pennsylvania"); // The geocode is shown as the label. 183 } 184 185 public void testSetPhoneCallDetails_NoGeocode() { 186 setPhoneCallDetailsWithNumberAndGeocode("+14125555555", "1-412-555-5555", null); 187 assertNameEquals("1-412-555-5555"); // The phone number is shown as the name. 188 assertLabelEquals("-"); // The empty geocode is shown as the label. 189 } 190 191 public void testSetPhoneCallDetails_EmptyGeocode() { 192 setPhoneCallDetailsWithNumberAndGeocode("+14125555555", "1-412-555-5555", ""); 193 assertNameEquals("1-412-555-5555"); // The phone number is shown as the name. 194 assertLabelEquals("-"); // The empty geocode is shown as the label. 195 } 196 197 public void testSetPhoneCallDetails_NoGeocodeForVoicemail() { 198 setPhoneCallDetailsWithNumberAndGeocode(TEST_VOICEMAIL_NUMBER, "", "United States"); 199 assertLabelEquals("-"); // The empty geocode is shown as the label. 200 } 201 202 public void testSetPhoneCallDetails_Highlighted() { 203 setPhoneCallDetailsWithNumber(TEST_VOICEMAIL_NUMBER, 204 Calls.PRESENTATION_ALLOWED, ""); 205 } 206 207 public void testSetCallDetailsHeader_NumberOnly() { 208 setCallDetailsHeaderWithNumber(TEST_NUMBER, Calls.PRESENTATION_ALLOWED); 209 assertEquals(View.VISIBLE, mNameView.getVisibility()); 210 assertEquals("Add to contacts", mNameView.getText().toString()); 211 } 212 213 public void testSetCallDetailsHeader_UnknownNumber() { 214 setCallDetailsHeaderWithNumber("", Calls.PRESENTATION_UNKNOWN); 215 assertEquals(View.VISIBLE, mNameView.getVisibility()); 216 assertEquals("Unknown", mNameView.getText().toString()); 217 } 218 219 public void testSetCallDetailsHeader_PrivateNumber() { 220 setCallDetailsHeaderWithNumber("", Calls.PRESENTATION_RESTRICTED); 221 assertEquals(View.VISIBLE, mNameView.getVisibility()); 222 assertEquals("Private number", mNameView.getText().toString()); 223 } 224 225 public void testSetCallDetailsHeader_PayphoneNumber() { 226 setCallDetailsHeaderWithNumber("", Calls.PRESENTATION_PAYPHONE); 227 assertEquals(View.VISIBLE, mNameView.getVisibility()); 228 assertEquals("Pay phone", mNameView.getText().toString()); 229 } 230 231 public void testSetCallDetailsHeader_VoicemailNumber() { 232 setCallDetailsHeaderWithNumber(TEST_VOICEMAIL_NUMBER, Calls.PRESENTATION_ALLOWED); 233 assertEquals(View.VISIBLE, mNameView.getVisibility()); 234 assertEquals("Voicemail", mNameView.getText().toString()); 235 } 236 237 public void testSetCallDetailsHeader() { 238 setCallDetailsHeader("John Doe"); 239 assertEquals(View.VISIBLE, mNameView.getVisibility()); 240 assertEquals("John Doe", mNameView.getText().toString()); 241 } 242 243 /** Asserts that the name text field contains the value of the given string resource. */ 244 private void assertNameEqualsResource(int resId) { 245 assertNameEquals(getContext().getString(resId)); 246 } 247 248 /** Asserts that the name text field contains the given string value. */ 249 private void assertNameEquals(String text) { 250 assertEquals(text, mViews.nameView.getText().toString()); 251 } 252 253 /** Asserts that the label text field contains the given string value. */ 254 private void assertLabelEquals(String text) { 255 assertEquals(text, mViews.labelView.getText().toString()); 256 } 257 258 /** Asserts that the date text field contains the given string value. */ 259 private void assertDateEquals(String text) { 260 assertEquals(text, mViews.callTypeAndDate.getText().toString()); 261 } 262 263 /** Asserts that the call type contains the images with the given drawables. */ 264 private void assertCallTypeIconsEquals(int... ids) { 265 assertEquals(ids.length, mViews.callTypeIcons.getCount()); 266 for (int index = 0; index < ids.length; ++index) { 267 int id = ids[index]; 268 assertEquals(id, mViews.callTypeIcons.getCallType(index)); 269 } 270 assertEquals(View.VISIBLE, mViews.callTypeIcons.getVisibility()); 271 assertEquals("Yesterday", mViews.callTypeAndDate.getText().toString()); 272 } 273 274 /** 275 * Asserts that the call type contains the images with the given drawables and shows the given 276 * text next to the icons. 277 */ 278 private void assertCallTypeIconsEqualsPlusOverflow(String overflowText, int... ids) { 279 assertEquals(ids.length, mViews.callTypeIcons.getCount()); 280 for (int index = 0; index < ids.length; ++index) { 281 int id = ids[index]; 282 assertEquals(id, mViews.callTypeIcons.getCallType(index)); 283 } 284 assertEquals(View.VISIBLE, mViews.callTypeIcons.getVisibility()); 285 assertEquals(overflowText + " Yesterday", mViews.callTypeAndDate.getText().toString()); 286 } 287 288 /** Sets the phone call details with default values and the given number. */ 289 private void setPhoneCallDetailsWithNumber(String number, int presentation, 290 String formattedNumber) { 291 mHelper.setPhoneCallDetails(mViews, 292 new PhoneCallDetails(number, presentation, formattedNumber, 293 TEST_COUNTRY_ISO, TEST_GEOCODE, 294 new int[]{ Calls.VOICEMAIL_TYPE }, TEST_DATE, TEST_DURATION), 295 true); 296 } 297 298 /** Sets the phone call details with default values and the given number. */ 299 private void setPhoneCallDetailsWithNumberAndGeocode(String number, String formattedNumber, 300 String geocodedLocation) { 301 mHelper.setPhoneCallDetails(mViews, 302 new PhoneCallDetails(number, Calls.PRESENTATION_ALLOWED, 303 formattedNumber, TEST_COUNTRY_ISO, geocodedLocation, 304 new int[]{ Calls.VOICEMAIL_TYPE }, TEST_DATE, TEST_DURATION), 305 true); 306 } 307 308 /** Sets the phone call details with default values and the given date. */ 309 private void setPhoneCallDetailsWithDate(long date) { 310 mHelper.setPhoneCallDetails(mViews, 311 new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED, 312 TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, TEST_GEOCODE, 313 new int[]{ Calls.INCOMING_TYPE }, date, TEST_DURATION), 314 false); 315 } 316 317 /** Sets the phone call details with default values and the given call types using icons. */ 318 private void setPhoneCallDetailsWithCallTypeIcons(int... callTypes) { 319 mHelper.setPhoneCallDetails(mViews, 320 new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED, 321 TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, TEST_GEOCODE, 322 callTypes, TEST_DATE, TEST_DURATION), 323 false); 324 } 325 326 private void setCallDetailsHeaderWithNumber(String number, int presentation) { 327 mHelper.setCallDetailsHeader(mNameView, 328 new PhoneCallDetails(number, presentation, 329 TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, TEST_GEOCODE, 330 new int[]{ Calls.INCOMING_TYPE }, TEST_DATE, TEST_DURATION)); 331 } 332 333 private void setCallDetailsHeader(String name) { 334 mHelper.setCallDetailsHeader(mNameView, 335 new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED, 336 TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, TEST_GEOCODE, 337 new int[]{ Calls.INCOMING_TYPE }, TEST_DATE, TEST_DURATION, 338 name, 0, "", null, null)); 339 } 340} 341