@SuppressWarnings("unchecked") <Z> Transformation<Z> getTransformation(Class<Z> resourceClass) { Transformation<Z> result = (Transformation<Z>) transformations.get(resourceClass); if (result == null) { for (Entry<Class<?>, Transformation<?>> entry : transformations.entrySet()) { if (entry.getKey().isAssignableFrom(resourceClass)) { result = (Transformation<Z>) entry.getValue(); break; } } } if (result == null) { if (transformations.isEmpty() && isTransformationRequired) { throw new IllegalArgumentException( "Missing transformation for " + resourceClass + ". If you wish to" + " ignore unknown resource types, use the optional transformation methods."); } else { return UnitTransformation.get(); } } return result; }
@Test public void testWritesBytesDirectlyToDiskIfTransformationIsUnitTransformation() { when(gifDrawable.getFrameTransformation()).thenReturn(UnitTransformation.<Bitmap>get()); String expected = "expected"; when(gifDrawable.getBuffer()).thenReturn(ByteBuffer.wrap(expected.getBytes())); encoder.encode(resource, file, options); assertThat(getEncodedData()).isEqualTo(expected); verify(gifEncoder, never()).start(any(OutputStream.class)); verify(parser, never()).setData(any(byte[].class)); verify(parser, never()).parseHeader(); }
@Test public void testEqualsHashCodeDigest() throws NoSuchAlgorithmException { @SuppressWarnings("unchecked") Transformation<Object> other = mock(Transformation.class); doAnswer(new Util.WriteDigest("other")).when(other) .updateDiskCacheKey(any(MessageDigest.class)); keyTester .addEquivalenceGroup( UnitTransformation.get(), UnitTransformation.get()) .addEquivalenceGroup(other) .addEmptyDigestRegressionTest(UnitTransformation.get()) .test(); } }
@Test @SuppressWarnings("unchecked") public void testSetsTransformationAsFrameTransformation() { Resource<GifDrawable> resource = mockResource(); GifDrawable gifDrawable = mock(GifDrawable.class); Transformation<Bitmap> unitTransformation = UnitTransformation.get(); when(gifDrawable.getFrameTransformation()).thenReturn(unitTransformation); when(gifDrawable.getIntrinsicWidth()).thenReturn(500); when(gifDrawable.getIntrinsicHeight()).thenReturn(500); when(resource.get()).thenReturn(gifDrawable); Bitmap firstFrame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(gifDrawable.getFirstFrame()).thenReturn(firstFrame); final int width = 123; final int height = 456; Bitmap expectedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Resource<Bitmap> expectedResource = mockResource(); when(expectedResource.get()).thenReturn(expectedBitmap); when(wrapped.transform(any(Context.class), Util.<Bitmap>anyResource(), anyInt(), anyInt())) .thenReturn(expectedResource); transformation.transform(context, resource, width, height); verify(gifDrawable).setFrameTransformation(isA(Transformation.class), eq(expectedBitmap)); }
@Nullable private GifDrawableResource decode( ByteBuffer byteBuffer, int width, int height, GifHeaderParser parser, Options options) { long startTime = LogTime.getLogTime(); try { final GifHeader header = parser.parseHeader(); if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) { // If we couldn't decode the GIF, we will end up with a frame count of 0. return null; } Bitmap.Config config = options.get(GifOptions.DECODE_FORMAT) == DecodeFormat.PREFER_RGB_565 ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888; int sampleSize = getSampleSize(header, width, height); GifDecoder gifDecoder = gifDecoderFactory.build(provider, header, byteBuffer, sampleSize); gifDecoder.setDefaultBitmapConfig(config); gifDecoder.advance(); Bitmap firstFrame = gifDecoder.getNextFrame(); if (firstFrame == null) { return null; } Transformation<Bitmap> unitTransformation = UnitTransformation.get(); GifDrawable gifDrawable = new GifDrawable(context, gifDecoder, unitTransformation, width, height, firstFrame); return new GifDrawableResource(gifDrawable); } finally { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Decoded GIF from stream in " + LogTime.getElapsedMillis(startTime)); } } }
@Test public void testReturnsGivenResource() { Resource<Object> resource = mockResource(); UnitTransformation<Object> transformation = UnitTransformation.get(); assertEquals(resource, transformation.transform(app, resource, 10, 10)); }
/** * Removes the current {@link Transformation}. * * @return This request builder. */ @SuppressWarnings("unchecked") public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> dontTransform() { Transformation<ResourceType> transformation = UnitTransformation.get(); return transform(transformation); }
private GifDrawableResource decode(byte[] data, int width, int height, GifHeaderParser parser, GifDecoder decoder) { final GifHeader header = parser.parseHeader(); if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) { // If we couldn't decode the GIF, we will end up with a frame count of 0. return null; } Bitmap firstFrame = decodeFirstFrame(decoder, header, data); if (firstFrame == null) { return null; } Transformation<Bitmap> unitTransformation = UnitTransformation.get(); GifDrawable gifDrawable = new GifDrawable(context, provider, bitmapPool, unitTransformation, width, height, header, data, firstFrame); return new GifDrawableResource(gifDrawable); }
@SuppressWarnings("unchecked") <Z> Transformation<Z> getTransformation(Class<Z> resourceClass) { Transformation<Z> result = (Transformation<Z>) transformations.get(resourceClass); if (result == null) { if (transformations.isEmpty() && isTransformationRequired) { throw new IllegalArgumentException( "Missing transformation for " + resourceClass + ". If you wish to" + " ignore unknown resource types, use the optional transformation methods."); } else { return UnitTransformation.get(); } } return result; }