Search in sources :

Example 1 with MediaVariations

use of com.facebook.imagepipeline.request.MediaVariations in project fresco by facebook.

the class MediaVariationsIndexDatabaseTest method testGetsNoCachedVariantsIfNothingStored.

@Test
public void testGetsNoCachedVariantsIfNothingStored() {
    MediaVariations mediaVariations = mMediaVariationsIndexDatabase.getCachedVariantsSync(MEDIA_ID, MediaVariations.newBuilderForMediaId(MEDIA_ID));
    assertThat(mediaVariations.getMediaId()).isEqualTo(MEDIA_ID);
    assertThat(mediaVariations.getVariantsCount()).isZero();
}
Also used : MediaVariations(com.facebook.imagepipeline.request.MediaVariations) Test(org.junit.Test)

Example 2 with MediaVariations

use of com.facebook.imagepipeline.request.MediaVariations in project fresco by facebook.

the class MediaVariationsIndexDatabaseTest method testMediaVariationsShouldForceRequestIfSetInProvidedBuilder.

@Test
public void testMediaVariationsShouldForceRequestIfSetInProvidedBuilder() {
    mMediaVariationsIndexDatabase.saveCachedVariantSync(MEDIA_ID, CACHE_CHOICE_1, CACHE_KEY_1, mEncodedImage1);
    MediaVariations.Builder builder = MediaVariations.newBuilderForMediaId(MEDIA_ID).setForceRequestForSpecifiedUri(true);
    MediaVariations mediaVariations = mMediaVariationsIndexDatabase.getCachedVariantsSync(MEDIA_ID, builder);
    assertThat(mediaVariations.shouldForceRequestForSpecifiedUri()).isTrue();
}
Also used : MediaVariations(com.facebook.imagepipeline.request.MediaVariations) Test(org.junit.Test)

Example 3 with MediaVariations

use of com.facebook.imagepipeline.request.MediaVariations in project fresco by facebook.

the class MediaVariationsIndexDatabaseTest method testGetsSameCachedVariantAfterBeingSavedIfOneSaved.

@Test
public void testGetsSameCachedVariantAfterBeingSavedIfOneSaved() {
    mMediaVariationsIndexDatabase.saveCachedVariantSync(MEDIA_ID, ImageRequest.CacheChoice.DEFAULT, CACHE_KEY_1, mEncodedImage1);
    MediaVariations mediaVariations = mMediaVariationsIndexDatabase.getCachedVariantsSync(MEDIA_ID, MediaVariations.newBuilderForMediaId(MEDIA_ID));
    assertThat(mediaVariations.getVariantsCount()).isEqualTo(1);
    assertVariantIsEqualTo(mediaVariations.getVariant(0), URI_1, WIDTH_1, HEIGHT_1, CACHE_CHOICE_1);
}
Also used : MediaVariations(com.facebook.imagepipeline.request.MediaVariations) Test(org.junit.Test)

Example 4 with MediaVariations

use of com.facebook.imagepipeline.request.MediaVariations in project fresco by facebook.

the class MediaVariationsFallbackProducerTest method testSendsNonFinalImageToConsumerAndStartsInputProducerIfRequestForcesRequestForSpecifiedUri.

@Test
public void testSendsNonFinalImageToConsumerAndStartsInputProducerIfRequestForcesRequestForSpecifiedUri() {
    MediaVariations mediaVariations = MediaVariations.newBuilderForMediaId(MEDIA_ID).setForceRequestForSpecifiedUri(true).build();
    when(mImageRequest.getMediaVariations()).thenReturn(mediaVariations);
    whenCacheContains(mDefaultBufferedDiskCache, CACHE_KEY_S, CACHE_KEY_M, CACHE_KEY_L);
    whenIndexDbContains(URI_M, SIZE_M, CacheChoice.DEFAULT);
    when(mImageRequest.getResizeOptions()).thenReturn(new ResizeOptions(SIZE_M - 10, SIZE_M - 10));
    mMediaVariationsFallbackProducer.produceResults(mConsumer, mProducerContext);
    verify(mConsumer).onNewResult(mImageM, false);
    verify(mConsumer, never()).onProgressUpdate(anyFloat());
    verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
    verifySuccessSentToListener(FOUND, NOT_USED_AS_LAST, MediaVariations.SOURCE_INDEX_DB, 1);
    verifyInputProducerProduceResultsWithNewConsumer();
    verifyZeroInteractions(mSmallImageBufferedDiskCache);
}
Also used : ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) MediaVariations(com.facebook.imagepipeline.request.MediaVariations) Test(org.junit.Test)

