From 3b26367a46b9e1b01f1ac8faf06501ff39105c84 Mon Sep 17 00:00:00 2001 From: Kwangsu Kim Date: Sun, 12 Mar 2017 12:03:09 +0900 Subject: [PATCH] Allow onlyScaleDown() with fit() method --- .../main/java/com/squareup/picasso/Request.java | 7 ++++--- .../squareup/picasso/RequestCreatorTest.java | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/picasso/src/main/java/com/squareup/picasso/Request.java b/picasso/src/main/java/com/squareup/picasso/Request.java index dbe2d160ee..b1a76ddc20 100644 --- a/picasso/src/main/java/com/squareup/picasso/Request.java +++ b/picasso/src/main/java/com/squareup/picasso/Request.java @@ -390,9 +390,6 @@ public Builder clearCenterInside() { * specified by {@link #resize(int, int)}. */ public Builder onlyScaleDown() { - if (targetHeight == 0 && targetWidth == 0) { - throw new IllegalStateException("onlyScaleDown can not be applied without resize"); - } onlyScaleDown = true; return this; } @@ -500,6 +497,10 @@ public Request build() { throw new IllegalStateException( "Center inside requires calling resize with positive width and height."); } + if (onlyScaleDown && (targetWidth == 0 && targetHeight == 0)) { + throw new IllegalStateException( + "onlyScaleDown requires calling resize with positive width and height."); + } if (priority == null) { priority = Priority.NORMAL; } diff --git a/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java b/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java index 61b88d172b..a82c6a82a6 100644 --- a/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java +++ b/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java @@ -401,6 +401,16 @@ public void intoImageViewWithSkipMemoryCachePolicy() { verify(picasso, never()).quickMemoryCacheCheck(URI_KEY_1); } + @Test + public void intoImageViewWithFitAndOnlyScaleDown() { + ImageView target = mockFitImageViewTarget(true); + when(target.getWidth()).thenReturn(100); + when(target.getHeight()).thenReturn(100); + new RequestCreator(picasso, URI_1, 0).fit().onlyScaleDown().into(target); + verify(picasso).enqueueAndSubmit(actionCaptor.capture()); + assertThat(actionCaptor.getValue().getRequest().onlyScaleDown).isTrue(); + } + @Test public void intoImageViewWithFitAndResizeThrows() { try { @@ -544,7 +554,7 @@ public void intoRemoteViewsNotificationWithFitThrows() { } @Test - public void intoTargetNoResizeWithCenterInsideOrCenterCropThrows() { + public void intoTargetNoResizeWithCenterInsideOrCenterCropOrOnlyScaleDownThrows() { try { new RequestCreator(picasso, URI_1, 0).centerInside().into(mockTarget()); fail("Center inside with unknown width should throw exception."); @@ -555,6 +565,11 @@ public void intoTargetNoResizeWithCenterInsideOrCenterCropThrows() { fail("Center inside with unknown height should throw exception."); } catch (IllegalStateException ignored) { } + try { + new RequestCreator(picasso, URI_1, 0).onlyScaleDown().into(mockTarget()); + fail("onlyScaleDown with unknown height should throw exception."); + } catch (IllegalStateException ignored) { + } } @Test public void appWidgetActionWithDefaultPriority() throws Exception {