aboutsummaryrefslogtreecommitdiff
path: root/library/src/test/java/com/bumptech/glide/load/engine/SourceResourceRunnerTest.java
blob: 647e432b006bf47cffdaffb9a860553d1126a672 (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
package com.bumptech.glide.load.engine;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.CacheLoader;
import com.bumptech.glide.load.Encoder;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.request.ResourceCallback;

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.RobolectricTestRunner;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static org.junit.Assert.assertEquals;
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.isNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(RobolectricTestRunner.class)
public class SourceResourceRunnerTest {
    private SourceResourceHarness harness;

    @Before
    public void setUp() {
        harness = new SourceResourceHarness();
    }

    @Test
    public void testCacheLoaderIsCalledWithOriginalKey() {
        harness.getRunner().run();

        verify(harness.cacheLoader).load(eq(harness.originalKey), eq(harness.cacheDecoder), eq(harness.width),
                eq(harness.height));
    }

    @Test
    public void testIfCacheLoaderReturnsOriginalResourceFetcherIsNotCalled() throws Exception {
        when(harness.cacheLoader.load(eq(harness.originalKey), eq(harness.cacheDecoder), eq(harness.width),
                eq(harness.height))).thenReturn(harness.decoded);

        harness.getRunner().run();

        verify(harness.fetcher, never()).loadData(any(Priority.class));
    }

    @Test
    public void testIfCacheLoaderReturnsOriginalResourceThenOriginalResourceIsTransformedAndReturned() {
        when(harness.cacheLoader.load(eq(harness.originalKey), eq(harness.cacheDecoder), eq(harness.width),
                eq(harness.height))).thenReturn(harness.decoded);

        harness.getRunner().run();

        verify(harness.cb).onResourceReady(eq(harness.transcoded));
    }

    @Test
    public void testEncoderIsCalledWithTransformedIfOriginalResourceInCache() {
        final SourceResourceRunner<Object, Object, Object> runner = harness.getRunner();
        when(harness.cacheLoader.load(eq(harness.originalKey), eq(harness.cacheDecoder), eq(harness.width),
                eq(harness.height))).thenReturn(harness.decoded);
        when(harness.factory.build(eq(harness.encoder), eq(harness.transformed))).thenReturn(harness.writer);

        runner.run();

        verify(harness.diskCache).put(eq(harness.key), eq(harness.writer));
    }

    @Test
    public void testResourceFetcherIsCalledIfOriginalNotInCache() throws Exception {
        harness.getRunner().run();

        verify(harness.fetcher).loadData(eq(harness.priority));
    }

    @Test
    public void testDecoderIsCalledIfFetched() throws Exception {
        Object fetched = new Object();
        when(harness.fetcher.loadData(eq(harness.priority))).thenReturn(fetched);

        harness.getRunner().run();

        verify(harness.decoder).decode(eq(fetched), eq(harness.width), eq(harness.height));
    }

    @Test
    public void testCallbackIsCalledWithTranscodedResourceIfFetchedAndDecoded() throws Exception {
        InputStream is = new ByteArrayInputStream(new byte[0]);
        when(harness.fetcher.loadData(eq(harness.priority))).thenReturn(is);
        when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.decoded);
        when(harness.transcoder.transcode(harness.decoded)).thenReturn(harness.transcoded);

        harness.getRunner().run();

        verify(harness.cb).onResourceReady(eq(harness.transcoded));
    }

    @Test
    public void testResourceIsWrittenToCacheIfFetchedAndDecoded() throws Exception {
        SourceResourceRunner<Object, Object, Object> runner = harness.getRunner();
        InputStream is = new ByteArrayInputStream(new byte[0]);
        when(harness.fetcher.loadData(eq(harness.priority))).thenReturn(is);
        when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.decoded);
        when(harness.factory.build(eq(harness.encoder), eq(harness.transformed))).thenReturn(harness.writer);

        runner.run();

        verify(harness.diskCache).put(eq(harness.key), eq(harness.writer));
    }

    @Test
    public void testResourceIsTransformedBeforeBeingWrittenToCache() throws Exception {
        SourceResourceRunner<Object, Object, Object> runner = harness.getRunner();
        InputStream is = new ByteArrayInputStream(new byte[0]);
        when(harness.fetcher.loadData(eq(harness.priority))).thenReturn(is);
        when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.decoded);
        when(harness.transformation.transform(eq(harness.decoded), eq(harness.width), eq(harness.height)))
                .thenReturn(harness.transformed);
        when(harness.factory.build(eq(harness.encoder), eq(harness.transformed))).thenReturn(harness.writer);

        runner.run();

        verify(harness.diskCache).put(eq(harness.key), eq(harness.writer));
    }

    @Test
    public void testDecodedResourceIsRecycledIfTransformedResourceIsDifferent() throws Exception {
        InputStream is = new ByteArrayInputStream(new byte[0]);
        when(harness.fetcher.loadData(eq(harness.priority))).thenReturn(is);
        when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.decoded);
        Resource transformed = mock(Resource.class);
        when(harness.transformation.transform(eq(harness.decoded), eq(harness.width), eq(harness.height)))
                .thenReturn(transformed);

        harness.getRunner().run();

        verify(harness.decoded).recycle();
    }

    @Test
    public void testFetcherIsCleanedUp() {
        harness.getRunner().run();

        verify(harness.fetcher).cleanup();
    }

    @Test
    public void testFetcherIsCleanedUpIfDecodeThrows() throws Exception {
        when(harness.fetcher.loadData(any(Priority.class))).thenReturn(new Object());
        when(harness.decoder.decode(anyObject(), anyInt(), anyInt())).thenThrow(new IOException("test"));

        harness.getRunner().run();

        verify(harness.fetcher).cleanup();
    }

    @Test
    public void testFetcherIsCleanedUpIfFetcherThrows() throws Exception {
        when(harness.fetcher.loadData(any(Priority.class))).thenThrow(new IOException("test"));

        harness.getRunner().run();

        verify(harness.fetcher).cleanup();
    }

    @Test
    public void testDecodedResourceIsNotRecycledIfResourceIsNotTransformed() throws Exception {
        InputStream is = new ByteArrayInputStream(new byte[0]);
        when(harness.fetcher.loadData(eq(harness.priority))).thenReturn(is);
        when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.decoded);
        when(harness.transformation.transform(eq(harness.decoded), eq(harness.width), eq(harness.height)))
                .thenReturn(harness.decoded);

        harness.getRunner().run();

        verify(harness.decoded, never()).recycle();
    }

    @Test
    public void testCallbackIsCalledIfFetchFails() throws Exception {
        Exception expected = new Exception("Test");
        when(harness.fetcher.loadData(eq(harness.priority))).thenThrow(expected);

        harness.getRunner().run();

        verify(harness.cb).onException(eq(expected));
    }

    @Test
    public void testCallbackIsCalledIfDecodeFails() throws Exception {
        when(harness.fetcher.loadData(eq(harness.priority))).thenReturn(new Object());
        when(harness.decoder.decode(anyObject(), anyInt(), anyInt())).thenReturn(null);

        harness.getRunner().run();

        verify(harness.cb).onException((Exception) isNull());
    }

    @Test
    public void testResourceFetcherCancelIsCalledWhenCancelled() {
        harness.getRunner().cancel();

        verify(harness.fetcher).cancel();
    }

    @Test
    public void testFetcherNotCalledIfCancelled() throws Exception {
        SourceResourceRunner<Object, Object, Object> runner = harness.getRunner();
        runner.cancel();
        runner.run();

        verify(harness.fetcher, never()).loadData(any(Priority.class));
    }

    @Test
    public void testPriorityMatchesPriority() {
        harness.priority = Priority.LOW;
        assertEquals(harness.priority.ordinal(), harness.getRunner()
                .getPriority());
    }

    @Test
    public void testDoesNotNormallyEncodeRetrievedData() throws Exception {
        Object data = new Object();
        when(harness.fetcher.loadData(any(Priority.class))).thenReturn(data);

        harness.getRunner().run();

        verify(harness.sourceEncoder, never()).encode(eq(data), any(OutputStream.class));
    }

    @Test
    public void testEncodesRetrievedDataIfAskedToCacheSource() throws Exception {
        harness.cacheSource = true;
        Object data = new Object();
        when(harness.fetcher.loadData(any(Priority.class))).thenReturn(data);
        when(harness.factory.build(eq(harness.sourceEncoder), eq(data))).thenReturn(harness.sourceWriter);

        harness.getRunner().run();

        verify(harness.diskCache).put(eq(harness.originalKey), eq(harness.sourceWriter));
    }

    @Test
    public void testReturnsDecodedDataFromCacheIfAskedToCacheSource() throws Exception {
        harness.cacheSource = true;
        Object data = new Object();
        when(harness.fetcher.loadData(any(Priority.class))).thenReturn(data);
        doAnswer(new Answer() {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                when(harness.cacheLoader.load(eq(harness.originalKey), eq(harness.cacheDecoder), eq(harness.width),
                        eq(harness.height))).thenReturn(harness.decoded);
                return null;
            }
        }).when(harness.diskCache).put(eq(harness.originalKey), any(DiskCache.Writer.class));

        harness.getRunner().run();

        verify(harness.cb).onResourceReady(eq(harness.transcoded));
    }

    @SuppressWarnings("unchecked")
    private static class SourceResourceHarness {
        CacheLoader cacheLoader = mock(CacheLoader.class);
        ResourceDecoder<File, Object> cacheDecoder = mock(ResourceDecoder.class);
        DataFetcher<Object> fetcher = mock(DataFetcher.class);
        ResourceDecoder<Object, Object> decoder = mock(ResourceDecoder.class);
        ResourceEncoder<Object> encoder = mock(ResourceEncoder.class);
        ResourceTranscoder<Object, Object> transcoder = mock(ResourceTranscoder.class);
        DiskCache diskCache = mock(DiskCache.class);
        Priority priority = Priority.LOW;
        ResourceCallback cb = mock(ResourceCallback.class);
        Resource<Object> decoded = mock(Resource.class);
        Resource<Object> transformed = mock(Resource.class);
        Resource<Object> transcoded = mock(Resource.class);
        Transformation<Object> transformation = mock(Transformation.class);
        SourceResourceRunner.WriterFactory factory = mock(SourceResourceRunner.WriterFactory.class);
        SourceResourceRunner.SourceWriter<Resource<Object>> writer = mock(SourceResourceRunner.SourceWriter.class);
        SourceResourceRunner.SourceWriter<Object> sourceWriter = mock(SourceResourceRunner.SourceWriter
                .class);
        boolean cacheSource = false;
        Encoder<Object> sourceEncoder = mock(Encoder.class);
        int width = 150;
        int height = 200;
        EngineKey key = mock(EngineKey.class);
        Key originalKey = mock(Key.class);

        public SourceResourceRunner<Object, Object, Object> getRunner() {
            return new SourceResourceRunner<Object, Object, Object>(key, width, height, cacheLoader, cacheDecoder,
                    fetcher, cacheSource, sourceEncoder, decoder, transformation, encoder, transcoder, diskCache,
                    priority, cb, factory);
        }

        public SourceResourceHarness() {
            when(key.getOriginalKey()).thenReturn(originalKey);
            when(transformation.transform(eq(decoded), eq(width), eq(height))).thenReturn(transformed);
            when(transcoder.transcode(eq(transformed))).thenReturn(transcoded);
        }
    }
}