@Before public void setUp() { object = new Anything(); resource = new SimpleResource<>(object); }
public Resource<SVG> decode(@NonNull InputStream source, int width, int height, @NonNull Options options) throws IOException { try { SVG svg = SVG.getFromInputStream(source); return new SimpleResource<>(svg); } catch (SVGParseException ex) { throw new IOException("Cannot load SVG from stream", ex); } } }
@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 @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 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(); }
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); }
public FileDescriptorBitmapDataLoadProvider(BitmapPool bitmapPool, DecodeFormat decodeFormat) { cacheDecoder = new FileToStreamDecoder<Bitmap>(new StreamBitmapDecoder(bitmapPool, decodeFormat)); sourceDecoder = new FileDescriptorBitmapDecoder(bitmapPool, decodeFormat); encoder = new BitmapEncoder(); sourceEncoder = NullEncoder.get(); }
bitmapTransformation .transform( any(Context.class), anyBitmapResource(), anyInt(), anyInt())) .thenAnswer(new ReturnGivenResource()); final Resource<Drawable> input = new SimpleResource<Drawable>(colorDrawable); transformation.transform(context, input, /*outWidth=*/ 100, /*outHeight=*/ 200);
private static GenericRequestBuilder<GifDecoder, GifDecoder, Bitmap, Bitmap> getRequestBuilder(Context context, GifDecoder gifDecoder, int width, int height, BitmapPool bitmapPool) { GifFrameResourceDecoder frameResourceDecoder = new GifFrameResourceDecoder(bitmapPool); GifFrameModelLoader frameLoader = new GifFrameModelLoader(); Encoder<GifDecoder> sourceEncoder = NullEncoder.get(); return Glide.with(context) .using(frameLoader, GifDecoder.class) .load(gifDecoder) .as(Bitmap.class) .sourceEncoder(sourceEncoder) .decoder(frameResourceDecoder) .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.NONE) .override(width, height); }
@Override public ResourceEncoder<File> getEncoder() { return NullResourceEncoder.get(); }
@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(expected = NullPointerException.class) public void testThrowsIfGivenNullData() { new SimpleResource<>(null); }
@Test public void testReturnsGivenObject() { assertEquals(object, resource.get()); }
/** * 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); }
@Nullable @Override public Resource<PictureDrawable> transcode(@NonNull Resource<SVG> toTranscode, @NonNull Options options) { SVG svg = toTranscode.get(); Picture picture = svg.renderToPicture(); PictureDrawable drawable = new PictureDrawable(picture); return new SimpleResource<>(drawable); } }
@Test public void testMultipleRequestListeners() { getNullModelRequest().addListener(listener1).addListener(listener2).into(target); verify(requestManager).track(any(Target.class), requestCaptor.capture()); requestCaptor.getValue().onResourceReady(new SimpleResource<>(new Object()), DataSource.LOCAL); verify(listener1) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); verify(listener2) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); }
@Test public void testListenerApiOverridesListeners() { getNullModelRequest().addListener(listener1).listener(listener2).into(target); verify(requestManager).track(any(Target.class), requestCaptor.capture()); requestCaptor.getValue().onResourceReady(new SimpleResource<>(new Object()), DataSource.LOCAL); // The #listener API removes any previous listeners, so the first listener should not be called. verify(listener1, never()) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); verify(listener2) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); }