@NonNull @Override public final Resource<Bitmap> transform( @NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) { 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"); } BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); Bitmap toTransform = resource.get(); int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth; int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight; Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight); final Resource<Bitmap> result; if (toTransform.equals(transformed)) { result = resource; } else { result = BitmapResource.obtain(transformed, bitmapPool); } return result; }
@Test public void testEquals() { BitmapTransformation otherBitmapTransformation = mock(BitmapTransformation.class); doAnswer(new Util.WriteDigest("bitmapTransformation")) .when(bitmapTransformation).updateDiskCacheKey(any(MessageDigest.class)); doAnswer(new Util.WriteDigest("otherBitmapTransformation")) .when(otherBitmapTransformation).updateDiskCacheKey(any(MessageDigest.class)); keyTester .addEquivalenceGroup( transformation, new DrawableTransformation(bitmapTransformation, /*isRequired=*/ true), new DrawableTransformation(bitmapTransformation, /*isRequired=*/ false)) .addEquivalenceGroup(bitmapTransformation) .addEquivalenceGroup(otherBitmapTransformation) .addEquivalenceGroup( new DrawableTransformation(otherBitmapTransformation, /*isRequired=*/ true), new DrawableTransformation(otherBitmapTransformation, /*isRequired=*/ false)) .addRegressionTest( new DrawableTransformation(bitmapTransformation, /*isRequired=*/ true), "eddf60c557a6315a489b8a3a19b12439a90381256289fbe9a503afa726230bd9") .addRegressionTest( new DrawableTransformation(otherBitmapTransformation, /*isRequired=*/ false), "40931536ed0ec97c39d4be10c44f5b69a86030ec575317f5a0f17e15a0ea9be8") .test(); }
@Test(expected = IllegalArgumentException.class) public void testThrowsIfGivenInvalidHeight() { BitmapTransformation transformation = new BitmapTransformation() { @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { } @Override protected Bitmap transform(@NonNull BitmapPool bitmapPool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { return null; } }; transformation.transform(context, mockResource(1, 1), 100, -1); }
@Test(expected = IllegalArgumentException.class) public void testThrowsIfGivenInvalidWidth() { BitmapTransformation transformation = new BitmapTransformation() { @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { } @Override protected Bitmap transform(@NonNull BitmapPool bitmapPool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { return null; } }; transformation.transform(context, mockResource(1, 1), -1, 100); }
@Test public void testPassesGivenArgumentsToTransform() { final int expectedWidth = 13; final int expectedHeight = 148; final Resource<Bitmap> resource = mockResource(223, 4123); BitmapTransformation transformation = new BitmapTransformation() { @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { } @Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { assertEquals(bitmapPool, pool); assertEquals(resource.get(), toTransform); assertEquals(expectedWidth, outWidth); assertEquals(expectedHeight, outHeight); return resource.get(); } }; transformation.transform(context, resource, expectedWidth, expectedHeight); }
@Test public void testReturnsNewResourceWhenBitmapTransformed() { final Bitmap transformed = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444); BitmapTransformation transformation = new BitmapTransformation() { @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { } @Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap bitmap, int outWidth, int outHeight) { return transformed; } }; Resource<Bitmap> resource = mockResource(1, 2); assertNotSame(resource, transformation.transform(context, resource, 100, 100)); }
@Test public void testReturnsNullIfTransformReturnsNull() { BitmapTransformation transform = new BitmapTransformation() { @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { } @Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { return null; } }; Resource<Bitmap> resource = mockResource(100, 100); assertNull(transform.transform(context, resource, 100, 100)); }
@Test public void testReturnsGivenResourceWhenBitmapNotTransformed() { BitmapTransformation transformation = new BitmapTransformation() { @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { } @Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { return toTransform; } }; Resource<Bitmap> resource = mockResource(100, 100); assertEquals(resource, transformation.transform(context, resource, 1, 1)); }
@Override public final Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { 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"); } Bitmap toTransform = resource.get(); int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth; int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight; Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight); final Resource<Bitmap> result; if (toTransform.equals(transformed)) { result = resource; } else { result = BitmapResource.obtain(transformed, bitmapPool); } return result; }
@Override public final Resource<Bitmap> transform( Context context, Resource<Bitmap> resource, int outWidth, int outHeight) { 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"); } BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); Bitmap toTransform = resource.get(); int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth; int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight; Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight); final Resource<Bitmap> result; if (toTransform.equals(transformed)) { result = resource; } else { result = BitmapResource.obtain(transformed, bitmapPool); } return result; }