ImageRequestTest.java revision 461a34b466cb4b13dbbc2ec6330b31e217b2ac4e
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 */
16package com.android.messaging.datamodel.media;
17
18import android.content.ContentResolver;
19import android.graphics.BitmapFactory;
20import android.net.Uri;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import com.android.messaging.BugleTestCase;
24import com.android.messaging.FakeFactory;
25import com.android.messaging.R;
26import com.android.messaging.datamodel.MemoryCacheManager;
27import com.android.messaging.util.ImageUtils;
28
29import org.mockito.ArgumentCaptor;
30import org.mockito.Matchers;
31import org.mockito.Mockito;
32import org.mockito.Spy;
33
34import java.io.IOException;
35
36@SmallTest
37public class ImageRequestTest extends BugleTestCase {
38    private static final int DOWNSAMPLE_IMAGE_SIZE = 2;
39
40    @Spy protected ImageUtils spyImageUtils;
41
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45        FakeFactory.register(getTestContext())
46                   .withMemoryCacheManager(new MemoryCacheManager())
47                   .withMediaCacheManager(new BugleMediaCacheManager());
48        spyImageUtils = Mockito.spy(new ImageUtils());
49        ImageUtils.set(spyImageUtils);
50    }
51
52    public void testLoadImageUnspecifiedSize() {
53        final String uriString = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
54                getContext().getPackageName() + "/" + R.drawable.ic_audio_light;
55        final Uri uri = Uri.parse(uriString);
56        final UriImageRequest imageRequest = new UriImageRequest(getContext(),
57                new UriImageRequestDescriptor(uri));
58        try {
59            final ImageResource imageResource = imageRequest.loadMediaBlocking(null);
60            final ArgumentCaptor<BitmapFactory.Options> options =
61                    ArgumentCaptor.forClass(BitmapFactory.Options.class);
62            Mockito.verify(spyImageUtils).calculateInSampleSize(
63                    options.capture(),
64                    Matchers.eq(ImageRequest.UNSPECIFIED_SIZE),
65                    Matchers.eq(ImageRequest.UNSPECIFIED_SIZE));
66            assertEquals(1, options.getValue().inSampleSize);
67            assertNotNull(imageResource);
68            assertNotNull(imageResource.getBitmap());
69
70            // Make sure there's no scaling on the bitmap.
71            final int bitmapWidth = imageResource.getBitmap().getWidth();
72            final int bitmapHeight = imageResource.getBitmap().getHeight();
73            assertEquals(options.getValue().outWidth, bitmapWidth);
74            assertEquals(options.getValue().outHeight, bitmapHeight);
75        } catch (final IOException e) {
76            fail("IO exception while trying to load image resource");
77        }
78    }
79
80    public void testLoadImageWithDownsampling() {
81        final String uriString = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
82                getContext().getPackageName() + "/" + R.drawable.ic_audio_light;
83        final Uri uri = Uri.parse(uriString);
84        final UriImageRequest imageRequest = new UriImageRequest(getContext(),
85                new UriImageRequestDescriptor(uri, DOWNSAMPLE_IMAGE_SIZE, DOWNSAMPLE_IMAGE_SIZE,
86                        false, true /* isStatic */, false /* cropToCircle */,
87                        ImageUtils.DEFAULT_CIRCLE_BACKGROUND_COLOR /* circleBackgroundColor */,
88                        ImageUtils.DEFAULT_CIRCLE_STROKE_COLOR /* circleStrokeColor */));
89        try {
90            final ImageResource imageResource = imageRequest.loadMediaBlocking(null);
91            final ArgumentCaptor<BitmapFactory.Options> options =
92                    ArgumentCaptor.forClass(BitmapFactory.Options.class);
93            Mockito.verify(spyImageUtils).calculateInSampleSize(
94                    options.capture(),
95                    Matchers.eq(DOWNSAMPLE_IMAGE_SIZE), Matchers.eq(DOWNSAMPLE_IMAGE_SIZE));
96            assertNotSame(1, options.getValue().inSampleSize);
97            assertNotNull(imageResource);
98            assertNotNull(imageResource.getBitmap());
99
100            // Make sure there's down sampling on the bitmap.
101            final int bitmapWidth = imageResource.getBitmap().getWidth();
102            final int bitmapHeight = imageResource.getBitmap().getHeight();
103            assertTrue(bitmapWidth >= DOWNSAMPLE_IMAGE_SIZE &&
104                    bitmapHeight >= DOWNSAMPLE_IMAGE_SIZE &&
105                    (bitmapWidth <= DOWNSAMPLE_IMAGE_SIZE * 4 ||
106                    bitmapHeight <= DOWNSAMPLE_IMAGE_SIZE * 4));
107        } catch (final IOException e) {
108            fail("IO exception while trying to load image resource");
109        }
110    }
111}
112