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