提交 29438d5d 编写于 作者: S Sam Judd

Add an empty constructor for SimpleTarget.

Fixes #170.
上级 f08ab536
package com.bumptech.glide.request.target; package com.bumptech.glide.request.target;
import com.bumptech.glide.request.animation.GlideAnimation; import com.bumptech.glide.request.animation.GlideAnimation;
import org.junit.Test; import org.junit.Test;
import static org.mockito.Mockito.mock;
public class SimpleTargetTest { public class SimpleTargetTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testThrowsIfGivenWidthIsLessThanZero() { public void testThrowsOnGetSizeIfGivenWidthIsLessThanZero() {
new SimpleTarget<Object>(-1, 1) { getTarget(-1, 1).getSize(mock(SizeReadyCallback.class));
}
@Override
public void onResourceReady(Object resource, GlideAnimation<Object> glideAnimation) {
} @Test(expected = IllegalArgumentException.class)
}; public void testThrowsOnGetSizeIfGivenWidthIsEqualToZero() {
getTarget(0, 1).getSize(mock(SizeReadyCallback.class));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testThrowsIfGivenWidthIsEqualToZero() { public void testThrowsOnGetSizeIfGivenHeightIsLessThanZero() {
new SimpleTarget<Object>(0, 1) { getTarget(1, -1).getSize(mock(SizeReadyCallback.class));
}
@Override @Test(expected = IllegalArgumentException.class)
public void onResourceReady(Object resource, GlideAnimation<Object> glideAnimation) { public void testThrowsOnGetSizeIfGivenHeightIsEqualToZero() {
getTarget(1, 0).getSize(mock(SizeReadyCallback.class));
}
} @Test
}; public void testCanBeConstructedWithoutDimensions() {
getTarget();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testThrowsIfGivenHeightIsLessThanZero() { public void testThrowsOnGetSizeIfConstructedWithoutDimensions() {
new SimpleTarget<Object>(1, -1) { getTarget().getSize(mock(SizeReadyCallback.class));
}
private SimpleTarget<Object> getTarget() {
return new SimpleTarget<Object>() {
@Override @Override
public void onResourceReady(Object resource, GlideAnimation<Object> glideAnimation) { public void onResourceReady(Object resource, GlideAnimation<? super Object> glideAnimation) {
// Do nothing.
} }
}; };
} }
@Test(expected = IllegalArgumentException.class) private SimpleTarget<Object> getTarget(int width, int height) {
public void testThrowsIfGivenHeightIsEqualToZero() { return new SimpleTarget<Object>(width, height) {
new SimpleTarget<Object>(1, 0) {
@Override @Override
public void onResourceReady(Object resource, GlideAnimation<Object> glideAnimation) { public void onResourceReady(Object resource, GlideAnimation<? super Object> glideAnimation) {
// Do nothing.
} }
}; };
} }
......
...@@ -27,6 +27,20 @@ public abstract class SimpleTarget<Z> extends BaseTarget<Z> { ...@@ -27,6 +27,20 @@ public abstract class SimpleTarget<Z> extends BaseTarget<Z> {
private final int width; private final int width;
private final int height; 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.
*
* <p>
* 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.
* </p>
*/
public SimpleTarget() {
this(-1, -1);
}
/** /**
* Constructor for the target that takes the desired dimensions of the decoded and/or transformed resource. * Constructor for the target that takes the desired dimensions of the decoded and/or transformed resource.
* *
...@@ -34,10 +48,6 @@ public abstract class SimpleTarget<Z> extends BaseTarget<Z> { ...@@ -34,10 +48,6 @@ public abstract class SimpleTarget<Z> extends BaseTarget<Z> {
* @param height The desired height of the resource. * @param height The desired height of the resource.
*/ */
public SimpleTarget(int width, int height) { 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.width = width;
this.height = height; this.height = height;
} }
...@@ -49,6 +59,10 @@ public abstract class SimpleTarget<Z> extends BaseTarget<Z> { ...@@ -49,6 +59,10 @@ public abstract class SimpleTarget<Z> extends BaseTarget<Z> {
*/ */
@Override @Override
public final void getSize(SizeReadyCallback cb) { 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); cb.onSizeReady(width, height);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册