aboutsummaryrefslogtreecommitdiff
path: root/library/src/test/java/com/bumptech/glide/GlideTest.java
blob: 878b43764e52ceed45534514be3fcda295bd4dd3 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
package com.bumptech.glide;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.load.Encoder;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.load.resource.bytes.BytesResource;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.request.GlideAnimation;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.tests.GlideShadowLooper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import static junit.framework.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Tests for the {@link Glide} interface and singleton.
 */
@RunWith(RobolectricTestRunner.class)
@Config(shadows = { GlideTest.ShadowFileDescriptorContentResolver.class, GlideTest.ShadowMediaMetadataRetriever.class,
        GlideShadowLooper.class })
public class GlideTest {
    private Target target = null;
    private ImageView imageView;

    @Before
    public void setUp() throws Exception {
        Glide.tearDown();
        // Ensure that target's size ready callback will be called synchronously.
        target = mock(Target.class);
        imageView = new ImageView(Robolectric.application);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
        doAnswer(new CallCallback()).when(target).getSize(any(Target.SizeReadyCallback.class));

        Handler bgHandler = mock(Handler.class);
        when(bgHandler.post(any(Runnable.class))).thenAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                Runnable runnable = (Runnable) invocation.getArguments()[0];
                runnable.run();
                return true;
            }
        });

        // Run all tasks on the main thread so they complete synchronously.
        ExecutorService service = mock(ExecutorService.class);
        when(service.submit(any(Runnable.class))).thenAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                Runnable runnable = (Runnable) invocation.getArguments()[0];
                runnable.run();
                return mock(Future.class);
            }
        });

        Glide.setup(new GlideBuilder(Robolectric.application)
                .setMemoryCache(mock(MemoryCache.class))
                .setDiskCache(mock(DiskCache.class))
                .setResizeService(service)
                .setDiskCacheService(service));
        DataFetcher<InputStream> mockStreamFetcher = mock(DataFetcher.class);
        when(mockStreamFetcher.getId()).thenReturn("fakeId");
        when(mockStreamFetcher.loadData(any(Priority.class))).thenReturn(new ByteArrayInputStream(new byte[0]));
        ModelLoader<GlideUrl, InputStream> mockUrlLoader = mock(ModelLoader.class);
        when(mockUrlLoader.getResourceFetcher(any(GlideUrl.class), anyInt(), anyInt())).thenReturn(mockStreamFetcher);
        ModelLoaderFactory<GlideUrl, InputStream> mockUrlLoaderFactory = mock(ModelLoaderFactory.class);
        when(mockUrlLoaderFactory.build(any(Context.class), any(GenericLoaderFactory.class)))
                .thenReturn(mockUrlLoader);

        Glide.get(getContext()).register(GlideUrl.class, InputStream.class, mockUrlLoaderFactory);
    }

    @After
    public void tearDown() {
        Glide.tearDown();
    }

    @Test
    public void testCanSetMemoryCategory() {
        MemoryCache memoryCache = mock(MemoryCache.class);
        BitmapPool bitmapPool = mock(BitmapPool.class);

        MemoryCategory memoryCategory = MemoryCategory.NORMAL;
        Glide glide = new GlideBuilder(getContext())
                .setMemoryCache(memoryCache)
                .setBitmapPool(bitmapPool)
                .createGlide();
        glide.setMemoryCategory(memoryCategory);

        verify(memoryCache).setSizeMultiplier(eq(memoryCategory.getMultiplier()));
        verify(bitmapPool).setSizeMultiplier(eq(memoryCategory.getMultiplier()));
    }

    @Test
    public void testClearMemory() {
        BitmapPool bitmapPool = mock(BitmapPool.class);
        MemoryCache memoryCache = mock(MemoryCache.class);

        Glide glide = new GlideBuilder(getContext())
                .setBitmapPool(bitmapPool)
                .setMemoryCache(memoryCache)
                .createGlide();

        glide.clearMemory();

        verify(bitmapPool).clearMemory();
        verify(memoryCache).clearMemory();
    }

    @Test
    public void testTrimMemory() {
        BitmapPool bitmapPool = mock(BitmapPool.class);
        MemoryCache memoryCache = mock(MemoryCache.class);

        Glide glide = new GlideBuilder(getContext())
                .setBitmapPool(bitmapPool)
                .setMemoryCache(memoryCache)
                .createGlide();

        final int level = 123;

        glide.trimMemory(level);

        verify(bitmapPool).trimMemory(eq(level));
        verify(memoryCache).trimMemory(eq(level));
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testGenericLoader() throws Exception {
        File expected = new File("test");

        Target<File> target = mock(Target.class);
        doAnswer(new CallCallback()).when(target).getSize(any(Target.SizeReadyCallback.class));

        GlideUrl glideUrl =  mock(GlideUrl.class);
        DataFetcher<File> dataFetcher = mock(DataFetcher.class);
        when(dataFetcher.loadData(any(Priority.class))).thenReturn(expected);
        when(dataFetcher.getId()).thenReturn("id");
        ModelLoader<GlideUrl, File> modelLoader = mock(ModelLoader.class);
        when(modelLoader.getResourceFetcher(eq(glideUrl), anyInt(), anyInt()))
                .thenReturn(dataFetcher);

        Resource<File> expectedResource = mock(Resource.class);
        when(expectedResource.get()).thenReturn(expected);
        ResourceDecoder<File, File> sourceDecoder = mock(ResourceDecoder.class);
        when(sourceDecoder.decode(eq(expected), anyInt(), anyInt())).thenReturn(expectedResource);
        when(sourceDecoder.getId()).thenReturn("sourceDecoderId");
        ResourceDecoder<File, File> cacheDecoder = mock(ResourceDecoder.class);
        when(cacheDecoder.getId()).thenReturn("cacheDecoderId");
        ResourceEncoder<File> encoder = mock(ResourceEncoder.class);
        when(encoder.getId()).thenReturn("encoderId");
        Encoder<File> sourceEncoder = mock(Encoder.class);
        when(sourceEncoder.getId()).thenReturn("sourceEncoderId");

        Glide.with(getContext())
                .using(modelLoader, File.class)
                .load(glideUrl)
                .as(File.class)
                .decoder(sourceDecoder)
                .cacheDecoder(cacheDecoder)
                .encoder(encoder)
                .sourceEncoder(sourceEncoder)
                .into(target);

        verify(target).onResourceReady(eq(expected), any(GlideAnimation.class));
    }

    @Test
    public void testFileDefaultLoaderWithInputStream() throws Exception {
        registerFailFactory(File.class, ParcelFileDescriptor.class);
        runTestFileDefaultLoader();
    }

    @Test
    public void testFileDefaultLoaderWithFileDescriptor() throws Exception {
        registerFailFactory(File.class, InputStream.class);
        runTestFileDefaultLoader();
    }

    @Test
    public void testFileDefaultLoader() {
        runTestFileDefaultLoader();
    }

    private void runTestFileDefaultLoader() {
        File file = new File("fake");
        mockUri(Uri.fromFile(file));

        Glide.with(getContext()).load(file).into(target);
        Glide.with(getContext()).load(file).into(imageView);

        verify(target).onResourceReady(any(Resource.class), any(GlideAnimation.class));
        verify(target).setRequest((Request) notNull());

        assertNotNull(imageView.getDrawable());
    }

    @Test
    public void testUrlDefaultLoader() throws MalformedURLException {
        URL url = new URL("http://www.google.com");

        Glide.with(getContext()).loadFromImage(url).into(target);
        Glide.with(getContext()).loadFromImage(url).into(imageView);

        verify(target).onResourceReady(any(Resource.class), any(GlideAnimation.class));
        verify(target).setRequest((Request) notNull());

        assertNotNull(imageView.getDrawable());
    }

    @Test
    public void testAsBitmapOption() {
        Uri uri = Uri.parse("content://something/else");
        mockUri(uri);

        Glide.with(getContext()).load(uri).asBitmap().into(target);

        verify(target).onResourceReady(any(Bitmap.class), any(GlideAnimation.class));
    }

    @Test
    public void testTranscodeOption() {
        Uri uri = Uri.parse("content://something/else");
        mockUri(uri);
        final byte[] bytes = new byte[0];

        ResourceTranscoder<Bitmap, byte[]> transcoder = mock(ResourceTranscoder.class);
        when(transcoder.getId()).thenReturn("bytes");
        when(transcoder.transcode(any(Resource.class))).thenReturn(new BytesResource(bytes));

        Glide.with(getContext())
                .load(uri)
                .asBitmap()
                .transcode(transcoder, byte[].class)
                .into(target);

        verify(target).onResourceReady(eq(bytes), any(GlideAnimation.class));
    }

    @Test
    public void testToBytesOption() {
        Uri uri = Uri.parse("content://something/else");
        mockUri(uri);

        Glide.with(getContext()).load(uri).asBitmap().toBytes().into(target);

        verify(target).onResourceReady(any(byte[].class), any(GlideAnimation.class));
    }

    @Test
    public void testUriDefaultLoaderWithInputStream() throws Exception {
        registerFailFactory(Uri.class, ParcelFileDescriptor.class);
        runTestUriDefaultLoader();
    }

    @Test
    public void testUriDefaultLoaderWithFileDescriptor() throws Exception {
        registerFailFactory(Uri.class, InputStream.class);
        runTestUriDefaultLoader();
    }

    @Test
    public void testUriDefaultLoader() {
        runTestUriDefaultLoader();
    }

    private void runTestUriDefaultLoader() {
        Uri uri = Uri.parse("content://test/something");
        mockUri(uri);

        Glide.with(getContext()).load(uri).into(target);
        Glide.with(getContext()).load(uri).into(imageView);

        verify(target).onResourceReady(anyObject(), any(GlideAnimation.class));
        verify(target).setRequest((Request) notNull());

        assertNotNull(imageView.getDrawable());
    }

    @Test
    public void testStringDefaultLoaderWithUrl() {
        runTestStringDefaultLoader("http://www.google.com");
    }

    @Test
    public void testFileStringDefaultLoaderWithInputStream() throws Exception {
        registerFailFactory(String.class, ParcelFileDescriptor.class);
        runTestFileStringDefaultLoader();
    }

    @Test
    public void testFileStringDefaultLoaderWithFileDescriptor() throws Exception {
        registerFailFactory(String.class, ParcelFileDescriptor.class);
        runTestFileStringDefaultLoader();
    }

    @Test
    public void testFileStringDefaultLoader() {
        runTestFileStringDefaultLoader();
    }

    private void runTestFileStringDefaultLoader() {
        String path = "/some/random/path";
        mockUri(Uri.fromFile(new File(path)));
        runTestStringDefaultLoader(path);
    }

    @Test
    public void testUriStringDefaultLoaderWithInputStream() throws Exception {
        registerFailFactory(String.class, ParcelFileDescriptor.class);
        runTestUriStringDefaultLoader();
    }

    @Test
    public void testUriStringDefaultLoaderWithFileDescriptor() throws Exception {
        registerFailFactory(String.class, InputStream.class);
        runTestUriStringDefaultLoader();
    }

    @Test
    public void testUriStringDefaultLoader() {
        runTestUriStringDefaultLoader();
    }

    private void runTestUriStringDefaultLoader() {
        String stringUri = "content://some/random/uri";
        mockUri(Uri.parse(stringUri));
        runTestStringDefaultLoader(stringUri);
    }

    private void runTestStringDefaultLoader(String string) {
        Glide.with(getContext())
                .load(string)
                .listener(new RequestListener<String, Drawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target target, boolean isFirstImage) {
                        if (!(e instanceof IOException)) {
                            throw new RuntimeException(e);
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, String model, Target target,
                            boolean isFromMemoryCache,
                            boolean isFirstResource) {
                        return false;
                    }
                })
                .into(target);
        Glide.with(getContext()).load(string).into(imageView);

        verify(target).onResourceReady(any(Resource.class), any(GlideAnimation.class));
        verify(target).setRequest((Request) notNull());

        assertNotNull(imageView.getDrawable());
    }

    @Test
    public void testIntegerDefaultLoaderWithInputStream() throws Exception {
        registerFailFactory(Integer.class, ParcelFileDescriptor.class);
        runTestIntegerDefaultLoader();
    }

    @Test
    public void testIntegerDefaultLoaderWithFileDescriptor() throws Exception {
        registerFailFactory(Integer.class, InputStream.class);
        runTestIntegerDefaultLoader();
    }

    @Test
    public void testIntegerDefaultLoader() {
        runTestIntegerDefaultLoader();
    }

    private void runTestIntegerDefaultLoader() {
        int integer = 1234;
        mockUri("android.resource://" + getContext().getPackageName() + "/" + integer);

        Glide.with(getContext()).load(integer).into(target);
        Glide.with(getContext()).load(integer).into(imageView);

        verify(target).onResourceReady(any(Resource.class), any(GlideAnimation.class));
        verify(target).setRequest((Request) notNull());

        assertNotNull(imageView.getDrawable());
    }

    @Test
    public void testByteArrayDefaultLoader() {
        byte[] bytes = new byte[10];
        Glide.with(getContext()).loadFromImage(bytes).into(target);
        Glide.with(getContext()).loadFromImage(bytes).into(imageView);

        verify(target).onResourceReady(any(Resource.class), any(GlideAnimation.class));
        verify(target).setRequest((Request) notNull());

        assertNotNull(imageView.getDrawable());
    }

    @Test
    public void testByteArrayWithIdDefaultLoader() {
        byte[] bytes = new byte[10];
        String id = "test";

        Glide.with(getContext()).loadFromImage(bytes, id).into(target);
        Glide.with(getContext()).loadFromImage(bytes, id).into(imageView);

        verify(target).onResourceReady(any(Resource.class), any(GlideAnimation.class));
        verify(target).setRequest((Request) notNull());

        assertNotNull(imageView.getDrawable());
    }

    @Test(expected = Exception.class)
    public void testUnregisteredModelThrowsException() {
        Float unregistered = 0.5f;
        Glide.with(getContext()).load(unregistered).into(target);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testUnregisteredModelWithGivenLoaderDoesNotThrow() {
        Float unregistered = 0.5f;
        StreamModelLoader<Float> mockLoader = mockStreamModelLoader(Float.class);
        Glide.with(getContext())
                .using(mockLoader)
                .load(unregistered)
                .into(target);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testNonDefaultModelWithRegisteredFactoryDoesNotThrow() {
        registerMockStreamModelLoader(Float.class);

        Glide.with(getContext()).load(0.5f).into(target);
    }

    @Test
    public void testReceivesGif() {
        String fakeUri = "content://fake";
        mockUri(fakeUri);
        Glide.with(getContext())
                .load(fakeUri)
                .asGif()
                .into(target);
        verify(target).onResourceReady(any(GifDrawable.class), any(GlideAnimation.class));
    }

    @Test
    public void testReceivesGifBytes() {
        String fakeUri = "content://fake";
        mockUri(fakeUri);
        Glide.with(getContext())
                .load(fakeUri)
                .asGif()
                .toBytes()
                .into(target);

        verify(target).onResourceReady(any(byte[].class), any(GlideAnimation.class));
    }

    @Test
    public void testReceivesBitmapBytes() {
        String fakeUri = "content://fake";
        mockUri(fakeUri);
        Glide.with(getContext())
                .load(fakeUri)
                .asBitmap()
                .toBytes()
                .into(target);

        verify(target).onResourceReady(any(byte[].class), any(GlideAnimation.class));
    }

    @Test
    public void testReceivesTranscodedData() {
        String fakeUri = "content://fake";
        mockUri(fakeUri);
        final Bitmap expected = Bitmap.createBitmap(1234, 6432, Bitmap.Config.ALPHA_8);
        Glide.with(getContext())
                .load(fakeUri)
                .asBitmap()
                .transcode(new ResourceTranscoder<Bitmap, Bitmap>() {
                    @Override
                    public Resource<Bitmap> transcode(Resource<Bitmap> toTranscode) {
                        return new BitmapResource(expected, mock(BitmapPool.class));
                    }

                    @Override
                    public String getId() {
                        return "id";
                    }
                }, Bitmap.class)
                .into(target);

        verify(target).onResourceReady(eq(expected), any(GlideAnimation.class));
    }

    @Test
    public void testNullModelInGenericImageLoadDoesNotThrow() {
        Glide.with(getContext()).loadFromImage((Double) null).into(target);
    }

    @Test
    public void testNullModelInGenericVideoLoadDoesNotThrow() {
        Glide.with(getContext()).loadFromVideo((Float) null).into(target);
    }

    @Test
    public void testNullModelInGenericLoadDoesNotThrow() {
        Glide.with(getContext()).load((Double) null).into(target);
    }

    @Test
    public void testNullModelDoesNotThrow() {
        String nullString = null;

        Drawable drawable = new ColorDrawable(Color.RED);
        Glide.with(getContext())
                .load(nullString)
                .placeholder(drawable)
                .into(target);

        verify(target).setPlaceholder(eq(drawable));
    }

    @Test
    public void testNullModelPrefersErrorDrawable() {
        String nullString = null;

        Drawable placeholder = new ColorDrawable(Color.GREEN);
        Drawable error = new ColorDrawable(Color.RED);

        Glide.with(getContext())
                .load(nullString)
                .placeholder(placeholder)
                .error(error)
                .into(target);

        verify(target).setPlaceholder(eq(error));
    }

    @Test
    public void testNullModelWithModelLoaderDoesNotThrow() {
        String nullString = null;
        Drawable drawable = new ColorDrawable(Color.RED);
        StreamModelLoader<String> modelLoader = mock(StreamModelLoader.class);
        Glide.with(getContext())
                .using(modelLoader)
                .load(nullString)
                .placeholder(drawable)
                .into(target);

        verify(target).setPlaceholder(eq(drawable));
    }

    private void mockUri(String uriString) {
        mockUri(Uri.parse(uriString));
    }

    @SuppressWarnings("unchecked")
    private <T, Z> void registerFailFactory(Class<T> failModel, Class<Z> failResource) throws Exception {
        DataFetcher<Z> failFetcher = mock(DataFetcher.class);
        when(failFetcher.loadData(any(Priority.class))).thenThrow(new IOException("test"));
        when(failFetcher.getId()).thenReturn("fakeId");
        ModelLoader<T, Z> failLoader = mock(ModelLoader.class);
        when(failLoader.getResourceFetcher(any(failModel), anyInt(), anyInt())).thenReturn(failFetcher);
        ModelLoaderFactory<T, Z> failFactory = mock(ModelLoaderFactory.class);
        when(failFactory.build(any(Context.class), any(GenericLoaderFactory.class))).thenReturn(failLoader);

        Glide.get(getContext()).register(failModel, failResource, failFactory);
    }


    private void mockUri(Uri uri) {
        ContentResolver contentResolver = Robolectric.application.getContentResolver();
        ShadowFileDescriptorContentResolver shadowContentResolver = Robolectric.shadowOf_(contentResolver);
        shadowContentResolver.registerInputStream(uri, new ByteArrayInputStream(new byte[0]));

        AssetFileDescriptor assetFileDescriptor = mock(AssetFileDescriptor.class);
        ParcelFileDescriptor parcelFileDescriptor = mock(ParcelFileDescriptor.class);
        when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(parcelFileDescriptor);

        shadowContentResolver.registerAssetFileDescriptor(uri, assetFileDescriptor);
    }

    private Context getContext() {
        return Robolectric.application;
    }

    @SuppressWarnings("unchecked")
    private <T> void registerMockStreamModelLoader(final Class<T> modelClass) {
        StreamModelLoader<T> modelLoader = mockStreamModelLoader(modelClass);
        ModelLoaderFactory<T, InputStream> modelLoaderFactory = mock(ModelLoaderFactory.class);
        when(modelLoaderFactory.build(any(Context.class), any(GenericLoaderFactory.class)))
                .thenReturn(modelLoader);

        Glide.get(Robolectric.application).register(modelClass, InputStream.class, modelLoaderFactory);
    }

    @SuppressWarnings("unchecked")
    private <T> StreamModelLoader<T> mockStreamModelLoader(final Class<T> modelClass) {
        StreamModelLoader<T> modelLoader = mock(StreamModelLoader.class);
        DataFetcher<InputStream> fetcher = mock(DataFetcher.class);
        try {
            when(fetcher.loadData(any(Priority.class))).thenReturn(new ByteArrayInputStream(new byte[0]));
        } catch (Exception e) { }
        when(fetcher.getId()).thenReturn(UUID.randomUUID().toString());
        when(modelLoader.getResourceFetcher(any(modelClass), anyInt(), anyInt()))
                .thenReturn(fetcher);

        return modelLoader;
    }

    private static class CallCallback implements Answer<Void> {
        private int width;
        private int height;

        public CallCallback() {
            this(100, 100);
        }

        public CallCallback(int width, int height) {
            this.width = width;
            this.height = height;
        }

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Target.SizeReadyCallback cb = (Target.SizeReadyCallback) invocation.getArguments()[0];
            cb.onSizeReady(width, height);
            return null;
        }
    }

    // TODO: Extending ShadowContentResolver results in exceptions because of some state issues where we seem to get
    // one content resolver shadow in one part of the test and a different one in a different part of the test. Each
    // one ends up with different registered uris, which causes tests to fail. We shouldn't need to do this, but
    // using static maps seems to fix the issue.
    @Implements(value = ContentResolver.class, resetStaticState = true)
    public static class ShadowFileDescriptorContentResolver {
        private static final Map<Uri, AssetFileDescriptor> uriToFileDescriptors = new HashMap<Uri, AssetFileDescriptor>();
        private static final Map<Uri, InputStream> uriToInputStreams = new HashMap<Uri, InputStream>();

        public void registerInputStream(Uri uri, InputStream inputStream) {
            uriToInputStreams.put(uri, inputStream);
        }

        public void registerAssetFileDescriptor(Uri uri, AssetFileDescriptor assetFileDescriptor) {
            uriToFileDescriptors.put(uri, assetFileDescriptor);
        }

        @Implementation
        public InputStream openInputStream(Uri uri) {
            if (!uriToInputStreams.containsKey(uri)) {
                throw new IllegalArgumentException("You must first register an InputStream for uri: " + uri);
            }
            return uriToInputStreams.get(uri);
        }

        @Implementation
        public AssetFileDescriptor openAssetFileDescriptor(Uri uri, String type) {
            if (!uriToFileDescriptors.containsKey(uri)) {
                throw new IllegalArgumentException("You must first register an AssetFileDescriptor for uri: " + uri);
            }
            return uriToFileDescriptors.get(uri);
        }

        public static void reset() {
            uriToInputStreams.clear();
            uriToFileDescriptors.clear();
        }
    }

    @Implements(MediaMetadataRetriever.class)
    public static class ShadowMediaMetadataRetriever {

        @Implementation
        @SuppressWarnings("unused")
        public Bitmap getFrameAtTime() {
            Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
            Robolectric.shadowOf(bitmap).appendDescription(" from MediaMetadataRetriever");
            return bitmap;
        }
    }
}