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 package com.android.dialer.contactinfo; 17 18 import android.app.Instrumentation; 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.graphics.Bitmap; 23 import android.graphics.drawable.BitmapDrawable; 24 import android.graphics.drawable.Drawable; 25 import android.net.Uri; 26 import android.support.test.InstrumentationRegistry; 27 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; 28 import android.test.AndroidTestCase; 29 import android.test.InstrumentationTestCase; 30 import android.text.TextUtils; 31 32 import com.android.contacts.common.GeoUtil; 33 import com.android.contacts.common.lettertiles.LetterTileDrawable; 34 import com.android.dialer.calllog.ContactInfo; 35 import com.android.dialer.calllog.ContactInfoHelper; 36 import com.android.dialer.tests.R; 37 38 public class ContactPhotoLoaderTest extends InstrumentationTestCase { 39 40 private Context mContext; 41 42 @Override 43 public void setUp() { 44 mContext = getInstrumentation().getTargetContext(); 45 } 46 47 public void testConstructor() { 48 ContactPhotoLoader loader = new ContactPhotoLoader(mContext, new ContactInfo()); 49 } 50 51 public void testConstructor_NullContext() { 52 try { 53 ContactPhotoLoader loader = new ContactPhotoLoader(null, new ContactInfo()); 54 fail(); 55 } catch (NullPointerException e) { 56 //expected 57 } 58 } 59 60 public void testConstructor_NullContactInfo() { 61 try { 62 ContactPhotoLoader loader = new ContactPhotoLoader(mContext, null); 63 fail(); 64 } catch (NullPointerException e) { 65 //expected 66 } 67 } 68 69 public void testGetIcon_Photo() { 70 ContactInfo info = getTestContactInfo(); 71 info.photoUri = getResourceUri(R.drawable.phone_icon); 72 ContactPhotoLoader loader = new ContactPhotoLoader(mContext, info); 73 assertTrue(loader.getIcon() instanceof RoundedBitmapDrawable); 74 } 75 76 public void testGetIcon_Photo_Invalid() { 77 ContactInfo info = getTestContactInfo(); 78 info.photoUri = Uri.parse("file://invalid/uri"); 79 ContactPhotoLoader loader = new ContactPhotoLoader(mContext, info); 80 //Should fall back to LetterTileDrawable 81 assertTrue(loader.getIcon() instanceof LetterTileDrawable); 82 } 83 84 public void testGetIcon_LetterTile() { 85 ContactInfo info = getTestContactInfo(); 86 ContactPhotoLoader loader = new ContactPhotoLoader(mContext, info); 87 assertTrue(loader.getIcon() instanceof LetterTileDrawable); 88 } 89 90 private Uri getResourceUri(int resId) { 91 Context testContext = getInstrumentation().getContext(); 92 Resources resources = testContext.getResources(); 93 94 assertNotNull(resources.getDrawable(resId)); 95 return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" 96 + testContext.getPackageName() 97 + '/' + resId); 98 } 99 100 private ContactInfo getTestContactInfo() { 101 ContactInfo info = new ContactInfo(); 102 info.name = "foo"; 103 info.lookupKey = "bar"; 104 return info; 105 } 106 }