Skip to content

Commit

Permalink
Allow SIZE_ORIGINAL in SimpleTarget and override.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Jan 21, 2015
1 parent 9456192 commit e099ef6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> getTarget() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,8 @@ public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> s
* @return This request builder.
*/
public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> 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;
Expand Down Expand Up @@ -754,9 +751,9 @@ private Request buildRequestRecursive(Target<TranscodeType> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,8 +44,7 @@ public BitmapTransformation(BitmapPool bitmapPool) {

@Override
public final Resource<Bitmap> transform(Resource<Bitmap> 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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions library/src/main/java/com/bumptech/glide/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit e099ef6

Please sign in to comment.