@NonNull @Override public Resource<BitmapDrawable> transform( @NonNull Context context, @NonNull Resource<BitmapDrawable> drawableResourceToTransform, int outWidth, int outHeight) { Resource<Drawable> toTransform = convertToDrawableResource(drawableResourceToTransform); Resource<Drawable> transformed = wrapped.transform(context, toTransform, outWidth, outHeight); return convertToBitmapDrawableResource(transformed); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); bitmapToTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); BitmapDrawable drawableToTransform = new BitmapDrawable(bitmapToTransform); context = RuntimeEnvironment.application; Glide.init(context, new GlideBuilder().setBitmapPool(bitmapPool)); when(drawableResourceToTransform.get()).thenReturn(drawableToTransform); transformation = new BitmapDrawableTransformation(wrapped); }
@Test public void testProvidesBitmapFromGivenResourceToWrappedTransformation() { int outWidth = 332; int outHeight = 111; Resource<Bitmap> transformed = Util.mockResource(); when(transformed.get()) .thenReturn(Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888)); when(wrapped.transform(anyContext(), Util.<Bitmap>anyResource(), anyInt(), anyInt())) .thenReturn(transformed); transformation.transform(context, drawableResourceToTransform, outWidth, outHeight); ArgumentCaptor<Resource<Bitmap>> captor = Util.cast(ArgumentCaptor.forClass(Resource.class)); verify(wrapped).transform(anyContext(), captor.capture(), eq(outWidth), eq(outHeight)); assertThat(captor.getValue().get()).isEqualTo(bitmapToTransform); }
@Test public void testEquals() throws NoSuchAlgorithmException { doAnswer(new Util.WriteDigest("wrapped")).when(wrapped) .updateDiskCacheKey(any(MessageDigest.class)); @SuppressWarnings("unchecked") Transformation<Bitmap> other = mock(Transformation.class); doAnswer(new Util.WriteDigest("other")).when(other) .updateDiskCacheKey(any(MessageDigest.class)); keyTester .addEquivalenceGroup( transformation, new BitmapDrawableTransformation(wrapped)) .addEquivalenceGroup(new BitmapDrawableTransformation(other)) .addEquivalenceGroup(wrapped) .addRegressionTest( transformation, "adbf45b08ad6468aa147e5b2a23758ef56ab631a2b70ad52501ca358441a34f3") .test(); } }
@Test public void testReturnsNewResourceIfTransformationDoesTransform() { int outWidth = 999; int outHeight = 555; Bitmap transformedBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.RGB_565); Resource<Bitmap> transformedBitmapResource = Util.mockResource(); when(transformedBitmapResource.get()).thenReturn(transformedBitmap); when(wrapped.transform(anyContext(), Util.<Bitmap>anyResource(), eq(outWidth), eq(outHeight))) .thenReturn(transformedBitmapResource); Resource<BitmapDrawable> transformed = transformation.transform(context, drawableResourceToTransform, outWidth, outHeight); assertThat(transformed.get().getBitmap()).isEqualTo(transformedBitmap); }
/** * Applies the given {@link Transformation} for * {@link Bitmap Bitmaps} to the default types ({@link Bitmap}, * {@link android.graphics.drawable.BitmapDrawable}, and ignores unknown types. * * <p>This will override previous calls to {@link #dontTransform()}. * * @param transformation Any {@link Transformation} for {@link Bitmap}s. * @see #transform(Transformation) * @see #transform(Class, Transformation) */ // Guaranteed to modify the current object by the isAutoCloneEnabledCheck. @SuppressWarnings("CheckResult") @CheckResult public RequestOptions optionalTransform(Transformation<Bitmap> transformation) { if (isAutoCloneEnabled) { return clone().optionalTransform(transformation); } optionalTransform(Bitmap.class, transformation); // TODO: remove BitmapDrawable decoder and this transformation. optionalTransform(BitmapDrawable.class, new BitmapDrawableTransformation(transformation)); return selfOrThrowIfLocked(); }
@Test public void testReturnsOriginalResourceIfTransformationDoesNotTransform() { int outWidth = 123; int outHeight = 456; when(wrapped.transform( anyContext(), Util.<Bitmap>anyResource(), eq(outWidth), eq(outHeight))) .thenAnswer(new Answer<Resource<Bitmap>>() { @SuppressWarnings("unchecked") @Override public Resource<Bitmap> answer(InvocationOnMock invocation) throws Throwable { return (Resource<Bitmap>) invocation.getArguments()[1]; } }); Resource<BitmapDrawable> transformed = transformation.transform(context, drawableResourceToTransform, outWidth, outHeight); assertThat(transformed).isEqualTo(drawableResourceToTransform); }