@Test public void testLoadStatusIsReturnedForExistingJob() { harness.doLoad(); Engine.LoadStatus loadStatus = harness.doLoad(); assertNotNull(loadStatus); }
@Test @SuppressWarnings("unchecked") public void testCallbackIsAddedToExistingRunnerWithExistingLoad() { harness.doLoad(); ResourceCallback newCallback = mock(ResourceCallback.class); harness.cb = newCallback; harness.doLoad(); verify(harness.job).addCallback(eq(newCallback)); }
@Test public void testNewRunnerIsNotCreatedAndPostedWithExistingLoad() { harness.doLoad(); harness.doLoad(); verify(harness.job, times(1)).start(any(EngineRunnable.class)); }
@Test public void testNullLoadStatusIsReturnedForCachedResource() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(mock(EngineResource.class)); Engine.LoadStatus loadStatus = harness.doLoad(); assertNull(loadStatus); }
@Test public void testNullLoadStatusIsReturnedIfResourceIsActive() { harness.activeResources.put( harness.cacheKey, new WeakReference<EngineResource<?>>(harness.resource)); assertNull(harness.doLoad()); }
@Test public void testEngineJobReceivesRemoveCallbackFromLoadStatus() { Engine.LoadStatus loadStatus = harness.doLoad(); loadStatus.cancel(); verify(harness.job).removeCallback(eq(harness.cb)); }
@Test public void testNewLoadIsNotStartedIfResourceIsCached() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(mock(EngineResource.class)); harness.doLoad(); verify(harness.job, never()).start(any(EngineRunnable.class)); }
@Test public void testResourceIsAcquiredIfReturnedFromCache() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); verify(harness.resource).acquire(); }
@Test public void testCacheIsCheckedIfMemoryCacheable() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); verify(harness.cb).onResourceReady(eq(harness.resource)); }
@Test public void testEngineIsSetAsResourceListenerOnJobComplete() { harness.doLoad(); harness.engine.onEngineJobComplete(harness.cacheKey, harness.resource); verify(harness.resource).setResourceListener(eq(harness.cacheKey), eq(harness.engine)); }
@Test public void testResourceIsNotReturnedFromActiveResourcesIfRefIsCleared() { harness.activeResources.put(harness.cacheKey, new WeakReference<EngineResource<?>>(null)); harness.doLoad(); verify(harness.cb, never()).onResourceReady(isNull(Resource.class)); }
@Test public void testRunnerIsRemovedFromRunnersOnEngineNotifiedJobComplete() { harness.doLoad(); harness.engine.onEngineJobComplete(harness.cacheKey, harness.resource); assertThat(harness.jobs).doesNotContainKey(harness.cacheKey); }
@Test public void testKeyIsRemovedFromActiveResourcesIfRefIsCleared() { harness.activeResources.put(harness.cacheKey, new WeakReference<EngineResource<?>>(null)); harness.doLoad(); assertThat(harness.activeResources).doesNotContainKey(harness.cacheKey); }
@Test public void testResourceIsAddedToActiveResourceIfReturnedFromCache() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); assertEquals(harness.resource, harness.activeResources.get(harness.cacheKey).get()); }
@Test public void testJobIsNotRemovedFromJobsIfOldJobIsCancelled() { harness.doLoad(); harness.engine.onEngineJobCancelled(mock(EngineJob.class), harness.cacheKey); assertEquals(harness.job, harness.jobs.get(harness.cacheKey)); }
@Test public void testResourceIsReturnedFromCacheIfPresent() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.doLoad(); verify(harness.cb).onResourceReady(eq(harness.resource)); }
@Test public void testRunnerIsRemovedFromRunnersOnEngineNotifiedJobCancel() { harness.doLoad(); harness.engine.onEngineJobCancelled(harness.job, harness.cacheKey); assertThat(harness.jobs).doesNotContainKey(harness.cacheKey); }
@Test public void testNewLoadIsNotStartedIfResourceIsActive() { harness.activeResources.put( harness.cacheKey, new WeakReference<EngineResource<?>>(harness.resource)); harness.doLoad(); verify(harness.job, never()).start(any(EngineRunnable.class)); }
@Test public void testResourceIsReturnedFromActiveResourcesIfPresent() { harness.activeResources.put( harness.cacheKey, new WeakReference<EngineResource<?>>(harness.resource)); harness.doLoad(); verify(harness.cb).onResourceReady(eq(harness.resource)); }
@Test public void testCacheIsNotCheckedIfNotMemoryCacheable() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); harness.isMemoryCacheable = false; harness.doLoad(); verify(harness.job).start(any(EngineRunnable.class)); }
@Test public void testResourceIsAcquiredIfReturnedFromActiveResources() { harness.activeResources.put( harness.cacheKey, new WeakReference<EngineResource<?>>(harness.resource)); harness.doLoad(); verify(harness.resource).acquire(); }
@Test public void testActiveResourcesIsNotCheckedIfReturnedFromCache() { when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(harness.resource); EngineResource other = mock(EngineResource.class); harness.activeResources.put(harness.cacheKey, new WeakReference<EngineResource<?>>(other)); harness.doLoad(); verify(harness.cb).onResourceReady(eq(harness.resource)); verify(harness.cb, never()).onResourceReady(eq(other)); }
@Test public void testActiveResourcesIsNotCheckedIfNotMemoryCacheable() { harness.activeResources.put( harness.cacheKey, new WeakReference<EngineResource<?>>(harness.resource)); harness.isMemoryCacheable = false; harness.doLoad(); verify(harness.resource, never()).acquire(); verify(harness.job).start(any(EngineRunnable.class)); }
@Test public void testKeyFactoryIsGivenNecessaryArguments() { harness.doLoad(); verify(harness.keyFactory) .buildKey( eq(ID), eq(harness.signature), eq(harness.width), eq(harness.height), eq(harness.cacheDecoder), eq(harness.decoder), eq(harness.transformation), eq(harness.encoder), eq(harness.transcoder), eq(harness.sourceEncoder)); }
@Test public void testHandlesNonEngineResourcesFromCacheIfPresent() { final Object expected = new Object(); Resource fromCache = mock(Resource.class); when(fromCache.get()).thenReturn(expected); when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(fromCache); doAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { Resource resource = (Resource) invocationOnMock.getArguments()[0]; assertEquals(expected, resource.get()); return null; } }) .when(harness.cb) .onResourceReady(any(Resource.class)); harness.doLoad(); verify(harness.cb).onResourceReady(any(Resource.class)); }
@Test public void testNewRunnerIsAddedToRunnersMap() { harness.doLoad(); assertThat(harness.jobs).containsKey(harness.cacheKey); }
@Test public void testLoadStatusIsReturnedForNewLoad() { assertNotNull(harness.doLoad()); }
@Test public void testCallbackIsAddedToNewEngineJobWithNoExistingLoad() { harness.doLoad(); verify(harness.job).addCallback(eq(harness.cb)); }
@Test public void testFactoryIsGivenNecessaryArguments() { harness.doLoad(); verify(harness.engineJobFactory).build(eq(harness.cacheKey), eq(harness.isMemoryCacheable)); }
@Test public void testJobIsPutInJobWithCacheKeyWithRelevantIds() { harness.doLoad(); assertThat(harness.jobs).containsEntry(harness.cacheKey, harness.job); }