From 29438d5d293744613485914005d2717e03cae485 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Fri, 3 Oct 2014 19:03:39 -0700 Subject: [PATCH] Add an empty constructor for SimpleTarget. Fixes #170. --- .../request/target/SimpleTargetTest.java | 53 ++++++++++--------- .../glide/request/target/SimpleTarget.java | 22 ++++++-- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/library/src/androidTest/java/com/bumptech/glide/request/target/SimpleTargetTest.java b/library/src/androidTest/java/com/bumptech/glide/request/target/SimpleTargetTest.java index 78e783823..1e9d28085 100644 --- a/library/src/androidTest/java/com/bumptech/glide/request/target/SimpleTargetTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/request/target/SimpleTargetTest.java @@ -1,51 +1,56 @@ package com.bumptech.glide.request.target; import com.bumptech.glide.request.animation.GlideAnimation; - import org.junit.Test; +import static org.mockito.Mockito.mock; + public class SimpleTargetTest { @Test(expected = IllegalArgumentException.class) - public void testThrowsIfGivenWidthIsLessThanZero() { - new SimpleTarget(-1, 1) { - - @Override - public void onResourceReady(Object resource, GlideAnimation glideAnimation) { + public void testThrowsOnGetSizeIfGivenWidthIsLessThanZero() { + getTarget(-1, 1).getSize(mock(SizeReadyCallback.class)); + } - } - }; + @Test(expected = IllegalArgumentException.class) + public void testThrowsOnGetSizeIfGivenWidthIsEqualToZero() { + getTarget(0, 1).getSize(mock(SizeReadyCallback.class)); } @Test(expected = IllegalArgumentException.class) - public void testThrowsIfGivenWidthIsEqualToZero() { - new SimpleTarget(0, 1) { + public void testThrowsOnGetSizeIfGivenHeightIsLessThanZero() { + getTarget(1, -1).getSize(mock(SizeReadyCallback.class)); + } - @Override - public void onResourceReady(Object resource, GlideAnimation glideAnimation) { + @Test(expected = IllegalArgumentException.class) + public void testThrowsOnGetSizeIfGivenHeightIsEqualToZero() { + getTarget(1, 0).getSize(mock(SizeReadyCallback.class)); + } - } - }; + @Test + public void testCanBeConstructedWithoutDimensions() { + getTarget(); } @Test(expected = IllegalArgumentException.class) - public void testThrowsIfGivenHeightIsLessThanZero() { - new SimpleTarget(1, -1) { + public void testThrowsOnGetSizeIfConstructedWithoutDimensions() { + getTarget().getSize(mock(SizeReadyCallback.class)); + } + private SimpleTarget getTarget() { + return new SimpleTarget() { @Override - public void onResourceReady(Object resource, GlideAnimation glideAnimation) { - + public void onResourceReady(Object resource, GlideAnimation glideAnimation) { + // Do nothing. } }; } - @Test(expected = IllegalArgumentException.class) - public void testThrowsIfGivenHeightIsEqualToZero() { - new SimpleTarget(1, 0) { - + private SimpleTarget getTarget(int width, int height) { + return new SimpleTarget(width, height) { @Override - public void onResourceReady(Object resource, GlideAnimation glideAnimation) { - + public void onResourceReady(Object resource, GlideAnimation glideAnimation) { + // Do nothing. } }; } diff --git a/library/src/main/java/com/bumptech/glide/request/target/SimpleTarget.java b/library/src/main/java/com/bumptech/glide/request/target/SimpleTarget.java index 1f62a1d80..78522c320 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/SimpleTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/SimpleTarget.java @@ -27,6 +27,20 @@ public abstract class SimpleTarget extends BaseTarget { private final int width; private final int height; + /** + * Constructor for the target that assumes you will have called + * {@link com.bumptech.glide.GenericRequestBuilder#override(int, int)} on the request builder this target is given + * to. + * + *

+ * Requests that load into this target will throw an {@link java.lang.IllegalArgumentException} if + * {@link com.bumptech.glide.GenericRequestBuilder#override(int, int)} was not called on the request builder. + *

+ */ + public SimpleTarget() { + this(-1, -1); + } + /** * Constructor for the target that takes the desired dimensions of the decoded and/or transformed resource. * @@ -34,10 +48,6 @@ public abstract class SimpleTarget extends BaseTarget { * @param height The desired height of the resource. */ public SimpleTarget(int width, int height) { - if (width <= 0 || height <= 0) { - throw new IllegalArgumentException("Width and height must both be > 0, but given width: " + width + " and" - + " height: " + height); - } this.width = width; this.height = height; } @@ -49,6 +59,10 @@ public abstract class SimpleTarget extends BaseTarget { */ @Override public final void getSize(SizeReadyCallback cb) { + if (width <= 0 || height <= 0) { + throw new IllegalArgumentException("Width and height must both be > 0, but given width: " + width + " and" + + " height: " + height + ", either provide dimensions in the constructor or call override()"); + } cb.onSizeReady(width, height); } } -- GitLab