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

import android.graphics.Bitmap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

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

/**
 * Tests for {@link com.bumptech.glide.gifdecoder.GifDecoder}.
 */
@RunWith(RobolectricTestRunner.class)
@Config(emulateSdk = 18)
public class GifDecoderTest {

    private MockProvider provider;

    @Before
    public void setUp() {
        provider = new MockProvider();
    }

    @Test
    public void testCanDecodeFramesFromTestGif() {
        byte[] data = TestUtil.readResourceData("partial_gif_decode.gif");
        GifHeaderParser headerParser = new GifHeaderParser();
        headerParser.setData(data);
        GifHeader header = headerParser.parseHeader();
        GifDecoder decoder = new GifDecoder(provider);
        decoder.setData(header, data);
        decoder.advance();
        Bitmap bitmap = decoder.getNextFrame();
        assertNotNull(bitmap);
        assertEquals(GifDecoder.STATUS_OK, decoder.getStatus());
    }

    @Test
    public void testFrameIndexStartsAtNegativeOne() {
        GifHeader gifheader = new GifHeader();
        gifheader.frameCount = 4;
        byte[] data = new byte[0];
        GifDecoder decoder = new GifDecoder(provider);
        decoder.setData(gifheader, data);
        assertEquals(-1, decoder.getCurrentFrameIndex());
    }

    @Test
    public void testAdvanceIncrementsFrameIndex() {
        GifHeader gifheader = new GifHeader();
        gifheader.frameCount = 4;
        byte[] data = new byte[0];
        GifDecoder decoder = new GifDecoder(provider);
        decoder.setData(gifheader, data);
        decoder.advance();
        assertEquals(0, decoder.getCurrentFrameIndex());
    }

    @Test
    public void testAdvanceWrapsIndexBackToZero() {
        GifHeader gifheader = new GifHeader();
        gifheader.frameCount = 2;
        byte[] data = new byte[0];
        GifDecoder decoder = new GifDecoder(provider);
        decoder.setData(gifheader, data);
        decoder.advance();
        decoder.advance();
        decoder.advance();
        assertEquals(0, decoder.getCurrentFrameIndex());
    }

    @Test
    public void testSettingDataResetsFramePointer() {
        GifHeader gifheader = new GifHeader();
        gifheader.frameCount = 4;
        byte[] data = new byte[0];
        GifDecoder decoder = new GifDecoder(provider);
        decoder.setData(gifheader, data);
        decoder.advance();
        decoder.advance();
        assertEquals(1, decoder.getCurrentFrameIndex());

        decoder.setData(gifheader, data);
        assertEquals(-1, decoder.getCurrentFrameIndex());
    }

    private static class MockProvider implements GifDecoder.BitmapProvider {

        @Override
        public Bitmap obtain(int width, int height, Bitmap.Config config) {
            Bitmap result = Bitmap.createBitmap(width, height, config);
            Robolectric.shadowOf(result).setMutable(true);
            return result;
        }

        @Override
        public void release(Bitmap bitmap) {
            // Do nothing.
        }
    }
}