private Consumer<T> createConsumer() { return new BaseConsumer<T>() { @Override protected void onNewResultImpl(@Nullable T newResult, @Status int status) { AbstractProducerToDataSourceAdapter.this.onNewResultImpl(newResult, status); } @Override protected void onFailureImpl(Throwable throwable) { AbstractProducerToDataSourceAdapter.this.onFailureImpl(throwable); } @Override protected void onCancellationImpl() { AbstractProducerToDataSourceAdapter.this.onCancellationImpl(); } @Override protected void onProgressUpdateImpl(float progress) { AbstractProducerToDataSourceAdapter.this.setProgress(progress); } }; }
@Test public void test_NI_a_NL_C() { mInternalConsumer.onNewResult(null, Consumer.NO_FLAGS); verify(mDataSubscriber1).onNewResult(mDataSource); verifyWithResult(null, INTERMEDIATE); testSubscribe(NO_INTERACTIONS); mInternalConsumer.onNewResult(null, Consumer.IS_LAST); verify(mRequestListener).onRequestSuccess( mSettableProducerContext.getImageRequest(), mRequestId, mSettableProducerContext.isPrefetch()); verify(mDataSubscriber1).onNewResult(mDataSource); verify(mDataSubscriber2).onNewResult(mDataSource); verifyWithResult(null, LAST); testClose(FINISHED, 2); }
@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); mResult1 = new Object(); mResult2 = new Object(); mResult3 = new Object(); mException = mock(Exception.class); mDataSubscriber1 = mock(DataSubscriber.class); mDataSubscriber2 = mock(DataSubscriber.class); mSettableProducerContext = mock(SettableProducerContext.class); when(mSettableProducerContext.getId()).thenReturn(mRequestId); when(mSettableProducerContext.isPrefetch()).thenReturn(true); mProducer = mock(Producer.class); mDataSource = ProducerToDataSourceAdapter.create( mProducer, mSettableProducerContext, mRequestListener); ArgumentCaptor<Consumer> captor = ArgumentCaptor.forClass(Consumer.class); verify(mRequestListener).onRequestStart( mSettableProducerContext.getImageRequest(), mSettableProducerContext.getCallerContext(), mRequestId, mSettableProducerContext.isPrefetch()); verify(mProducer).produceResults(captor.capture(), any(SettableProducerContext.class)); mInternalConsumer = captor.getValue(); mDataSource.subscribe(mDataSubscriber1, CallerThreadExecutor.getInstance()); }
@Test public void test_C_I_a() { testClose(NOT_FINISHED, 1); mInternalConsumer.onNewResult(mResult2, Consumer.NO_FLAGS); verifyClosed(NOT_FINISHED, null); testSubscribe(NO_INTERACTIONS); }
@Test public void test_C_L_a() { testClose(NOT_FINISHED, 1); mInternalConsumer.onNewResult(mResult2, Consumer.IS_LAST); verifyClosed(NOT_FINISHED, null); testSubscribe(NO_INTERACTIONS); }
@Test public void test_L_I_a_C() { testNewResult(mResult1, LAST, 1); mInternalConsumer.onNewResult(mResult2, Consumer.NO_FLAGS); verifyWithResult(mResult1, LAST); testSubscribe(ON_NEW_RESULT); testClose(FINISHED, 2); }
@Test public void test_L_L_a_C() { testNewResult(mResult1, LAST, 1); mInternalConsumer.onNewResult(mResult2, Consumer.IS_LAST); verifyWithResult(mResult1, LAST); testSubscribe(ON_NEW_RESULT); testClose(FINISHED, 2); }
@Test public void test_F_I_a_C() { testFailure(null, 1); mInternalConsumer.onNewResult(mResult1, Consumer.NO_FLAGS); verifyFailed(null, mException); testSubscribe(ON_FAILURE); testClose(mException); }
@Test public void test_F_L_a_C() { testFailure(null, 1); mInternalConsumer.onNewResult(mResult1, Consumer.IS_LAST); verifyFailed(null, mException); testSubscribe(ON_FAILURE); testClose(mException); }
@Test public void test_NI_S_a_C() { mInternalConsumer.onNewResult(null, Consumer.NO_FLAGS); verify(mDataSubscriber1).onNewResult(mDataSource); verifyWithResult(null, INTERMEDIATE); testNewResult(mResult1, LAST, 1); testSubscribe(ON_NEW_RESULT); testClose(FINISHED, 2); }
@Test public void test_I_NL_a_C() { testNewResult(mResult1, INTERMEDIATE, 1); mInternalConsumer.onNewResult(null, Consumer.IS_LAST); verify(mRequestListener).onRequestSuccess( mSettableProducerContext.getImageRequest(), mRequestId, mSettableProducerContext.isPrefetch()); verify(mDataSubscriber1).onNewResult(mDataSource); verifyWithResult(null, LAST); testSubscribe(ON_NEW_RESULT); testClose(FINISHED, 2); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); mResourceReleaser = mock(ResourceReleaser.class); mResultRef1 = CloseableReference.of(new Object(), mResourceReleaser); mResultRef2 = CloseableReference.of(new Object(), mResourceReleaser); mResultRef3 = CloseableReference.of(new Object(), mResourceReleaser); mException = mock(Exception.class); mDataSubscriber1 = mock(DataSubscriber.class); mDataSubscriber2 = mock(DataSubscriber.class); mSettableProducerContext = mock(SettableProducerContext.class); when(mSettableProducerContext.getId()).thenReturn(mRequestId); when(mSettableProducerContext.isPrefetch()).thenReturn(false); mProducer = mock(Producer.class); mDataSource = CloseableProducerToDataSourceAdapter.create( mProducer, mSettableProducerContext, mRequestListener); ArgumentCaptor<Consumer> captor = ArgumentCaptor.forClass(Consumer.class); verify(mRequestListener).onRequestStart( mSettableProducerContext.getImageRequest(), mSettableProducerContext.getCallerContext(), mRequestId, mSettableProducerContext.isPrefetch()); verify(mProducer).produceResults(captor.capture(), any(SettableProducerContext.class)); mInternalConsumer = captor.getValue(); mDataSource.subscribe(mDataSubscriber1, CallerThreadExecutor.getInstance()); }
@Test public void test_C_I_a() { testClose(NOT_FINISHED, 1); mInternalConsumer.onNewResult(mResultRef2, Consumer.NO_FLAGS); verifyClosed(NOT_FINISHED, null); testSubscribe(NO_INTERACTIONS); }
@Test public void test_C_L_a() { testClose(NOT_FINISHED, 1); mInternalConsumer.onNewResult(mResultRef2, Consumer.IS_LAST); verifyClosed(NOT_FINISHED, null); testSubscribe(NO_INTERACTIONS); }
@Test public void test_L_I_a_C() { testNewResult(mResultRef1, LAST, 1); mInternalConsumer.onNewResult(mResultRef2, Consumer.NO_FLAGS); verifyWithResult(mResultRef1, LAST); testSubscribe(ON_NEW_RESULT); testClose(FINISHED, 2); }
@Test public void test_L_L_a_C() { testNewResult(mResultRef1, LAST, 1); mInternalConsumer.onNewResult(mResultRef2, Consumer.IS_LAST); verifyWithResult(mResultRef1, LAST); testSubscribe(ON_NEW_RESULT); testClose(FINISHED, 2); }
@Test public void test_F_I_a_C() { testFailure(null, 1); mInternalConsumer.onNewResult(mResultRef1, Consumer.NO_FLAGS); verifyFailed(null, mException); testSubscribe(ON_FAILURE); testClose(mException); }
@Test public void test_F_L_a_C() { testFailure(null, 1); mInternalConsumer.onNewResult(mResultRef1, Consumer.IS_LAST); verifyFailed(null, mException); testSubscribe(ON_FAILURE); testClose(mException); }
@Test public void test_NI_S_a_C() { mInternalConsumer.onNewResult(null, Consumer.NO_FLAGS); verify(mDataSubscriber1).onNewResult(mDataSource); verifyWithResult(null, INTERMEDIATE); testNewResult(mResultRef1, LAST, 1); testSubscribe(ON_NEW_RESULT); testClose(FINISHED, 2); }
@Test public void test_I_NL_a_C() { testNewResult(mResultRef1, INTERMEDIATE, 1); mInternalConsumer.onNewResult(null, Consumer.IS_LAST); verify(mRequestListener).onRequestSuccess( mSettableProducerContext.getImageRequest(), mRequestId, mSettableProducerContext.isPrefetch()); verify(mDataSubscriber1).onNewResult(mDataSource); verifyWithResult(null, LAST); testSubscribe(ON_NEW_RESULT); testClose(FINISHED, 2); }
private void verifyPrefetchToDiskCache( DataSource<Void> dataSource, Producer<Void> prefetchProducerSequence, Priority priority) { assertFalse(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(priority, producerContextArgumentCaptor.getValue().getPriority()); }
@Test public void testFetchLocalEncodedImage() { Producer<CloseableReference<PooledByteBuffer>> encodedSequence = mock(Producer.class); when(mProducerSequenceFactory.getEncodedImageProducerSequence(mImageRequest)) .thenReturn(encodedSequence); when(mImageRequest.getSourceUri()).thenReturn(Uri.parse("file:///local/file")); DataSource<CloseableReference<PooledByteBuffer>> dataSource = mImagePipeline.fetchEncodedImage(mImageRequest, mCallerContext); assertFalse(dataSource.isFinished()); ArgumentCaptor<ImageRequest> argumentCaptor = ArgumentCaptor.forClass(ImageRequest.class); verify(mRequestListener1).onRequestStart( argumentCaptor.capture(), eq(mCallerContext), eq("0"), eq(false)); ImageRequest capturedImageRequest = argumentCaptor.getValue(); assertSame(mImageRequest.getSourceUri(), capturedImageRequest.getSourceUri()); verify(mRequestListener2).onRequestStart( argumentCaptor.capture(), eq(mCallerContext), eq("0"), eq(false)); capturedImageRequest = argumentCaptor.getValue(); assertSame(mImageRequest.getSourceUri(), capturedImageRequest.getSourceUri()); ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class); verify(encodedSequence) .produceResults(any(Consumer.class), producerContextArgumentCaptor.capture()); assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected()); assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH); }