Search in sources :

Example 76 with EncodedImage

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());
}
Also used : InputStream(java.io.InputStream) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 77 with EncodedImage

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);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) Before(org.junit.Before)

Example 78 with EncodedImage

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());
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 79 with EncodedImage

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);
    }
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 80 with EncodedImage

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) Uri(android.net.Uri) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey)

Aggregations

EncodedImage (com.facebook.imagepipeline.image.EncodedImage)108 Test (org.junit.Test)32 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)20 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)12 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)12 CacheKey (com.facebook.cache.common.CacheKey)11 Rect (android.graphics.Rect)9 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)9 InputStream (java.io.InputStream)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)9 AnimatedDrawableBackend (com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend)8 AnimatedImageCompositor (com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor)8 ImageDecodeOptions (com.facebook.imagepipeline.common.ImageDecodeOptions)8 Before (org.junit.Before)8 Bitmap (android.graphics.Bitmap)7 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)7 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)7 FakeClock (com.facebook.imagepipeline.testing.FakeClock)6 TestExecutorService (com.facebook.imagepipeline.testing.TestExecutorService)6