/** * Verify that stateful image results, both intermediate and final, are never cached. */ @Test public void testDoNotCacheStatefulImage() { when(mCloseableImage1.isStateful()).thenReturn(true); when(mCloseableImage2.isStateful()).thenReturn(true); setupBitmapMemoryCacheGetNotFound(); setupInputProducerStreamingSuccess(); when(mMemoryCache.get(mBitmapMemoryCacheKey)).thenReturn(null); mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext); verify(mConsumer).onNewResult(mIntermediateImageReference, Consumer.NO_FLAGS); verify(mConsumer).onNewResult(mFinalImageReference, Consumer.IS_LAST); verify(mMemoryCache, never()).cache( any(BitmapMemoryCacheKey.class), any(CloseableReference.class)); }
private void writeCacheEntry( PrintStream writer, CountingMemoryCacheInspector.DumpInfoEntry<CacheKey, CloseableImage> entry) { if (!(entry.key instanceof BitmapMemoryCacheKey)) { writer.println("Undefined: " + entry.key.getClass()); } BitmapMemoryCacheKey cacheKey = (BitmapMemoryCacheKey) entry.key; writer.println(formatStrLocaleSafe( "size: %7.2fkB (%4d x %4d) key: %s, %s, duration: %dms", entry.value.get().getSizeInBytes() / KB, entry.value.get().getWidth(), entry.value.get().getHeight(), entry.key, cacheKey.getCallerContext(), RealtimeSinceBootClock.get().now() - cacheKey.getInBitmapCacheSince())); }
@Override public CacheKey getPostprocessedBitmapCacheKey(ImageRequest request, Object callerContext) { final Postprocessor postprocessor = request.getPostprocessor(); final CacheKey postprocessorCacheKey; final String postprocessorName; if (postprocessor != null) { postprocessorCacheKey = postprocessor.getPostprocessorCacheKey(); postprocessorName = postprocessor.getClass().getName(); } else { postprocessorCacheKey = null; postprocessorName = null; } return new BitmapMemoryCacheKey( getCacheKeySourceUri(request.getSourceUri()), request.getResizeOptions(), request.getRotationOptions(), request.getImageDecodeOptions(), postprocessorCacheKey, postprocessorName, callerContext); }
@Override public CacheKey getPostprocessedBitmapCacheKey(ImageRequest request, Object callerContext) { if (request instanceof LJImageRequest) { LJImageRequest ljImageRequest = (LJImageRequest) request; final Postprocessor postprocessor = request.getPostprocessor(); final CacheKey postprocessorCacheKey; final String postprocessorName; if (postprocessor != null) { postprocessorCacheKey = postprocessor .getPostprocessorCacheKey(); postprocessorName = postprocessor.getClass().getName(); } else { postprocessorCacheKey = null; postprocessorName = null; } return new BitmapMemoryCacheKey(getCacheKey(ljImageRequest), ljImageRequest .getResizeOptions(), ljImageRequest .getRotationOptions(), ljImageRequest .getImageDecodeOptions(), postprocessorCacheKey, postprocessorName, callerContext); } return super.getPostprocessedBitmapCacheKey(request, callerContext); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); mBitmapMemoryCacheProducer = new BitmapMemoryCacheProducer(mMemoryCache, mCacheKeyFactory, mInputProducer); mBitmapMemoryCacheKey = mock(BitmapMemoryCacheKey.class); mCloseableImage1 = mock(CloseableImage.class); when(mCloseableImage1.getQualityInfo()).thenReturn(ImmutableQualityInfo.FULL_QUALITY); mCloseableImage2 = mock(CloseableImage.class); when(mCloseableImage2.getQualityInfo()) .thenReturn(ImmutableQualityInfo.of(INTERMEDIATE_SCAN_2, true, false)); mFinalImageReference = CloseableReference.of(mCloseableImage1); mIntermediateImageReference = CloseableReference.of(mCloseableImage2); mFinalImageReferenceClone = mFinalImageReference.clone(); mIntermediateImageReferenceClone = mIntermediateImageReference.clone(); when(mMemoryCache.cache(mBitmapMemoryCacheKey, mIntermediateImageReference)) .thenReturn(mIntermediateImageReferenceClone); when(mMemoryCache.cache(mBitmapMemoryCacheKey, mFinalImageReference)) .thenReturn(mFinalImageReferenceClone); when(mProducerContext.getImageRequest()).thenReturn(mImageRequest); when(mProducerContext.getListener()).thenReturn(mProducerListener); when(mProducerContext.getId()).thenReturn(mRequestId); when(mProducerContext.getLowestPermittedRequestLevel()) .thenReturn(ImageRequest.RequestLevel.FULL_FETCH); when(mProducerContext.getCallerContext()).thenReturn(PRODUCER_NAME); when(mProducerListener.requiresExtraMap(mRequestId)).thenReturn(true); when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest, PRODUCER_NAME)) .thenReturn(mBitmapMemoryCacheKey); }
@Test public void testDoNotCachePartialResults() { setupBitmapMemoryCacheGetNotFound(); setupInputProducerStreamingSuccessWithStatusFlags(Consumer.IS_PARTIAL_RESULT); when(mMemoryCache.get(mBitmapMemoryCacheKey)).thenReturn(null); mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext); verify(mConsumer).onNewResult(mIntermediateImageReference, Consumer.IS_PARTIAL_RESULT); verify(mConsumer) .onNewResult(mFinalImageReference, Consumer.IS_LAST | Consumer.IS_PARTIAL_RESULT); verify(mMemoryCache, never()).cache( any(BitmapMemoryCacheKey.class), any(CloseableReference.class)); }
@Test public void testEvictFromMemoryCache() { String uriString = "http://dummy/string"; Uri uri = Uri.parse(uriString); mImagePipeline.evictFromMemoryCache(uri); CacheKey dummyCacheKey = mock(CacheKey.class); ArgumentCaptor<Predicate> bitmapCachePredicateCaptor = ArgumentCaptor.forClass(Predicate.class); verify(mBitmapMemoryCache).removeAll(bitmapCachePredicateCaptor.capture()); Predicate<CacheKey> bitmapMemoryCacheKeyPredicate = bitmapCachePredicateCaptor.getValue(); BitmapMemoryCacheKey bitmapMemoryCacheKey1 = mock(BitmapMemoryCacheKey.class); BitmapMemoryCacheKey bitmapMemoryCacheKey2 = mock(BitmapMemoryCacheKey.class); when(bitmapMemoryCacheKey1.containsUri(uri)).thenReturn(true); when(bitmapMemoryCacheKey2.containsUri(uri)).thenReturn(false); assertTrue(bitmapMemoryCacheKeyPredicate.apply(bitmapMemoryCacheKey1)); assertFalse(bitmapMemoryCacheKeyPredicate.apply(bitmapMemoryCacheKey2)); assertFalse(bitmapMemoryCacheKeyPredicate.apply(dummyCacheKey)); ArgumentCaptor<Predicate> encodedMemoryCachePredicateCaptor = ArgumentCaptor.forClass(Predicate.class); verify(mEncodedMemoryCache).removeAll(encodedMemoryCachePredicateCaptor.capture()); Predicate<CacheKey> encodedMemoryCacheKeyPredicate = encodedMemoryCachePredicateCaptor.getValue(); SimpleCacheKey simpleCacheKey1 = new SimpleCacheKey(uriString); SimpleCacheKey simpleCacheKey2 = new SimpleCacheKey("rubbish"); assertTrue(encodedMemoryCacheKeyPredicate.apply(simpleCacheKey1)); assertFalse(encodedMemoryCacheKeyPredicate.apply(simpleCacheKey2)); assertFalse(encodedMemoryCacheKeyPredicate.apply(dummyCacheKey)); }
@Test public void testClearMemoryCaches() { String uriString = "http://dummy/string"; Uri uri = Uri.parse(uriString); CacheKey dummyCacheKey = mock(CacheKey.class); mImagePipeline.clearMemoryCaches(); ArgumentCaptor<Predicate> bitmapCachePredicateCaptor = ArgumentCaptor.forClass(Predicate.class); verify(mBitmapMemoryCache).removeAll(bitmapCachePredicateCaptor.capture()); Predicate<CacheKey> bitmapMemoryCacheKeyPredicate = bitmapCachePredicateCaptor.getValue(); BitmapMemoryCacheKey bitmapMemoryCacheKey1 = mock(BitmapMemoryCacheKey.class); BitmapMemoryCacheKey bitmapMemoryCacheKey2 = mock(BitmapMemoryCacheKey.class); when(bitmapMemoryCacheKey1.containsUri(uri)).thenReturn(true); when(bitmapMemoryCacheKey2.containsUri(uri)).thenReturn(false); assertTrue(bitmapMemoryCacheKeyPredicate.apply(bitmapMemoryCacheKey1)); assertTrue(bitmapMemoryCacheKeyPredicate.apply(bitmapMemoryCacheKey2)); assertTrue(bitmapMemoryCacheKeyPredicate.apply(dummyCacheKey)); ArgumentCaptor<Predicate> encodedMemoryCachePredicateCaptor = ArgumentCaptor.forClass(Predicate.class); verify(mEncodedMemoryCache).removeAll(encodedMemoryCachePredicateCaptor.capture()); Predicate<CacheKey> encodedMemoryCacheKeyPredicate = encodedMemoryCachePredicateCaptor.getValue(); SimpleCacheKey simpleCacheKey1 = new SimpleCacheKey(uriString); SimpleCacheKey simpleCacheKey2 = new SimpleCacheKey("rubbish"); assertTrue(encodedMemoryCacheKeyPredicate.apply(simpleCacheKey1)); assertTrue(encodedMemoryCacheKeyPredicate.apply(simpleCacheKey2)); assertTrue(encodedMemoryCacheKeyPredicate.apply(dummyCacheKey)); }
@Override public CacheKey getBitmapCacheKey(ImageRequest request, Object callerContext) { return new BitmapMemoryCacheKey( getCacheKeySourceUri(request.getSourceUri()), request.getResizeOptions(), request.getRotationOptions(), request.getImageDecodeOptions(), null, null, callerContext); }
@Override public CacheKey getBitmapCacheKey(ImageRequest request, Object callerContext) { if (request instanceof LJImageRequest) { LJImageRequest ljImageRequest = (LJImageRequest) request; return new BitmapMemoryCacheKey(getCacheKey(ljImageRequest), ljImageRequest .getResizeOptions(), ljImageRequest .getRotationOptions(), ljImageRequest .getImageDecodeOptions(), null, null, callerContext); } return super.getBitmapCacheKey(request, callerContext); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); mMultiplexProducer = new BitmapMemoryCacheKeyMultiplexProducer(mCacheKeyFactory, mInputProducer); mImageRequest1 = mock(ImageRequest.class); mImageRequest2 = mock(ImageRequest.class); mProducerContext1 = new SettableProducerContext( mImageRequest1, "id1", mProducerListener, mCallerContext, ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM); mProducerContext2 = new SettableProducerContext( mImageRequest1, "id2", mProducerListener, mCallerContext, ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM); mProducerContext3 = new SettableProducerContext( mImageRequest2, "id3", mProducerListener, mCallerContext, ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM); mBitmapMemoryCacheKey1 = mock(BitmapMemoryCacheKey.class); mBitmapMemoryCacheKey2 = mock(BitmapMemoryCacheKey.class); mConsumer1 = mock(Consumer.class); mConsumer2 = mock(Consumer.class); mConsumer3 = mock(Consumer.class); mFinalCloseableImage1 = mock(CloseableImage.class); mFinalCloseableImage2 = mock(CloseableImage.class); mIntermediateCloseableImage1 = mock(CloseableImage.class); mIntermediateCloseableImage2 = mock(CloseableImage.class); mFinalImageReference1 = CloseableReference.of(mFinalCloseableImage1); mFinalImageReference2 = CloseableReference.of(mFinalCloseableImage2); mIntermediateImageReference1 = CloseableReference.of(mIntermediateCloseableImage1); mIntermediateImageReference2 = CloseableReference.of(mIntermediateCloseableImage2); when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest1, mCallerContext)) .thenReturn(mBitmapMemoryCacheKey1); when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest2, mCallerContext)) .thenReturn(mBitmapMemoryCacheKey2); doAnswer( new Answer() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { if (mForwardingConsumer1 == null) { mForwardingConsumer1 = (Consumer) invocation.getArguments()[0]; mMultiplexedContext1 = (BaseProducerContext) invocation.getArguments()[1]; } else { mForwardingConsumer2 = (Consumer) invocation.getArguments()[0]; mMultiplexedContext2 = (BaseProducerContext) invocation.getArguments()[1]; } return null; } }).when(mInputProducer).produceResults(any(Consumer.class), any(ProducerContext.class)); }