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