1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
package com.bumptech.glide.resize.bitmap_recycle;
import android.graphics.Bitmap;
import android.test.AndroidTestCase;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE;
public class LruBitmapPoolTest extends AndroidTestCase {
private static final int MAX_SIZE = 10;
private MockStrategy strategy;
private LruBitmapPool pool;
@Override
protected void setUp() throws Exception {
strategy = new MockStrategy();
pool = new LruBitmapPool(MAX_SIZE, strategy);
}
public void testICanAddAndGetABitmap() {
fillPool(pool, 1);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
pool.put(bitmap);
assertNotNull(pool.get(100, 100, Bitmap.Config.ARGB_8888));
}
public void testImmutableBitmapsAreNotAdded() {
pool.put(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, false));
assertEquals(0, strategy.bitmaps.size());
}
public void testItIsSizeLimited() {
fillPool(pool, MAX_SIZE + 2);
assertEquals(2, strategy.numRemoves);
}
public void testBitmapLargerThanPoolIsNotAdded() {
strategy = new MockStrategy() {
@Override
public int getSize(Bitmap bitmap) {
return 4;
}
};
pool = new LruBitmapPool(3, strategy);
pool.put(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888));
assertEquals(0, strategy.numRemoves);
assertEquals(0, strategy.numPuts);
}
public void testClearMemoryRemovesAllBitmaps() {
fillPool(pool, MAX_SIZE);
pool.clearMemory();
assertEquals(MAX_SIZE, strategy.numRemoves);
}
public void testEvictedBitmapsAreRecycled() {
fillPool(pool, MAX_SIZE);
List<Bitmap> bitmaps = new ArrayList<Bitmap>(MAX_SIZE);
for (Bitmap b : strategy.bitmaps) {
bitmaps.add(b);
}
pool.clearMemory();
for (Bitmap b : bitmaps) {
assertTrue(b.isRecycled());
}
}
public void testTrimMemoryBackgroundOrLessRemovesHalfOfBitmaps() {
testTrimMemory(MAX_SIZE, TRIM_MEMORY_BACKGROUND, MAX_SIZE / 2);
}
public void testTrimMemoryBackgroundOrLessRemovesNoBitmapsIfPoolLessThanHalfFull() {
testTrimMemory(MAX_SIZE / 2, TRIM_MEMORY_BACKGROUND, 0);
}
public void testTrimMemoryModerateOrGreaterRemovesAllBitmaps() {
for (int trimLevel : new int[] { TRIM_MEMORY_MODERATE, TRIM_MEMORY_COMPLETE }) {
testTrimMemory(MAX_SIZE, trimLevel, MAX_SIZE);
}
}
private void testTrimMemory(int fillSize, int trimLevel, int expectedSize) {
MockStrategy strategy = new MockStrategy();
LruBitmapPool pool = new LruBitmapPool(MAX_SIZE, strategy);
fillPool(pool, fillSize);
pool.trimMemory(trimLevel);
assertEquals("Failed level=" + trimLevel, expectedSize, strategy.numRemoves);
}
private void fillPool(LruBitmapPool pool, int fillCount) {
for (int i = 0; i < fillCount; i++) {
pool.put(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888));
}
}
private static final int getSize(Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
private static class MockStrategy implements LruPoolStrategy {
private LinkedList<Bitmap> bitmaps = new LinkedList<Bitmap>();
private int numRemoves;
private int numPuts;
@Override
public void put(Bitmap bitmap) {
numPuts++;
bitmaps.add(bitmap);
}
@Override
public Bitmap get(int width, int height, Bitmap.Config config) {
return bitmaps.removeLast();
}
@Override
public Bitmap removeLast() {
numRemoves++;
return bitmaps.removeLast();
}
@Override
public String logBitmap(Bitmap bitmap) {
return null;
}
@Override
public String logBitmap(int width, int height, Bitmap.Config config) {
return null;
}
@Override
public int getSize(Bitmap bitmap) {
return 1;
}
}
}
|