use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class AddImageTransformMetaDataProducerTest method testOnNewResultNotLastNotJpeg.
@Test
public void testOnNewResultNotLastNotJpeg() {
when(ImageFormatChecker.getImageFormat_WrapIOException(any(InputStream.class))).thenReturn(DefaultImageFormats.WEBP_SIMPLE);
when(BitmapUtil.decodeDimensions(any(InputStream.class))).thenReturn(null);
mAddMetaDataConsumer.onNewResult(mIntermediateResult, Consumer.NO_FLAGS);
ArgumentCaptor<EncodedImage> argumentCaptor = ArgumentCaptor.forClass(EncodedImage.class);
verify(mConsumer).onNewResult(argumentCaptor.capture(), eq(Consumer.NO_FLAGS));
EncodedImage encodedImage = argumentCaptor.getValue();
assertTrue(EncodedImage.isValid(encodedImage));
assertEquals(DefaultImageFormats.WEBP_SIMPLE, encodedImage.getImageFormat());
assertEquals(0, encodedImage.getRotationAngle());
assertEquals(-1, encodedImage.getWidth());
assertEquals(-1, encodedImage.getHeight());
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class BranchOnSeparateImagesProducerTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mInputProducer1 = mock(Producer.class);
mInputProducer2 = mock(Producer.class);
BranchOnSeparateImagesProducer branchOnSeparateImagesProducer = new BranchOnSeparateImagesProducer(mInputProducer1, mInputProducer2);
when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);
when(mImageRequest.getPreferredWidth()).thenReturn(WIDTH);
when(mImageRequest.getPreferredHeight()).thenReturn(HEIGHT);
when(mImageRequest.getResizeOptions()).thenReturn(new ResizeOptions(WIDTH, HEIGHT));
when(mImageRequest.getLocalThumbnailPreviewsEnabled()).thenReturn(true);
mIntermediateResult = mock(EncodedImage.class);
mFirstProducerFinalResult = mock(EncodedImage.class);
mSecondProducerFinalResult = mock(EncodedImage.class);
PooledByteBuffer mockByteBuffer = mock(PooledByteBuffer.class);
when(mIntermediateResult.getByteBufferRef()).thenReturn(CloseableReference.of(mockByteBuffer));
when(mFirstProducerFinalResult.getByteBufferRef()).thenReturn(CloseableReference.of(mockByteBuffer));
when(mSecondProducerFinalResult.getByteBufferRef()).thenReturn(CloseableReference.of(mockByteBuffer));
mFirstProducerConsumer = null;
mSecondProducerConsumer = null;
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
mFirstProducerConsumer = (Consumer<EncodedImage>) invocation.getArguments()[0];
return null;
}
}).when(mInputProducer1).produceResults(any(Consumer.class), any(ProducerContext.class));
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
mSecondProducerConsumer = (Consumer<EncodedImage>) invocation.getArguments()[0];
return null;
}
}).when(mInputProducer2).produceResults(any(Consumer.class), any(ProducerContext.class));
branchOnSeparateImagesProducer.produceResults(mConsumer, mProducerContext);
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class BufferedDiskCacheTest method testFromStagingAreaLater.
@Test
public void testFromStagingAreaLater() throws Exception {
Task<EncodedImage> readTask = mBufferedDiskCache.get(mCacheKey, mIsCancelled);
assertFalse(readTask.isCompleted());
when(mStagingArea.get(mCacheKey)).thenReturn(mEncodedImage);
mReadPriorityExecutor.runUntilIdle();
EncodedImage result = readTask.getResult();
assertSame(result, mEncodedImage);
verify(mFileCache, never()).getResource(eq(mCacheKey));
// Ref count should be equal to 3 (One for mCloseableReference, one that is cloned when
// mEncodedImage is created and a third one that is cloned when the method getByteBufferRef is
// called in EncodedImage).
assertEquals(3, result.getByteBufferRef().getUnderlyingReferenceTestOnly().getRefCountTestOnly());
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class NetworkFetchProducer method notifyConsumer.
protected static void notifyConsumer(PooledByteBufferOutputStream pooledOutputStream, @Consumer.Status int status, @Nullable BytesRange responseBytesRange, Consumer<EncodedImage> consumer, ProducerContext context) {
CloseableReference<PooledByteBuffer> result = CloseableReference.of(pooledOutputStream.toByteBuffer());
EncodedImage encodedImage = null;
try {
encodedImage = new EncodedImage(result);
encodedImage.setBytesRange(responseBytesRange);
encodedImage.parseMetaData();
context.setEncodedImageOrigin(EncodedImageOrigin.NETWORK);
consumer.onNewResult(encodedImage, status);
} finally {
EncodedImage.closeSafely(encodedImage);
CloseableReference.closeSafely(result);
}
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class PartialDiskCacheProducer method produceResults.
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
final ImageRequest imageRequest = producerContext.getImageRequest();
final boolean isDiskCacheEnabledForRead = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.DISK_READ);
final boolean isDiskCacheEnabledForWrite = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.DISK_WRITE);
if (!isDiskCacheEnabledForRead && !isDiskCacheEnabledForWrite) {
mInputProducer.produceResults(consumer, producerContext);
return;
}
final ProducerListener2 listener = producerContext.getProducerListener();
listener.onProducerStart(producerContext, PRODUCER_NAME);
final Uri uriForPartialCacheKey = createUriForPartialCacheKey(imageRequest);
final CacheKey partialImageCacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, uriForPartialCacheKey, producerContext.getCallerContext());
if (!isDiskCacheEnabledForRead) {
listener.onProducerFinishWithSuccess(producerContext, PRODUCER_NAME, getExtraMap(listener, producerContext, false, 0));
startInputProducer(consumer, producerContext, partialImageCacheKey, null);
return;
}
final AtomicBoolean isCancelled = new AtomicBoolean(false);
final Task<EncodedImage> diskLookupTask = mDefaultBufferedDiskCache.get(partialImageCacheKey, isCancelled);
final Continuation<EncodedImage, Void> continuation = onFinishDiskReads(consumer, producerContext, partialImageCacheKey);
diskLookupTask.continueWith(continuation);
subscribeTaskForRequestCancellation(isCancelled, producerContext);
}
Aggregations