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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
package com.bumptech.glide.load.engine.bitmap_recycle;
import android.graphics.Bitmap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowBitmap;
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;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class LruBitmapPoolTest {
private static final int MAX_SIZE = 10;
private MockStrategy strategy;
private LruBitmapPool pool;
@Before
public void setUp() throws Exception {
strategy = new MockStrategy();
pool = new LruBitmapPool(MAX_SIZE, strategy);
}
@Test
public void testICanAddAndGetABitmap() {
fillPool(pool, 1);
pool.put(createMutableBitmap());
assertNotNull(pool.get(100, 100, Bitmap.Config.ARGB_8888));
}
@Test
public void testImmutableBitmapsAreNotAdded() {
Bitmap bitmap = createMutableBitmap();
Robolectric.shadowOf(bitmap).setMutable(false);
pool.put(bitmap);
assertThat(strategy.bitmaps, empty());
}
@Test
public void testItIsSizeLimited() {
fillPool(pool, MAX_SIZE + 2);
assertEquals(2, strategy.numRemoves);
}
@Test
public void testBitmapLargerThanPoolIsNotAdded() {
strategy = new MockStrategy() {
@Override
public int getSize(Bitmap bitmap) {
return 4;
}
};
pool = new LruBitmapPool(3, strategy);
pool.put(createMutableBitmap());
assertEquals(0, strategy.numRemoves);
assertEquals(0, strategy.numPuts);
}
@Test
public void testClearMemoryRemovesAllBitmaps() {
fillPool(pool, MAX_SIZE);
pool.clearMemory();
assertEquals(MAX_SIZE, strategy.numRemoves);
}
@Test
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());
}
}
@Test
public void testTrimMemoryBackgroundOrLessRemovesHalfOfBitmaps() {
testTrimMemory(MAX_SIZE, TRIM_MEMORY_BACKGROUND, MAX_SIZE / 2);
}
@Test
public void testTrimMemoryBackgroundOrLessRemovesNoBitmapsIfPoolLessThanHalfFull() {
testTrimMemory(MAX_SIZE / 2, TRIM_MEMORY_BACKGROUND, 0);
}
@Test
public void testTrimMemoryModerateOrGreaterRemovesAllBitmaps() {
for (int trimLevel : new int[] { TRIM_MEMORY_MODERATE, TRIM_MEMORY_COMPLETE }) {
testTrimMemory(MAX_SIZE, trimLevel, MAX_SIZE);
}
}
@Test
public void testPassesArgb888ToStrategyAsConfigForRequestsWithNullConfigsOnGet() {
LruPoolStrategy strategy = mock(LruPoolStrategy.class);
LruBitmapPool pool = new LruBitmapPool(100, strategy);
Bitmap expected = createMutableBitmap();
when(strategy.get(anyInt(), anyInt(), eq(Bitmap.Config.ARGB_8888))).thenReturn(expected);
Bitmap result = pool.get(100, 100, null);
assertEquals(expected, result);
}
@Test
public void testPassesArgb8888ToStrategyAsConfigForRequestsWithNullConfigsOnGetDirty() {
LruPoolStrategy strategy = mock(LruPoolStrategy.class);
LruBitmapPool pool = new LruBitmapPool(100, strategy);
Bitmap expected = createMutableBitmap();
when(strategy.get(anyInt(), anyInt(), eq(Bitmap.Config.ARGB_8888))).thenReturn(expected);
Bitmap result = pool.getDirty(100, 100, null);
assertEquals(expected, result);
}
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);
}
@Test
public void testCanIncreaseSizeDynamically() {
int sizeMultiplier = 2;
pool.setSizeMultiplier(2);
fillPool(pool, MAX_SIZE * sizeMultiplier);
assertEquals(0, strategy.numRemoves);
}
@Test
public void testCanDecreaseSizeDynamically() {
fillPool(pool, MAX_SIZE);
assertEquals(0, strategy.numRemoves);
float sizeMultiplier = 0.5f;
pool.setSizeMultiplier(sizeMultiplier);
assertEquals(Math.round(MAX_SIZE * sizeMultiplier), strategy.numRemoves);
}
@Test
public void testCanResetSizeDynamically() {
int sizeMultiplier = 2;
pool.setSizeMultiplier(sizeMultiplier);
fillPool(pool, MAX_SIZE * sizeMultiplier);
pool.setSizeMultiplier(1);
assertEquals(Math.round(MAX_SIZE * sizeMultiplier) - MAX_SIZE, strategy.numRemoves);
}
@Test
public void testCanGetCurrentMaxSize() {
assertEquals(MAX_SIZE, pool.getMaxSize());
}
@Test
public void testMaxSizeChangesAfterSizeMultiplier() {
pool.setSizeMultiplier(2);
assertEquals(2 * MAX_SIZE, pool.getMaxSize());
}
private void fillPool(LruBitmapPool pool, int fillCount) {
for (int i = 0; i < fillCount; i++) {
pool.put(createMutableBitmap());
}
}
private Bitmap createMutableBitmap() {
Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Robolectric.shadowOf(bitmap).setMutable(true);
return bitmap;
}
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;
}
}
}
|