Search in sources :

Example 31 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class LocalResourceFetchProducerTest method testFetchLocalResource.

@Test
public void testFetchLocalResource() throws Exception {
    PooledByteBuffer pooledByteBuffer = mock(PooledByteBuffer.class);
    when(mPooledByteBufferFactory.newByteBuffer(any(InputStream.class), eq(TEST_DATA_LENGTH))).thenReturn(pooledByteBuffer);
    when(mResources.openRawResource(eq(TEST_ID))).thenReturn(new ByteArrayInputStream(new byte[TEST_DATA_LENGTH]));
    mLocalResourceFetchProducer.produceResults(mConsumer, mProducerContext);
    mExecutor.runUntilIdle();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer)

Example 32 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer 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 : ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 33 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class DecodeProducerTest method testNewResult_Intermediate_pJPEG.

@Test
public void testNewResult_Intermediate_pJPEG() {
    setupNetworkUri();
    Consumer<EncodedImage> consumer = produceResults();
    InOrder inOrder = inOrder(mJobScheduler, mProgressiveJpegParser);
    ArgumentCaptor<EncodedImage> argumentCaptor = ArgumentCaptor.forClass(EncodedImage.class);
    // preview scan; schedule
    when(mJobScheduler.updateJob(mEncodedImage, false)).thenReturn(true);
    when(mProgressiveJpegParser.parseMoreData(any(EncodedImage.class))).thenReturn(true);
    when(mProgressiveJpegParser.getBestScanNumber()).thenReturn(PREVIEW_SCAN);
    consumer.onNewResult(mEncodedImage, false);
    inOrder.verify(mJobScheduler).updateJob(mEncodedImage, false);
    inOrder.verify(mProgressiveJpegParser).parseMoreData(argumentCaptor.capture());
    inOrder.verify(mJobScheduler).scheduleJob();
    assertSame(((EncodedImage) argumentCaptor.getValue()).getUnderlyingReferenceTestOnly(), mByteBufferRef.getUnderlyingReferenceTestOnly());
    // no data parsed; ignore
    PooledByteBuffer pooledByteBuffer2 = mockPooledByteBuffer(210);
    CloseableReference<PooledByteBuffer> ref2 = CloseableReference.of(pooledByteBuffer2);
    EncodedImage encodedImage2 = mockEncodedImage(ref2);
    when(mJobScheduler.updateJob(encodedImage2, false)).thenReturn(true);
    when(mProgressiveJpegParser.parseMoreData(encodedImage2)).thenReturn(false);
    when(mProgressiveJpegParser.getBestScanNumber()).thenReturn(PREVIEW_SCAN);
    consumer.onNewResult(encodedImage2, false);
    inOrder.verify(mJobScheduler).updateJob(encodedImage2, false);
    inOrder.verify(mProgressiveJpegParser).parseMoreData(argumentCaptor.capture());
    inOrder.verify(mJobScheduler, never()).scheduleJob();
    assertSame(((EncodedImage) argumentCaptor.getValue()).getUnderlyingReferenceTestOnly(), ref2.getUnderlyingReferenceTestOnly());
    // same scan; ignore
    PooledByteBuffer pooledByteBuffer3 = mockPooledByteBuffer(220);
    CloseableReference<PooledByteBuffer> ref3 = CloseableReference.of(pooledByteBuffer3);
    EncodedImage encodedImage3 = mockEncodedImage(ref3);
    when(mJobScheduler.updateJob(encodedImage3, false)).thenReturn(true);
    when(mProgressiveJpegParser.parseMoreData(encodedImage3)).thenReturn(true);
    when(mProgressiveJpegParser.getBestScanNumber()).thenReturn(PREVIEW_SCAN);
    consumer.onNewResult(encodedImage3, false);
    inOrder.verify(mJobScheduler).updateJob(encodedImage3, false);
    inOrder.verify(mProgressiveJpegParser).parseMoreData(argumentCaptor.capture());
    inOrder.verify(mJobScheduler, never()).scheduleJob();
    assertSame(((EncodedImage) argumentCaptor.getValue()).getUnderlyingReferenceTestOnly(), ref3.getUnderlyingReferenceTestOnly());
    // scan not for decode; ignore
    PooledByteBuffer pooledByteBuffer4 = mockPooledByteBuffer(300);
    CloseableReference<PooledByteBuffer> ref4 = CloseableReference.of(pooledByteBuffer4);
    EncodedImage encodedImage4 = mockEncodedImage(ref4);
    when(mJobScheduler.updateJob(encodedImage4, false)).thenReturn(true);
    when(mProgressiveJpegParser.parseMoreData(encodedImage4)).thenReturn(true);
    when(mProgressiveJpegParser.getBestScanNumber()).thenReturn(IGNORED_SCAN);
    consumer.onNewResult(encodedImage4, false);
    inOrder.verify(mJobScheduler).updateJob(encodedImage4, false);
    inOrder.verify(mProgressiveJpegParser).parseMoreData(argumentCaptor.capture());
    inOrder.verify(mJobScheduler, never()).scheduleJob();
    assertSame(((EncodedImage) argumentCaptor.getValue()).getUnderlyingReferenceTestOnly(), ref4.getUnderlyingReferenceTestOnly());
    // good-enough scan; schedule
    PooledByteBuffer pooledByteBuffer5 = mockPooledByteBuffer(500);
    CloseableReference<PooledByteBuffer> ref5 = CloseableReference.of(pooledByteBuffer5);
    EncodedImage encodedImage5 = mockEncodedImage(ref5);
    when(mJobScheduler.updateJob(encodedImage5, false)).thenReturn(true);
    when(mProgressiveJpegParser.parseMoreData(encodedImage5)).thenReturn(true);
    when(mProgressiveJpegParser.getBestScanNumber()).thenReturn(GOOD_ENOUGH_SCAN);
    consumer.onNewResult(encodedImage5, false);
    inOrder.verify(mJobScheduler).updateJob(encodedImage5, false);
    inOrder.verify(mProgressiveJpegParser).parseMoreData(argumentCaptor.capture());
    inOrder.verify(mJobScheduler).scheduleJob();
    assertSame(((EncodedImage) argumentCaptor.getValue()).getUnderlyingReferenceTestOnly(), ref5.getUnderlyingReferenceTestOnly());
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 34 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class DecodeProducerTest method mockPooledByteBuffer.

private static PooledByteBuffer mockPooledByteBuffer(int size) {
    PooledByteBuffer pooledByteBuffer = mock(PooledByteBuffer.class);
    when(pooledByteBuffer.size()).thenReturn(size);
    return pooledByteBuffer;
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer)

Aggregations

PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)34 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)14 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)7 InputStream (java.io.InputStream)7 Bitmap (android.graphics.Bitmap)6 BitmapFactory (android.graphics.BitmapFactory)3 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)3 TargetApi (android.annotation.TargetApi)2 Uri (android.net.Uri)2 PooledByteBufferInputStream (com.facebook.common.memory.PooledByteBufferInputStream)2 AnimatedImage (com.facebook.imagepipeline.animated.base.AnimatedImage)2 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)2 BitmapCounter (com.facebook.imagepipeline.memory.BitmapCounter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Before (org.junit.Before)2 Rect (android.graphics.Rect)1 ExifInterface (android.media.ExifInterface)1 BinaryResource (com.facebook.binaryresource.BinaryResource)1 CacheKey (com.facebook.cache.common.CacheKey)1 PooledByteBufferOutputStream (com.facebook.common.memory.PooledByteBufferOutputStream)1