ContactsAsyncHelperTest.java revision 3037ac6f171b6a3627494bb10042ab7adb34366a
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 17package com.android.server.telecom.tests; 18 19import android.content.Context; 20import android.graphics.Bitmap; 21import android.graphics.drawable.BitmapDrawable; 22import android.graphics.drawable.Drawable; 23import android.net.Uri; 24import android.test.suitebuilder.annotation.SmallTest; 25 26import com.android.server.telecom.ContactsAsyncHelper; 27 28import org.mockito.ArgumentCaptor; 29 30import java.io.FileNotFoundException; 31import java.io.InputStream; 32 33import static org.mockito.Matchers.any; 34import static org.mockito.Matchers.anyInt; 35import static org.mockito.Matchers.anyObject; 36import static org.mockito.Matchers.eq; 37import static org.mockito.Matchers.isNull; 38import static org.mockito.Mockito.never; 39import static org.mockito.Mockito.spy; 40import static org.mockito.Mockito.timeout; 41import static org.mockito.Mockito.verify; 42 43public class ContactsAsyncHelperTest extends TelecomTestCase { 44 private static final Uri SAMPLE_CONTACT_PHOTO_URI = Uri.parse( 45 "android.resource://com.android.server.telecom.tests/" 46 + R.drawable.contacts_sample_photo); 47 48 private static final Uri SAMPLE_CONTACT_PHOTO_URI_SMALL = Uri.parse( 49 "android.resource://com.android.server.telecom.tests/" 50 + R.drawable.contacts_sample_photo_small); 51 52 private static final int TOKEN = 4847524; 53 private static final int TEST_TIMEOUT = 500; 54 private static final Object COOKIE = new Object(); 55 56 public static class ImageLoadListenerImpl 57 implements ContactsAsyncHelper.OnImageLoadCompleteListener { 58 @Override 59 public void onImageLoadComplete(int token, Drawable photo, 60 Bitmap photoIcon, Object cookie) { 61 } 62 } 63 64 private ImageLoadListenerImpl mListener = spy(new ImageLoadListenerImpl()); 65 66 private ContactsAsyncHelper.ContentResolverAdapter mWorkingContentResolverAdapter = 67 new ContactsAsyncHelper.ContentResolverAdapter() { 68 @Override 69 public InputStream openInputStream(Context context, Uri uri) 70 throws FileNotFoundException { 71 return context.getContentResolver().openInputStream(uri); 72 } 73 }; 74 75 private ContactsAsyncHelper.ContentResolverAdapter mNullContentResolverAdapter = 76 new ContactsAsyncHelper.ContentResolverAdapter() { 77 @Override 78 public InputStream openInputStream(Context context, Uri uri) 79 throws FileNotFoundException { 80 return null; 81 } 82 }; 83 84 @Override 85 public void setUp() throws Exception { 86 mContext = getTestContext(); 87 super.setUp(); 88 } 89 90 @SmallTest 91 public void testEmptyUri() throws Exception { 92 ContactsAsyncHelper cah = new ContactsAsyncHelper(mNullContentResolverAdapter); 93 try { 94 cah.startObtainPhotoAsync(TOKEN, mContext, null, mListener, COOKIE); 95 } catch (IllegalStateException e) { 96 // expected to fail 97 } 98 Thread.sleep(TEST_TIMEOUT); 99 verify(mListener, never()).onImageLoadComplete(anyInt(), 100 any(Drawable.class), any(Bitmap.class), anyObject()); 101 } 102 103 @SmallTest 104 public void testNullReturnFromOpenInputStream() { 105 ContactsAsyncHelper cah = new ContactsAsyncHelper(mNullContentResolverAdapter); 106 cah.startObtainPhotoAsync(TOKEN, mContext, SAMPLE_CONTACT_PHOTO_URI, mListener, COOKIE); 107 108 verify(mListener, timeout(TEST_TIMEOUT)).onImageLoadComplete(eq(TOKEN), 109 isNull(Drawable.class), isNull(Bitmap.class), eq(COOKIE)); 110 } 111 112 @SmallTest 113 public void testImageScaling() { 114 ContactsAsyncHelper cah = new ContactsAsyncHelper(mWorkingContentResolverAdapter); 115 cah.startObtainPhotoAsync(TOKEN, mContext, SAMPLE_CONTACT_PHOTO_URI, mListener, COOKIE); 116 117 ArgumentCaptor<Drawable> photoCaptor = ArgumentCaptor.forClass(Drawable.class); 118 ArgumentCaptor<Bitmap> iconCaptor = ArgumentCaptor.forClass(Bitmap.class); 119 120 verify(mListener, timeout(TEST_TIMEOUT)).onImageLoadComplete(eq(TOKEN), 121 photoCaptor.capture(), iconCaptor.capture(), eq(COOKIE)); 122 123 Bitmap capturedPhoto = ((BitmapDrawable) photoCaptor.getValue()).getBitmap(); 124 assertTrue(getExpectedPhoto(SAMPLE_CONTACT_PHOTO_URI).sameAs(capturedPhoto)); 125 int iconSize = mContext.getResources() 126 .getDimensionPixelSize(R.dimen.notification_icon_size); 127 assertTrue(iconSize >= iconCaptor.getValue().getHeight()); 128 assertTrue(iconSize >= iconCaptor.getValue().getWidth()); 129 } 130 131 @SmallTest 132 public void testNoScaling() { 133 ContactsAsyncHelper cah = new ContactsAsyncHelper(mWorkingContentResolverAdapter); 134 cah.startObtainPhotoAsync(TOKEN, mContext, SAMPLE_CONTACT_PHOTO_URI_SMALL, 135 mListener, COOKIE); 136 137 ArgumentCaptor<Drawable> photoCaptor = ArgumentCaptor.forClass(Drawable.class); 138 ArgumentCaptor<Bitmap> iconCaptor = ArgumentCaptor.forClass(Bitmap.class); 139 140 verify(mListener, timeout(TEST_TIMEOUT)).onImageLoadComplete(eq(TOKEN), 141 photoCaptor.capture(), iconCaptor.capture(), eq(COOKIE)); 142 143 Bitmap capturedPhoto = ((BitmapDrawable) photoCaptor.getValue()).getBitmap(); 144 assertTrue(getExpectedPhoto(SAMPLE_CONTACT_PHOTO_URI_SMALL).sameAs(capturedPhoto)); 145 assertTrue(capturedPhoto.sameAs(iconCaptor.getValue())); 146 } 147 148 private Bitmap getExpectedPhoto(Uri uri) { 149 InputStream is; 150 try { 151 is = mContext.getContentResolver().openInputStream(uri); 152 } catch (FileNotFoundException e) { 153 return null; 154 } 155 156 Drawable d = Drawable.createFromStream(is, uri.toString()); 157 return ((BitmapDrawable) d).getBitmap(); 158 } 159} 160