Example 5 with MediaVariations

use of com.facebook.imagepipeline.request.MediaVariations in project fresco by facebook.

the class MediaVariationsFallbackProducer method produceResults.

public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final ResizeOptions resizeOptions = imageRequest.getResizeOptions();
    final MediaVariations mediaVariations = imageRequest.getMediaVariations();
    if (!imageRequest.isDiskCacheEnabled() || resizeOptions == null || resizeOptions.height <= 0 || resizeOptions.width <= 0) {
        startInputProducerWithExistingConsumer(consumer, producerContext);
        return;
    }
    final String mediaId;
    @MediaVariations.Source final String source;
    if (mediaVariations == null) {
        if (mMediaIdExtractor == null) {
            mediaId = null;
            source = null;
        } else {
            mediaId = mMediaIdExtractor.getMediaIdFrom(imageRequest.getSourceUri());
            source = MediaVariations.SOURCE_ID_EXTRACTOR;
        }
    } else {
        mediaId = mediaVariations.getMediaId();
        source = MediaVariations.SOURCE_INDEX_DB;
    }
    if (mediaVariations == null && mediaId == null) {
        startInputProducerWithExistingConsumer(consumer, producerContext);
        return;
    }
    producerContext.getListener().onProducerStart(producerContext.getId(), PRODUCER_NAME);
    final AtomicBoolean isCancelled = new AtomicBoolean(false);
    if (mediaVariations != null && mediaVariations.getVariantsCount() > 0) {
        chooseFromVariants(consumer, producerContext, imageRequest, mediaVariations, resizeOptions, isCancelled);
    } else {
        MediaVariations.Builder mediaVariationsBuilder = MediaVariations.newBuilderForMediaId(mediaId).setForceRequestForSpecifiedUri(mediaVariations != null && mediaVariations.shouldForceRequestForSpecifiedUri()).setSource(source);
        Task<MediaVariations> indexedMediaVariationsTask = mMediaVariationsIndex.getCachedVariants(mediaId, mediaVariationsBuilder);
        indexedMediaVariationsTask.continueWith(new Continuation<MediaVariations, Object>() {

            @Override
            public Object then(Task<MediaVariations> task) throws Exception {
                if (task.isCancelled() || task.isFaulted()) {
                    return task;
                } else {
                    try {
                        if (task.getResult() == null) {
                            startInputProducerWithWrappedConsumer(consumer, producerContext, mediaId);
                            return null;
                        } else {
                            return chooseFromVariants(consumer, producerContext, imageRequest, task.getResult(), resizeOptions, isCancelled);
                        }
                    } catch (Exception e) {
                        return null;
                    }
                }
            }
        });
    }
    subscribeTaskForRequestCancellation(isCancelled, producerContext);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CancellationException(java.util.concurrent.CancellationException) MediaVariations(com.facebook.imagepipeline.request.MediaVariations)

Aggregations

MediaVariations (com.facebook.imagepipeline.request.MediaVariations)9 Test (org.junit.Test)7 ResizeOptions (com.facebook.imagepipeline.common.ResizeOptions)2 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)2 CacheKey (com.facebook.cache.common.CacheKey)1 BufferedDiskCache (com.facebook.imagepipeline.cache.BufferedDiskCache)1 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)1 CancellationException (java.util.concurrent.CancellationException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1