blob: 81263aeb760536caf4ca0a3f31be093dff5267f6 [file] [log] [blame]
Hall Liu9ccc43d2015-12-15 14:18:33 -08001/*
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;
Hall Liuc8a396b2017-12-27 18:23:28 -080024import android.support.test.InstrumentationRegistry;
Hall Liu84b6c872016-02-03 12:19:00 -080025import android.test.suitebuilder.annotation.SmallTest;
Hall Liu9ccc43d2015-12-15 14:18:33 -080026
27import com.android.server.telecom.ContactsAsyncHelper;
28
Hall Liuc8a396b2017-12-27 18:23:28 -080029import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.junit.runners.JUnit4;
Hall Liu9ccc43d2015-12-15 14:18:33 -080033import org.mockito.ArgumentCaptor;
34
35import java.io.FileNotFoundException;
36import java.io.InputStream;
37
Hall Liuc8a396b2017-12-27 18:23:28 -080038import static org.junit.Assert.assertTrue;
Hall Liu9ccc43d2015-12-15 14:18:33 -080039import static org.mockito.Matchers.any;
40import static org.mockito.Matchers.anyInt;
41import static org.mockito.Matchers.anyObject;
42import static org.mockito.Matchers.eq;
43import static org.mockito.Matchers.isNull;
Hall Liu3037ac62016-09-22 14:40:03 -070044import static org.mockito.Mockito.never;
Hall Liu9ccc43d2015-12-15 14:18:33 -080045import static org.mockito.Mockito.spy;
46import static org.mockito.Mockito.timeout;
47import static org.mockito.Mockito.verify;
48
Hall Liuc8a396b2017-12-27 18:23:28 -080049@RunWith(JUnit4.class)
Hall Liu9ccc43d2015-12-15 14:18:33 -080050public 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 Liuc8a396b2017-12-27 18:23:28 -080092 @Before
Hall Liu9ccc43d2015-12-15 14:18:33 -080093 public void setUp() throws Exception {
Hall Liu9ccc43d2015-12-15 14:18:33 -080094 super.setUp();
Hall Liuc8a396b2017-12-27 18:23:28 -080095 mContext = InstrumentationRegistry.getTargetContext();
Hall Liu9ccc43d2015-12-15 14:18:33 -080096 }
97
Hall Liu84b6c872016-02-03 12:19:00 -080098 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -080099 @Test
Hall Liu3037ac62016-09-22 14:40:03 -0700100 public void testEmptyUri() throws Exception {
Hall Liu9ccc43d2015-12-15 14:18:33 -0800101 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 Liu3037ac62016-09-22 14:40:03 -0700107 Thread.sleep(TEST_TIMEOUT);
108 verify(mListener, never()).onImageLoadComplete(anyInt(),
Hall Liu9ccc43d2015-12-15 14:18:33 -0800109 any(Drawable.class), any(Bitmap.class), anyObject());
110 }
111
Hall Liu84b6c872016-02-03 12:19:00 -0800112 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -0800113 @Test
Hall Liu9ccc43d2015-12-15 14:18:33 -0800114 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 Liu84b6c872016-02-03 12:19:00 -0800122 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -0800123 @Test
Hall Liu9ccc43d2015-12-15 14:18:33 -0800124 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 Liu84b6c872016-02-03 12:19:00 -0800142 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -0800143 @Test
Hall Liu9ccc43d2015-12-15 14:18:33 -0800144 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}