1 /* 2 * Copyright (C) 2018 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 android.util.imagepool; 18 19 import org.junit.Test; 20 21 import android.util.imagepool.Bucket.BucketCreationMetaData; 22 import android.util.imagepool.ImagePool.Image.Orientation; 23 24 import java.awt.image.BufferedImage; 25 import java.lang.ref.SoftReference; 26 27 import static org.junit.Assert.assertNotNull; 28 import static org.junit.Assert.assertNull; 29 30 public class ImagePoolHelperTest { 31 32 @Test testGetBufferedImage()33 public void testGetBufferedImage() { 34 int width = 10; 35 int height = 10; 36 int numberOfCopiesInBucket = 10; 37 int maxCacheSize = width * height * 4 * 5; // can fit 5 width | height buffer 38 Bucket bucket = new Bucket(); 39 BucketCreationMetaData metaData = new BucketCreationMetaData( 40 width, height, BufferedImage.TYPE_INT_ARGB, numberOfCopiesInBucket, Orientation 41 .NONE, maxCacheSize); 42 ImagePoolStats stats = new ImagePoolStatsProdImpl(); 43 44 assertNotNull(ImagePoolHelper.getBufferedImage(bucket, metaData, stats)); 45 } 46 47 @Test testGetBufferedImageRecurse()48 public void testGetBufferedImageRecurse() { 49 int width = 10; 50 int height = 10; 51 int numberOfCopiesToRequestInBucket = 1; 52 int numberOfCopiesInBucket = 10; 53 int maxCacheSize = width * height * 4 * numberOfCopiesToRequestInBucket; 54 55 Bucket bucket = new Bucket(); 56 for (int i = 0; i < numberOfCopiesInBucket; i++) { 57 bucket.mBufferedImageRef.add(new SoftReference<>(null)); 58 } 59 BucketCreationMetaData metaData = new BucketCreationMetaData( 60 width, height, BufferedImage.TYPE_INT_ARGB, numberOfCopiesToRequestInBucket, Orientation 61 .NONE, maxCacheSize); 62 ImagePoolStats stats = new ImagePoolStatsProdImpl(); 63 64 assertNotNull(ImagePoolHelper.getBufferedImage(bucket, metaData, stats)); 65 } 66 67 @Test testRecurseThenHitCacheLimit()68 public void testRecurseThenHitCacheLimit() { 69 int width = 10; 70 int height = 10; 71 int numberOfCopiesToRequestInBucket = 1; 72 int numberOfCopiesInBucket = 10; 73 int maxCacheSize = width * height * 4 * numberOfCopiesToRequestInBucket / 2; 74 75 Bucket bucket = new Bucket(); 76 for (int i = 0; i < numberOfCopiesInBucket; i++) { 77 bucket.mBufferedImageRef.add(new SoftReference<>(null)); 78 } 79 BucketCreationMetaData metaData = new BucketCreationMetaData( 80 width, height, BufferedImage.TYPE_INT_ARGB, numberOfCopiesToRequestInBucket, Orientation 81 .NONE, maxCacheSize); 82 ImagePoolStats stats = new ImagePoolStatsProdImpl(); 83 84 assertNull(ImagePoolHelper.getBufferedImage(bucket, metaData, stats)); 85 } 86 87 @Test testBucketHasImageToReturn()88 public void testBucketHasImageToReturn() { 89 int width = 10; 90 int height = 10; 91 int numberOfCopiesToRequestInBucket = 1; 92 int numberOfCopiesInBucket = 10; 93 int maxCacheSize = width * height * 4 * numberOfCopiesToRequestInBucket / 2; 94 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); 95 96 Bucket bucket = new Bucket(); 97 for (int i = 0; i < numberOfCopiesInBucket; i++) { 98 bucket.mBufferedImageRef.add(new SoftReference<>(null)); 99 } 100 bucket.mBufferedImageRef.add(new SoftReference<>(image)); 101 BucketCreationMetaData metaData = new BucketCreationMetaData( 102 width, height, BufferedImage.TYPE_INT_ARGB, numberOfCopiesToRequestInBucket, Orientation 103 .NONE, maxCacheSize); 104 ImagePoolStats stats = new ImagePoolStatsProdImpl(); 105 106 assertNotNull(ImagePoolHelper.getBufferedImage(bucket, metaData, stats)); 107 } 108 }