@Test
  public void testDontCacheIntermediateImageAsAlreadyHaveSameQuality() {
    setupInputProducerStreamingSuccess();
    CloseableImage closeableImage = mock(CloseableImage.class);
    when(closeableImage.getQualityInfo())
        .thenReturn(ImmutableQualityInfo.of(INTERMEDIATE_SCAN_2, true, false));
    CloseableReference<CloseableImage> closeableImageRef = CloseableReference.of(closeableImage);
    setupBitmapMemoryCacheGetSuccessOnSecondRead(closeableImageRef);
    mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
    verify(mMemoryCache, never()).cache(mBitmapMemoryCacheKey, mIntermediateImageReference);
    verify(mMemoryCache).cache(mBitmapMemoryCacheKey, mFinalImageReference);
    verify(mConsumer).onNewResult(closeableImageRef, Consumer.NO_FLAGS);
    verify(mConsumer).onNewResult(mFinalImageReferenceClone, Consumer.IS_LAST);
    Assert.assertTrue(!mFinalImageReferenceClone.isValid());
    Assert.assertEquals(
        0, closeableImageRef.getUnderlyingReferenceTestOnly().getRefCountTestOnly());
    verify(mProducerListener).onProducerStart(mProducerContext, PRODUCER_NAME);
    Map<String, String> extraMap =
        ImmutableMap.of(BitmapMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "false");
    verify(mProducerListener)
        .onProducerFinishWithSuccess(mProducerContext, PRODUCER_NAME, extraMap);
    verify(mProducerListener, never())
        .onUltimateProducerReached(eq(mProducerContext), anyString(), anyBoolean());
  }
Verify that stateful image results, both intermediate and final, are never cached.
imagepipeline/src/test/java/com/facebook/imagepipeline/producers/BitmapMemoryCacheProducerTest.java