aboutsummaryrefslogtreecommitdiff
path: root/third_party/gif_decoder/src/androidTest/java/com/bumptech/glide/gifdecoder/test/GifBytesTestUtilTest.java
blob: 88106ea6b19348634db2bd7d045970cbfe21fd3c (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
package com.bumptech.glide.gifdecoder.test;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Arrays;

import static org.junit.Assert.assertArrayEquals;

/**
 * Tests for {@link com.bumptech.glide.gifdecoder.test.GifBytesTestUtil}.
 */
public class GifBytesTestUtilTest {

    @Test
    public void testWriteHeaderAndLsdWithoutGct() {
        ByteBuffer buffer = ByteBuffer.allocate(GifBytesTestUtil.HEADER_LENGTH);
        GifBytesTestUtil.writeHeaderAndLsd(buffer, 8, 16, false, 0);

        byte[] expected = new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x00, 0x08, 0x00, 0x10, 0x20, 0x00, 0x00};

        assertArrayEquals(expected, buffer.array());
    }

    @Test
    public void testWriteHeaderAndLsdWithGct() {
        ByteBuffer buffer = ByteBuffer.allocate(GifBytesTestUtil.HEADER_LENGTH);
        GifBytesTestUtil.writeHeaderAndLsd(buffer, 8, 16, true, 4);

        byte[] expected = new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x00, 0x08, 0x00, 0x10, (byte) 0xA4, 0x00,
                0x00};

        assertArrayEquals(expected, buffer.array());
    }

    @Test
    public void testWriteImageDescriptorWithoutColorTable() {
        ByteBuffer buffer = ByteBuffer.allocate(GifBytesTestUtil.IMAGE_DESCRIPTOR_LENGTH);
        GifBytesTestUtil.writeImageDescriptor(buffer, 10, 9, 8, 7, false, 0);

        byte[] expected = new byte[] {
                // Image separator.
                0x2C,
                // Image left.
                0x00, 0x0A,
                // Image right.
                0x00, 0X09,
                // Image width.
                0x00, 0x08,
                // Image height.
                0x00, 0x07,
                // Packed field.
                0x00
        };

        assertArrayEquals(expected, buffer.array());
    }

    @Test
    public void testWriteImageDescriptorWithColorTable() {
        ByteBuffer buffer = ByteBuffer.allocate(GifBytesTestUtil.IMAGE_DESCRIPTOR_LENGTH);
        GifBytesTestUtil.writeImageDescriptor(buffer, 10, 9, 8, 7, true, 4);

        byte packedField =
                // Set LCT flag
                (byte) 0x80
                // Size of color table (2^(N + 1) == 4)
                | 0x01;

        byte[] expected = new byte[] {
                // Image separator.
                0x2C,
                // Image left.
                0x00, 0x0A,
                // Image right.
                0x00, 0X09,
                // Image width.
                0x00, 0x08,
                // Image height.
                0x00, 0x07,
                packedField
        };

        assertArrayEquals(expected, buffer.array());
    }

    @Test
    public void testWriteColorTable() {
        final int numColors = 4;
        ByteBuffer buffer = ByteBuffer.allocate(GifBytesTestUtil.getColorTableLength(numColors));
        GifBytesTestUtil.writeColorTable(buffer, numColors);

        byte[] expected = new byte[] {
                // First color.
                0x00, 0x00, 0x00,
                // Second color.
                0x00, 0x00, 0x01,
                // Third color.
                0x00, 0x00, 0x02,
                // Fourth color.
                0x00, 0x00, 0x03,
        };


        assertArrayEquals("expected=" + Arrays.toString(expected) + " received=" + Arrays.toString(buffer.array()),
                expected, buffer.array());
    }

    @Test
    public void testWriteFakeImageData() {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        GifBytesTestUtil.writeFakeImageData(buffer, 2);

        byte[] expected = new byte[] { 0x02, 0x01, 0x01, 0x00 };

        assertArrayEquals(expected, buffer.array());
    }
}