@Override public GifDrawableResource decode(@NonNull ByteBuffer source, int width, int height, @NonNull Options options) { final GifHeaderParser parser = parserPool.obtain(source); try { return decode(source, width, height, parser, options); } finally { parserPool.release(parser); } }
@Test public void testReturnsParserToPool() throws IOException { decoder.decode(ByteBuffer.allocate(10), 100, 100, options); verify(parserPool).release(eq(parser)); }
@Test public void testReturnsNullIfParsedHeaderHasOpenError() { when(gifHeader.getStatus()).thenReturn(GifDecoder.STATUS_OPEN_ERROR); assertNull(decoder.decode(ByteBuffer.allocate(10), 100, 100, options)); }
@Test public void testReturnsNullIfParsedHeaderHasFormatError() { when(gifHeader.getStatus()).thenReturn(GifDecoder.STATUS_FORMAT_ERROR); assertNull(decoder.decode(ByteBuffer.allocate(10), 100, 100, options)); }
@Test public void testReturnsParserToPoolWhenParserThrows() { when(parser.parseHeader()).thenThrow(new RuntimeException("Test")); try { decoder.decode(ByteBuffer.allocate(10), 100, 100, options); fail("Failed to receive expected exception"); } catch (RuntimeException e) { // Expected. } verify(parserPool).release(eq(parser)); }
@Test public void testReturnsNullIfParsedHeaderHasZeroFrames() throws IOException { when(gifHeader.getNumFrames()).thenReturn(0); assertNull(decoder.decode(ByteBuffer.allocate(10), 100, 100, options)); }
@Test public void testReturnsNullIfGifDecoderFailsToDecodeFirstFrame() { when(gifHeader.getNumFrames()).thenReturn(1); when(gifHeader.getStatus()).thenReturn(GifDecoder.STATUS_OK); when(gifDecoder.getNextFrame()).thenReturn(null); assertNull(decoder.decode(ByteBuffer.allocate(10), 100, 100, options)); } }