diff --git a/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java b/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java index 1ba119546bb56d3a97fde8f680c7bb5aa2fbb5f8..3375caef0e4534002b74310f1526c268fdb08100 100644 --- a/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java +++ b/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java @@ -13,34 +13,35 @@ import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.model.ImageVideoWrapper; import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; +import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperTransformation; import com.bumptech.glide.load.resource.transcode.ResourceTranscoder; import com.bumptech.glide.manager.Lifecycle; import com.bumptech.glide.manager.RequestTracker; import com.bumptech.glide.provider.LoadProvider; -import com.bumptech.glide.request.animation.DrawableCrossFadeViewAnimation; import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.animation.DrawableCrossFadeViewAnimation; import com.bumptech.glide.request.animation.ViewPropertyAnimation; import com.bumptech.glide.request.target.Target; import java.io.File; /** - * A class for creating a request to load a {@link android.graphics.drawable.Drawable}. + * A class for creating a request to load a {@link GlideDrawable}. * * @param The type of model that will be loaded into the target. */ public class DrawableRequestBuilder - extends GenericRequestBuilder + extends GenericRequestBuilder implements BitmapOptions, DrawableOptions { private final Glide glide; private final Context context; DrawableRequestBuilder(Context context, ModelType model, - LoadProvider loadProvider, Glide glide, + LoadProvider loadProvider, Glide glide, RequestTracker requestTracker, Lifecycle lifecycle) { - super(context, model, loadProvider, Drawable.class, glide, requestTracker, lifecycle); + super(context, model, loadProvider, GlideDrawable.class, glide, requestTracker, lifecycle); this.context = context; this.glide = glide; @@ -49,17 +50,17 @@ public class DrawableRequestBuilder } /** - * Loads and displays the {@link Drawable} retrieved by the given thumbnail request if it finishes before this - * request. Best used for loading thumbnail {@link Drawable}s that are smaller and will be loaded more quickly - * than the fullsize {@link Drawable}. There are no guarantees about the order in which the requests will actually - * finish. However, if the thumb request completes after the full request, the thumb {@link Drawable} will never - * replace the full image. + * Loads and displays the {@link GlideDrawable} retrieved by the given thumbnail request if it finishes before this + * request. Best used for loading thumbnail {@link GlideDrawable}s that are smaller and will be loaded more quickly + * than the fullsize {@link GlideDrawable}. There are no guarantees about the order in which the requests will + * actually finish. However, if the thumb request completes after the full request, the thumb {@link GlideDrawable} + * will never replace the full image. * * @see #thumbnail(float) * *

* Note - Any options on the main request will not be passed on to the thumbnail request. For example, if - * you want an animation to occur when either the full {@link Drawable} loads or the thumbnail loads, + * you want an animation to occur when either the full {@link GlideDrawable} loads or the thumbnail loads, * you need to call {@link #animate(int)} on both the thumb and the full request. For a simpler thumbnail * option where these options are applied to the humbnail as well, see {@link #thumbnail(float)}. *

@@ -82,7 +83,7 @@ public class DrawableRequestBuilder */ @Override public DrawableRequestBuilder thumbnail( - GenericRequestBuilder thumbnailRequest) { + GenericRequestBuilder thumbnailRequest) { super.thumbnail(thumbnailRequest); return this; } @@ -142,7 +143,7 @@ public class DrawableRequestBuilder } /** - * Transform {@link android.graphics.drawable.Drawable}s using the given + * Transform {@link GlideDrawable}s using the given * {@link com.bumptech.glide.load.resource.bitmap.BitmapTransformation}s. * *

@@ -163,7 +164,7 @@ public class DrawableRequestBuilder } /** - * Transform {@link Drawable}s using {@link com.bumptech.glide.load.resource.bitmap.CenterCrop}. + * Transform {@link GlideDrawable}s using {@link com.bumptech.glide.load.resource.bitmap.CenterCrop}. * * @see #fitCenter() * @see #transform(com.bumptech.glide.load.resource.bitmap.BitmapTransformation...) @@ -177,8 +178,7 @@ public class DrawableRequestBuilder } /** - * Transform {@link android.graphics.drawable.Drawable}s using - * {@link com.bumptech.glide.load.resource.bitmap.FitCenter}. + * Transform {@link GlideDrawable}s using {@link com.bumptech.glide.load.resource.bitmap.FitCenter}. * * @see #centerCrop() * @see #transform(com.bumptech.glide.load.resource.bitmap.BitmapTransformation...) @@ -192,8 +192,8 @@ public class DrawableRequestBuilder } /** - * Transform {@link android.graphics.drawable.Drawable}s using the given {@link android.graphics.Bitmap} - * transformations. Replaces any previous transformations. + * Transform {@link GlideDrawable}s using the given {@link android.graphics.Bitmap} transformations. Replaces any + * previous transformations. * * @see #fitCenter() * @see #centerCrop() @@ -228,7 +228,8 @@ public class DrawableRequestBuilder * {@inheritDoc} */ @Override - public DrawableRequestBuilder transcoder(ResourceTranscoder transcoder) { + public DrawableRequestBuilder transcoder( + ResourceTranscoder transcoder) { super.transcoder(transcoder); return this; } @@ -237,7 +238,7 @@ public class DrawableRequestBuilder * {@inheritDoc} */ public DrawableRequestBuilder crossFade() { - super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory()); + super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory()); return this; } @@ -245,7 +246,7 @@ public class DrawableRequestBuilder * {@inheritDoc} */ public DrawableRequestBuilder crossFade(int duration) { - super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory(duration)); + super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory(duration)); return this; } @@ -253,7 +254,7 @@ public class DrawableRequestBuilder * {@inheritDoc} */ public DrawableRequestBuilder crossFade(Animation animation, int duration) { - super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory(animation, duration)); + super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory(animation, duration)); return this; } @@ -261,7 +262,7 @@ public class DrawableRequestBuilder * {@inheritDoc} */ public DrawableRequestBuilder crossFade(int animationId, int duration) { - super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory(context, animationId, + super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory(context, animationId, duration)); return this; } @@ -342,7 +343,7 @@ public class DrawableRequestBuilder * {@inheritDoc} */ @Override - public DrawableRequestBuilder listener(RequestListener requestListener) { + public DrawableRequestBuilder listener(RequestListener requestListener) { super.listener(requestListener); return this; } @@ -405,7 +406,7 @@ public class DrawableRequestBuilder * @return {@inheritDoc} */ @Override - public Target into(ImageView view) { + public Target into(ImageView view) { return super.into(view); } diff --git a/library/src/main/java/com/bumptech/glide/DrawableTypeRequest.java b/library/src/main/java/com/bumptech/glide/DrawableTypeRequest.java index fc68ae7e413adf17df3bd5db04c0ed420d67e9c5..cf36024911fa6cdbce9156fc2f643694f299ab36 100644 --- a/library/src/main/java/com/bumptech/glide/DrawableTypeRequest.java +++ b/library/src/main/java/com/bumptech/glide/DrawableTypeRequest.java @@ -1,12 +1,12 @@ package com.bumptech.glide; import android.content.Context; -import android.graphics.drawable.Drawable; import android.os.ParcelFileDescriptor; import com.bumptech.glide.load.model.ImageVideoModelLoader; import com.bumptech.glide.load.model.ImageVideoWrapper; import com.bumptech.glide.load.model.ModelLoader; +import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; import com.bumptech.glide.load.resource.transcode.ResourceTranscoder; import com.bumptech.glide.manager.Lifecycle; @@ -61,7 +61,7 @@ public class DrawableTypeRequest extends DrawableRequestBuilder glideAnimation) { + public void onResourceReady(final Bitmap resource, GlideAnimation glideAnimation) { this.resource = resource; mainHandler.postAtTime(this, targetTime); } diff --git a/library/src/main/java/com/bumptech/glide/load/resource/transcode/GifBitmapWrapperDrawableTranscoder.java b/library/src/main/java/com/bumptech/glide/load/resource/transcode/GifBitmapWrapperDrawableTranscoder.java index c453b092e8a977fead2211595405ae410397b250..d226d68d8a7911d3e740b1171dac89d16f58764a 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/transcode/GifBitmapWrapperDrawableTranscoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/transcode/GifBitmapWrapperDrawableTranscoder.java @@ -1,9 +1,10 @@ package com.bumptech.glide.load.resource.transcode; import android.graphics.Bitmap; -import android.graphics.drawable.Drawable; import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable; +import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; /** @@ -11,28 +12,28 @@ import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; * {@link Bitmap} or an {@link com.bumptech.glide.load.resource.gif.GifDrawable} into an * {@link android.graphics.drawable.Drawable}. */ -public class GifBitmapWrapperDrawableTranscoder implements ResourceTranscoder { - private final ResourceTranscoder bitmapDrawableResourceTranscoder; +public class GifBitmapWrapperDrawableTranscoder implements ResourceTranscoder { + private final ResourceTranscoder bitmapDrawableResourceTranscoder; public GifBitmapWrapperDrawableTranscoder( - ResourceTranscoder bitmapDrawableResourceTranscoder) { + ResourceTranscoder bitmapDrawableResourceTranscoder) { this.bitmapDrawableResourceTranscoder = bitmapDrawableResourceTranscoder; } @SuppressWarnings("unchecked") @Override - public Resource transcode(Resource toTranscode) { + public Resource transcode(Resource toTranscode) { GifBitmapWrapper gifBitmap = toTranscode.get(); Resource bitmapResource = gifBitmap.getBitmapResource(); - final Resource result; + final Resource result; if (bitmapResource != null) { result = bitmapDrawableResourceTranscoder.transcode(bitmapResource); } else { result = gifBitmap.getGifResource(); } // This is unchecked but always safe, anything that extends a Drawable can be safely cast to a Drawable. - return (Resource) result; + return (Resource) result; } @Override diff --git a/library/src/main/java/com/bumptech/glide/load/resource/transcode/GlideBitmapDrawableTranscoder.java b/library/src/main/java/com/bumptech/glide/load/resource/transcode/GlideBitmapDrawableTranscoder.java index b1c55b40f040fe2dd8acd4873abda52a45de4017..9d9ed5d41a05895cfc57b7ec43feb86e9411b89b 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/transcode/GlideBitmapDrawableTranscoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/transcode/GlideBitmapDrawableTranscoder.java @@ -29,6 +29,6 @@ public class GlideBitmapDrawableTranscoder implements ResourceTranscoder implements FutureTarget, Runnable { * A callback that should never be invoked directly. */ @Override - public synchronized void onResourceReady(R resource, GlideAnimation glideAnimation) { + public synchronized void onResourceReady(R resource, GlideAnimation glideAnimation) { // We might get a null result. resultReceived = true; this.resource = resource; diff --git a/library/src/main/java/com/bumptech/glide/request/target/DrawableImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/DrawableImageViewTarget.java index c55fde7e1a3ab51be14b37343a1601f6c8c65a6b..e21babd9ebfbdd2750692f07307b32268c61edb0 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/DrawableImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/DrawableImageViewTarget.java @@ -1,23 +1,43 @@ package com.bumptech.glide.request.target; -import android.graphics.drawable.Animatable; -import android.graphics.drawable.Drawable; import android.widget.ImageView; +import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.request.animation.GlideAnimation; /** * A {@link com.bumptech.glide.request.target.Target} that can display an {@link android.graphics.drawable.Drawable} in * an {@link android.widget.ImageView}. */ -public class DrawableImageViewTarget extends ImageViewTarget { +public class DrawableImageViewTarget extends ImageViewTarget { private static final float SQUARE_RATIO_MARGIN = 0.05f; private final ImageView view; - private Drawable resource; + private int maxLoopCount; + private GlideDrawable resource; + /** + * Constructor for an {@link com.bumptech.glide.request.target.Target} that can display an + * {@link com.bumptech.glide.load.resource.drawable.GlideDrawable} in an {@link android.widget.ImageView}. + * + * @param view The view to display the drawable in. + */ public DrawableImageViewTarget(ImageView view) { + this(view, GlideDrawable.LOOP_FOREVER); + } + + /** + * Constructor for an {@link com.bumptech.glide.request.target.Target} that can display an + * {@link com.bumptech.glide.load.resource.drawable.GlideDrawable} in an {@link android.widget.ImageView}. + * + * @param view The view to display the drawable in. + * @param maxLoopCount A value to pass to to {@link com.bumptech.glide.load.resource.drawable.GlideDrawable}s + * indicating how many times they should repeat their animation (if they have one). See + * {@link com.bumptech.glide.load.resource.drawable.GlideDrawable#setLoopCount(int)}. + */ + public DrawableImageViewTarget(ImageView view, int maxLoopCount) { super(view); this.view = view; + this.maxLoopCount = maxLoopCount; } /** @@ -30,8 +50,8 @@ public class DrawableImageViewTarget extends ImageViewTarget { * @param animation {@inheritDoc} */ @Override - public void onResourceReady(Drawable resource, GlideAnimation animation) { - if (!(resource instanceof Animatable)) { + public void onResourceReady(GlideDrawable resource, GlideAnimation animation) { + if (!resource.isAnimated()) { //TODO: Try to generalize this to other sizes/shapes. // This is a dirty hack that tries to make loading square thumbnails and then square full images less costly // by forcing both the smaller thumb and the larger version to have exactly the same intrinsic dimensions. @@ -47,9 +67,8 @@ public class DrawableImageViewTarget extends ImageViewTarget { } super.onResourceReady(resource, animation); this.resource = resource; - if (resource instanceof Animatable) { - ((Animatable) resource).start(); - } + resource.setLoopCount(maxLoopCount); + resource.start(); } /** @@ -59,21 +78,21 @@ public class DrawableImageViewTarget extends ImageViewTarget { * @param resource The {@link android.graphics.drawable.Drawable} to display in the view. */ @Override - protected void setResource(Drawable resource) { + protected void setResource(GlideDrawable resource) { view.setImageDrawable(resource); } @Override public void onStart() { - if (resource instanceof Animatable) { - ((Animatable) resource).start(); + if (resource != null) { + resource.start(); } } @Override public void onStop() { - if (resource instanceof Animatable) { - ((Animatable) resource).stop(); + if (resource != null) { + resource.stop(); } } } diff --git a/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java index 718e149e9937b162ccc7f27d7a57b5af2b64fb2d..dbc0db2c90cb90f527ce9f19561280fda19260d8 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java @@ -71,7 +71,7 @@ public abstract class ImageViewTarget extends ViewTarget implem } @Override - public void onResourceReady(Z resource, GlideAnimation glideAnimation) { + public void onResourceReady(Z resource, GlideAnimation glideAnimation) { if (glideAnimation == null || !glideAnimation.animate(resource, this)) { setResource(resource); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/SquaringDrawable.java b/library/src/main/java/com/bumptech/glide/request/target/SquaringDrawable.java index 879c0395f7731547c287c6d7928271ec9530c56a..2fdd22c1d8a0436f75dc04652067349e27c64405 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/SquaringDrawable.java +++ b/library/src/main/java/com/bumptech/glide/request/target/SquaringDrawable.java @@ -7,17 +7,19 @@ import android.graphics.PorterDuff; import android.graphics.Rect; import android.graphics.drawable.Drawable; +import com.bumptech.glide.load.resource.drawable.GlideDrawable; + /** * A wrapper drawable to square the wrapped drawable so that it expands to fill a square with exactly the given side * length. The goal of this drawable is to ensure that square thumbnail drawables always match the size of the view * they will be displayed in to avoid a costly requestLayout call. This class should not be used with views or drawables * that are not square. */ -public class SquaringDrawable extends Drawable { - private final Drawable wrapped; - private int side; +public class SquaringDrawable extends GlideDrawable { + private final GlideDrawable wrapped; + private final int side; - public SquaringDrawable(Drawable wrapped, int side) { + public SquaringDrawable(GlideDrawable wrapped, int side) { this.wrapped = wrapped; this.side = side; } @@ -146,4 +148,29 @@ public class SquaringDrawable extends Drawable { public int getOpacity() { return wrapped.getOpacity(); } + + @Override + public boolean isAnimated() { + return wrapped.isAnimated(); + } + + @Override + public void setLoopCount(int loopCount) { + wrapped.setLoopCount(loopCount); + } + + @Override + public void start() { + wrapped.start(); + } + + @Override + public void stop() { + wrapped.stop(); + } + + @Override + public boolean isRunning() { + return wrapped.isRunning(); + } } diff --git a/library/src/main/java/com/bumptech/glide/request/target/Target.java b/library/src/main/java/com/bumptech/glide/request/target/Target.java index 736d85c98510203ffbbf0a54224f0c15253b435b..30dd200b8844a7688ed7505589b8d78621252d9e 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/Target.java +++ b/library/src/main/java/com/bumptech/glide/request/target/Target.java @@ -64,7 +64,7 @@ public interface Target extends LifecycleListener { * * @param resource the loaded resource. */ - public void onResourceReady(R resource, GlideAnimation glideAnimation); + public void onResourceReady(R resource, GlideAnimation glideAnimation); /** * A lifecycle callback that is called when a load is cancelled and its resources are freed. diff --git a/library/src/test/java/com/bumptech/glide/GlideTest.java b/library/src/test/java/com/bumptech/glide/GlideTest.java index 0decb374ca197a1b297c5d0f3229aec2967c3106..43ab8c70e2e826969d59e1ff13b056e334bfc334 100644 --- a/library/src/test/java/com/bumptech/glide/GlideTest.java +++ b/library/src/test/java/com/bumptech/glide/GlideTest.java @@ -29,6 +29,7 @@ import com.bumptech.glide.load.model.ModelLoaderFactory; import com.bumptech.glide.load.model.stream.StreamModelLoader; import com.bumptech.glide.load.resource.bitmap.BitmapResource; import com.bumptech.glide.load.resource.bytes.BytesResource; +import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.load.resource.gif.GifDrawable; import com.bumptech.glide.load.resource.transcode.ResourceTranscoder; import com.bumptech.glide.request.Request; @@ -392,7 +393,7 @@ public class GlideTest { private void runTestStringDefaultLoader(String string) { Glide.with(getContext()) .load(string) - .listener(new RequestListener() { + .listener(new RequestListener() { @Override public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { if (!(e instanceof IOException)) { @@ -402,7 +403,7 @@ public class GlideTest { } @Override - public boolean onResourceReady(Drawable resource, String model, Target target, + public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { return false; diff --git a/library/src/test/java/com/bumptech/glide/load/resource/transcode/GifBitmapWrapperDrawableTranscoderTest.java b/library/src/test/java/com/bumptech/glide/load/resource/transcode/GifBitmapWrapperDrawableTranscoderTest.java index 35d8f6b2da222eb2d21b09ce6f27727673323743..fbc9ab56e8dd55745ec933771b8349f3327c12a5 100644 --- a/library/src/test/java/com/bumptech/glide/load/resource/transcode/GifBitmapWrapperDrawableTranscoderTest.java +++ b/library/src/test/java/com/bumptech/glide/load/resource/transcode/GifBitmapWrapperDrawableTranscoderTest.java @@ -1,9 +1,9 @@ package com.bumptech.glide.load.resource.transcode; import android.graphics.Bitmap; -import android.graphics.drawable.Drawable; import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable; import com.bumptech.glide.load.resource.gif.GifDrawable; import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; import com.bumptech.glide.tests.Util; @@ -21,7 +21,7 @@ import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) public class GifBitmapWrapperDrawableTranscoderTest { private GifBitmapWrapperDrawableTranscoder transcoder; - private ResourceTranscoder bitmapTranscoder; + private ResourceTranscoder bitmapTranscoder; @Before public void setUp() { @@ -52,7 +52,7 @@ public class GifBitmapWrapperDrawableTranscoderTest { private static class TranscoderHarness { Resource gifBitmapResource = mock(Resource.class); GifBitmapWrapper gifBitmap = mock(GifBitmapWrapper.class); - Resource expected = mock(Resource.class); + Resource expected = mock(Resource.class); public TranscoderHarness() { when(gifBitmapResource.get()).thenReturn(gifBitmap); diff --git a/library/src/test/java/com/bumptech/glide/request/target/DrawableImageViewTargetTest.java b/library/src/test/java/com/bumptech/glide/request/target/DrawableImageViewTargetTest.java index e8d29e0c7db7485269cea178aaf31c142756faaf..fa29be1fedda2b951d4e62080840dc6584ddba50 100644 --- a/library/src/test/java/com/bumptech/glide/request/target/DrawableImageViewTargetTest.java +++ b/library/src/test/java/com/bumptech/glide/request/target/DrawableImageViewTargetTest.java @@ -1,15 +1,13 @@ package com.bumptech.glide.request.target; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.ColorFilter; -import android.graphics.drawable.Animatable; -import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.widget.ImageView; +import com.bumptech.glide.load.resource.drawable.GlideDrawable; + import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.invocation.InvocationOnMock; @@ -33,7 +31,7 @@ public class DrawableImageViewTargetTest { public void testSetsDrawableOnViewInSetResource() { ImageView view = new ImageView(Robolectric.application); DrawableImageViewTarget target = new DrawableImageViewTarget(view); - Drawable expected = new ColorDrawable(Color.GRAY); + GlideDrawable expected = new MockAnimatedDrawable(); target.setResource(expected); @@ -46,7 +44,7 @@ public class DrawableImageViewTargetTest { when(mockView.getWidth()).thenReturn(100); when(mockView.getHeight()).thenReturn(100); DrawableImageViewTarget target = new DrawableImageViewTarget(mockView); - Drawable drawable = new ColorDrawable(Color.RED) { + GlideDrawable drawable = new MockAnimatedDrawable() { @Override public int getIntrinsicHeight() { return 100; @@ -78,7 +76,7 @@ public class DrawableImageViewTargetTest { when(mockView.getWidth()).thenReturn(100); when(mockView.getHeight()).thenReturn(100); DrawableImageViewTarget target = new DrawableImageViewTarget(mockView); - Drawable drawable = new AnimationDrawable() { + GlideDrawable drawable = new MockAnimatedDrawable() { @Override public int getIntrinsicHeight() { return 100; @@ -108,7 +106,7 @@ public class DrawableImageViewTargetTest { when(mockView.getWidth()).thenReturn(100); when(mockView.getHeight()).thenReturn(100); DrawableImageViewTarget target = new DrawableImageViewTarget(mockView); - Drawable drawable = new ColorDrawable(Color.RED) { + GlideDrawable drawable = new MockAnimatedDrawable() { @Override public int getIntrinsicHeight() { return 100; @@ -138,7 +136,7 @@ public class DrawableImageViewTargetTest { when(mockView.getWidth()).thenReturn(100); when(mockView.getHeight()).thenReturn(150); DrawableImageViewTarget target = new DrawableImageViewTarget(mockView); - Drawable drawable = new ColorDrawable(Color.RED) { + GlideDrawable drawable = new MockAnimatedDrawable() { @Override public int getIntrinsicHeight() { return 100; @@ -182,6 +180,12 @@ public class DrawableImageViewTargetTest { assertTrue(drawable.isStarted); } + @Test + public void testDoesNotStartNullDrawablesOnStart() { + DrawableImageViewTarget target = new DrawableImageViewTarget(new ImageView(Robolectric.application)); + target.onStart(); + } + @Test public void testStopsAnimatedDrawablesOnStop() { MockAnimatedDrawable drawable = new MockAnimatedDrawable(); @@ -192,8 +196,25 @@ public class DrawableImageViewTargetTest { assertFalse(drawable.isStarted); } - private static class MockAnimatedDrawable extends Drawable implements Animatable { + @Test + public void testDoesNotStopNullDrawablesOnStop() { + DrawableImageViewTarget target = new DrawableImageViewTarget(new ImageView(Robolectric.application)); + target.onStop(); + } + + @Test + public void testSetsLoopCountOnDrawable() { + int maxLoopCount = 6; + MockAnimatedDrawable drawable = new MockAnimatedDrawable(); + DrawableImageViewTarget target = new DrawableImageViewTarget(new ImageView(Robolectric.application), + maxLoopCount); + target.onResourceReady(drawable, null); + assertEquals(maxLoopCount, drawable.loopCount); + } + + private static class MockAnimatedDrawable extends GlideDrawable { private boolean isStarted; + private int loopCount; @Override public void start() { @@ -229,5 +250,15 @@ public class DrawableImageViewTargetTest { public int getOpacity() { return 0; } + + @Override + public boolean isAnimated() { + return false; + } + + @Override + public void setLoopCount(int loopCount) { + this.loopCount = loopCount; + } } } \ No newline at end of file