PhoneCallDetailsHelperTest.java revision 9de38681c1d037100a978a5820c09cb74c0d6fee
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.util.LocaleTestUtils; 20import com.android.internal.telephony.CallerInfo; 21 22import android.content.Context; 23import android.graphics.Color; 24import android.graphics.drawable.ColorDrawable; 25import android.graphics.drawable.Drawable; 26import android.provider.CallLog.Calls; 27import android.test.AndroidTestCase; 28import android.widget.ImageView; 29import android.widget.LinearLayout; 30import android.widget.TextView; 31 32import java.util.GregorianCalendar; 33import java.util.Locale; 34 35/** 36 * Unit tests for {@link PhoneCallDetailsHelper}. 37 */ 38public class PhoneCallDetailsHelperTest extends AndroidTestCase { 39 /** The number to be used to access the voicemail. */ 40 private static final String TEST_VOICEMAIL_NUMBER = "125"; 41 /** The date of the call log entry. */ 42 private static final long TEST_DATE = 1300000000; 43 /** The number of the caller/callee in the log entry. */ 44 private static final String TEST_NUMBER = "1-412-555-5555"; 45 /** A drawable to be used for incoming calls. */ 46 private static final Drawable TEST_INCOMING_DRAWABLE = new ColorDrawable(Color.BLACK); 47 /** A drawable to be used for outgoing calls. */ 48 private static final Drawable TEST_OUTGOING_DRAWABLE = new ColorDrawable(Color.BLUE); 49 /** A drawable to be used for missed calls. */ 50 private static final Drawable TEST_MISSED_DRAWABLE = new ColorDrawable(Color.RED); 51 /** A drawable to be used for voicemails. */ 52 private static final Drawable TEST_VOICEMAIL_DRAWABLE = new ColorDrawable(Color.CYAN); 53 54 /** The object under test. */ 55 private PhoneCallDetailsHelper mHelper; 56 /** The views to fill. */ 57 private PhoneCallDetailsViews mViews; 58 59 @Override 60 protected void setUp() throws Exception { 61 super.setUp(); 62 Context context = getContext(); 63 mHelper = new PhoneCallDetailsHelper(context, context.getResources(), 64 TEST_VOICEMAIL_NUMBER, TEST_INCOMING_DRAWABLE, TEST_OUTGOING_DRAWABLE, 65 TEST_MISSED_DRAWABLE, TEST_VOICEMAIL_DRAWABLE); 66 mViews = PhoneCallDetailsViews.createForTest(new TextView(context), 67 new LinearLayout(context), new TextView(context), new TextView(context)); 68 } 69 70 @Override 71 protected void tearDown() throws Exception { 72 mViews = null; 73 mHelper = null; 74 super.tearDown(); 75 } 76 77 public void testSetPhoneCallDetails_Unknown() { 78 setPhoneCallDetailsWithNumber(CallerInfo.UNKNOWN_NUMBER); 79 assertNameEqualsResource(R.string.unknown); 80 } 81 82 public void testSetPhoneCallDetails_Private() { 83 setPhoneCallDetailsWithNumber(CallerInfo.PRIVATE_NUMBER); 84 assertNameEqualsResource(R.string.private_num); 85 } 86 87 public void testSetPhoneCallDetails_Payphone() { 88 setPhoneCallDetailsWithNumber(CallerInfo.PAYPHONE_NUMBER); 89 assertNameEqualsResource(R.string.payphone); 90 } 91 92 public void testSetPhoneCallDetails_Voicemail() { 93 setPhoneCallDetailsWithNumber(TEST_VOICEMAIL_NUMBER); 94 assertNameEqualsResource(R.string.voicemail); 95 } 96 97 public void testSetPhoneCallDetails_Normal() { 98 setPhoneCallDetailsWithNumber("1-412-555-1212"); 99 assertNameEquals("1-412-555-1212"); 100 } 101 102 public void testSetPhoneCallDetails_Date() { 103 LocaleTestUtils localeTestUtils = new LocaleTestUtils(getContext()); 104 localeTestUtils.setLocale(Locale.US); 105 try { 106 mHelper.setCurrentTimeForTest( 107 new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis()); 108 109 setPhoneCallDetailsWithDate( 110 new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis()); 111 assertDateEquals("0 mins ago"); 112 113 setPhoneCallDetailsWithDate( 114 new GregorianCalendar(2011, 5, 3, 12, 0, 0).getTimeInMillis()); 115 assertDateEquals("1 hour ago"); 116 117 setPhoneCallDetailsWithDate( 118 new GregorianCalendar(2011, 5, 2, 13, 0, 0).getTimeInMillis()); 119 assertDateEquals("yesterday"); 120 121 setPhoneCallDetailsWithDate( 122 new GregorianCalendar(2011, 5, 1, 13, 0, 0).getTimeInMillis()); 123 assertDateEquals("2 days ago"); 124 } finally { 125 localeTestUtils.restoreLocale(); 126 } 127 } 128 129 public void testSetPhoneCallDetails_CallType() { 130 setPhoneCallDetailsWithCallType(Calls.INCOMING_TYPE); 131 assertCallTypeIconsEquals(TEST_INCOMING_DRAWABLE); 132 133 setPhoneCallDetailsWithCallType(Calls.OUTGOING_TYPE); 134 assertCallTypeIconsEquals(TEST_OUTGOING_DRAWABLE); 135 136 setPhoneCallDetailsWithCallType(Calls.MISSED_TYPE); 137 assertCallTypeIconsEquals(TEST_MISSED_DRAWABLE); 138 139 setPhoneCallDetailsWithCallType(Calls.VOICEMAIL_TYPE); 140 assertCallTypeIconsEquals(TEST_VOICEMAIL_DRAWABLE); 141 } 142 143 /** Asserts that the name text field contains the value of the given string resource. */ 144 private void assertNameEqualsResource(int resId) { 145 assertNameEquals(getContext().getString(resId)); 146 } 147 148 /** Asserts that the name text field contains the given string value. */ 149 private void assertNameEquals(String text) { 150 assertEquals(text, mViews.nameView.getText().toString()); 151 } 152 153 /** Asserts that the date text field contains the given string value. */ 154 private void assertDateEquals(String text) { 155 assertEquals(text, mViews.dateView.getText().toString()); 156 } 157 158 /** Asserts that the call type linear layout contains the images with the given drawables. */ 159 private void assertCallTypeIconsEquals(Drawable... drawables) { 160 assertEquals(drawables.length, mViews.callTypesLayout.getChildCount()); 161 for (int index = 0; index < drawables.length; ++index) { 162 Drawable drawable = drawables[index]; 163 ImageView imageView = (ImageView) mViews.callTypesLayout.getChildAt(index); 164 assertEquals(drawable, imageView.getDrawable()); 165 } 166 } 167 168 /** Sets the phone call details with default values and the given number. */ 169 private void setPhoneCallDetailsWithNumber(String number) { 170 mHelper.setPhoneCallDetails(mViews, 171 new PhoneCallDetails(number, Calls.INCOMING_TYPE, TEST_DATE)); 172 } 173 174 /** Sets the phone call details with default values and the given date. */ 175 private void setPhoneCallDetailsWithDate(long date) { 176 mHelper.setPhoneCallDetails(mViews, 177 new PhoneCallDetails(TEST_NUMBER, Calls.INCOMING_TYPE, date)); 178 } 179 180 /** Sets the phone call details with default values and the given call type. */ 181 private void setPhoneCallDetailsWithCallType(int callType) { 182 mHelper.setPhoneCallDetails(mViews, new PhoneCallDetails(TEST_NUMBER, callType, TEST_DATE)); 183 } 184} 185