Skip to content

Commit

Permalink
Add an original size constant.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Nov 21, 2014
1 parent b6763ae commit 827fc08
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@

import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.request.target.Target;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, emulateSdk = 18)
public class BitmapTransformationTest {

@Mock
private BitmapPool bitmapPool;

@Before
public void setUp() {
bitmapPool = mock(BitmapPool.class);
MockitoAnnotations.initMocks(this);
}

@Test
Expand All @@ -42,14 +46,12 @@ public String getId() {
}
};

Resource<Bitmap> resource = mock(Resource.class);
when(resource.get()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444));
Resource<Bitmap> resource = mockResource(100, 100);
assertEquals(resource, transformation.transform(resource, 1, 1));
}

@Test
public void testReturnsNewResourceWhenBitmapTransformed() {
final Bitmap toTransform = Bitmap.createBitmap(1, 2, Bitmap.Config.RGB_565);
final Bitmap transformed = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444);
BitmapTransformation transformation = new BitmapTransformation(bitmapPool) {
@Override
Expand All @@ -63,34 +65,31 @@ public String getId() {
}
};

Resource<Bitmap> resource = mock(Resource.class);
when(resource.get()).thenReturn(toTransform);

Resource<Bitmap> resource = mockResource(1, 2);
assertNotSame(resource, transformation.transform(resource, 100, 100));
}

@Test
public void testPassesGivenArgumentsToTransform() {
final int expectedWidth = 13;
final int expectedHeight = 148;
final Bitmap expected = Bitmap.createBitmap(223, 4123, Bitmap.Config.RGB_565);
final Resource<Bitmap> resource = mockResource(223, 4123);
BitmapTransformation transformation = new BitmapTransformation(bitmapPool) {
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
assertEquals(bitmapPool, pool);
assertEquals(expected, toTransform);
assertEquals(resource.get(), toTransform);
assertEquals(expectedWidth, outWidth);
assertEquals(expectedHeight, outHeight);
return expected;
return resource.get();
}

@Override
public String getId() {
return null;
}
};
Resource<Bitmap> resource = mock(Resource.class);
when(resource.get()).thenReturn(expected);

transformation.transform(resource, expectedWidth, expectedHeight);
}

Expand Down Expand Up @@ -145,8 +144,58 @@ protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, in
}
};

Resource<Bitmap> resource = mock(Resource.class);
when(resource.get()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565));
Resource<Bitmap> resource = mockResource(100, 100);
assertNull(transform.transform(resource, 100, 100));
}

@Test
public void testCallsTransformWithGivenBitmapWidthIfWidthIsSizeOriginal() {
SizeTrackingTransform transform = new SizeTrackingTransform();

int expectedWidth = 200;
Resource<Bitmap> resource = mockResource(expectedWidth, 300);
transform.transform(resource, Target.SIZE_ORIGINAL, 500);

assertEquals(expectedWidth, transform.givenWidth);
}

@Test
public void testCallsTransformWithGivenBitmapHeightIfHeightIsSizeOriginal() {
SizeTrackingTransform transform = new SizeTrackingTransform();

int expectedHeight = 500;
Resource<Bitmap> resource = mockResource(123, expectedHeight);
transform.transform(resource, 444, expectedHeight);

assertEquals(expectedHeight, transform.givenHeight);
}

@SuppressWarnings("unchecked")
private Resource<Bitmap> mockResource(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Resource<Bitmap> resource = mock(Resource.class);
when(resource.get()).thenReturn(bitmap);
return resource;
}

private class SizeTrackingTransform extends BitmapTransformation {
int givenWidth;
int givenHeight;

public SizeTrackingTransform() {
super(bitmapPool);
}

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
givenWidth = outWidth;
givenHeight = outHeight;
return null;
}

@Override
public String getId() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -135,6 +136,22 @@ public void testFitCenterReturnsGivenBitmapIfGivenBitmapHeightMatchesExactly() {
assertTrue(toFit == transformed);
}

