diff --git a/library/src/main/java/com/bumptech/glide/load/resource/bitmap/ImageVideoBitmapDecoder.java b/library/src/main/java/com/bumptech/glide/load/resource/bitmap/ImageVideoBitmapDecoder.java index 161de5aff382e949516cc915511e93b9b34f549e..a258ee713bf498d28a12b8e80c7ad93d8220d93b 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/bitmap/ImageVideoBitmapDecoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/bitmap/ImageVideoBitmapDecoder.java @@ -36,7 +36,10 @@ public class ImageVideoBitmapDecoder implements ResourceDecoder { + private static final DefaultFactory DEFAULT_FACTORY = new DefaultFactory(); + private MediaMetadataRetrieverFactory factory; + + interface MediaMetadataRetrieverFactory { + public MediaMetadataRetriever build(); + } + + public VideoBitmapDecoder() { + this(DEFAULT_FACTORY); + } + + + VideoBitmapDecoder(MediaMetadataRetrieverFactory factory) { + this.factory = factory; + } + @Override public Bitmap decode(ParcelFileDescriptor resource, BitmapPool bitmapPool, int outWidth, int outHeight, DecodeFormat decodeFormat) throws IOException { - MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); + MediaMetadataRetriever mediaMetadataRetriever = factory.build(); mediaMetadataRetriever.setDataSource(resource.getFileDescriptor()); Bitmap result = mediaMetadataRetriever.getFrameAtTime(); mediaMetadataRetriever.release(); @@ -23,6 +39,13 @@ public class VideoBitmapDecoder implements BitmapDecoder { @Override public String getId() { - return "VideoBitmapDecoder.com.bumptech.glide.load.data.bitmap"; + return "VideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap"; + } + + private static class DefaultFactory implements MediaMetadataRetrieverFactory { + @Override + public MediaMetadataRetriever build() { + return new MediaMetadataRetriever(); + } } } diff --git a/library/src/test/java/com/bumptech/glide/load/resource/bitmap/ImageVideoBitmapDecoderTest.java b/library/src/test/java/com/bumptech/glide/load/resource/bitmap/ImageVideoBitmapDecoderTest.java index 07def562db0a22f9c8989a65a3a0154e4ad1eba2..d16fd78397afdee6064cd174946c4c9c354691c3 100644 --- a/library/src/test/java/com/bumptech/glide/load/resource/bitmap/ImageVideoBitmapDecoderTest.java +++ b/library/src/test/java/com/bumptech/glide/load/resource/bitmap/ImageVideoBitmapDecoderTest.java @@ -82,6 +82,16 @@ public class ImageVideoBitmapDecoderTest { verify(harness.streamDecoder, never()).decode(any(InputStream.class), anyInt(), anyInt()); } + @Test + public void testDoesNotTryToDecodeNullFileDescriptor() throws IOException { + when(harness.wrapper.getStream()).thenReturn(null); + when(harness.wrapper.getFileDescriptor()).thenReturn(null); + + harness.decoder.decode(harness.wrapper, 100, 102); + + verify(harness.fileDescriptorDecoder, never()).decode(any(ParcelFileDescriptor.class), anyInt(), anyInt()); + } + @Test public void testHasValidId() { Util.assertClassHasValidId(ImageVideoBitmapDecoder.class, harness.decoder.getId()); @@ -94,6 +104,5 @@ public class ImageVideoBitmapDecoderTest { ResourceDecoder fileDescriptorDecoder = mock(ResourceDecoder.class); ImageVideoBitmapDecoder decoder = new ImageVideoBitmapDecoder(streamDecoder, fileDescriptorDecoder); ImageVideoWrapper wrapper = mock(ImageVideoWrapper.class); - } } diff --git a/library/src/test/java/com/bumptech/glide/load/resource/bitmap/VideoBitmapDecoderTest.java b/library/src/test/java/com/bumptech/glide/load/resource/bitmap/VideoBitmapDecoderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7c5db62ff7360000768923f7a182ac88c2849bff --- /dev/null +++ b/library/src/test/java/com/bumptech/glide/load/resource/bitmap/VideoBitmapDecoderTest.java @@ -0,0 +1,79 @@ +package com.bumptech.glide.load.resource.bitmap; + +import android.graphics.Bitmap; +import android.media.MediaMetadataRetriever; +import android.os.ParcelFileDescriptor; +import com.bumptech.glide.load.DecodeFormat; +import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; +import com.bumptech.glide.tests.Util; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.io.FileDescriptor; +import java.io.IOException; + +import static junit.framework.Assert.assertEquals; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(RobolectricTestRunner.class) +public class VideoBitmapDecoderTest { + private BitmapPool bitmapPool; + private DecodeFormat decodeFormat; + private ParcelFileDescriptor resource; + private VideoBitmapDecoder decoder; + private VideoBitmapDecoder.MediaMetadataRetrieverFactory factory; + private MediaMetadataRetriever retriever; + + @Before + public void setup() { + bitmapPool = mock(BitmapPool.class); + decodeFormat = DecodeFormat.ALWAYS_ARGB_8888; + resource = mock(ParcelFileDescriptor.class); + factory = mock(VideoBitmapDecoder.MediaMetadataRetrieverFactory.class); + retriever = mock(MediaMetadataRetriever.class); + when(factory.build()).thenReturn(retriever); + decoder = new VideoBitmapDecoder(new VideoBitmapDecoder.MediaMetadataRetrieverFactory() { + @Override + public MediaMetadataRetriever build() { + return factory.build(); + } + }); + } + + @Test + public void testReturnsRetrievedFrameForResource() throws IOException { + Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); + when(retriever.getFrameAtTime()).thenReturn(expected); + + FileDescriptor toSet = FileDescriptor.in; + when(resource.getFileDescriptor()).thenReturn(toSet); + Bitmap result = decoder.decode(resource, bitmapPool, 100, 100, decodeFormat); + + verify(retriever).setDataSource(eq(toSet)); + assertEquals(expected, result); + } + + @Test + public void testReleasesMediaMetadataRetriever() throws IOException { + decoder.decode(resource, bitmapPool, 1, 2, decodeFormat); + + verify(retriever).release(); + } + + @Test + public void testClosesResource() throws IOException { + decoder.decode(resource, bitmapPool, 1, 2, decodeFormat); + + verify(resource).close(); + } + + @Test + public void testHasValidId() { + Util.assertClassHasValidId(VideoBitmapDecoder.class, decoder.getId()); + } +}