Home | History | Annotate | Download | only in calllog
      1 /*
      2  * Copyright (C) 2011 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.view.View;
     24 
     25 import com.android.dialer.PhoneCallDetails;
     26 import com.android.dialer.PhoneCallDetailsHelper;
     27 import com.android.dialer.R;
     28 
     29 /**
     30  * Unit tests for {@link CallLogListItemHelper}.
     31  */
     32 public class CallLogListItemHelperTest extends AndroidTestCase {
     33     /** A test phone number for phone calls. */
     34     private static final String TEST_NUMBER = "14125555555";
     35     /** The formatted version of {@link #TEST_NUMBER}. */
     36     private static final String TEST_FORMATTED_NUMBER = "1-412-255-5555";
     37     /** A test date value for phone calls. */
     38     private static final long TEST_DATE = 1300000000;
     39     /** A test duration value for phone calls. */
     40     private static final long TEST_DURATION = 62300;
     41     /** A test voicemail number. */
     42     private static final String TEST_VOICEMAIL_NUMBER = "123";
     43     /** The country ISO name used in the tests. */
     44     private static final String TEST_COUNTRY_ISO = "US";
     45     /** The geocoded location used in the tests. */
     46     private static final String TEST_GEOCODE = "United States";
     47 
     48     /** The object under test. */
     49     private CallLogListItemHelper mHelper;
     50 
     51     /** The views used in the tests. */
     52     private CallLogListItemViews mViews;
     53     private PhoneNumberDisplayHelper mPhoneNumberHelper;
     54     private PhoneNumberDisplayHelper mPhoneNumberDisplayHelper;
     55 
     56     private Resources mResources;
     57 
     58     @Override
     59     protected void setUp() throws Exception {
     60         super.setUp();
     61         Context context = getContext();
     62         mResources = context.getResources();
     63         CallTypeHelper callTypeHelper = new CallTypeHelper(mResources);
     64         final TestPhoneNumberUtilsWrapper phoneUtils = new TestPhoneNumberUtilsWrapper(
     65                 TEST_VOICEMAIL_NUMBER);
     66         PhoneCallDetailsHelper phoneCallDetailsHelper = new PhoneCallDetailsHelper(
     67                 mResources, callTypeHelper, phoneUtils);
     68         mPhoneNumberDisplayHelper = new PhoneNumberDisplayHelper(mResources);
     69         mHelper = new CallLogListItemHelper(phoneCallDetailsHelper, mPhoneNumberDisplayHelper,
     70                 mResources);
     71         mViews = CallLogListItemViews.createForTest(context);
     72     }
     73 
     74     @Override
     75     protected void tearDown() throws Exception {
     76         mHelper = null;
     77         mViews = null;
     78         super.tearDown();
     79     }
     80 
     81     public void testSetPhoneCallDetails() {
     82         setPhoneCallDetailsWithNumber("12125551234", Calls.PRESENTATION_ALLOWED,
     83                 "1-212-555-1234");
     84         assertEquals(View.VISIBLE, mViews.callBackButtonView.getVisibility());
     85     }
     86 
     87     public void testSetPhoneCallDetails_Unknown() {
     88         setPhoneCallDetailsWithNumber("", Calls.PRESENTATION_UNKNOWN, "");
     89         assertNoCallIntent();
     90     }
     91 
     92     public void testSetPhoneCallDetails_Private() {
     93         setPhoneCallDetailsWithNumber("", Calls.PRESENTATION_RESTRICTED, "");
     94         assertNoCallIntent();
     95     }
     96 
     97     public void testSetPhoneCallDetails_Payphone() {
     98         setPhoneCallDetailsWithNumber("", Calls.PRESENTATION_PAYPHONE, "");
     99         assertNoCallIntent();
    100     }
    101 
    102     public void testSetPhoneCallDetails_VoicemailNumber() {
    103         setPhoneCallDetailsWithNumber(TEST_VOICEMAIL_NUMBER,
    104                 Calls.PRESENTATION_ALLOWED, TEST_VOICEMAIL_NUMBER);
    105         assertEquals(View.VISIBLE, mViews.voicemailButtonView.getVisibility());
    106     }
    107 
    108     public void testSetPhoneCallDetails_ReadVoicemail() {
    109         setPhoneCallDetailsWithTypes(Calls.VOICEMAIL_TYPE);
    110         assertEquals(View.VISIBLE, mViews.voicemailButtonView.getVisibility());
    111     }
    112 
    113     public void testSetPhoneCallDetails_UnreadVoicemail() {
    114         setUnreadPhoneCallDetailsWithTypes(Calls.VOICEMAIL_TYPE);
    115         assertEquals(View.VISIBLE, mViews.voicemailButtonView.getVisibility());
    116     }
    117 
    118     public void testSetPhoneCallDetails_VoicemailFromUnknown() {
    119         setPhoneCallDetailsWithNumberAndType("", Calls.PRESENTATION_UNKNOWN,
    120                 "", Calls.VOICEMAIL_TYPE);
    121         assertEquals(View.VISIBLE, mViews.voicemailButtonView.getVisibility());
    122     }
    123 
    124     /**
    125      * Test getCallDescriptionID method used to get the accessibility description for calls.
    126      * Test case where an answered unknown call is received.
    127      */
    128     public void testGetCallDescriptionID_UnknownAnswered() {
    129         PhoneCallDetails details = new PhoneCallDetails("", Calls.PRESENTATION_UNKNOWN, "",
    130                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    131                 new int[]{Calls.INCOMING_TYPE}, TEST_DATE, TEST_DURATION);
    132         assertEquals(R.string.description_incoming_answered_call,
    133                 mHelper.getCallDescriptionStringID(details));
    134     }
    135 
    136     /**
    137      * Test getCallDescriptionID method used to get the accessibility description for calls.
    138      * Test case where an missed unknown call is received.
    139      */
    140     public void testGetCallDescriptionID_UnknownMissed() {
    141         PhoneCallDetails details = new PhoneCallDetails("", Calls.PRESENTATION_UNKNOWN, "",
    142                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    143                 new int[]{Calls.MISSED_TYPE}, TEST_DATE, TEST_DURATION);
    144         assertEquals(R.string.description_incoming_missed_call,
    145                 mHelper.getCallDescriptionStringID(details));
    146     }
    147 
    148     /**
    149      * Test getCallDescriptionID method used to get the accessibility description for calls.
    150      * Test case where an missed unknown call is received and a voicemail was left.
    151      */
    152     public void testGetCallDescriptionID_UnknownVoicemail() {
    153         PhoneCallDetails details = new PhoneCallDetails("", Calls.PRESENTATION_UNKNOWN, "",
    154                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    155                 new int[]{Calls.VOICEMAIL_TYPE}, TEST_DATE, TEST_DURATION);
    156         assertEquals(R.string.description_incoming_missed_call,
    157                 mHelper.getCallDescriptionStringID(details));
    158     }
    159 
    160     /**
    161      * Test getCallDescriptionID method used to get the accessibility description for calls.
    162      * Test case where an answered call from a known caller is received.
    163      */
    164     public void testGetCallDescriptionID_KnownAnswered() {
    165         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    166                 TEST_FORMATTED_NUMBER,
    167                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    168                 new int[]{Calls.INCOMING_TYPE}, TEST_DATE, TEST_DURATION);
    169         assertEquals(R.string.description_incoming_answered_call,
    170                 mHelper.getCallDescriptionStringID(details));
    171     }
    172 
    173     /**
    174      * Test getCallDescriptionID method used to get the accessibility description for calls.
    175      * Test case where a missed call from a known caller is received.
    176      */
    177     public void testGetCallDescriptionID_KnownMissed() {
    178         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    179                 TEST_FORMATTED_NUMBER,
    180                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    181                 new int[]{Calls.MISSED_TYPE}, TEST_DATE, TEST_DURATION);
    182         assertEquals(R.string.description_incoming_missed_call,
    183                 mHelper.getCallDescriptionStringID(details));
    184     }
    185 
    186     /**
    187      * Test getCallDescriptionID method used to get the accessibility description for calls.
    188      * Test case where a missed call from a known caller is received and a voicemail was left.
    189      */
    190     public void testGetCallDescriptionID_KnownVoicemail() {
    191         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    192                 TEST_FORMATTED_NUMBER,
    193                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    194                 new int[]{Calls.VOICEMAIL_TYPE}, TEST_DATE, TEST_DURATION);
    195         assertEquals(R.string.description_incoming_missed_call,
    196                 mHelper.getCallDescriptionStringID(details));
    197     }
    198 
    199     /**
    200      * Test getCallDescriptionID method used to get the accessibility description for calls.
    201      * Test case where an outgoing call is made to a known number and there is a history of
    202      * only a single call for this caller.
    203      */
    204     public void testGetCallDescriptionID_OutgoingSingle() {
    205         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    206                 TEST_FORMATTED_NUMBER,
    207                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    208                 new int[]{Calls.OUTGOING_TYPE}, TEST_DATE, TEST_DURATION);
    209         assertEquals(R.string.description_outgoing_call,
    210                 mHelper.getCallDescriptionStringID(details));
    211     }
    212 
    213     /**
    214      * Test getCallDescriptionID method used to get the accessibility description for calls.
    215      * Test case where an outgoing call is made to a known number and there is a history of
    216      * many calls for this caller.
    217      */
    218     public void testGetCallDescriptionID_OutgoingMultiple() {
    219         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    220                 TEST_FORMATTED_NUMBER,
    221                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    222                 new int[]{Calls.OUTGOING_TYPE, Calls.OUTGOING_TYPE}, TEST_DATE, TEST_DURATION);
    223         assertEquals(R.string.description_outgoing_call,
    224                 mHelper.getCallDescriptionStringID(details));
    225     }
    226 
    227     /**
    228      * Test getCallDescription method used to get the accessibility description for calls.
    229      * For outgoing calls, we should NOT have "New Voicemail" in the description.
    230      */
    231     public void testGetCallDescription_NoVoicemailOutgoing() {
    232         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    233                 TEST_FORMATTED_NUMBER,
    234                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    235                 new int[]{Calls.OUTGOING_TYPE, Calls.OUTGOING_TYPE}, TEST_DATE, TEST_DURATION);
    236         CharSequence description = mHelper.getCallDescription(getContext(), details);
    237         assertFalse(description.toString()
    238                 .contains(this.mResources.getString(R.string.description_new_voicemail)));
    239     }
    240 
    241     /**
    242      * Test getCallDescription method used to get the accessibility description for calls.
    243      * For regular incoming calls, we should NOT have "New Voicemail" in the description.
    244      */
    245     public void testGetCallDescription_NoVoicemailIncoming() {
    246         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    247                 TEST_FORMATTED_NUMBER,
    248                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    249                 new int[]{Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE}, TEST_DATE, TEST_DURATION);
    250         CharSequence description = mHelper.getCallDescription(getContext(), details);
    251         assertFalse(description.toString()
    252                 .contains(this.mResources.getString(R.string.description_new_voicemail)));
    253     }
    254 
    255     /**
    256      * Test getCallDescription method used to get the accessibility description for calls.
    257      * For regular missed calls, we should NOT have "New Voicemail" in the description.
    258      */
    259     public void testGetCallDescription_NoVoicemailMissed() {
    260         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    261                 TEST_FORMATTED_NUMBER,
    262                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    263                 new int[]{Calls.MISSED_TYPE, Calls.OUTGOING_TYPE}, TEST_DATE, TEST_DURATION);
    264         CharSequence description = mHelper.getCallDescription(getContext(), details);
    265         assertFalse(description.toString()
    266                 .contains(this.mResources.getString(R.string.description_new_voicemail)));
    267     }
    268 
    269     /**
    270      * Test getCallDescription method used to get the accessibility description for calls.
    271      * For voicemail calls, we should have "New Voicemail" in the description.
    272      */
    273     public void testGetCallDescription_Voicemail() {
    274         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    275                 TEST_FORMATTED_NUMBER,
    276                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    277                 new int[]{Calls.VOICEMAIL_TYPE, Calls.OUTGOING_TYPE}, TEST_DATE, TEST_DURATION);
    278         CharSequence description = mHelper.getCallDescription(getContext(), details);
    279         assertTrue(description.toString()
    280                 .contains(this.mResources.getString(R.string.description_new_voicemail)));
    281     }
    282 
    283     /**
    284      * Test getCallDescription method used to get the accessibility description for calls.
    285      * Test that the "X calls" message is not present if there is only a single call.
    286      */
    287     public void testGetCallDescription_NumCallsSingle() {
    288         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    289                 TEST_FORMATTED_NUMBER,
    290                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    291                 new int[]{Calls.VOICEMAIL_TYPE}, TEST_DATE, TEST_DURATION);
    292         CharSequence description = mHelper.getCallDescription(getContext(), details);
    293 
    294         // Rather than hard coding the "X calls" string message, we'll generate it with an empty
    295         // number of calls, and trim the resulting string.  This gets us just the word "calls",
    296         // and ensures any trivial changes to that string resource won't unnecessarily break
    297         // the unit test.
    298         assertFalse(description.toString()
    299                 .contains(this.mResources.getString(R.string.description_num_calls, "").trim()));
    300     }
    301 
    302     /**
    303      * Test getCallDescription method used to get the accessibility description for calls.
    304      * Test that the "X calls" message is present if there are many calls.
    305      */
    306     public void testGetCallDescription_NumCallsMultiple() {
    307         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    308                 TEST_FORMATTED_NUMBER,
    309                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    310                 new int[]{Calls.VOICEMAIL_TYPE, Calls.INCOMING_TYPE}, TEST_DATE, TEST_DURATION);
    311         CharSequence description = mHelper.getCallDescription(getContext(), details);
    312         assertTrue(description.toString()
    313                 .contains(this.mResources.getString(R.string.description_num_calls, 2)));
    314     }
    315 
    316     /**
    317      * Test getCallDescription method used to get the accessibility description for calls.
    318      * Test that the "Video call." message is present if the call had video capability.
    319      */
    320     public void testGetCallDescription_Video() {
    321         PhoneCallDetails details = new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    322                 TEST_FORMATTED_NUMBER,
    323                 TEST_COUNTRY_ISO, TEST_GEOCODE,
    324                 new int[]{Calls.INCOMING_TYPE, Calls.INCOMING_TYPE}, TEST_DATE, TEST_DURATION,
    325                 null, null, Calls.FEATURES_VIDEO, null, null);
    326 
    327         CharSequence description = mHelper.getCallDescription(getContext(), details);
    328         assertTrue(description.toString()
    329                 .contains(this.mResources.getString(R.string.description_video_call, 2)));
    330     }
    331 
    332     /** Asserts that the primary action view does not have a call intent. */
    333     private void assertNoCallIntent() {
    334         Object intentProvider = (IntentProvider)mViews.primaryActionView.getTag();
    335         // The intent provider should be null as there is no ability to make a call.
    336         assertNull(intentProvider);
    337     }
    338 
    339     /** Sets the details of a phone call using the specified phone number. */
    340     private void setPhoneCallDetailsWithNumber(String number,
    341             int presentation, String formattedNumber) {
    342         setPhoneCallDetailsWithNumberAndType(number, presentation,
    343                 formattedNumber, Calls.INCOMING_TYPE);
    344     }
    345 
    346     /** Sets the details of a phone call using the specified phone number. */
    347     private void setPhoneCallDetailsWithNumberAndType(String number,
    348             int presentation, String formattedNumber, int callType) {
    349         mHelper.setPhoneCallDetails(getContext(), mViews,
    350                 new PhoneCallDetails(number, presentation, formattedNumber,
    351                         TEST_COUNTRY_ISO, TEST_GEOCODE,
    352                         new int[]{ callType }, TEST_DATE, TEST_DURATION)
    353         );
    354     }
    355 
    356     /** Sets the details of a phone call using the specified call type. */
    357     private void setPhoneCallDetailsWithTypes(int... types) {
    358         mHelper.setPhoneCallDetails(getContext() ,mViews,
    359                 new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    360                         TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, TEST_GEOCODE,
    361                         types, TEST_DATE, TEST_DURATION)
    362         );
    363     }
    364 
    365     /** Sets the details of an unread phone call using the specified call type. */
    366     private void setUnreadPhoneCallDetailsWithTypes(int... types) {
    367         mHelper.setPhoneCallDetails(getContext(), mViews,
    368                 new PhoneCallDetails(TEST_NUMBER, Calls.PRESENTATION_ALLOWED,
    369                         TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO, TEST_GEOCODE,
    370                         types, TEST_DATE, TEST_DURATION)
    371         );
    372     }
    373 }
    374