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