@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 testDoesNotHandleStreamIfEnabledButNotAGif() throws IOException { assertThat(decoder.handles(ByteBuffer.allocate(0), options)).isFalse(); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); gifHeader = Mockito.spy(new GifHeader()); when(parser.parseHeader()).thenReturn(gifHeader); when(parserPool.obtain(isA(ByteBuffer.class))).thenReturn(parser); when(decoderFactory.build(isA(GifDecoder.BitmapProvider.class), eq(gifHeader), isA(ByteBuffer.class), anyInt())) .thenReturn(gifDecoder); List<ImageHeaderParser> parsers = new ArrayList<>(); parsers.add(new DefaultImageHeaderParser()); options = new Options(); decoder = new ByteBufferGifDecoder( RuntimeEnvironment.application, parsers, bitmapPool, new LruArrayPool(ARRAY_POOL_SIZE_BYTES), parserPool, decoderFactory); }
@Nullable private GifDrawableResource decode( ByteBuffer byteBuffer, int width, int height, GifHeaderParser parser, Options options) { long startTime = LogTime.getLogTime(); try { final GifHeader header = parser.parseHeader(); if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) { // If we couldn't decode the GIF, we will end up with a frame count of 0. return null; } Bitmap.Config config = options.get(GifOptions.DECODE_FORMAT) == DecodeFormat.PREFER_RGB_565 ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888; int sampleSize = getSampleSize(header, width, height); GifDecoder gifDecoder = gifDecoderFactory.build(provider, header, byteBuffer, sampleSize); gifDecoder.setDefaultBitmapConfig(config); gifDecoder.advance(); Bitmap firstFrame = gifDecoder.getNextFrame(); if (firstFrame == null) { return null; } Transformation<Bitmap> unitTransformation = UnitTransformation.get(); GifDrawable gifDrawable = new GifDrawable(context, gifDecoder, unitTransformation, width, height, firstFrame); return new GifDrawableResource(gifDrawable); } finally { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Decoded GIF from stream in " + LogTime.getElapsedMillis(startTime)); } } }
arrayPool); ByteBufferGifDecoder byteBufferGifDecoder = new ByteBufferGifDecoder(context, imageHeaderParsers, bitmapPool, arrayPool); ResourceDecoder<ParcelFileDescriptor, Bitmap> parcelFileDescriptorVideoDecoder = VideoDecoder.parcel(bitmapPool);
@Test public void testReturnsParserToPool() throws IOException { decoder.decode(ByteBuffer.allocate(10), 100, 100, options); verify(parserPool).release(eq(parser)); }
@Test public void testHandlesStreamIfContainsGifHeaderAndDisabledIsNotSet() throws IOException { assertThat(decoder.handles(ByteBuffer.wrap(GIF_HEADER), options)).isTrue(); }
@Test public void testReturnsNullIfParsedHeaderHasOpenError() { when(gifHeader.getStatus()).thenReturn(GifDecoder.STATUS_OPEN_ERROR); assertNull(decoder.decode(ByteBuffer.allocate(10), 100, 100, options)); }
@Test public void testHandlesStreamIfContainsGifHeaderAndDisabledIsFalse() throws IOException { options.set(GifOptions.DISABLE_ANIMATION, false); assertThat(decoder.handles(ByteBuffer.wrap(GIF_HEADER), options)).isTrue(); }
@Test public void testReturnsNullIfParsedHeaderHasFormatError() { when(gifHeader.getStatus()).thenReturn(GifDecoder.STATUS_FORMAT_ERROR); assertNull(decoder.decode(ByteBuffer.allocate(10), 100, 100, options)); }
@Test public void testDoesNotHandleStreamIfDisabled() throws IOException { options.set(GifOptions.DISABLE_ANIMATION, true); assertThat(decoder.handles(ByteBuffer.wrap(GIF_HEADER), options)).isFalse(); }
@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 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)); } }
@Test public void testReturnsNullIfParsedHeaderHasZeroFrames() throws IOException { when(gifHeader.getNumFrames()).thenReturn(0); assertNull(decoder.decode(ByteBuffer.allocate(10), 100, 100, options)); }