private EngineResource<?> getEngineResourceFromCache(Key key) { Resource<?> cached = cache.remove(key); final EngineResource<?> result; if (cached == null) { result = null; } else if (cached instanceof EngineResource) { // Save an object allocation if we've cached an EngineResource (the typical case). result = (EngineResource<?>) cached; } else { result = new EngineResource<>(cached, true /*isMemoryCacheable*/, true /*isRecyclable*/); } return result; }
@Test public void testResourceIsReturnedFromCacheIfPresent() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); verify(harness.cb).onResourceReady(eq(harness.resource), eq(DataSource.MEMORY_CACHE)); }
@Test public void testCacheIsCheckedIfMemoryCacheable() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); verify(harness.cb).onResourceReady(eq(harness.resource), eq(DataSource.MEMORY_CACHE)); }
@Test public void testNewLoadIsNotStartedIfResourceIsCached() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); verify(harness.job, never()).start(any(DecodeJob.class)); }
@Test public void testCacheIsNotCheckedIfNotMemoryCacheable() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.isMemoryCacheable = false; harness.doLoad(); verify(harness.job).start((DecodeJob) any()); }
@Test public void testResourceIsAcquiredIfReturnedFromCache() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); verify(harness.resource).acquire(); }
@Test public void load_withResourceInActiveResources_doesNotCheckMemoryCache() { harness.activeResources.activate(harness.cacheKey, harness.resource); harness.doLoad(); verify(harness.cb).onResourceReady(eq(harness.resource), eq(DataSource.MEMORY_CACHE)); verify(harness.cache, never()).remove(any(Key.class)); }
@Test public void testNullLoadStatusIsReturnedForCachedResource() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); Engine.LoadStatus loadStatus = harness.doLoad(); assertNull(loadStatus); }
@Test public void testResourceIsAddedToActiveResourceIfReturnedFromCache() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); EngineResource<?> activeResource = harness.activeResources.get(harness.cacheKey); assertThat(activeResource).isEqualTo(harness.resource); }
@Test public void testHandlesNonEngineResourcesFromCacheIfPresent() { final Object expected = new Object(); @SuppressWarnings("rawtypes") Resource fromCache = mockResource(); when(fromCache.get()).thenReturn(expected); when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(fromCache); doAnswer( new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) { Resource<?> resource = (Resource<?>) invocationOnMock.getArguments()[0]; assertEquals(expected, resource.get()); return null; } }) .when(harness.cb) .onResourceReady(anyResource(), isADataSource()); harness.doLoad(); verify(harness.cb).onResourceReady(anyResource(), isADataSource()); }
@SuppressWarnings("unchecked") private EngineResource<?> getEngineResourceFromCache(Key key) { Resource<?> cached = cache.remove(key); final EngineResource result; if (cached == null) { result = null; } else if (cached instanceof EngineResource) { // Save an object allocation if we've cached an EngineResource (the typical case). result = (EngineResource) cached; } else { result = new EngineResource(cached, true /*isCacheable*/); } return result; }
@SuppressWarnings("unchecked") private EngineResource<?> getEngineResourceFromCache(Key key) { Resource<?> cached = cache.remove(key); final EngineResource<?> result; if (cached == null) { result = null; } else if (cached instanceof EngineResource) { // Save an object allocation if we've cached an EngineResource (the typical case). result = (EngineResource<?>) cached; } else { result = new EngineResource<>(cached, true /*isMemoryCacheable*/); } return result; }