Search in sources :

Example 21 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class JobSchedulerTest method testUpdate_Last.

@Test
public void testUpdate_Last() {
    EncodedImage encodedImage = fakeEncodedImage();
    assertTrue(mJobScheduler.updateJob(encodedImage, true));
    assertNotSame(encodedImage, mJobScheduler.mEncodedImage);
    assertReferencesEqual(encodedImage, mJobScheduler.mEncodedImage);
    assertEquals(true, mJobScheduler.mIsLast);
    assertEquals(0, mTestScheduledExecutorService.getPendingCount());
    assertEquals(0, mTestExecutorService.getPendingCount());
    assertEquals(0, mTestJobRunnable.jobs.size());
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class JobSchedulerTest method testSchedule_Last_Running_And_Pending.

@Test
public void testSchedule_Last_Running_And_Pending() {
    EncodedImage encodedImage1 = fakeEncodedImage();
    mJobScheduler.updateJob(encodedImage1, true);
    assertTrue(mJobScheduler.scheduleJob());
    final EncodedImage encodedImage2 = fakeEncodedImage();
    final EncodedImage encodedImage3 = fakeEncodedImage();
    Executors.newFixedThreadPool(1).execute(new Runnable() {

        @Override
        public void run() {
            // wait until the job starts running
            waitForCondition(mTestJobRunnable.running, true);
            assertEquals(0, mTestScheduledExecutorService.getPendingCount());
            assertEquals(0, mTestExecutorService.getPendingCount());
            assertEquals(0, mTestJobRunnable.jobs.size());
            mJobScheduler.updateJob(encodedImage2, true);
            assertEquals(JobScheduler.JobState.RUNNING, mJobScheduler.mJobState);
            assertTrue(mJobScheduler.scheduleJob());
            assertEquals(JobScheduler.JobState.RUNNING_AND_PENDING, mJobScheduler.mJobState);
            mJobScheduler.updateJob(encodedImage3, true);
            assertEquals(JobScheduler.JobState.RUNNING_AND_PENDING, mJobScheduler.mJobState);
            assertTrue(mJobScheduler.scheduleJob());
            assertEquals(JobScheduler.JobState.RUNNING_AND_PENDING, mJobScheduler.mJobState);
            mTestJobRunnable.wait.set(false);
        }
    });
    // block running until the above code executed on another thread finishes
    mTestJobRunnable.wait.set(true);
    mFakeClockForTime.incrementBy(0);
    mFakeClockForScheduled.incrementBy(0);
    // this line blocks
    mFakeClockForWorker.incrementBy(0);
    assertEquals(JobScheduler.JobState.QUEUED, mJobScheduler.mJobState);
    assertEquals(1, mTestScheduledExecutorService.getPendingCount());
    assertEquals(0, mTestExecutorService.getPendingCount());
    assertEquals(1, mTestJobRunnable.jobs.size());
    assertJobsEqual(mTestJobRunnable.jobs.get(0), encodedImage1, true);
    mFakeClockForTime.incrementBy(INTERVAL);
    mFakeClockForScheduled.incrementBy(INTERVAL);
    mFakeClockForWorker.incrementBy(INTERVAL);
    assertEquals(JobScheduler.JobState.IDLE, mJobScheduler.mJobState);
    assertEquals(0, mTestScheduledExecutorService.getPendingCount());
    assertEquals(0, mTestExecutorService.getPendingCount());
    assertEquals(2, mTestJobRunnable.jobs.size());
    assertJobsEqual(mTestJobRunnable.jobs.get(1), encodedImage3, true);
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class LocalAssetFetchProducerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(mAssetManager.openFd(eq(TEST_FILENAME))).thenReturn(mAssetFileDescriptor);
    when(mAssetFileDescriptor.getLength()).thenReturn((long) TEST_DATA_LENGTH);
    when(mPooledByteBufferFactory.newByteBuffer(any(InputStream.class), eq(TEST_DATA_LENGTH))).thenReturn(mPooledByteBuffer);
    mExecutor = new TestExecutorService(new FakeClock());
    mLocalAssetFetchProducer = new LocalAssetFetchProducer(mExecutor, mPooledByteBufferFactory, mAssetManager);
    mProducerContext = new SettableProducerContext(mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM);
    when(mImageRequest.getSourceUri()).thenReturn(Uri.parse("asset:///" + TEST_FILENAME));
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            mCapturedEncodedImage = EncodedImage.cloneOrNull((EncodedImage) invocation.getArguments()[0]);
            return null;
        }
    }).when(mConsumer).onNewResult(notNull(EncodedImage.class), anyBoolean());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FakeClock(com.facebook.imagepipeline.testing.FakeClock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 24 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class BranchOnSeparateImagesProducerTest method testFirstProducerReturnsTwoResultsThumbnailsNotAllowed.

@Test
public void testFirstProducerReturnsTwoResultsThumbnailsNotAllowed() {
    when(mImageRequest.getLocalThumbnailPreviewsEnabled()).thenReturn(false);
    EncodedImage intermediateEncodedImage = new EncodedImage(mIntermediateResult.getByteBufferRef());
    intermediateEncodedImage.setImageFormat(DefaultImageFormats.JPEG);
    intermediateEncodedImage.setRotationAngle(-1);
    intermediateEncodedImage.setWidth(WIDTH / 2);
    intermediateEncodedImage.setHeight(HEIGHT / 2);
    mFirstProducerConsumer.onNewResult(intermediateEncodedImage, false);
    verify(mConsumer, never()).onNewResult(intermediateEncodedImage, false);
    EncodedImage finalEncodedImage = new EncodedImage(mFirstProducerFinalResult.getByteBufferRef());
    finalEncodedImage.setImageFormat(DefaultImageFormats.JPEG);
    finalEncodedImage.setRotationAngle(-1);
    finalEncodedImage.setWidth(WIDTH);
    finalEncodedImage.setHeight(HEIGHT);
    mFirstProducerConsumer.onNewResult(finalEncodedImage, true);
    verify(mConsumer).onNewResult(finalEncodedImage, true);
    verify(mInputProducer2, never()).produceResults(any(Consumer.class), eq(mProducerContext));
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 25 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class BranchOnSeparateImagesProducerTest method testFirstProducerReturnsIntermediateResultThenGoodEnoughResult.

@Test
public void testFirstProducerReturnsIntermediateResultThenGoodEnoughResult() {
    EncodedImage intermediateEncodedImage = new EncodedImage(mIntermediateResult.getByteBufferRef());
    intermediateEncodedImage.setImageFormat(DefaultImageFormats.JPEG);
    intermediateEncodedImage.setRotationAngle(-1);
    intermediateEncodedImage.setWidth(WIDTH);
    intermediateEncodedImage.setHeight(HEIGHT);
    mFirstProducerConsumer.onNewResult(intermediateEncodedImage, false);
    verify(mConsumer).onNewResult(intermediateEncodedImage, false);
    EncodedImage finalEncodedImage = new EncodedImage(mFirstProducerFinalResult.getByteBufferRef());
    finalEncodedImage.setImageFormat(DefaultImageFormats.JPEG);
    finalEncodedImage.setRotationAngle(-1);
    finalEncodedImage.setWidth(WIDTH);
    finalEncodedImage.setHeight(HEIGHT);
    mFirstProducerConsumer.onNewResult(finalEncodedImage, true);
    verify(mConsumer).onNewResult(finalEncodedImage, true);
    verify(mInputProducer2, never()).produceResults(any(Consumer.class), eq(mProducerContext));
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Aggregations

EncodedImage (com.facebook.imagepipeline.image.EncodedImage)91 Test (org.junit.Test)24 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)19 CacheKey (com.facebook.cache.common.CacheKey)9 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)9 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)9 InputStream (java.io.InputStream)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 Bitmap (android.graphics.Bitmap)7 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)7 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)6 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)6 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)6 FakeClock (com.facebook.imagepipeline.testing.FakeClock)6 TestExecutorService (com.facebook.imagepipeline.testing.TestExecutorService)6 Before (org.junit.Before)6 Rect (android.graphics.Rect)5 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)5 ArrayList (java.util.ArrayList)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5