aboutsummaryrefslogtreecommitdiff
path: root/third_party/gif_decoder/src/androidTest/java/com/bumptech/glide/gifdecoder/GifHeaderParserTest.java
blob: 3433153e618f71025d615e0309a1a2ece9d0e471 (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
package com.bumptech.glide.gifdecoder;

import com.bumptech.glide.gifdecoder.test.GifBytesTestUtil;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
 * Tests for {@link com.bumptech.glide.gifdecoder.GifHeaderParser}.
 */
public class GifHeaderParserTest {
    private GifHeaderParser parser;

    @Before
    public void setUp() {
        parser = new GifHeaderParser();
    }

    @Test
    public void testReturnsHeaderWithFormatErrorIfDoesNotStartWithGifHeader() {
        parser.setData("wrong_header".getBytes());
        GifHeader result = parser.parseHeader();
        assertEquals(GifDecoder.STATUS_FORMAT_ERROR, result.status);
    }

    @Test
    public void testCanReadValidHeaderAndLSD() {
        final int width = 10;
        final int height = 20;
        ByteBuffer buffer = ByteBuffer.allocate(GifBytesTestUtil.HEADER_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
        GifBytesTestUtil.writeHeaderAndLsd(buffer, width, height, false, 0);

        parser.setData(buffer.array());
        GifHeader header = parser.parseHeader();
        assertEquals(width, header.width);
        assertEquals(height, header.height);
        assertFalse(header.gctFlag);
        // 2^(1+0) == 2^1 == 2.
        assertEquals(2, header.gctSize);
        assertEquals(0, header.bgIndex);
        assertEquals(0, header.pixelAspect);
    }

    @Test
    public void testCanParseHeaderOfTestImageWithoutGraphicalExtension() throws IOException {
        byte[] data = readResourceData("gif_without_graphical_control_extension.gif");
        parser.setData(data);
        GifHeader header = parser.parseHeader();
        assertEquals(1, header.frameCount);
        assertNotNull(header.frames.get(0));
        assertEquals(GifDecoder.STATUS_OK, header.status);
    }

    @Test
    public void testCanReadImageDescriptorWithoutGraphicalExtension() {
        final int lzwMinCodeSize = 2;
        ByteBuffer buffer = ByteBuffer.allocate(
                GifBytesTestUtil.HEADER_LENGTH
                + GifBytesTestUtil.IMAGE_DESCRIPTOR_LENGTH
                + GifBytesTestUtil.getImageDataSize(lzwMinCodeSize)
        ).order(ByteOrder.LITTLE_ENDIAN);
        GifBytesTestUtil.writeHeaderAndLsd(buffer, 1, 1, false, 0);
        GifBytesTestUtil.writeImageDescriptor(buffer, 0, 0, 1, 1, false /*hasLct*/, 0);
        GifBytesTestUtil.writeFakeImageData(buffer, lzwMinCodeSize);

        parser.setData(buffer.array());
        GifHeader header = parser.parseHeader();
        assertEquals(1, header.width);
        assertEquals(1, header.height);
        assertEquals(1, header.frameCount);
        assertNotNull(header.frames.get(0));
    }

    @Test
    public void testSetsFrameLocalColorTableToNullIfNoColorTable() {
        final int lzwMinCodeSize = 2;
        ByteBuffer buffer = ByteBuffer.allocate(
                GifBytesTestUtil.HEADER_LENGTH
                + GifBytesTestUtil.IMAGE_DESCRIPTOR_LENGTH
                + GifBytesTestUtil.getImageDataSize(lzwMinCodeSize)
        ).order(ByteOrder.LITTLE_ENDIAN);
        GifBytesTestUtil.writeHeaderAndLsd(buffer, 1, 1, false, 0);
        GifBytesTestUtil.writeImageDescriptor(buffer, 0, 0, 1, 1, false /*hasLct*/, 0);
        GifBytesTestUtil.writeFakeImageData(buffer, lzwMinCodeSize);

        parser.setData(buffer.array());
        GifHeader header = parser.parseHeader();
        assertEquals(1, header.width);
        assertEquals(1, header.height);
        assertEquals(1, header.frameCount);
        assertNotNull(header.frames.get(0));
        assertNull(header.frames.get(0).lct);
    }

    @Test
    public void testSetsFrameLocalColorTableIfHasColorTable() {
        final int lzwMinCodeSize = 2;
        final int numColors = 4;
        ByteBuffer buffer = ByteBuffer.allocate(
                GifBytesTestUtil.HEADER_LENGTH
                + GifBytesTestUtil.IMAGE_DESCRIPTOR_LENGTH
                + GifBytesTestUtil.getImageDataSize(lzwMinCodeSize)
                + GifBytesTestUtil.getColorTableLength(numColors)
        ).order(ByteOrder.LITTLE_ENDIAN);
        GifBytesTestUtil.writeHeaderAndLsd(buffer, 1, 1, false, 0);
        GifBytesTestUtil.writeImageDescriptor(buffer, 0, 0, 1, 1, true /*hasLct*/, numColors);
        GifBytesTestUtil.writeColorTable(buffer, numColors);
        GifBytesTestUtil.writeFakeImageData(buffer, 2);

        parser.setData(buffer.array());
        GifHeader header = parser.parseHeader();
        assertEquals(1, header.width);
        assertEquals(1, header.height);
        assertEquals(1, header.frameCount);
        assertNotNull(header.frames.get(0));

        GifFrame frame = header.frames.get(0);
        assertNotNull(frame.lct);
    }

    @Test
    public void testCanParseMultipleFrames() {
        final int lzwMinCodeSize = 2;
        final int expectedFrames = 3;

        final int frameSize = GifBytesTestUtil.IMAGE_DESCRIPTOR_LENGTH
                + GifBytesTestUtil.getImageDataSize(lzwMinCodeSize);
        ByteBuffer buffer = ByteBuffer.allocate(
                GifBytesTestUtil.HEADER_LENGTH + expectedFrames * frameSize
        ).order(ByteOrder.LITTLE_ENDIAN);

        GifBytesTestUtil.writeHeaderAndLsd(buffer, 1, 1, false, 0);
        for (int i = 0; i < expectedFrames; i++) {
            GifBytesTestUtil.writeImageDescriptor(buffer, 0, 0, 1, 1, false /*hasLct*/, 0 /*numColors*/);
            GifBytesTestUtil.writeFakeImageData(buffer, 2);
        }

        parser.setData(buffer.array());
        GifHeader header = parser.parseHeader();
        assertEquals(expectedFrames, header.frameCount);
        assertEquals(expectedFrames, header.frames.size());
    }

    private InputStream openResource(String imageName) throws IOException {
        return getClass().getResourceAsStream("/" + imageName);
    }

    private byte[] readResourceData(String imageName) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        InputStream is = null;
        try {
            is = openResource(imageName);
            int read;
            while ((read = is.read(buffer)) != -1) {
                os.write(buffer, 0, read);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore.
                }
            }
        }
        return os.toByteArray();
    }
}