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