提交 ea01ddd7 编写于 作者: S Sam Judd

Add GifDecoder and GifResource*

上级 b38d03ba
package com.bumptech.glide.load.resource.drawable;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.Resource;
public class DrawableResource extends Resource<Drawable> {
private final Drawable drawable;
private final Resource wrapped;
public DrawableResource(Drawable drawable, Resource wrapped) {
this.drawable = drawable;
this.wrapped = wrapped;
}
@Override
public Drawable get() {
return drawable;
}
@Override
public int getSize() {
return wrapped.getSize();
}
@Override
protected void recycleInternal() {
wrapped.recycle();
}
}
package com.bumptech.glide.load.resource.drawable;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.resource.gif.GifDrawable;
public class GifBitmap {
private Resource<GifDrawable> gifResource;
private Resource<Bitmap> bitmapResource;
private Resources resources;
public GifBitmap(Resources resources, Resource<Bitmap> bitmapResource) {
this.resources = resources;
this.bitmapResource = bitmapResource;
}
public GifBitmap(Resource<GifDrawable> gifResource) {
this.gifResource = gifResource;
}
public int getSize() {
return 0;
}
public Resource<Bitmap> getBitmapResource() {
return bitmapResource;
}
public Resource<GifDrawable> getGifResource() {
return gifResource;
}
public Drawable getDrawable() {
if (gifResource != null) {
return gifResource.get();
} else {
return new BitmapDrawable(resources, bitmapResource.get());
}
}
}
package com.bumptech.glide.load.resource.drawable;
import com.bumptech.glide.Resource;
public class GifBitmapResource extends Resource<GifBitmap> {
private GifBitmap data;
public GifBitmapResource(GifBitmap data) {
this.data = data;
}
@Override
public GifBitmap get() {
return data;
}
@Override
public int getSize() {
return data.getSize();
}
@Override
protected void recycleInternal() {
}
}
package com.bumptech.glide.load.resource.drawable;
import android.content.Context;
import android.graphics.Bitmap;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.model.ImageVideoWrapper;
import com.bumptech.glide.load.resource.bitmap.ImageHeaderParser;
import com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.util.ByteArrayPool;
import java.io.IOException;
import java.io.InputStream;
public class GifBitmapResourceDecoder implements ResourceDecoder<ImageVideoWrapper, GifBitmap> {
private Context context;
private final ResourceDecoder<ImageVideoWrapper, Bitmap> bitmapDecoder;
private final ResourceDecoder<InputStream, GifDrawable> gifDecoder;
public GifBitmapResourceDecoder(Context context, ResourceDecoder<ImageVideoWrapper, Bitmap> bitmapDecoder,
ResourceDecoder<InputStream, GifDrawable> gifDecoder) {
this.context = context;
this.bitmapDecoder = bitmapDecoder;
this.gifDecoder = gifDecoder;
}
@Override
public Resource<GifBitmap> decode(ImageVideoWrapper source, int width, int height) throws IOException {
ByteArrayPool pool = ByteArrayPool.get();
InputStream is = source.getStream();
GifBitmap result = null;
if (is != null) {
byte[] tempBytes = pool.getBytes();
RecyclableBufferedInputStream bis = new RecyclableBufferedInputStream(is, tempBytes);
bis.mark(1024);
ImageHeaderParser.ImageType type = new ImageHeaderParser(bis).getType();
bis.reset();
if (type == ImageHeaderParser.ImageType.GIF) {
Resource<GifDrawable> gifResource = gifDecoder.decode(is, width, height);
result = new GifBitmap(gifResource);
}
pool.releaseBytes(tempBytes);
}
if (result == null) {
Resource<Bitmap> bitmapResource = bitmapDecoder.decode(source, width, height);
result = new GifBitmap(context.getResources(), bitmapResource);
}
return new GifBitmapResource(result);
}
@Override
public String getId() {
return "GifBitmapResourceDecoder.com.bumptech.glide.load.resource.drawable";
}
}
package com.bumptech.glide.load.resource.drawable;
import android.graphics.Bitmap;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import java.io.OutputStream;
public class GifBitmapResourceEncoder implements ResourceEncoder<GifBitmap> {
private final ResourceEncoder<Bitmap> bitmapEncoder;
private final ResourceEncoder<GifDrawable> gifEncoder;
public GifBitmapResourceEncoder(ResourceEncoder<Bitmap> bitmapEncoder, ResourceEncoder<GifDrawable> gifEncoder) {
this.bitmapEncoder = bitmapEncoder;
this.gifEncoder = gifEncoder;
}
@Override
public boolean encode(Resource<GifBitmap> resource, OutputStream os) {
final GifBitmap gifBitmap = resource.get();
final Resource<Bitmap> bitmapResource = gifBitmap.getBitmapResource();
if (bitmapResource != null) {
return bitmapEncoder.encode(bitmapResource, os);
} else {
return gifEncoder.encode(gifBitmap.getGifResource(), os);
}
}
@Override
public String getId() {
return "GifBitmapResourceEncoder.com.bumptech.glide.load.resource.drawable";
}
}
package com.bumptech.glide.load.resource.drawable;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.model.ImageVideoWrapper;
import java.io.IOException;
import java.io.InputStream;
public class GifBitmapStreamResourceDecoder implements ResourceDecoder<InputStream, GifBitmap>{
private ResourceDecoder<ImageVideoWrapper, GifBitmap> gifBitmapDecoder;
public GifBitmapStreamResourceDecoder(ResourceDecoder<ImageVideoWrapper, GifBitmap> gifBitmapDecoder) {
this.gifBitmapDecoder = gifBitmapDecoder;
}
@Override
public Resource<GifBitmap> decode(InputStream source, int width, int height) throws IOException {
return gifBitmapDecoder.decode(new ImageVideoWrapper(source, null), width, height);
}
@Override
public String getId() {
return "GifBitmapStreamResourceDecoder.com.bumptech.glide.resource.drawable";
}
}
package com.bumptech.glide.load.resource.gif;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
public class GifDrawable extends Drawable implements Animatable, GifFrameManager.FrameCallback {
private final Paint paint;
private final GifFrameManager frameManager;
private GifDecoder decoder;
private boolean isRunning;
private Bitmap currentFrame;
public GifDrawable(GifDecoder decoder, GifFrameManager frameManager) {
this.decoder = decoder;
this.frameManager = frameManager;
paint = new Paint();
}
GifDecoder getDecoder() {
return decoder;
}
@Override
public void start() {
if (!isRunning) {
isRunning = true;
frameManager.getNextFrame(decoder, this);
invalidateSelf();
}
}
@Override
public boolean setVisible(boolean visible, boolean restart) {
if (!visible) {
stop();
} else {
start();
}
return super.setVisible(visible, restart);
}
@Override
public int getIntrinsicWidth() {
return decoder.getWidth();
}
@Override
public int getIntrinsicHeight() {
return decoder.getHeight();
}
@Override
public void stop() {
isRunning = false;
}
@Override
public boolean isRunning() {
return isRunning;
}
// For testing.
void setIsRunning(boolean isRunning) {
this.isRunning = isRunning;
}
@Override
public void draw(Canvas canvas) {
if (currentFrame != null) {
canvas.drawBitmap(currentFrame, 0, 0, paint);
}
}
@Override
public void setAlpha(int i) {
paint.setAlpha(i);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
paint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return decoder.isTransparent() ? PixelFormat.TRANSPARENT : PixelFormat.OPAQUE;
}
@Override
public void onFrameRead(Bitmap frame) {
if (!isRunning) {
return;
}
if (frame != null) {
currentFrame = frame;
invalidateSelf();
}
frameManager.getNextFrame(decoder, this);
}
public void recycle() {
frameManager.clear();
}
}
package com.bumptech.glide.load.resource.gif;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.ModelLoader;
public class GifFrameLoader implements ModelLoader<GifDecoder, GifDecoder> {
private String gifId;
//TODO: this should be in the model.
public GifFrameLoader(String gifId) {
this.gifId = gifId;
}
@Override
public DataFetcher<GifDecoder> getResourceFetcher(GifDecoder model, int width, int height) {
return new GifFrameDataFetcher(model);
}
@Override
public String getId(GifDecoder model) {
return gifId + model.getCurrentFrameIndex();
}
private static class GifFrameDataFetcher implements DataFetcher<GifDecoder> {
private GifDecoder decoder;
public GifFrameDataFetcher(GifDecoder decoder) {
this.decoder = decoder;
}
@Override
public GifDecoder loadData(Priority priority) throws Exception {
return decoder;
}
@Override
public void cleanup() { }
@Override
public void cancel() { }
}
}
package com.bumptech.glide.load.resource.gif;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder;
import com.bumptech.glide.request.target.SimpleTarget;
import java.util.UUID;
class GifFrameManager {
static final long MIN_FRAME_DELAY = 16;
private final String id;
private final MemorySizeCalculator calculator;
private BitmapPool bitmapPool;
private final Handler mainHandler;
private Context context;
private DelayTarget current;
private DelayTarget next;
public interface FrameCallback {
public void onFrameRead(Bitmap frame);
}
public GifFrameManager(Context context, BitmapPool bitmapPool) {
this(context, UUID.randomUUID().toString(), bitmapPool, new Handler(Looper.getMainLooper()));
}
public GifFrameManager(Context context, String id, BitmapPool bitmapPool, Handler mainHandler) {
this.context = context;
this.id = id;
this.bitmapPool = bitmapPool;
this.mainHandler = mainHandler;
calculator = new MemorySizeCalculator(context);
}
public void getNextFrame(final GifDecoder decoder, FrameCallback cb) {
decoder.advance();
boolean skipCache = decoder.getDecodedFrameByteSize() > calculator.getMemoryCacheSize() / 2;
long targetTime = SystemClock.uptimeMillis() + (Math.min(MIN_FRAME_DELAY, decoder.getNextDelay()));
next = new DelayTarget(decoder, cb, targetTime, mainHandler);
Glide.with(context)
.using(new GifFrameLoader(id), GifDecoder.class)
.load(decoder)
.as(Bitmap.class)
.decoder(new GifFrameResourceDecoder())
.cacheDecoder(new StreamBitmapDecoder(bitmapPool))
.skipMemoryCache(skipCache)
.skipDiskCache(true)
.into(next);
}
public void clear() {
if (current != null) {
Glide.clear(current);
mainHandler.removeCallbacks(current);
}
if (next != null) {
Glide.clear(next);
mainHandler.removeCallbacks(next);
}
}
class DelayTarget extends SimpleTarget<Bitmap> implements Runnable {
private FrameCallback cb;
private long targetTime;
private Handler mainHandler;
private Bitmap resource;
public DelayTarget(GifDecoder decoder, FrameCallback cb, long targetTime, Handler mainHandler) {
super(decoder.getWidth(), decoder.getHeight());
this.cb = cb;
this.targetTime = targetTime;
this.mainHandler = mainHandler;
}
@Override
public void onResourceReady(final Bitmap resource) {
this.resource = resource;
mainHandler.postAtTime(this, targetTime);
if (current != null) {
Glide.clear(current);
}
current = next;
next = null;
}
@Override
public void run() {
cb.onFrameRead(resource);
}
}
}
package com.bumptech.glide.load.resource.gif;
import android.graphics.Bitmap;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.ResourceDecoder;
import java.io.IOException;
class GifFrameResourceDecoder implements ResourceDecoder<GifDecoder, Bitmap> {
@Override
public Resource<Bitmap> decode(GifDecoder source, int width, int height) throws IOException {
return source.getNextFrame();
}
@Override
public String getId() {
return "GifFrameResourceDecoder.com.bumptech.glide.load.resource.gif";
}
}
package com.bumptech.glide.load.resource.gif;
import com.bumptech.glide.Resource;
//TODO: make this safe for multiple consumers.
public class GifResource extends Resource<GifDrawable> {
private final GifDrawable drawable;
private final GifDecoder decoder;
public GifResource(GifDecoder decoder, GifFrameManager frameManager) {
this(decoder, new GifDrawable(decoder, frameManager));
}
GifResource(GifDecoder gifDecoder, GifDrawable gifDrawable) {
decoder = gifDecoder;
drawable = gifDrawable;
}
@Override
public GifDrawable get() {
return drawable;
}
@Override
public int getSize() {
return decoder.getGifByteSize();
}
@Override
protected void recycleInternal() {
drawable.stop();
drawable.recycle();
}
}
package com.bumptech.glide.load.resource.gif;
import android.content.Context;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.io.IOException;
import java.io.InputStream;
public class GifResourceDecoder implements ResourceDecoder<InputStream, GifDrawable> {
private Context context;
private BitmapPool bitmapPool;
public GifResourceDecoder(Context context) {
this(context, Glide.get(context).getBitmapPool());
}
public GifResourceDecoder(Context context, BitmapPool bitmapPool) {
this.context = context;
this.bitmapPool = bitmapPool;
}
@Override
public GifResource decode(InputStream source, int width, int height) throws IOException {
GifDecoder gifDecoder = new GifDecoder(bitmapPool);
gifDecoder.read(source, 0);
GifFrameManager frameManager = new GifFrameManager(context, bitmapPool);
return new GifResource(gifDecoder, frameManager);
}
@Override
public String getId() {
return "GifResourceDecoder.com.bumptech.glide.load.gif";
}
}
package com.bumptech.glide.load.resource.gif;
import android.util.Log;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.ResourceEncoder;
import java.io.IOException;
import java.io.OutputStream;
public class GifResourceEncoder implements ResourceEncoder<GifDrawable> {
private static final String TAG = "GifEncoder";
@Override
public boolean encode(Resource<GifDrawable> resource, OutputStream os) {
boolean result = true;
try {
os.write(resource.get().getDecoder().getData());
} catch (IOException e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Failed to encode gif", e);
}
result = false;
}
return result;
}
@Override
public String getId() {
return "GifEncoder.com.bumptech.glide.load.resource.gif";
}
}
package com.bumptech.glide.load.resource.transcode;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.resource.drawable.DrawableResource;
import com.bumptech.glide.load.resource.drawable.GifBitmap;
public class GifBitmapDrawableTranscoder implements ResourceTranscoder<GifBitmap, Drawable> {
private final Resources resources;
public GifBitmapDrawableTranscoder(Context context) {
resources = context.getResources();
}
@Override
public Resource<Drawable> transcode(Resource<GifBitmap> toTranscode) {
GifBitmap gifBitmap = toTranscode.get();
Resource<Bitmap> bitmapResource = gifBitmap.getBitmapResource();
final Resource resource;
final Drawable drawable;
if (bitmapResource != null) {
resource = bitmapResource;
drawable = new BitmapDrawable(resources, bitmapResource.get());
} else {
resource = gifBitmap.getGifResource();
drawable = gifBitmap.getGifResource().get();
}
return new DrawableResource(drawable, resource);
}
@Override
public String getId() {
return "GifBitmapDrawableTranscoder.com.bumptech.glide.load.resource.transcode";
}
}
package com.bumptech.glide.load.resource.drawable;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import com.bumptech.glide.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class DrawableResourceTest {
private ColorDrawable drawable;
private DrawableResource resource;
private Resource wrapped;
@Before
public void setUp() {
drawable = new ColorDrawable(Color.RED);
wrapped = mock(Resource.class);
resource = new DrawableResource(drawable, wrapped);
}
@Test
public void testReturnsGivenSize() {
final int size = 100;
when(wrapped.getSize()).thenReturn(size);
assertEquals(size, resource.getSize());
}
@Test
public void testGetReturnsGivenDrawable() {
assertEquals(drawable, resource.get());
}
@Test
public void testRecyclesWrappedWhenRecycled() {
resource.recycleInternal();
verify(wrapped).recycle();
}
}
package com.bumptech.glide.load.resource.drawable;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.ParcelFileDescriptor;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.model.ImageVideoWrapper;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class GifBitmapResourceDecoderTest {
private ResourceDecoder<ImageVideoWrapper, Bitmap> bitmapDecoder;
private ResourceDecoder<InputStream, GifDrawable> gifDecoder;
private GifBitmapResourceDecoder decoder;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
bitmapDecoder = mock(ResourceDecoder.class);
gifDecoder = mock(ResourceDecoder.class);
decoder = new GifBitmapResourceDecoder(Robolectric.application, bitmapDecoder, gifDecoder);
}
@Test
public void testDecoderUsesGifDecoderResultIfGif() throws IOException {
GifDrawable expected = mock(GifDrawable.class);
Resource<GifDrawable> gifDrawableResource = mock(Resource.class);
when(gifDrawableResource.get()).thenReturn(expected);
when(gifDecoder.decode(any(InputStream.class), anyInt(), anyInt())).thenReturn(gifDrawableResource);
byte[] data = new byte[] { 'G', 'I', 'F'};
ImageVideoWrapper wrapper = new ImageVideoWrapper(new ByteArrayInputStream(data), null);
Resource<GifBitmap> result = decoder.decode(wrapper, 100, 100);
assertEquals(expected, result.get().getDrawable());
}
@Test
public void testDecoderUsesBitmapDecoderIfStreamIsNotGif() throws IOException {
Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Resource<Bitmap> bitmapResource = mock(Resource.class);
when(bitmapResource.get()).thenReturn(expected);
when(bitmapDecoder.decode(any(ImageVideoWrapper.class), anyInt(), anyInt())).thenReturn(bitmapResource);
byte[] data = new byte[] { 'A', 'I', 'F'};
ImageVideoWrapper wrapper = new ImageVideoWrapper(new ByteArrayInputStream(data), null);
Resource<GifBitmap> result = decoder.decode(wrapper, 100, 100);
BitmapDrawable bitmapDrawable = (BitmapDrawable) result.get().getDrawable();
assertEquals(expected, bitmapDrawable.getBitmap());
}
@Test
public void testDecoderUsesBitmapDecoderIfIsFileDescriptor() throws IOException {
Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Resource<Bitmap> bitmapResource = mock(Resource.class);
when(bitmapResource.get()).thenReturn(expected);
when(bitmapDecoder.decode(any(ImageVideoWrapper.class), anyInt(), anyInt())).thenReturn(bitmapResource);
ImageVideoWrapper wrapper = new ImageVideoWrapper(null, mock(ParcelFileDescriptor.class));
Resource<GifBitmap> result = decoder.decode(wrapper, 100, 100);
BitmapDrawable bitmapDrawable = (BitmapDrawable) result.get().getDrawable();
assertEquals(expected, bitmapDrawable.getBitmap());
}
}
package com.bumptech.glide.load.resource.drawable;
import android.graphics.Bitmap;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.any;
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 GifBitmapResourceEncoderTest {
private ResourceEncoder<Bitmap> bitmapEncoder;
private ResourceEncoder<GifDrawable> gifEncoder;
private GifBitmapResourceEncoder encoder;
private Resource<GifBitmap> resource;
private GifBitmap gifBitmap;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
bitmapEncoder = mock(ResourceEncoder.class);
gifEncoder = mock(ResourceEncoder.class);
encoder = new GifBitmapResourceEncoder(bitmapEncoder, gifEncoder);
resource = mock(Resource.class);
gifBitmap = mock(GifBitmap.class);
when(resource.get()).thenReturn(gifBitmap);
}
@Test
public void testEncodesWithBitmapEncoderIfHasBitmapResource() {
Resource<Bitmap> bitmapResource = mock(Resource.class);
when(gifBitmap.getBitmapResource()).thenReturn(bitmapResource);
ByteArrayOutputStream os = new ByteArrayOutputStream();
encoder.encode(resource, os);
verify(bitmapEncoder).encode(eq(bitmapResource), eq(os));
}
@Test
public void testReturnsBitmapEncoderResultIfHasBitmapResource() {
Resource<Bitmap> bitmapResource = mock(Resource.class);
when(gifBitmap.getBitmapResource()).thenReturn(bitmapResource);
when(bitmapEncoder.encode(any(Resource.class), any(OutputStream.class))).thenReturn(true);
assertTrue(encoder.encode(resource, new ByteArrayOutputStream()));
when(bitmapEncoder.encode(any(Resource.class), any(OutputStream.class))).thenReturn(false);
assertFalse(encoder.encode(resource, new ByteArrayOutputStream()));
}
@Test
public void testEncodesWithGifEncoderIfHasGif() {
Resource<GifDrawable> gifResource = mock(Resource.class);
when(gifBitmap.getGifResource()).thenReturn(gifResource);
ByteArrayOutputStream os = new ByteArrayOutputStream();
encoder.encode(resource, os);
verify(gifEncoder).encode(eq(gifResource), eq(os));
}
@Test
public void testReturnsGifEncoderResultIfHasGifResource() {
Resource<GifDrawable> gifResource = mock(Resource.class);
when(gifBitmap.getGifResource()).thenReturn(gifResource);
when(gifEncoder.encode(any(Resource.class), any(OutputStream.class))).thenReturn(true);
assertTrue(encoder.encode(resource, new ByteArrayOutputStream()));
when(gifEncoder.encode(any(Resource.class), any(OutputStream.class))).thenReturn(false);
assertFalse(encoder.encode(resource, new ByteArrayOutputStream()));
}
@Test
public void testReturnsNonNullId() {
assertNotNull(encoder.getId());
}
}
package com.bumptech.glide.load.resource.drawable;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.model.ImageVideoWrapper;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class GifBitmapStreamResourceDecoderTest {
ResourceDecoder<ImageVideoWrapper, GifBitmap> gifBitmapDecoder;
private GifBitmapStreamResourceDecoder decoder;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
gifBitmapDecoder = mock(ResourceDecoder.class);
decoder = new GifBitmapStreamResourceDecoder(gifBitmapDecoder);
}
@Test
public void testReturnsWrappedDecoderResult() throws IOException {
int width = 100;
int height = 110;
Resource<GifBitmap> expected = mock(Resource.class);
when(gifBitmapDecoder.decode(any(ImageVideoWrapper.class), eq(width), eq(height))).thenReturn(expected);
assertEquals(expected, decoder.decode(new ByteArrayInputStream(new byte[0]), width, height));
}
@Test
public void testPassesGivenInputStreamWrappedAsImageVideoWrapper() throws IOException {
final InputStream expected = new ByteArrayInputStream(new byte[0]);
when(gifBitmapDecoder.decode(any(ImageVideoWrapper.class), anyInt(), anyInt())).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
ImageVideoWrapper wrapper = (ImageVideoWrapper) invocation.getArguments()[0];
assertEquals(expected, wrapper.getStream());
return null;
}
});
decoder.decode(expected, 1, 2);
}
@Test
public void testReturnsNonNullId() {
assertNotNull(decoder.getId());
}
}
package com.bumptech.glide.load.resource.gif;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class GifDrawableTest {
private GifDecoder gifDecoder;
private GifDrawable drawable;
private GifFrameManager frameManager = mock(GifFrameManager.class);
private Drawable.Callback cb = mock(Drawable.Callback.class);
@Before
public void setUp() {
gifDecoder = mock(GifDecoder.class);
drawable = new GifDrawable(gifDecoder, frameManager);
drawable.setCallback(cb);
}
@Test
public void testReturnsDecoderWidth() {
int width = 123;
when(gifDecoder.getWidth()).thenReturn(width);
assertEquals(width, drawable.getIntrinsicWidth());
}
@Test
public void testReturnsDecoderHeight() {
int height = 321;
when(gifDecoder.getHeight()).thenReturn(height);
assertEquals(height, drawable.getIntrinsicHeight());
}
@Test
public void testShouldNotDrawNullBitmap() {
Canvas canvas = mock(Canvas.class);
drawable.draw(canvas);
verify(canvas, never()).drawBitmap((Bitmap) isNull(), anyInt(), anyInt(), any(Paint.class));
}
@Test
public void testRequestsNextFrameOnStart() {
drawable.start();
verify(frameManager).getNextFrame(eq(gifDecoder), eq(drawable));
}
@Test
public void testShouldInvalidateSelfOnRun() {
drawable.start();
verify(cb).invalidateDrawable(eq(drawable));
}
@Test
public void testShouldNotScheduleItselfIfAlreadyRunning() {
drawable.start();
drawable.start();
verify(frameManager, times(1)).getNextFrame(eq(gifDecoder), eq(drawable));
}
@Test
public void testReturnsFalseFromIsRunningWhenNotRunning() {
assertFalse(drawable.isRunning());
}
@Test
public void testReturnsTrueFromIsRunningWhenRunning() {
drawable.start();
assertTrue(drawable.isRunning());
}
@Test
public void testStartsLoadingNextFrameWhenCurrentFinishes() {
drawable.setIsRunning(true);
drawable.onFrameRead(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888));
verify(frameManager).getNextFrame(eq(gifDecoder), eq(drawable));
}
@Test
public void testInvalidatesSelfWhenFrameReady() {
drawable.setIsRunning(true);
drawable.onFrameRead(Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565));
verify(cb).invalidateDrawable(eq(drawable));
}
@Test
public void testDoesNotStartLoadingNextFrameWhenCurrentFinishesIfNotRunn() {
drawable.setIsRunning(false);
drawable.onFrameRead(Bitmap.createBitmap(10, 100, Bitmap.Config.ARGB_8888));
verify(frameManager, never()).getNextFrame(eq(gifDecoder), eq(drawable));
}
@Test
public void testSetsIsRunningFalseOnStop() {
drawable.start();
drawable.stop();
assertFalse(drawable.isRunning());
}
@Test
public void testStopsOnSetVisibleFalse() {
drawable.start();
drawable.setVisible(false, true);
assertFalse(drawable.isRunning());
}
@Test
public void testStartsOnSetVisibleTrue() {
drawable.setVisible(true, true);
assertTrue(drawable.isRunning());
}
@Test
public void testGetOpacityReturnsTransparentfDecoderHasTransparency() {
when(gifDecoder.isTransparent()).thenReturn(true);
assertEquals(PixelFormat.TRANSPARENT, drawable.getOpacity());
}
@Test
public void testGetOpacityReturnsOpaqueIfDecoderDoesNotHaveTransparency() {
when(gifDecoder.isTransparent()).thenReturn(false);
assertEquals(PixelFormat.OPAQUE, drawable.getOpacity());
}
@Test
public void testRecycleCallsClearOnFrameManager() {
drawable.recycle();
verify(frameManager).clear();
}
}
package com.bumptech.glide.load.resource.gif;
import android.graphics.Bitmap;
import com.bumptech.glide.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.io.IOException;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class GifFrameResourceDecoderTest {
private GifDecoder gifDecoder;
private GifFrameResourceDecoder resourceDecoder;
@Before
public void setUp() {
gifDecoder = mock(GifDecoder.class);
resourceDecoder = new GifFrameResourceDecoder();
}
@Test
public void testReturnsNonNullId() {
assertNotNull(resourceDecoder.getId());
}
@Test
public void testReturnsFrameFromGifDecoder() throws IOException {
Resource<Bitmap> resource = mock(Resource.class);
when(gifDecoder.getNextFrame()).thenReturn(resource);
assertEquals(resource, resourceDecoder.decode(gifDecoder, 100, 100));
}
}
package com.bumptech.glide.load.resource.gif;
import com.bumptech.glide.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class GifResourceEncoderTest {
private Resource<GifDrawable> resource;
private byte[] data;
private GifResourceEncoder encoder;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
resource = mock(Resource.class);
data = new byte[]{ 2, 3, 5, 8 };
GifDecoder decoder = mock(GifDecoder.class);
GifDrawable drawable = mock(GifDrawable.class);
when(drawable.getDecoder()).thenReturn(decoder);
when(resource.get()).thenReturn(drawable);
when(decoder.getData()).thenReturn(data);
encoder = new GifResourceEncoder();
}
@Test
public void testWritesDataToOutputStream() {
ByteArrayOutputStream os = new ByteArrayOutputStream();
encoder.encode(resource, os);
assertTrue(Arrays.equals(data, os.toByteArray()));
}
@Test
public void testReturnsTrueIfWriteCompletes() {
assertTrue(encoder.encode(resource, new ByteArrayOutputStream()));
}
@Test
public void testReturnsFalseIfWriteFails() {
OutputStream os = new ByteArrayOutputStream() {
@Override
public void write(byte[] buffer) throws IOException {
super.write(buffer); //To change body of overridden methods use File | Settings | File Templates.
throw new IOException("Test");
}
};
assertFalse(encoder.encode(resource, os));
}
}
package com.bumptech.glide.load.resource.gif;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class GifResourceTest {
private GifDecoder decoder;
private GifDrawable gifDrawable;
private GifResource resource;
@Before
public void setUp() {
decoder = mock(GifDecoder.class);
gifDrawable = mock(GifDrawable.class);
resource = new GifResource(decoder, gifDrawable);
}
@Test
public void testSizeReturnsGifDecoderGifByteSize() {
int size = 1234;
when(decoder.getGifByteSize()).thenReturn(size);
assertEquals(size, resource.getSize());
}
@Test
public void testReturnsNonNullDrawable() {
assertNotNull(resource.get());
}
@Test
public void testStopsGifDrawableOnRecycle() {
resource.recycle();
verify(gifDrawable).stop();
}
@Test
public void testRecyclesGifDrawableOnRecycle() {
resource.recycle();
verify(gifDrawable).recycle();
}
}
package com.bumptech.glide.load.resource.transcode;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.Resource;
import com.bumptech.glide.load.resource.drawable.GifBitmap;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class GifBitmapDrawableTranscoderTest {
private GifBitmapDrawableTranscoder transcoder;
@Before
public void setUp() {
transcoder = new GifBitmapDrawableTranscoder(Robolectric.application);
}
@Test
public void testReturnsBitmapDrawableIfGifBitmapHasBitmap() {
GifBitmapWithBitmapHarness harness = new GifBitmapWithBitmapHarness();
BitmapDrawable transcoded = (BitmapDrawable) transcoder.transcode(harness.gifBitmapResource).get();
assertEquals(harness.expected, transcoded.getBitmap());
}
@Test
public void testReturnedResourceHasBitmapSizeIfGifBitmapHasBitmap() {
final int size = 100;
GifBitmapWithBitmapHarness harness = new GifBitmapWithBitmapHarness();
when(harness.bitmapResource.getSize()).thenReturn(size);
Resource<Drawable> transcoded = transcoder.transcode(harness.gifBitmapResource);
assertEquals(size, transcoded.getSize());
}
@Test
public void testReturnsGifDrawableIfGifBitmapHasGif() {
GifBitmapWithGifHarness harness = new GifBitmapWithGifHarness();
Drawable transcoded = transcoder.transcode(harness.gifBitmapResource).get();
assertEquals(harness.expected, transcoded);
}
@Test
public void testReturnedResourceHasGifDrawableSizeIfGifBitmapHasGif() {
final int size = 200;
GifBitmapWithGifHarness harness = new GifBitmapWithGifHarness();
when(harness.gifResource.getSize()).thenReturn(size);
Resource<Drawable> transcoded = transcoder.transcode(harness.gifBitmapResource);
assertEquals(size, transcoded.getSize());
}
@Test
public void testHasNonNullId() {
assertNotNull(transcoder.getId());
}
private static class TranscoderHarness {
Resource<GifBitmap> gifBitmapResource = mock(Resource.class);
GifBitmap gifBitmap = mock(GifBitmap.class);
public TranscoderHarness() {
when(gifBitmapResource.get()).thenReturn(gifBitmap);
}
}
private static class GifBitmapWithBitmapHarness extends TranscoderHarness {
Bitmap expected = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
Resource<Bitmap> bitmapResource = mock(Resource.class);
public GifBitmapWithBitmapHarness() {
super();
when(bitmapResource.get()).thenReturn(expected);
when(gifBitmap.getBitmapResource()).thenReturn(bitmapResource);
}
}
private static class GifBitmapWithGifHarness extends TranscoderHarness {
GifDrawable expected = mock(GifDrawable.class);
Resource<GifDrawable> gifResource = mock(Resource.class);
public GifBitmapWithGifHarness() {
super();
when(gifResource.get()).thenReturn(expected);
when(gifBitmap.getGifResource()).thenReturn(gifResource);
}
}
}
......@@ -4,6 +4,11 @@
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/test"
android:background="@android:color/holo_red_light"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
......
......@@ -13,10 +13,13 @@ import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.gif.GifDecoder;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.samples.flickr.api.Api;
import com.bumptech.glide.samples.flickr.api.Photo;
......@@ -75,6 +78,16 @@ public class FlickrSearchActivity extends SherlockFragmentActivity {
.penaltyLog()
.build());
InputStream inputStream = getResources().openRawResource(R.raw.test_animated);
GifDecoder gifDecoder = new GifDecoder();
gifDecoder.read(inputStream, 0);
GifDrawable drawable = new GifDrawable(gifDecoder);
ImageView testView = (ImageView) findViewById(R.id.test);
drawable.setCallback(testView);
testView.setImageDrawable(drawable);
drawable.start();
Glide.get(this).register(Photo.class, InputStream.class, new FlickrModelLoader.Factory());
searching = findViewById(R.id.searching);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册