public SettableProducerContext( ImageRequest imageRequest, String id, ProducerListener producerListener, Object callerContext, ImageRequest.RequestLevel lowestPermittedRequestLevel, boolean isPrefetch, boolean isIntermediateResultExpected, Priority priority) { super( imageRequest, id, producerListener, callerContext, lowestPermittedRequestLevel, isPrefetch, isIntermediateResultExpected, priority); }
public BaseProducerContext( ImageRequest imageRequest, String id, ProducerListener producerListener, Object callerContext, ImageRequest.RequestLevel lowestPermittedRequestLevel, boolean isPrefetch, boolean isIntermediateResultExpected, Priority priority) { mImageRequest = imageRequest; mId = id; mProducerListener = producerListener; mCallerContext = callerContext; mLowestPermittedRequestLevel = lowestPermittedRequestLevel; mIsPrefetch = isPrefetch; mPriority = priority; mIsIntermediateResultExpected = isIntermediateResultExpected; mIsCancelled = false; mCallbacks = new ArrayList<>(); }
/** * Submits a request for prefetching to the bitmap cache. * * <p> Beware that if your network fetcher doesn't support priorities prefetch requests may slow * down images which are immediately required on screen. * * @param imageRequest the request to submit * @return a DataSource that can safely be ignored. */ public DataSource<Void> prefetchToBitmapCache( ImageRequest imageRequest, Object callerContext) { if (!mIsPrefetchEnabledSupplier.get()) { return DataSources.immediateFailedDataSource(PREFETCH_EXCEPTION); } try { Producer<Void> producerSequence = mSuppressBitmapPrefetchingSupplier.get() ? mProducerSequenceFactory.getEncodedImagePrefetchProducerSequence(imageRequest) : mProducerSequenceFactory.getDecodedImagePrefetchProducerSequence(imageRequest); return submitPrefetchRequest( producerSequence, imageRequest, ImageRequest.RequestLevel.FULL_FETCH, callerContext, Priority.MEDIUM); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
/** * Submits a request for prefetching to the disk cache. * * <p> Beware that if your network fetcher doesn't support priorities prefetch requests may slow * down images which are immediately required on screen. * * @param imageRequest the request to submit * @param priority custom priority for the fetch * @return a DataSource that can safely be ignored. */ public DataSource<Void> prefetchToDiskCache( ImageRequest imageRequest, Object callerContext, Priority priority) { if (!mIsPrefetchEnabledSupplier.get()) { return DataSources.immediateFailedDataSource(PREFETCH_EXCEPTION); } try { Producer<Void> producerSequence = mProducerSequenceFactory.getEncodedImagePrefetchProducerSequence(imageRequest); return submitPrefetchRequest( producerSequence, imageRequest, ImageRequest.RequestLevel.FULL_FETCH, callerContext, priority); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
@Test public void testCreatingRequestFromExistingRequest() { ImageRequest original = ImageRequestBuilder .newBuilderWithSource(Uri.parse("http://frescolib.org/image.jpg")) .setCacheChoice(ImageRequest.CacheChoice.SMALL) .setImageDecodeOptions(new ImageDecodeOptionsBuilder().build()) .setLocalThumbnailPreviewsEnabled(true) .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE) .setMediaVariations(MediaVariations.forMediaId("A113")) .setPostprocessor(new BasePostprocessor() { @Override public String getName() { return super.getName(); } }) .setProgressiveRenderingEnabled(true) .setRequestListener(new RequestLoggingListener()) .setResizeOptions(new ResizeOptions(20, 20)) .setRotationOptions(RotationOptions.forceRotation(RotationOptions.ROTATE_90)) .setRequestPriority(Priority.HIGH) .build(); ImageRequest copy = ImageRequestBuilder.fromRequest(original).build(); assertThat(copy).isEqualTo(original); }
@Test public void testGetHigherPriority() throws Exception { assertEquals(HIGH, Priority.getHigherPriority(null, HIGH)); assertEquals(HIGH, Priority.getHigherPriority(LOW, HIGH)); assertEquals(HIGH, Priority.getHigherPriority(MEDIUM, HIGH)); assertEquals(HIGH, Priority.getHigherPriority(HIGH, HIGH)); assertEquals(HIGH, Priority.getHigherPriority(HIGH, MEDIUM)); assertEquals(HIGH, Priority.getHigherPriority(HIGH, LOW)); assertEquals(HIGH, Priority.getHigherPriority(HIGH, null)); assertEquals(MEDIUM, Priority.getHigherPriority(null, MEDIUM)); assertEquals(MEDIUM, Priority.getHigherPriority(LOW, MEDIUM)); assertEquals(MEDIUM, Priority.getHigherPriority(MEDIUM, MEDIUM)); assertEquals(MEDIUM, Priority.getHigherPriority(MEDIUM, LOW)); assertEquals(MEDIUM, Priority.getHigherPriority(MEDIUM, null)); assertEquals(LOW, Priority.getHigherPriority(null, LOW)); assertEquals(LOW, Priority.getHigherPriority(LOW, LOW)); assertEquals(LOW, Priority.getHigherPriority(LOW, null)); assertEquals(null, Priority.getHigherPriority(null, null)); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); mExecutor = new TestExecutorService(new FakeClock()); mQualifiedResourceFetchProducer = new QualifiedResourceFetchProducer( mExecutor, mPooledByteBufferFactory, mContentResolver); mContentUri = UriUtil.getUriForQualifiedResource(PACKAGE_NAME, RESOURCE_ID); mProducerContext = new SettableProducerContext( mImageRequest, REQUEST_ID, mProducerListener, CALLER_CONTEXT, ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM); when(mImageRequest.getSourceUri()).thenReturn(mContentUri); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); mExecutor = new TestExecutorService(new FakeClock()); mLocalVideoThumbnailProducer = new LocalVideoThumbnailProducer( mExecutor, RuntimeEnvironment.application.getContentResolver()); mFile = new File(RuntimeEnvironment.application.getExternalFilesDir(null), TEST_FILENAME); mockStatic(ThumbnailUtils.class); mProducerContext = new SettableProducerContext( mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, false, false, Priority.MEDIUM); when(mImageRequest.getSourceFile()).thenReturn(mFile); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); mProducerContext = new SettableProducerContext( mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM); mTestExecutorService = new TestExecutorService(new FakeClock()); mThreadHandoffProducer = new ThreadHandoffProducer( mInputProducer, new ThreadHandoffProducerQueue(mTestExecutorService)); }
@Test public void testPrefetchToBitmapCache() { Producer<Void> prefetchProducerSequence = mock(Producer.class); when(mProducerSequenceFactory.getDecodedImagePrefetchProducerSequence(mImageRequest)) .thenReturn(prefetchProducerSequence); DataSource<Void> dataSource = mImagePipeline.prefetchToBitmapCache(mImageRequest, mCallerContext); assertTrue(!dataSource.isFinished()); verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", true); verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", true); ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class); verify(prefetchProducerSequence) .produceResults(any(Consumer.class), producerContextArgumentCaptor.capture()); assertFalse(producerContextArgumentCaptor.getValue().isIntermediateResultExpected()); assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.MEDIUM); }
@Test public void testFetchDecodedImage() { Producer<CloseableReference<CloseableImage>> decodedSequence = mock(Producer.class); when(mProducerSequenceFactory.getDecodedImageProducerSequence(mImageRequest)) .thenReturn(decodedSequence); DataSource<CloseableReference<CloseableImage>> dataSource = mImagePipeline.fetchDecodedImage(mImageRequest, mCallerContext); assertFalse(dataSource.isFinished()); verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", false); verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", false); ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class); verify(decodedSequence) .produceResults(any(Consumer.class), producerContextArgumentCaptor.capture()); assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected()); assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH); }
@Test public void testFetchDecodedImageWithRequestLevel() { Producer<CloseableReference<CloseableImage>> decodedSequence = mock(Producer.class); when(mProducerSequenceFactory.getDecodedImageProducerSequence(mImageRequest)) .thenReturn(decodedSequence); DataSource<CloseableReference<CloseableImage>> dataSource = mImagePipeline.fetchDecodedImage( mImageRequest, mCallerContext, ImageRequest.RequestLevel.DISK_CACHE); assertFalse(dataSource.isFinished()); verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", false); verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", false); ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class); verify(decodedSequence) .produceResults(any(Consumer.class), producerContextArgumentCaptor.capture()); assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected()); assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH); assertEquals( producerContextArgumentCaptor.getValue().getLowestPermittedRequestLevel(), ImageRequest.RequestLevel.DISK_CACHE); }
@Test public void testFetchFromBitmapCacheDueToMethodCall() { Producer<CloseableReference<CloseableImage>> bitmapCacheSequence = mock(Producer.class); when(mProducerSequenceFactory.getDecodedImageProducerSequence(mImageRequest)) .thenReturn(bitmapCacheSequence); mImagePipeline.fetchImageFromBitmapCache(mImageRequest, mCallerContext); verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", false); verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", false); ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class); verify(bitmapCacheSequence) .produceResults(any(Consumer.class), producerContextArgumentCaptor.capture()); assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected()); assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH); assertEquals( producerContextArgumentCaptor.getValue().getLowestPermittedRequestLevel(), ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE); }
@Test public void testFetchFromBitmapCacheDueToImageRequest() { Producer<CloseableReference<CloseableImage>> bitmapCacheSequence = mock(Producer.class); when(mImageRequest.getLowestPermittedRequestLevel()) .thenReturn(ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE); when(mProducerSequenceFactory.getDecodedImageProducerSequence(mImageRequest)) .thenReturn(bitmapCacheSequence); mImagePipeline.fetchDecodedImage(mImageRequest, mCallerContext); verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", false); verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", false); ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class); verify(bitmapCacheSequence) .produceResults(any(Consumer.class), producerContextArgumentCaptor.capture()); assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected()); assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH); assertEquals( producerContextArgumentCaptor.getValue().getLowestPermittedRequestLevel(), ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE); }
@Test public void testGetBitmapCacheGetSupplier() { Supplier<DataSource<CloseableReference<CloseableImage>>> dataSourceSupplier = mImagePipeline.getDataSourceSupplier( mImageRequest, mCallerContext, ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE); Producer<CloseableReference<CloseableImage>> bitmapCacheSequence = mock(Producer.class); when(mProducerSequenceFactory.getDecodedImageProducerSequence(mImageRequest)) .thenReturn(bitmapCacheSequence); dataSourceSupplier.get(); verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", false); verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", false); ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class); verify(bitmapCacheSequence) .produceResults(any(Consumer.class), producerContextArgumentCaptor.capture()); assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected()); assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH); }
@Test public void testGetFullFetchSupplier() { Supplier<DataSource<CloseableReference<CloseableImage>>> dataSourceSupplier = mImagePipeline .getDataSourceSupplier(mImageRequest, mCallerContext, ImageRequest.RequestLevel.FULL_FETCH); Producer<CloseableReference<CloseableImage>> decodedSequence = mock(Producer.class); when(mProducerSequenceFactory.getDecodedImageProducerSequence(mImageRequest)) .thenReturn(decodedSequence); DataSource<CloseableReference<CloseableImage>> dataSource = dataSourceSupplier.get(); assertFalse(dataSource.isFinished()); verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", false); verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", false); ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class); verify(decodedSequence) .produceResults(any(Consumer.class), producerContextArgumentCaptor.capture()); assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected()); assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); mTestExecutorService = new TestExecutorService(new FakeClock()); mPostprocessorProducer = new PostprocessorProducer( mInputProducer, mPlatformBitmapFactory, mTestExecutorService); mProducerContext = new SettableProducerContext( mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, false /* isPrefetch */, false /* isIntermediateResultExpected */, Priority.MEDIUM); when(mImageRequest.getPostprocessor()).thenReturn(mPostprocessor); mResults = new ArrayList<>(); when(mPostprocessor.getName()).thenReturn(POSTPROCESSOR_NAME); when(mProducerListener.requiresExtraMap(mRequestId)).thenReturn(true); doAnswer( new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { mResults.add( ((CloseableReference<CloseableImage>) invocation.getArguments()[0]).clone()); return null; } } ).when(mConsumer).onNewResult(any(CloseableReference.class), anyInt()); mInOrder = inOrder(mPostprocessor, mProducerListener, mConsumer); }
private synchronized Priority computePriority() { Priority priority = Priority.LOW; for (Pair<Consumer<T>, ProducerContext> pair : mConsumerContextPairs) { priority = Priority.getHigherPriority(priority, pair.second.getPriority()); } return priority; }
private DataSource<Void> submitPrefetchRequest( Producer<Void> producerSequence, ImageRequest imageRequest, ImageRequest.RequestLevel lowestPermittedRequestLevelOnSubmit, Object callerContext, Priority priority) { final RequestListener requestListener = getRequestListenerForRequest(imageRequest); try { ImageRequest.RequestLevel lowestPermittedRequestLevel = ImageRequest.RequestLevel.getMax( imageRequest.getLowestPermittedRequestLevel(), lowestPermittedRequestLevelOnSubmit); SettableProducerContext settableProducerContext = new SettableProducerContext( imageRequest, generateUniqueFutureId(), requestListener, callerContext, lowestPermittedRequestLevel, /* isPrefetch */ true, /* isIntermediateResultExpected */ false, priority); return ProducerToDataSourceAdapter.create( producerSequence, settableProducerContext, requestListener); } catch (Exception exception) { return DataSources.immediateFailedDataSource(exception); } }
@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), anyInt()); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(mResources.openRawResourceFd(eq(TEST_ID))).thenReturn(mAssetFileDescriptor); when(mAssetFileDescriptor.getLength()).thenReturn((long) TEST_DATA_LENGTH); mExecutor = new TestExecutorService(new FakeClock()); mLocalResourceFetchProducer = new LocalResourceFetchProducer( mExecutor, mPooledByteBufferFactory, mResources ); mProducerContext = new SettableProducerContext( mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM); when(mImageRequest.getSourceUri()).thenReturn(Uri.parse("res:///" + TEST_ID)); 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), anyInt()); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); PowerMockito.mockStatic(SystemClock.class); mNetworkFetchProducer = new NetworkFetchProducer( mPooledByteBufferFactory, mByteArrayPool, mNetworkFetcher); mProducerContext = new SettableProducerContext( mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, false /* isPrefetch */, true /* isIntermediateResultExpected */, Priority.MEDIUM); mFetchState = new FetchState(mConsumer, mProducerContext); mCommonByteArray = new byte[10]; when(mByteArrayPool.get(anyInt())).thenReturn(mCommonByteArray); when(mPooledByteBufferFactory.newOutputStream(anyInt())) .thenReturn(mPooledByteBufferOutputStream); when(mPooledByteBufferFactory.newOutputStream()) .thenReturn(mPooledByteBufferOutputStream); when(mPooledByteBufferOutputStream.toByteBuffer()).thenReturn(mPooledByteBuffer); when(mProducerListener.requiresExtraMap(anyString())).thenReturn(true); when(mNetworkFetcher.getExtraMap(any(FetchState.class), anyInt())).thenReturn(mExtrasMap); when(mNetworkFetcher.createFetchState(eq(mConsumer), eq(mProducerContext))) .thenReturn(mFetchState); mTestExecutor = Executors.newCachedThreadPool(); }
private void setupImageRequest(String requestId, ImageRequest imageRequest) { mImageRequest = imageRequest; mRequestId = requestId; mProducerContext = new SettableProducerContext( mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, /* isPrefetch */ false, /* isIntermediateResultExpected */ true, Priority.MEDIUM); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); mExecutor = new TestExecutorService(new FakeClock()); mLocalContentUriFetchProducer = new LocalContentUriFetchProducer( mExecutor, mPooledByteBufferFactory, mContentResolver ); mContentUri = Uri.fromFile(mock(File.class)); mProducerContext = new SettableProducerContext( mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM); when(mImageRequest.getSourceUri()).thenReturn(mContentUri); 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), anyInt()); }