-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a clearMemory method to ImageManager and BitmapPool
- Loading branch information
Showing
6 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
library/tests/src/com/bumptech/glide/ImageManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.bumptech.glide; | ||
|
||
import android.graphics.Bitmap; | ||
import android.test.AndroidTestCase; | ||
import com.bumptech.glide.resize.ImageManager; | ||
import com.bumptech.glide.resize.bitmap_recycle.BitmapPool; | ||
import com.bumptech.glide.resize.bitmap_recycle.BitmapPoolAdapter; | ||
import com.bumptech.glide.resize.cache.MemoryCache; | ||
import com.bumptech.glide.resize.cache.MemoryCacheAdapter; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class ImageManagerTest extends AndroidTestCase { | ||
|
||
public void testClearMemory() { | ||
final AtomicInteger clearsCalled = new AtomicInteger(); | ||
// The pool's clear method must be called after the cache's so that the bitmaps from the cache do not just | ||
// refill the pool | ||
BitmapPool bitmapPool = new BitmapPoolAdapter() { | ||
@Override | ||
public void clearMemory() { | ||
assertEquals(2, clearsCalled.incrementAndGet()); | ||
} | ||
}; | ||
MemoryCache memoryCache = new MemoryCacheAdapter() { | ||
@Override | ||
public void clearMemory() { | ||
super.clearMemory(); | ||
assertEquals(1, clearsCalled.incrementAndGet()); | ||
} | ||
}; | ||
|
||
ImageManager im = new ImageManager.Builder(getContext()) | ||
.setBitmapPool(bitmapPool) | ||
.setMemoryCache(memoryCache) | ||
.build(); | ||
|
||
im.clearMemory(); | ||
|
||
assertEquals(2, clearsCalled.get()); | ||
} | ||
} | ||
|
59 changes: 59 additions & 0 deletions
59
library/tests/src/com/bumptech/glide/LruBitmapPoolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.bumptech.glide; | ||
|
||
import android.graphics.Bitmap; | ||
import android.test.AndroidTestCase; | ||
import com.bumptech.glide.resize.bitmap_recycle.LruBitmapPool; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LruBitmapPoolTest extends AndroidTestCase { | ||
private static final int SIZE = 1024 * 1024; | ||
private LruBitmapPool pool; | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
pool = new LruBitmapPool(SIZE); | ||
} | ||
|
||
public void testClearMemoryRemovesAllBitmaps() { | ||
List<Bitmap> bitmaps = fillPool(); | ||
assertTrue(bitmaps.size() >= 2); | ||
|
||
Bitmap first = bitmaps.get(0); | ||
assertNotNull(pool.get(first.getWidth(), first.getHeight(), first.getConfig())); | ||
pool.clearMemory(); | ||
assertNull(pool.get(first.getWidth(), first.getHeight(), first.getConfig())); | ||
} | ||
|
||
public void testClearMemoryCallsRecycleOnRemovedBitmaps() { | ||
List<Bitmap> bitmaps = fillPool(); | ||
pool.clearMemory(); | ||
for (Bitmap bitmap : bitmaps) { | ||
assertTrue(bitmap.isRecycled()); | ||
} | ||
} | ||
|
||
public List<Bitmap> fillPool() { | ||
List<Bitmap> bitmaps = new ArrayList<Bitmap>(); | ||
Bitmap toPut = getBitmap(); | ||
int bitmapSize = getSize(toPut); | ||
for (int i = 0; i < (SIZE / bitmapSize); i++) { | ||
bitmaps.add(Bitmap.createBitmap(toPut)); | ||
} | ||
for (Bitmap bitmap : bitmaps) { | ||
pool.put(bitmap); | ||
} | ||
assertTrue(bitmaps.size() > 0); | ||
return bitmaps; | ||
} | ||
|
||
private static int getSize(Bitmap bitmap) { | ||
return bitmap.getRowBytes() * bitmap.getHeight(); | ||
} | ||
|
||
private static Bitmap getBitmap() { | ||
return Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); | ||
} | ||
} |