@Test
public void testCenterCropReturnsNullIfGivenBitmapIsNull() {
Bitmap transformed = TransformationUtils.centerCrop(null /*recycled*/, null /*toCrop*/, 100, 100);
assertNull(transformed);
}

@Test
public void testCenterCropReturnsGivenBitmapIfGivenBitmapExactlyMatchesGivenDimensions() {
Bitmap toCrop = Bitmap.createBitmap(200, 300, Bitmap.Config.ARGB_8888);
Bitmap transformed = TransformationUtils.centerCrop(null /*recycled*/, toCrop, toCrop.getWidth(),
toCrop.getHeight());

// Robolectric incorrectly implements equals() for Bitmaps, we want the original object not just an equivalent.
assertTrue(toCrop == transformed);
}

@Test
public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() {
Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Expand Down Expand Up @@ -179,7 +196,7 @@ public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
}

@Test
public void testSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlpha() {
public void testCenterCropSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlpha() {
Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

toTransform.setHasAlpha(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,134 @@ public void testSizeCallbackIsCalledSynchronouslyIfLayoutParamsConcreteSizeSet()
verify(cb).onSizeReady(eq(dimens), eq(dimens));
}

private void setDisplayDimens(Integer width, Integer height) {
WindowManager windowManager = (WindowManager) Robolectric.application.getSystemService(Context.WINDOW_SERVICE);
ShadowDisplay shadowDisplay = Robolectric.shadowOf(windowManager.getDefaultDisplay());
if (width != null) {
shadowDisplay.setWidth(width);
}

if (height != null) {
shadowDisplay.setHeight(height);
}
}

private void setDisplayWidth(int width) {
setDisplayDimens(width, null);
}

private void setDisplayHeight(int height) {
setDisplayDimens(null, height);
}

@Test
public void testSizeCallbackIsCalledSynchronouslyWithScreenSizeIfLayoutParamsWrapContent() {
public void testBothParamsWrapContent() {
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.setLayoutParams(layoutParams);

int width = 1234;
int height = 674;
WindowManager windowManager = (WindowManager) view.getContext()
.getSystemService(Context.WINDOW_SERVICE);
ShadowDisplay shadowDisplay = Robolectric.shadowOf(windowManager.getDefaultDisplay());
shadowDisplay.setWidth(width);
shadowDisplay.setHeight(height);
int width = 123;
int height = 456;
setDisplayDimens(width, height);
SizeReadyCallback cb = mock(SizeReadyCallback.class);
target.getSize(cb);

verify(cb).onSizeReady(eq(width), eq(height));
}

@Test
public void testWrapContentWidthWithValidHeight() {
int displayWidth = 500;
setDisplayWidth(displayWidth);

int height = 100;
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, height);
view.setLayoutParams(params);

SizeReadyCallback cb = mock(SizeReadyCallback.class);
target.getSize(cb);

verify(cb).onSizeReady(eq(displayWidth), eq(height));
}

@Test
public void testWrapContentHeightWithValidWidth() {
int displayHeight = 700;
setDisplayHeight(displayHeight);
int width = 100;
LayoutParams params = new LayoutParams(width, LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);

SizeReadyCallback cb = mock(SizeReadyCallback.class);
target.getSize(cb);

verify(cb).onSizeReady(eq(width), eq(displayHeight));
}

@Test
public void testWrapContentWidthWithMatchParentHeight() {
int displayWidth = 1234;
setDisplayWidth(displayWidth);

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
view.setLayoutParams(params);

SizeReadyCallback cb = mock(SizeReadyCallback.class);
target.getSize(cb);

verify(cb, never()).onSizeReady(anyInt(), anyInt());

int height = 32;
SizedShadowView shadowView = Robolectric.shadowOf_(view);
shadowView.setHeight(height);

PreDrawShadowViewTreeObserver shadowObserver = Robolectric.shadowOf_(view.getViewTreeObserver());
shadowObserver.fireOnPreDrawListeners();

verify(cb).onSizeReady(eq(displayWidth), eq(height));
}

@Test
public void testWrapContentHeightWithMatchParentWidth() {
int displayHeight = 5812;
setDisplayHeight(displayHeight);

LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);

SizeReadyCallback cb = mock(SizeReadyCallback.class);
target.getSize(cb);

verify(cb, never()).onSizeReady(anyInt(), anyInt());

int width = 32;
SizedShadowView shadowView = Robolectric.shadowOf_(view);
shadowView.setWidth(width);

PreDrawShadowViewTreeObserver shadowObserver = Robolectric.shadowOf_(view.getViewTreeObserver());
shadowObserver.fireOnPreDrawListeners();

verify(cb).onSizeReady(eq(width), eq(displayHeight));
}

@Test
public void testMatchParentWidthAndHeight() {
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
view.setLayoutParams(params);

SizeReadyCallback cb = mock(SizeReadyCallback.class);
target.getSize(cb);

verify(cb, never()).onSizeReady(anyInt(), anyInt());

int width = 32;
int height = 45;
SizedShadowView shadowView = Robolectric.shadowOf_(view);
shadowView.setWidth(width);
shadowView.setHeight(height);

PreDrawShadowViewTreeObserver shadowObserver = Robolectric.shadowOf_(view.getViewTreeObserver());
shadowObserver.fireOnPreDrawListeners();

verify(cb).onSizeReady(eq(width), eq(height));
}

Expand Down Expand Up @@ -299,7 +411,7 @@ public void testDoesNotThrowOnPreDrawIfViewTreeObserverIsDead() {

@Test(expected = NullPointerException.class)
public void testThrowsIfGivenNullView() {
ViewTarget viewTarget = new TestViewTarget(null);
new TestViewTarget(null);
}

@Implements(ViewTreeObserver.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,7 @@ public Target<TranscodeType> into(ImageView view) {
break;
//$CASES-OMITTED$
default:
// silently ignore
break;
// Do nothing.
}
}

Expand All @@ -654,10 +653,10 @@ public Target<TranscodeType> into(ImageView view) {
/**
* Returns a future that can be used to do a blocking get on a background thread.
*
* @param width The desired width in pixels (note this will be overriden by {@link #override(int, int)} if
* previously called).
* @param height The desired height in pixels (note this will be overriden by {@link #override(int, int)}}
* if previously called).
* @param width The desired width in pixels, or {@link Target#SIZE_ORIGINAL}. This will be overridden by
* {@link #override * (int, int)} if previously called.
* @param height The desired height in pixels, or {@link Target#SIZE_ORIGINAL}. This will be overridden by
* {@link #override * (int, int)}} if previously called).
*
* @see Glide#clear(com.bumptech.glide.request.FutureTarget)
*
Expand Down Expand Up @@ -689,13 +688,34 @@ public void run() {
* available quickly.
* </p>
*
*
* @see com.bumptech.glide.ListPreloader
*
* @param width The desired width in pixels, or {@link Target#SIZE_ORIGINAL}. This will be overridden by
* {@link #override * (int, int)} if previously called.
* @param height The desired height in pixels, or {@link Target#SIZE_ORIGINAL}. This will be overridden by
* {@link #override * (int, int)}} if previously called).
* @return A {@link Target} that can be used to cancel the load via
* {@link Glide#clear(com.bumptech.glide.request.target.Target)}.
*/
public Target<TranscodeType> preload(int width, int height) {
final PreloadTarget<TranscodeType> target = PreloadTarget.obtain(width, height);
return into(target);
}

/**
* Preloads the resource into the cache using {@link Target#SIZE_ORIGINAL} as the target width and height.
* Equivalent to calling {@link #preload(int, int)} with {@link Target#SIZE_ORIGINAL} as the width and height.
*
* @see #preload(int, int)
*
* @return A {@link Target} that can be used to cancel the load via
* {@link Glide#clear(com.bumptech.glide.request.target.Target)}.
*/
public Target<TranscodeType> preload() {
return preload(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
}

void applyCenterCrop() {
// To be implemented by subclasses when possible.
}
Expand Down
Loading

0 comments on commit 827fc08

Please sign in to comment.