23
23
import com .bumptech .glide .load .engine .bitmap_recycle .LruBitmapPool ;
24
24
import com .bumptech .glide .load .engine .cache .LruResourceCache ;
25
25
import com .bumptech .glide .load .engine .cache .MemoryCacheAdapter ;
26
+ import com .bumptech .glide .load .engine .executor .GlideExecutor ;
27
+ import com .bumptech .glide .load .engine .executor .MockGlideExecutor ;
26
28
import com .bumptech .glide .request .RequestListener ;
27
29
import com .bumptech .glide .request .target .Target ;
28
30
import com .bumptech .glide .test .ConcurrencyHelper ;
@@ -45,18 +47,31 @@ public class LoadBitmapTest {
45
47
46
48
private final ConcurrencyHelper concurrency = new ConcurrencyHelper ();
47
49
private Context context ;
50
+ private GlideBuilder glideBuilder ;
48
51
49
52
@ Before
50
53
public void setUp () {
51
54
MockitoAnnotations .initMocks (this );
52
55
context = InstrumentationRegistry .getTargetContext ();
56
+
57
+ // Clearing the future here can race with clearing the EngineResource held on to by EngineJob
58
+ // while it's notifying callbacks. Forcing all executors to use the same thread avoids the race
59
+ // by making our clear and EngineJob's clear run on the same thread.
60
+ GlideExecutor mainThreadExecutor = MockGlideExecutor .newMainThreadExecutor ();
61
+ glideBuilder =
62
+ new GlideBuilder ()
63
+ .setSourceExecutor (mainThreadExecutor )
64
+ .setDiskCacheExecutor (mainThreadExecutor )
65
+ .setAnimationExecutor (mainThreadExecutor );
53
66
}
54
67
55
68
@ Test
56
69
public void clearFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecycleBitmap () {
57
- Glide .init (context , new GlideBuilder ()
58
- .setMemoryCache (new MemoryCacheAdapter ())
59
- .setBitmapPool (new BitmapPoolAdapter ()));
70
+ Glide .init (
71
+ context ,
72
+ new GlideBuilder ()
73
+ .setMemoryCache (new MemoryCacheAdapter ())
74
+ .setBitmapPool (new BitmapPoolAdapter ()));
60
75
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
61
76
Target <Drawable > target =
62
77
concurrency .wait (
@@ -74,9 +89,11 @@ public void clearFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecycleBi
74
89
75
90
@ Test
76
91
public void transformFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecycleBitmap () {
77
- Glide .init (context , new GlideBuilder ()
78
- .setMemoryCache (new MemoryCacheAdapter ())
79
- .setBitmapPool (new BitmapPoolAdapter ()));
92
+ Glide .init (
93
+ context ,
94
+ new GlideBuilder ()
95
+ .setMemoryCache (new MemoryCacheAdapter ())
96
+ .setBitmapPool (new BitmapPoolAdapter ()));
80
97
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
81
98
concurrency .wait (
82
99
GlideApp .with (context )
@@ -90,9 +107,11 @@ public void transformFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecyc
90
107
91
108
@ Test
92
109
public void clearFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap () {
93
- Glide .init (context , new GlideBuilder ()
94
- .setMemoryCache (new MemoryCacheAdapter ())
95
- .setBitmapPool (new BitmapPoolAdapter ()));
110
+ Glide .init (
111
+ context ,
112
+ new GlideBuilder ()
113
+ .setMemoryCache (new MemoryCacheAdapter ())
114
+ .setBitmapPool (new BitmapPoolAdapter ()));
96
115
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
97
116
Target <Drawable > target =
98
117
concurrency .wait (
@@ -109,9 +128,11 @@ public void clearFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap() {
109
128
110
129
@ Test
111
130
public void transformFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap () {
112
- Glide .init (context , new GlideBuilder ()
113
- .setMemoryCache (new MemoryCacheAdapter ())
114
- .setBitmapPool (new BitmapPoolAdapter ()));
131
+ Glide .init (
132
+ context ,
133
+ new GlideBuilder ()
134
+ .setMemoryCache (new MemoryCacheAdapter ())
135
+ .setBitmapPool (new BitmapPoolAdapter ()));
115
136
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
116
137
concurrency .wait (
117
138
GlideApp .with (context )
@@ -124,9 +145,11 @@ public void transformFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap()
124
145
125
146
@ Test
126
147
public void clearFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycleBitmap () {
127
- Glide .init (context , new GlideBuilder ()
128
- .setMemoryCache (new MemoryCacheAdapter ())
129
- .setBitmapPool (new BitmapPoolAdapter ()));
148
+ Glide .init (
149
+ context ,
150
+ new GlideBuilder ()
151
+ .setMemoryCache (new MemoryCacheAdapter ())
152
+ .setBitmapPool (new BitmapPoolAdapter ()));
130
153
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
131
154
Target <Bitmap > target =
132
155
concurrency .wait (
@@ -144,9 +167,11 @@ public void clearFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycleBitm
144
167
145
168
@ Test
146
169
public void transformFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycleBitmap () {
147
- Glide .init (context , new GlideBuilder ()
148
- .setMemoryCache (new MemoryCacheAdapter ())
149
- .setBitmapPool (new BitmapPoolAdapter ()));
170
+ Glide .init (
171
+ context ,
172
+ new GlideBuilder ()
173
+ .setMemoryCache (new MemoryCacheAdapter ())
174
+ .setBitmapPool (new BitmapPoolAdapter ()));
150
175
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
151
176
concurrency .wait (
152
177
GlideApp .with (context )
@@ -161,9 +186,11 @@ public void transformFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycle
161
186
@ Test
162
187
public void loadFromRequestManager_withBitmap_doesNotLoadFromDiskCache () {
163
188
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
164
- Glide .init (context , new GlideBuilder ()
165
- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
166
- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
189
+ Glide .init (
190
+ context ,
191
+ glideBuilder
192
+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
193
+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
167
194
Target <Drawable > target =
168
195
concurrency .wait (
169
196
GlideApp .with (context )
@@ -200,9 +227,11 @@ public void run() {
200
227
@ Test
201
228
public void loadFromRequestBuilder_asDrawable_withBitmap_doesNotLoadFromDiskCache () {
202
229
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
203
- Glide .init (context , new GlideBuilder ()
204
- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
205
- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
230
+ Glide .init (
231
+ context ,
232
+ glideBuilder
233
+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
234
+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
206
235
Target <Drawable > target =
207
236
concurrency .wait (
208
237
GlideApp .with (context )
@@ -240,9 +269,11 @@ public void run() {
240
269
@ Test
241
270
public void loadFromRequestBuilder_asDrawable_withBitmapAndStrategyBeforeLoad_notFromCache () {
242
271
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
243
- Glide .init (context , new GlideBuilder ()
244
- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
245
- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
272
+ Glide .init (
273
+ context ,
274
+ glideBuilder
275
+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
276
+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
246
277
Target <Drawable > target =
247
278
concurrency .wait (
248
279
GlideApp .with (context )
@@ -281,9 +312,11 @@ public void run() {
281
312
@ Test
282
313
public void loadFromRequestBuilder_asBitmap_withBitmap_doesNotLoadFromDiskCache () {
283
314
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
284
- Glide .init (context , new GlideBuilder ()
285
- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
286
- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
315
+ Glide .init (
316
+ context ,
317
+ glideBuilder
318
+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
319
+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
287
320
Target <Bitmap > target =
288
321
concurrency .wait (
289
322
GlideApp .with (context )
@@ -322,9 +355,12 @@ public void run() {
322
355
@ Test
323
356
public void loadFromRequestBuilder_asBitmap_withBitmapAndStrategyBeforeLoad_notFromCache () {
324
357
Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
325
- Glide .init (context , new GlideBuilder ()
326
- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
327
- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
358
+ Glide .init (
359
+ context ,
360
+ glideBuilder
361
+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
362
+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
363
+
328
364
Target <Bitmap > target =
329
365
concurrency .wait (
330
366
GlideApp .with (context )
0 commit comments