Search in sources :

Example 1 with LruCache

use of com.bumptech.glide.util.LruCache in project glide by bumptech.

the class LruResourceCacheTest method testPreventEviction.

@Test
public void testPreventEviction() {
    final MemoryCache cache = new LruResourceCache(100);
    final Resource<?> first = getResource(30);
    final Key firstKey = new MockKey();
    cache.put(firstKey, first);
    Resource<?> second = getResource(30);
    Key secondKey = new MockKey();
    cache.put(secondKey, second);
    Resource<?> third = getResource(30);
    Key thirdKey = new MockKey();
    cache.put(thirdKey, third);
    cache.setResourceRemovedListener(new ResourceRemovedListener() {

        @Override
        public void onResourceRemoved(@NonNull Resource<?> removed) {
            if (removed == first) {
                cache.put(firstKey, first);
            }
        }
    });
    // trims from 100 to 50, having 30+30+30 items, it should trim to 1 item
    cache.trimMemory(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
    // and that 1 item must be first, because it's forced to return to cache in the listener
    @SuppressWarnings("unchecked") LruCache<Key, Resource<?>> lruCache = (LruCache<Key, Resource<?>>) cache;
    assertTrue(lruCache.contains(firstKey));
    assertFalse(lruCache.contains(secondKey));
    assertFalse(lruCache.contains(thirdKey));
}
Also used : LruCache(com.bumptech.glide.util.LruCache) Util.mockResource(com.bumptech.glide.tests.Util.mockResource) Resource(com.bumptech.glide.load.engine.Resource) Util.anyResource(com.bumptech.glide.tests.Util.anyResource) ResourceRemovedListener(com.bumptech.glide.load.engine.cache.MemoryCache.ResourceRemovedListener) Key(com.bumptech.glide.load.Key) Test(org.junit.Test)

Aggregations

Key (com.bumptech.glide.load.Key)1 Resource (com.bumptech.glide.load.engine.Resource)1 ResourceRemovedListener (com.bumptech.glide.load.engine.cache.MemoryCache.ResourceRemovedListener)1 Util.anyResource (com.bumptech.glide.tests.Util.anyResource)1 Util.mockResource (com.bumptech.glide.tests.Util.mockResource)1 LruCache (com.bumptech.glide.util.LruCache)1 Test (org.junit.Test)1