1 /* 2 * Copyright (C) 2016 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 static org.mockito.Matchers.anyString; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.when; 22 23 import android.provider.CallLog; 24 import android.test.AndroidTestCase; 25 26 import com.android.dialer.R; 27 28 /** 29 * Unit tests for {@link CallLogNotificationsHelper}. 30 */ 31 public class CallLogNotificationsHelperTest extends AndroidTestCase { 32 private static final String TEST_COUNTRY_ISO = "US"; 33 private static final String TEST_VALID_NUMBER = "14125555555"; 34 private static final String TEST_INVALID_NUMBER = "asdna128937123"; 35 private static final String TEST_FORMATTED_NUMBER = "1 412-555-5555"; 36 private static final String TEST_E164_NUMBER = "+14125555555"; 37 38 private final ContactInfoHelper mContactInfoHelper = mock(ContactInfoHelper.class); 39 40 private CallLogNotificationsHelper mCallLogNotificationsHelper; 41 42 @Override 43 public void setUp() throws Exception { 44 super.setUp(); 45 mCallLogNotificationsHelper = new CallLogNotificationsHelper(getContext(), 46 null, null, mContactInfoHelper, TEST_COUNTRY_ISO); 47 } 48 49 public void testGetContactInfo_ValidNumberValidPresentationValidIso() { 50 ContactInfo contactInfo = getContactInfo( 51 TEST_VALID_NUMBER, CallLog.Calls.PRESENTATION_UNKNOWN, TEST_COUNTRY_ISO); 52 assertEquals(TEST_VALID_NUMBER, contactInfo.number); 53 assertEquals(mContext.getResources().getString(R.string.unknown), contactInfo.name); 54 assertEquals(TEST_E164_NUMBER, contactInfo.normalizedNumber); 55 } 56 57 public void testGetContactInfo_ValidNumberInvalidPresentationValidIso() { 58 ContactInfo contactInfo = getContactInfo(TEST_VALID_NUMBER, -1, TEST_COUNTRY_ISO); 59 assertEquals(TEST_VALID_NUMBER, contactInfo.number); 60 assertEquals(TEST_FORMATTED_NUMBER, contactInfo.name); 61 assertEquals(TEST_E164_NUMBER, contactInfo.normalizedNumber); 62 } 63 64 public void testGetContactInfo_ValidNumberValidPresentationNullIso() { 65 ContactInfo contactInfo = getContactInfo( 66 TEST_VALID_NUMBER, CallLog.Calls.PRESENTATION_UNKNOWN, null); 67 assertEquals(TEST_VALID_NUMBER, contactInfo.number); 68 assertEquals(mContext.getResources().getString(R.string.unknown), contactInfo.name); 69 assertEquals(TEST_E164_NUMBER, contactInfo.normalizedNumber); 70 } 71 72 public void testGetContactInfo_ValidNumberInvalidPresentationNullIso() { 73 ContactInfo contactInfo = getContactInfo( 74 TEST_VALID_NUMBER, -1, null); 75 assertEquals(TEST_VALID_NUMBER, contactInfo.number); 76 assertEquals(TEST_FORMATTED_NUMBER, contactInfo.name); 77 assertEquals(TEST_E164_NUMBER, contactInfo.normalizedNumber); 78 } 79 80 public void testGetContactInfo_NullNumberValidPresentationValidIso() { 81 ContactInfo contactInfo = getContactInfo( 82 null, CallLog.Calls.PRESENTATION_UNKNOWN, TEST_COUNTRY_ISO); 83 assertEquals("", contactInfo.number); 84 assertEquals(mContext.getResources().getString(R.string.unknown), contactInfo.name); 85 assertNull(contactInfo.normalizedNumber); 86 } 87 88 public void testGetContactInfo_NullNumberInvalidPresentationValidIso() { 89 ContactInfo contactInfo = getContactInfo(null, -1, TEST_COUNTRY_ISO); 90 assertEquals("", contactInfo.number); 91 assertEquals(mContext.getResources().getString(R.string.unknown), contactInfo.name); 92 assertNull(contactInfo.normalizedNumber); 93 } 94 95 public void testGetContactInfo_NullNumberValidPresentationNullIso() { 96 ContactInfo contactInfo = getContactInfo(null, CallLog.Calls.PRESENTATION_RESTRICTED, null); 97 assertEquals("", contactInfo.number); 98 assertEquals(mContext.getResources().getString(R.string.private_num), contactInfo.name); 99 assertNull(contactInfo.normalizedNumber); 100 } 101 102 public void testGetContactInfo_NullNumberInValidPresentationNullIso() { 103 ContactInfo contactInfo = getContactInfo(null, -1, null); 104 assertEquals("", contactInfo.number); 105 assertEquals(mContext.getResources().getString(R.string.unknown), contactInfo.name); 106 assertNull(contactInfo.normalizedNumber); 107 } 108 109 public void testGetContactInfo_InvalidNumberInValidPresentationNullIso() { 110 ContactInfo contactInfo = getContactInfo(TEST_INVALID_NUMBER, -1, null); 111 assertEquals(TEST_INVALID_NUMBER, contactInfo.name); 112 assertEquals(TEST_INVALID_NUMBER, contactInfo.formattedNumber); 113 assertEquals(null, contactInfo.normalizedNumber); 114 } 115 116 public void testGetContactInfo_NonNullCachedLookup() { 117 when(mContactInfoHelper.lookupNumber(anyString(), anyString())).thenReturn(null); 118 ContactInfo contactInfo = getContactInfo(TEST_VALID_NUMBER, -1, TEST_COUNTRY_ISO); 119 assertEquals(TEST_VALID_NUMBER, contactInfo.number); 120 assertEquals(TEST_FORMATTED_NUMBER, contactInfo.formattedNumber); 121 } 122 123 public void testGetContactInfo_NullCachedLookup() { 124 ContactInfo cachedContactInfo = new ContactInfo(); 125 cachedContactInfo.number = TEST_VALID_NUMBER; 126 cachedContactInfo.formattedNumber = TEST_FORMATTED_NUMBER; 127 when(mContactInfoHelper.lookupNumber(anyString(), anyString())) 128 .thenReturn(cachedContactInfo); 129 ContactInfo contactInfo = getContactInfo(TEST_VALID_NUMBER, -1, TEST_COUNTRY_ISO); 130 assertEquals(TEST_VALID_NUMBER, contactInfo.number); 131 assertEquals(TEST_FORMATTED_NUMBER, contactInfo.name); 132 } 133 134 private ContactInfo getContactInfo(String number, int presentation, String countryIso) { 135 return mCallLogNotificationsHelper.getContactInfo(number, presentation, countryIso); 136 } 137 } 138