diff --git a/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java b/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java index 4f7f8302b2d42371d8b4d5e55d98cedd6ec1eb7a..340a4850b273622255e13c364bb93d08fcef7ff1 100644 --- a/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java +++ b/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java @@ -92,7 +92,7 @@ public class DrawableRequestBuilder extends } public DrawableRequestBuilder bitmapTransform(Transformation bitmapTransformation) { - return transform(new GifBitmapWrapperTransformation(context, bitmapTransformation)); + return transform(new GifBitmapWrapperTransformation(bitmapTransformation)); } @Override diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifData.java b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifData.java index 34e6537ab82868d23280d0d1c2954a058cf76ff1..c57d08fc0e167333a98609ad84614368745b1102 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifData.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifData.java @@ -16,15 +16,20 @@ public class GifData { private final GifHeader header; private final byte[] data; private String gifId; + private final int targetWidth; + private final int targetHeight; private final List drawables = new ArrayList(); private Transformation frameTransformation; - public GifData(Context context, BitmapPool bitmapPool, String gifId, GifHeader header, byte[] data) { + public GifData(Context context, BitmapPool bitmapPool, String gifId, GifHeader header, byte[] data, + int targetWidth, int targetHeight) { this.context = context; this.bitmapPool = bitmapPool; this.header = header; this.data = data; this.gifId = gifId; + this.targetWidth = targetWidth; + this.targetHeight = targetHeight; } @SuppressWarnings("unchecked") @@ -47,7 +52,8 @@ public class GifData { public GifDrawable getDrawable() { GifDecoder gifDecoder = new GifDecoder(bitmapPool); gifDecoder.setData(gifId, header, data); - GifFrameManager frameManager = new GifFrameManager(context, gifDecoder, getFrameTransformation()); + GifFrameManager frameManager = new GifFrameManager(context, gifDecoder, getFrameTransformation(), + targetWidth, targetHeight); GifDrawable result = new GifDrawable(gifDecoder, frameManager); drawables.add(result); diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDataTransformation.java b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDataTransformation.java new file mode 100644 index 0000000000000000000000000000000000000000..cb2b9924a726701c950c89c65a47a46c0dee41cb --- /dev/null +++ b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDataTransformation.java @@ -0,0 +1,28 @@ +package com.bumptech.glide.load.resource.gif; + +import android.graphics.Bitmap; +import com.bumptech.glide.Resource; +import com.bumptech.glide.load.MultiTransformation; +import com.bumptech.glide.load.Transformation; + +public class GifDataTransformation implements Transformation { + private Transformation wrapped; + + public GifDataTransformation(Transformation wrapped) { + this.wrapped = wrapped; + } + + @Override + public Resource transform(Resource resource, int outWidth, int outHeight) { + GifData data = resource.get(); + Transformation newTransformation = + new MultiTransformation(data.getFrameTransformation(), wrapped); + data.setFrameTransformation(newTransformation); + return resource; + } + + @Override + public String getId() { + return wrapped.getId(); + } +} diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java index 636c1c3f063c1410eecc20bbe367115c8d874ca9..ae2846619e0293c231e01d1912270d9846f15b9e 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java @@ -13,6 +13,8 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager private final Paint paint; private final GifFrameManager frameManager; + private int width; + private int height; private GifDecoder decoder; private boolean isRunning; private Bitmap currentFrame; @@ -21,6 +23,8 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager public GifDrawable(GifDecoder decoder, GifFrameManager frameManager) { this.decoder = decoder; this.frameManager = frameManager; + width = -1; + height = -1; paint = new Paint(); } @@ -44,14 +48,15 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager return super.setVisible(visible, restart); } + @Override public int getIntrinsicWidth() { - return decoder.getWidth(); + return width; } @Override public int getIntrinsicHeight() { - return decoder.getHeight(); + return height; } @Override @@ -96,6 +101,12 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager if (!isRunning) { return; } + if (width == -1) { + width = frame.getWidth(); + } + if (height == -1) { + height = frame.getHeight(); + } if (frame != null) { currentFrame = frame; diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameManager.java b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameManager.java index eab88f49ac6d2742b0e4c5949f890628c837b7ac..f79d3bbb8c69ed201a2f8b26bd41164e66acb3b9 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameManager.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameManager.java @@ -32,23 +32,29 @@ class GifFrameManager { private final Context context; private Transformation transformation; + private final int targetWidth; + private final int targetHeight; private DelayTarget current; private DelayTarget next; + private int frameSize = -1; public interface FrameCallback { public void onFrameRead(Bitmap frame); } - public GifFrameManager(Context context, GifDecoder decoder, Transformation transformation) { - this(context, decoder, new Handler(Looper.getMainLooper()), transformation); + public GifFrameManager(Context context, GifDecoder decoder, Transformation transformation, int targetWidth, + int targetHeight) { + this(context, decoder, new Handler(Looper.getMainLooper()), transformation, targetWidth, targetHeight); } public GifFrameManager(Context context, GifDecoder decoder, Handler mainHandler, - Transformation transformation) { + Transformation transformation, int targetWidth, int targetHeight) { this.context = context; this.decoder = decoder; this.mainHandler = mainHandler; this.transformation = transformation; + this.targetWidth = targetWidth; + this.targetHeight = targetHeight; calculator = new MemorySizeCalculator(context); frameLoader = new GifFrameModelLoader(); frameResourceDecoder = new GifFrameResourceDecoder(); @@ -70,14 +76,22 @@ class GifFrameManager { return transformation; } + private int getEstimatedTotalFrameSize() { + if (frameSize == -1) { + return decoder.getDecodedFramesByteSizeSum(); + } else { + return frameSize * decoder.getFrameCount(); + } + } + public void getNextFrame(FrameCallback cb) { decoder.advance(); // We don't want to blow out the entire memory cache with frames of gifs, so try to set some // maximum size beyond which we will always just decode one frame at a time. - boolean skipCache = decoder.getDecodedFramesByteSizeSum() > calculator.getMemoryCacheSize() / 2; + boolean skipCache = getEstimatedTotalFrameSize() > calculator.getMemoryCacheSize() / 2; long targetTime = SystemClock.uptimeMillis() + (Math.min(MIN_FRAME_DELAY, decoder.getNextDelay())); - next = new DelayTarget(decoder, cb, targetTime, mainHandler); + next = new DelayTarget(cb, targetTime); Glide.with(context) .using(frameLoader, GifDecoder.class) @@ -105,18 +119,18 @@ class GifFrameManager { class DelayTarget extends SimpleTarget 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()); + public DelayTarget(FrameCallback cb, long targetTime) { + super(targetWidth, targetHeight); this.cb = cb; this.targetTime = targetTime; - this.mainHandler = mainHandler; } @Override public void onResourceReady(final Bitmap resource) { + // Ignore allocationByteSize, we only want the minimum frame size. + frameSize = resource.getHeight() * resource.getRowBytes(); this.resource = resource; mainHandler.postAtTime(this, targetTime); if (current != null) { diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceDecoder.java b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceDecoder.java index 423252cbbeb09cb353c27b34636fa8c774356098..c22d5a999851b59f091564cc4cf0615c0c07901d 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceDecoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceDecoder.java @@ -35,7 +35,7 @@ public class GifResourceDecoder implements ResourceDecoder byte[] data = inputStreamToBytes(source); GifHeader header = new GifHeaderParser(data).parseHeader(); String id = getGifId(data); - return new GifDataResource(new GifData(context, bitmapPool, id, header, data)); + return new GifDataResource(new GifData(context, bitmapPool, id, header, data, width, height)); } @Override diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformation.java b/library/src/main/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformation.java index 50d2fb71d5ce6594c2dd256e9d040616a35948f9..9b4ffcfe8371919774cda8a17c78e2172997ef5b 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformation.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformation.java @@ -1,45 +1,47 @@ package com.bumptech.glide.load.resource.gifbitmap; -import android.content.Context; import android.graphics.Bitmap; import com.bumptech.glide.Resource; -import com.bumptech.glide.load.MultiTransformation; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.resource.gif.GifData; -import com.bumptech.glide.load.resource.gif.GifDataResource; +import com.bumptech.glide.load.resource.gif.GifDataTransformation; public class GifBitmapWrapperTransformation implements Transformation { - private Context context; - private Transformation wrapped; + private Transformation bitmapTransformation; + private Transformation gifDataTransformation; - public GifBitmapWrapperTransformation(Context context, Transformation wrapped) { - this.context = context; - this.wrapped = wrapped; + public GifBitmapWrapperTransformation(Transformation bitmapTransformation) { + this(bitmapTransformation, new GifDataTransformation(bitmapTransformation)); + } + + GifBitmapWrapperTransformation(Transformation bitmapTransformation, + Transformation gifDataTransformation) { + this.bitmapTransformation = bitmapTransformation; + this.gifDataTransformation = gifDataTransformation; } @Override public Resource transform(Resource resource, int outWidth, int outHeight) { Resource bitmapResource = resource.get().getBitmapResource(); - if (bitmapResource != null) { - Resource transformed = wrapped.transform(bitmapResource, outWidth, outHeight); + Resource gifResource = resource.get().getGifResource(); + if (bitmapResource != null && bitmapTransformation != null) { + Resource transformed = bitmapTransformation.transform(bitmapResource, outWidth, outHeight); if (transformed != bitmapResource) { - GifBitmapWrapper gifBitmap = new GifBitmapWrapper(transformed, null); + GifBitmapWrapper gifBitmap = new GifBitmapWrapper(transformed, resource.get().getGifResource()); + return new GifBitmapWrapperResource(gifBitmap); + } + } else if (gifResource != null && gifDataTransformation != null) { + Resource transformed = gifDataTransformation.transform(gifResource, outWidth, outHeight); + if (transformed != gifResource) { + GifBitmapWrapper gifBitmap = new GifBitmapWrapper(resource.get().getBitmapResource(), transformed); return new GifBitmapWrapperResource(gifBitmap); } - } else { - //TODO: this should be pushed down into a GifData transformation? - Resource gifResource = resource.get().getGifResource(); - GifData gifData = gifResource.get(); - Transformation newTransformation = - new MultiTransformation(gifData.getFrameTransformation(), wrapped); - gifData.setFrameTransformation(newTransformation); - return new GifBitmapWrapperResource(new GifBitmapWrapper(null, new GifDataResource(gifData))); } return resource; } @Override public String getId() { - return wrapped.getId(); + return bitmapTransformation.getId(); } } diff --git a/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTest.java b/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTest.java index af411f77c8939b718e36faf9378be3feb077d849..853339383b61f719fa3548766a5545ce2b301127 100644 --- a/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTest.java +++ b/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTest.java @@ -29,7 +29,7 @@ public class GifDataTest { BitmapPool bitmapPool = mock(BitmapPool.class); GifHeader header = mock(GifHeader.class); bytes = new byte[] { 'G', 'I', 'F' }; - data = new GifData(Robolectric.application, bitmapPool, "gifId", header, bytes); + data = new GifData(Robolectric.application, bitmapPool, "gifId", header, bytes, 123, 456); } @Test diff --git a/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTransformationTest.java b/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTransformationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e2e345cf224a2225e028f384888cf15258fc32b9 --- /dev/null +++ b/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTransformationTest.java @@ -0,0 +1,66 @@ +package com.bumptech.glide.load.resource.gif; + +import android.graphics.Bitmap; +import com.bumptech.glide.Resource; +import com.bumptech.glide.load.Transformation; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.robolectric.RobolectricTestRunner; + +import static junit.framework.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(RobolectricTestRunner.class) +public class GifDataTransformationTest { + Transformation wrapped; + GifDataTransformation transformation; + + @SuppressWarnings("unchecked") + @Before + public void setUp() { + wrapped = mock(Transformation.class); + transformation = new GifDataTransformation(wrapped); + } + + @Test + public void testReturnsWrappedTransformationId() { + final String id = "testId"; + when(wrapped.getId()).thenReturn(id); + + assertEquals(id, transformation.getId()); + } + + @Test + public void testSetsTransformationAsFrameTransformation() { + Resource resource = mock(Resource.class); + GifData gifData = mock(GifData.class); + when(gifData.getFrameTransformation()).thenReturn(Transformation.NONE); + when(resource.get()).thenReturn(gifData); + + final Resource toTransform = mock(Resource.class); + + final int width = 123; + final int height = 456; + doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + Transformation transformation = (Transformation) invocation.getArguments()[0]; + transformation.transform(toTransform, width, height); + return null; + } + }).when(gifData).setFrameTransformation(any(Transformation.class)); + + transformation.transform(resource, width, height); + + verify(gifData).setFrameTransformation(any(Transformation.class)); + verify(wrapped).transform(eq(toTransform), eq(width), eq(height)); + } +} diff --git a/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDrawableTest.java b/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDrawableTest.java index 08745731f534bb3486fb2c5782768a92b0baf860..7ef58bca485a59d682bbdc2652481a9a8ee9d28c 100644 --- a/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDrawableTest.java +++ b/library/src/test/java/com/bumptech/glide/load/resource/gif/GifDrawableTest.java @@ -39,17 +39,33 @@ public class GifDrawableTest { } @Test - public void testReturnsDecoderWidth() { + public void testReturnsInvalidWidthBeforeFirstFrame() { + assertEquals(-1, drawable.getIntrinsicWidth()); + } + + @Test + public void testReturnsInvalidHeightBeforeFirstFrame() { + assertEquals(-1, drawable.getIntrinsicHeight()); + } + + @Test + public void testReturnsFrameWidthAfterFirstFrame() { int width = 123; - when(gifDecoder.getWidth()).thenReturn(width); + Bitmap bitmap = Bitmap.createBitmap(width, 1231, Bitmap.Config.ARGB_8888); + + drawable.setIsRunning(true); + drawable.onFrameRead(bitmap); assertEquals(width, drawable.getIntrinsicWidth()); } @Test - public void testReturnsDecoderHeight() { - int height = 321; - when(gifDecoder.getHeight()).thenReturn(height); + public void testReturnsFrameHeightAfterFirstFrame() { + int height = 456; + Bitmap bitmap = Bitmap.createBitmap(1, height, Bitmap.Config.RGB_565); + + drawable.setIsRunning(true); + drawable.onFrameRead(bitmap); assertEquals(height, drawable.getIntrinsicHeight()); } diff --git a/library/src/test/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformationTest.java b/library/src/test/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformationTest.java index 5e173774d5beda95a2a357e13cf6039b7ad2982d..e0cd2560f58760d87343151594114a1806d67fc7 100644 --- a/library/src/test/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformationTest.java +++ b/library/src/test/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformationTest.java @@ -7,11 +7,11 @@ import com.bumptech.glide.load.resource.gif.GifData; 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.assertNotSame; +import static junit.framework.Assert.assertSame; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -19,13 +19,43 @@ import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) public class GifBitmapWrapperTransformationTest { private Transformation bitmapTransformation; - private GifBitmapWrapperTransformation transformation; + private Transformation gifTransformation; @SuppressWarnings("unchecked") @Before public void setUp() { bitmapTransformation = mock(Transformation.class); - transformation = new GifBitmapWrapperTransformation(Robolectric.application, bitmapTransformation); + gifTransformation = mock(Transformation.class); + } + + private class BitmapResourceHarness { + Resource bitmapResource = mock(Resource.class); + GifBitmapWrapper gifBitmapWrapper = mock(GifBitmapWrapper.class); + Resource resource = mock(Resource.class); + GifBitmapWrapperTransformation transformation = new GifBitmapWrapperTransformation(bitmapTransformation); + int width = 123; + int height = 456; + + public BitmapResourceHarness() { + when(gifBitmapWrapper.getBitmapResource()).thenReturn(bitmapResource); + when(resource.get()).thenReturn(gifBitmapWrapper); + } + } + + private class GifResourceHarness { + GifData gifData = mock(GifData.class); + Resource gifResource = mock(Resource.class); + GifBitmapWrapper gifBitmapWrapper = mock(GifBitmapWrapper.class); + Resource resource = mock(Resource.class); + GifBitmapWrapperTransformation transformation = new GifBitmapWrapperTransformation(null, gifTransformation); + int width = 123; + int height = 456; + + public GifResourceHarness() { + when(gifResource.get()).thenReturn(gifData); + when(gifBitmapWrapper.getGifResource()).thenReturn(gifResource); + when(resource.get()).thenReturn(gifBitmapWrapper); + } } @Test @@ -33,51 +63,80 @@ public class GifBitmapWrapperTransformationTest { String expectedId = "asdfas"; when(bitmapTransformation.getId()).thenReturn(expectedId); - assertEquals(expectedId, transformation.getId()); + assertEquals(expectedId, new GifBitmapWrapperTransformation(bitmapTransformation).getId()); } @Test - public void testAppliesTransformationToBitmapResourceAndReturnsNewGifBitmapResource() { - int dimens = 123; - Resource initial = mock(Resource.class); + public void testAppliesBitmapTransformationIfBitmapTransformationIsGivenAndResourceHasBitmapResource() { + BitmapResourceHarness harness = new BitmapResourceHarness(); - Resource transformed = mock(Resource.class); - when(bitmapTransformation.transform(eq(initial), eq(dimens), eq(dimens))).thenReturn(transformed); + Resource transformedBitmapResource = mock(Resource.class); + when(bitmapTransformation.transform(eq(harness.bitmapResource), eq(harness.width), eq(harness.height))) + .thenReturn(transformedBitmapResource); + Resource transformed = harness.transformation.transform(harness.resource, harness.width, + harness.height); - GifBitmapWrapper gifBitmap = mock(GifBitmapWrapper.class); - when(gifBitmap.getBitmapResource()).thenReturn(initial); - Resource gifBitmapResource = mock(Resource.class); - when(gifBitmapResource.get()).thenReturn(gifBitmap); + assertNotSame(harness.resource, transformed); + assertEquals(transformedBitmapResource, transformed.get().getBitmapResource()); + } + + @Test + public void testReturnsOriginalResourceIfTransformationDoesNotTransformGivenBitmapResource() { + BitmapResourceHarness harness = new BitmapResourceHarness(); - assertEquals(transformed, transformation.transform(gifBitmapResource, dimens, dimens).get() - .getBitmapResource()); + when(bitmapTransformation.transform(eq(harness.bitmapResource), eq(harness.width), eq(harness.height))) + .thenReturn(harness.bitmapResource); + Resource transformed = harness.transformation.transform(harness.resource, harness.width, + harness.height); + + assertSame(harness.resource, transformed); } @Test - public void testReturnsNewGifBitmapResourceIfNoBitmapResource() { - GifBitmapWrapper gifBitmap = mock(GifBitmapWrapper.class); - Resource gifBitmapResource = mock(Resource.class); - when(gifBitmapResource.get()).thenReturn(gifBitmap); + public void testReturnsOriginalResourceIfBitmapTransformationIsGivenButResourceHasNoBitmapResource() { + BitmapResourceHarness harness = new BitmapResourceHarness(); + when(harness.gifBitmapWrapper.getBitmapResource()).thenReturn(null); - GifData gifData = mock(GifData.class); - Resource gifDataResource = mock(Resource.class); - when(gifDataResource.get()).thenReturn(gifData); - when(gifBitmap.getGifResource()).thenReturn(gifDataResource); + Resource transformed = harness.transformation.transform(harness.resource, harness.width, + harness.height); - assertNotSame(gifBitmapResource, transformation.transform(gifBitmapResource, 100, 100)); + assertSame(harness.resource, transformed); } @Test - public void testReturnsGivenResourceIfWrappedTransformationDoesNotTransformBitmapResource() { - int dimens = 321; - Resource initial = mock(Resource.class); - GifBitmapWrapper gifBitmap = mock(GifBitmapWrapper.class); - when(gifBitmap.getBitmapResource()).thenReturn(initial); - Resource gifBitmapResource = mock(Resource.class); - when(gifBitmapResource.get()).thenReturn(gifBitmap); + public void testAppliesGifTransformationIfGifTransformationGivenAndResourceHasGifResource() { + GifResourceHarness harness = new GifResourceHarness(); + Resource transformedGifResource = mock(Resource.class); + when(gifTransformation.transform(eq(harness.gifResource), eq(harness.width), eq(harness.height))) + .thenReturn(transformedGifResource); + Resource transformed = harness.transformation.transform(harness.resource, harness.width, + harness.height); + + assertNotSame(harness.resource, transformed); + assertEquals(transformedGifResource, transformed.get().getGifResource()); + } + + @Test + public void testReturnsOriginalresourceIfTransformationDoesNotTransformGivenGifResource() { + GifResourceHarness harness = new GifResourceHarness(); + when(gifTransformation.transform(eq(harness.gifResource), eq(harness.width), eq(harness.height))) + .thenReturn(harness.gifResource); - when(bitmapTransformation.transform(eq(initial), eq(dimens), eq(dimens))).thenReturn(initial); + Resource transformed = harness.transformation.transform(harness.resource, harness.width, + harness.height); - assertEquals(gifBitmapResource, transformation.transform(gifBitmapResource, dimens, dimens)); + assertSame(harness.resource, transformed); + } + + @Test + public void testReturnsOriginalResourceIfGifTransformationIsGivenButResourceHasNoGifResource() { + GifResourceHarness harness = new GifResourceHarness(); + when(harness.gifBitmapWrapper.getGifResource()).thenReturn(null); + + Resource transformed = harness.transformation.transform(harness.resource, harness.width, + harness.height); + + assertSame(harness.resource, transformed); } } +