diff --git a/core/src/main/java/com/novoda/imageloader/core/cache/LruBitmapCache.java b/core/src/main/java/com/novoda/imageloader/core/cache/LruBitmapCache.java index eecbd84..ed21120 100644 --- a/core/src/main/java/com/novoda/imageloader/core/cache/LruBitmapCache.java +++ b/core/src/main/java/com/novoda/imageloader/core/cache/LruBitmapCache.java @@ -42,22 +42,10 @@ public class LruBitmapCache implements CacheManager { public LruBitmapCache(Context context, int percentageOfMemoryForCache) { int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass(); - if(memClass == 0) { - memClass = DEFAULT_MEMORY_CAPACITY_FOR_DEVICES_OLDER_THAN_API_LEVEL_4; - } - if(percentageOfMemoryForCache < 0) { - percentageOfMemoryForCache = 0; - } - if(percentageOfMemoryForCache > 81) { - percentageOfMemoryForCache = 80; - } - this.capacity = (int) ((memClass * percentageOfMemoryForCache * 1024L * 1024L) / 100L); - if(this.capacity <= 0) { - this.capacity = 1024*1024*4; - } + this.capacity = calculateCacheSize(memClass, percentageOfMemoryForCache); reset(); } - + /** * Setting the default memory size to 25% percent of the total memory * available of the application. @@ -67,6 +55,12 @@ public LruBitmapCache(Context context) { this(context, DEFAULT_MEMORY_CACHE_PERCENTAGE); } + /** + * Empty constructor for testing purposes + */ + protected LruBitmapCache() { + } + private void reset() { if (cache != null) { cache.evictAll(); @@ -94,4 +88,21 @@ public void clean() { reset(); } + public int calculateCacheSize(int memClass, int percentageOfMemoryForCache) { + if(memClass == 0) { + memClass = DEFAULT_MEMORY_CAPACITY_FOR_DEVICES_OLDER_THAN_API_LEVEL_4; + } + if(percentageOfMemoryForCache < 0) { + percentageOfMemoryForCache = 0; + } + if(percentageOfMemoryForCache > 81) { + percentageOfMemoryForCache = 80; + } + int capacity = (int) ((memClass * percentageOfMemoryForCache * 1024L * 1024L) / 100L); + if(capacity <= 0) { + capacity = 1024*1024*4; + } + + return capacity; + } } diff --git a/core/src/test/java/com/novoda/imageloader/core/cache/LruBitmapCacheTest.java b/core/src/test/java/com/novoda/imageloader/core/cache/LruBitmapCacheTest.java new file mode 100644 index 0000000..aa092d5 --- /dev/null +++ b/core/src/test/java/com/novoda/imageloader/core/cache/LruBitmapCacheTest.java @@ -0,0 +1,27 @@ +package com.novoda.imageloader.core.cache; + +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; + +public class LruBitmapCacheTest { + + @Test + public void verifyCalculateCacheSizeDoesNotIntOverflow() { + LruBitmapCache cache = new LruBitmapCacheTestOverride(); + + long undividedResult = 200L * 25L * 1024L * 1024L; + assertTrue(undividedResult > Integer.MAX_VALUE); + + long dividedResult = undividedResult / 100L; + assertTrue(dividedResult < Integer.MAX_VALUE); + + long calculatedResult = cache.calculateCacheSize(200, 25); + assertEquals(dividedResult, calculatedResult); + } + + private class LruBitmapCacheTestOverride extends LruBitmapCache { + + } +}