diff --git a/library/src/androidTest/java/com/bumptech/glide/GenericRequestBuilderTest.java b/library/src/androidTest/java/com/bumptech/glide/GenericRequestBuilderTest.java index 5de866ddf3..15b91130ee 100644 --- a/library/src/androidTest/java/com/bumptech/glide/GenericRequestBuilderTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/GenericRequestBuilderTest.java @@ -75,6 +75,16 @@ public void testThrowsWhenOverrideHeightEqualToZero() { getNullModelRequest().override(100, 0); } + @Test + public void testDoesNotThrowWhenWidthIsSizeOriginal() { + getNullModelRequest().override(Target.SIZE_ORIGINAL, 100); + } + + @Test + public void testDoesNotThrowWhenHeightIsSizeOriginal() { + getNullModelRequest().override(100, Target.SIZE_ORIGINAL); + } + @Test public void testDoesNotThrowWhenModelAndLoaderNull() { new GenericRequestBuilder(Robolectric.application, null, null, Object.class, mock(Glide.class), requestTracker, 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 7575ea8d4e..509c1d8053 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 @@ -36,9 +36,14 @@ public void testCanBeConstructedWithoutDimensions() { getTarget(); } - @Test(expected = IllegalArgumentException.class) - public void testThrowsOnGetSizeIfConstructedWithoutDimensions() { - getTarget().getSize(mock(SizeReadyCallback.class)); + @Test + public void testConstructorDoesNotThrowWithSizeOriginal() { + getTarget(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); + } + + @Test + public void testGetSizeDoesNotThrowWithSizeOriginal() { + getTarget(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).getSize(mock(SizeReadyCallback.class)); } private SimpleTarget getTarget() { diff --git a/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java b/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java index b8eb0fc686..ef49da3c25 100644 --- a/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java +++ b/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java @@ -510,11 +510,8 @@ public GenericRequestBuilder s * @return This request builder. */ public GenericRequestBuilder override(int width, int height) { - if (width <= 0) { - throw new IllegalArgumentException("Width must be > 0"); - } - if (height <= 0) { - throw new IllegalArgumentException("Height must be > 0"); + if (!Util.isValidDimensions(width, height)) { + throw new IllegalArgumentException("Width and height must be Target#SIZE_ORIGINAL or > 0"); } this.overrideWidth = width; this.overrideHeight = height; @@ -754,9 +751,9 @@ private Request buildRequestRecursive(Target target, ThumbnailReq thumbnailRequestBuilder.priority = getThumbnailPriority(); } - if (overrideWidth > 0 && overrideHeight > 0 - && thumbnailRequestBuilder.overrideWidth < 0 - && thumbnailRequestBuilder.overrideHeight < 0) { + if (Util.isValidDimensions(overrideWidth, overrideHeight) + && !Util.isValidDimensions(thumbnailRequestBuilder.overrideWidth, + thumbnailRequestBuilder.overrideHeight)) { thumbnailRequestBuilder.override(overrideWidth, overrideHeight); } diff --git a/library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapTransformation.java b/library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapTransformation.java index d119964fb6..326a50e864 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapTransformation.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapTransformation.java @@ -8,6 +8,7 @@ import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.request.target.Target; +import com.bumptech.glide.util.Util; /** * A simple {@link com.bumptech.glide.load.Transformation} for transforming {@link android.graphics.Bitmap}s that @@ -43,8 +44,7 @@ public BitmapTransformation(BitmapPool bitmapPool) { @Override public final Resource transform(Resource resource, int outWidth, int outHeight) { - if ((outWidth <= 0 && outWidth != Target.SIZE_ORIGINAL) - || (outHeight <= 0 && outHeight != Target.SIZE_ORIGINAL)) { + if (!Util.isValidDimensions(outWidth, outHeight)) { throw new IllegalArgumentException("Cannot apply transformation on width: " + outWidth + " or height: " + outHeight + " less than or equal to zero and not Target.SIZE_ORIGINAL"); } 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 c55e29ab86..b3262c03d4 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 @@ -1,5 +1,7 @@ package com.bumptech.glide.request.target; +import com.bumptech.glide.util.Util; + /** * A simple {@link com.bumptech.glide.request.target.Target} base class with default (usually noop) implementations * of non essential methods that allows the caller to specify an exact width/height. Typicaly use cases look something @@ -52,9 +54,10 @@ public SimpleTarget(int width, int height) { */ @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()"); + if (!Util.isValidDimensions(width, height)) { + throw new IllegalArgumentException("Width and height must both be > 0 or Target#SIZE_ORIGINAL, but given" + + " width: " + width + " and height: " + height + ", either provide dimensions in the constructor" + + " or call override()"); } cb.onSizeReady(width, height); } diff --git a/library/src/main/java/com/bumptech/glide/util/Util.java b/library/src/main/java/com/bumptech/glide/util/Util.java index cc0743fe70..7ba905dd31 100644 --- a/library/src/main/java/com/bumptech/glide/util/Util.java +++ b/library/src/main/java/com/bumptech/glide/util/Util.java @@ -5,6 +5,8 @@ import android.os.Build; import android.os.Looper; +import com.bumptech.glide.request.target.Target; + import java.util.ArrayDeque; import java.util.Queue; @@ -107,6 +109,17 @@ private static int getBytesPerPixel(Bitmap.Config config) { return bytesPerPixel; } + /** + * Returns true if width and height are both > 0 and/or equal to {@link Target#SIZE_ORIGINAL}. + */ + public static boolean isValidDimensions(int width, int height) { + return isValidDimension(width) && isValidDimension(height); + } + + private static boolean isValidDimension(int dimen) { + return dimen > 0 || dimen == Target.SIZE_ORIGINAL; + } + /** * Throws an {@link java.lang.IllegalArgumentException} if called on a thread other than the main thread. */