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.messaging.datamodel;
18 
19 import android.content.res.Resources;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 
23 import androidx.test.filters.SmallTest;
24 
25 import com.android.messaging.BugleTestCase;
26 import com.android.messaging.FakeFactory;
27 import com.android.messaging.R;
28 
29 import org.mockito.Mock;
30 
31 import java.util.HashSet;
32 import java.util.Set;
33 
34 @SmallTest
35 public class BitmapPoolTest extends BugleTestCase {
36     private static final int POOL_SIZE = 5;
37     private static final int IMAGE_DIM = 1;
38     private static final String NAME = "BitmapPoolTest";
39 
40     @Mock private MemoryCacheManager mockMemoryCacheManager;
41 
42     @Override
setUp()43     protected void setUp() throws Exception {
44         super.setUp();
45         FakeFactory.register(getTestContext())
46                 .withMemoryCacheManager(mockMemoryCacheManager);
47     }
48 
fillPoolAndGetPoolContents(final BitmapPool pool, final int width, final int height)49     private Set<Bitmap> fillPoolAndGetPoolContents(final BitmapPool pool, final int width,
50             final int height) {
51         final Set<Bitmap> returnedBitmaps = new HashSet<Bitmap>();
52         for (int i = 0; i < POOL_SIZE; i++) {
53             final Bitmap temp = pool.createOrReuseBitmap(width, height);
54             assertFalse(returnedBitmaps.contains(temp));
55             returnedBitmaps.add(temp);
56         }
57         for (final Bitmap b : returnedBitmaps) {
58             pool.reclaimBitmap(b);
59         }
60         assertTrue(pool.isFull(width, height));
61         return returnedBitmaps;
62     }
63 
testCreateAndPutBackInPoolTest()64     public void testCreateAndPutBackInPoolTest() {
65         final BitmapPool pool = new BitmapPool(POOL_SIZE, NAME);
66         final Bitmap bitmap = pool.createOrReuseBitmap(IMAGE_DIM, IMAGE_DIM);
67         assertFalse(bitmap.isRecycled());
68         assertFalse(pool.isFull(IMAGE_DIM, IMAGE_DIM));
69         pool.reclaimBitmap(bitmap);
70 
71         // Don't recycle because the pool isn't full yet.
72         assertFalse(bitmap.isRecycled());
73     }
74 
testCreateBeyondFullAndCheckReuseTest()75     public void testCreateBeyondFullAndCheckReuseTest() {
76         final BitmapPool pool = new BitmapPool(POOL_SIZE, NAME);
77         final Set<Bitmap> returnedBitmaps =
78                 fillPoolAndGetPoolContents(pool, IMAGE_DIM, IMAGE_DIM);
79         final Bitmap overflowBitmap = pool.createOrReuseBitmap(IMAGE_DIM, IMAGE_DIM);
80         assertFalse(overflowBitmap.isRecycled());
81         assertTrue(returnedBitmaps.contains(overflowBitmap));
82     }
83 
84     /**
85      * Make sure that we have the correct options to create mutable for bitmap pool reuse.
86      */
testAssertBitmapOptionsAreMutable()87     public void testAssertBitmapOptionsAreMutable() {
88         final BitmapFactory.Options options =
89                 BitmapPool.getBitmapOptionsForPool(false, IMAGE_DIM, IMAGE_DIM);
90         assertTrue(options.inMutable);
91     }
92 
testDecodeFromResourceBitmap()93     public void testDecodeFromResourceBitmap() {
94         final BitmapPool pool = new BitmapPool(POOL_SIZE, NAME);
95         final BitmapFactory.Options options =
96                 BitmapPool.getBitmapOptionsForPool(true, IMAGE_DIM, IMAGE_DIM);
97         final Resources resources = getContext().getResources();
98         final Bitmap resourceBitmap = pool.decodeSampledBitmapFromResource(
99                 R.drawable.msg_bubble_incoming, resources, options, IMAGE_DIM, IMAGE_DIM);
100         assertNotNull(resourceBitmap);
101         assertTrue(resourceBitmap.getByteCount() > 0);
102     }
103 }
104