aboutsummaryrefslogtreecommitdiff
path: root/library/src/test/java/com/bumptech/glide/load/resource/FileToStreamDecoderTest.java
blob: 12bc9d3b0fc149c2b414768162e898e36ebb00f0 (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
package com.bumptech.glide.load.resource;

import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;

import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

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

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class FileToStreamDecoderTest {

    private ResourceDecoder<InputStream, Object> streamDecoder;
    private FileToStreamDecoder<Object> decoder;
    private FileToStreamDecoder.FileOpener fileOpener;

    @SuppressWarnings("unchecked")
    @Before
    public void setUp() {
        fileOpener = mock(FileToStreamDecoder.FileOpener.class);
        streamDecoder = mock(ResourceDecoder.class);
        decoder = new FileToStreamDecoder<Object>(streamDecoder, fileOpener);
    }

    @Test
    public void testHasEmptyId() {
        assertEquals("", decoder.getId());
    }

    @Test
    public void testReturnsResourceFromStreamDecoder() throws IOException {
        File file = new File("test");
        InputStream expected = new ByteArrayInputStream(new byte[0]);
        when(fileOpener.open(eq(file))).thenReturn(expected);
        Resource<Object> resource = mock(Resource.class);
        int width = 123;
        int height = 456;

        when(streamDecoder.decode(eq(expected), eq(width), eq(height))).thenReturn(resource);

        assertEquals(resource, decoder.decode(file, width, height));
    }

    @Test
    public void testClosesStream() throws IOException {
        InputStream is = mock(InputStream.class);
        when(fileOpener.open(any(File.class))).thenReturn(is);

        decoder.decode(new File("test"), 100, 100);

        verify(is).close();
    }

    @Test
    public void testClosesStreamIfStreamDecoderThrows() throws IOException {
        InputStream is = mock(InputStream.class);
        when(fileOpener.open(any(File.class))).thenReturn(is);

        when(streamDecoder.decode(eq(is), anyInt(), anyInt())).thenAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                throw new IOException("test");
            }
        });

        try {
            decoder.decode(new File("test"), 100, 100);
        } catch (IOException e) {
            // Expected.
        }

        verify(is).close();
    }
}