1 /* 2 * Copyright (C) 2015 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.net.Uri; 20 import android.provider.ContactsContract.Contacts; 21 import android.provider.ContactsContract.PhoneLookup; 22 import android.test.AndroidTestCase; 23 import android.test.suitebuilder.annotation.MediumTest; 24 25 import com.android.contacts.common.ContactsUtils; 26 import com.android.contacts.common.test.mocks.ContactsMockContext; 27 import com.android.contacts.common.test.mocks.MockContentProvider.Query; 28 29 import junit.framework.Assert; 30 31 @MediumTest 32 public class ContactInfoHelperTest extends AndroidTestCase { 33 34 private static final String TEST_COUNTRY_ISO = "US"; 35 private static final String TEST_DISPLAY_NAME = "Display Name"; 36 private static final String TEST_DISPLAY_NAME_ALTERNATIVE = "Name, Display"; 37 private static final String[] TEST_DISPLAY_NAME_ALTERNATIVE_ROW = new String[]{ 38 TEST_DISPLAY_NAME_ALTERNATIVE}; 39 private static final String TEST_LOOKUP_KEY = "lookupKey"; 40 private static final String[] TEST_LOOKUP_ROW = new String[]{null, TEST_DISPLAY_NAME, 41 null, null, null, null, null, TEST_LOOKUP_KEY, null}; 42 43 private Uri displayNameAlternativeUri; 44 private ContactsMockContext mContext; 45 private ContactInfo mContactInfo; 46 private ContactInfoHelper mContactInfoHelper; 47 48 @Override setUp()49 public void setUp() throws Exception { 50 super.setUp(); 51 52 displayNameAlternativeUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, 53 TEST_LOOKUP_KEY); 54 mContext = new ContactsMockContext(getContext()); 55 mContactInfo = new ContactInfo(); 56 mContactInfo.name = TEST_DISPLAY_NAME; 57 mContactInfo.nameAlternative = TEST_DISPLAY_NAME_ALTERNATIVE; 58 mContactInfoHelper = new ContactInfoHelper(mContext, TEST_COUNTRY_ISO); 59 } 60 testLookupContactFromUri_NullUri()61 public void testLookupContactFromUri_NullUri() { 62 Assert.assertNull(mContactInfoHelper.lookupContactFromUri(null, false)); 63 } 64 testLookupContactFromUri_NoResults()65 public void testLookupContactFromUri_NoResults() { 66 setUpQueryExpectations(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, 67 PhoneQuery.getPhoneLookupProjection(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI)); 68 69 Assert.assertEquals(ContactInfo.EMPTY, mContactInfoHelper.lookupContactFromUri( 70 PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, false)); 71 mContext.verify(); 72 } 73 testLookupContactFromUri_NoDisplayNameAlternative()74 public void testLookupContactFromUri_NoDisplayNameAlternative() { 75 setUpQueryExpectations(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, 76 PhoneQuery.getPhoneLookupProjection(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI), 77 TEST_LOOKUP_ROW); 78 setUpQueryExpectations(displayNameAlternativeUri, 79 PhoneQuery.DISPLAY_NAME_ALTERNATIVE_PROJECTION); 80 81 ContactInfo contactInfo = mContactInfoHelper.lookupContactFromUri( 82 PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, false); 83 Assert.assertEquals(TEST_DISPLAY_NAME, contactInfo.name); 84 Assert.assertNull(contactInfo.nameAlternative); 85 mContext.verify(); 86 } 87 testLookupContactFromUri_HasDisplayNameAlternative()88 public void testLookupContactFromUri_HasDisplayNameAlternative() { 89 setUpQueryExpectations(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, 90 PhoneQuery.getPhoneLookupProjection(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI), 91 TEST_LOOKUP_ROW); 92 setUpQueryExpectations(displayNameAlternativeUri, 93 PhoneQuery.DISPLAY_NAME_ALTERNATIVE_PROJECTION, TEST_DISPLAY_NAME_ALTERNATIVE_ROW); 94 95 ContactInfo contactInfo = mContactInfoHelper.lookupContactFromUri( 96 PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, false); 97 Assert.assertEquals(TEST_DISPLAY_NAME, contactInfo.name); 98 Assert.assertEquals(TEST_DISPLAY_NAME_ALTERNATIVE, contactInfo.nameAlternative); 99 mContext.verify(); 100 } 101 testLookupDisplayNameAlternative_NullLookup()102 public void testLookupDisplayNameAlternative_NullLookup() { 103 Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, null, 104 ContactsUtils.USER_TYPE_CURRENT)); 105 } 106 testLookupDisplayNameAlternative_NoResults()107 public void testLookupDisplayNameAlternative_NoResults() { 108 setUpQueryExpectations(displayNameAlternativeUri, 109 PhoneQuery.DISPLAY_NAME_ALTERNATIVE_PROJECTION); 110 Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, 111 TEST_LOOKUP_KEY, ContactsUtils.USER_TYPE_CURRENT)); 112 mContext.verify(); 113 } 114 testLookupDisplayNameAlternative_HasDisplayNameAlternative()115 public void testLookupDisplayNameAlternative_HasDisplayNameAlternative() { 116 setUpQueryExpectations(displayNameAlternativeUri, 117 PhoneQuery.DISPLAY_NAME_ALTERNATIVE_PROJECTION, TEST_DISPLAY_NAME_ALTERNATIVE_ROW); 118 Assert.assertEquals(TEST_DISPLAY_NAME_ALTERNATIVE, 119 mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY 120 , ContactsUtils.USER_TYPE_CURRENT)); 121 mContext.verify(); 122 } 123 124 /* 125 * Sets up query expectations to return the given row for all queries for the given 126 * uri and projection. If row is null, an empty cursor is returned for query calls 127 */ setUpQueryExpectations(Uri uri, String[] projection, String...row)128 private void setUpQueryExpectations(Uri uri, String[] projection, String...row) { 129 Query query = mContext.getContactsProvider().expectQuery(uri) 130 .withProjection(projection).withAnySelection().withAnySortOrder(); 131 if (row == null || row.length == 0) { 132 query.returnEmptyCursor(); 133 return; 134 } 135 query.returnRow(row); 136 } 137 } 138