/** * Replacing the value for a key doesn't cause an eviction but it does bring the replaced entry to * the front of the queue. */ @Test public void putCauseEviction() { PlatformLruCache cache = new PlatformLruCache(3); cache.set("a", A); cache.set("b", B); cache.set("c", C); cache.set("b", D); assertSnapshot(cache, "a", A, "c", C, "b", D); }
@Test public void cannotPutNullKey() { PlatformLruCache cache = new PlatformLruCache(3); try { cache.set(null, A); fail(); } catch (NullPointerException expected) { } }
@Test public void clearPrefixedKey() { PlatformLruCache cache = new PlatformLruCache(3); cache.set("Hello\nAlice!", A); cache.set("Hello\nBob!", B); cache.set("Hello\nEve!", C); cache.set("Hellos\nWorld!", D); cache.clearKeyUri("Hello"); assertThat(cache.cache.snapshot()).hasSize(1); assertThat(cache.cache.snapshot()).containsKey("Hellos\nWorld!"); }
@Test public void cannotPutNullValue() { PlatformLruCache cache = new PlatformLruCache(3); try { cache.set("a", null); fail(); } catch (NullPointerException expected) { } }
void performComplete(BitmapHunter hunter) { if (shouldWriteToMemoryCache(hunter.data.memoryPolicy)) { RequestHandler.Result result = hunter.getResult(); if (result != null) { Bitmap bitmap = result.getBitmap(); if (bitmap != null) { cache.set(hunter.getKey(), bitmap); } } } hunterMap.remove(hunter.getKey()); deliver(hunter); }
/** * Synchronously fulfill this request. Must not be called from the main thread. */ @Nullable // TODO make non-null and always throw? public Bitmap get() throws IOException { long started = System.nanoTime(); checkNotMain(); if (deferred) { throw new IllegalStateException("Fit cannot be used with get."); } if (!data.hasImage()) { return null; } Request request = createRequest(started); Action action = new GetAction(picasso, request); RequestHandler.Result result = forRequest(picasso, picasso.dispatcher, picasso.cache, picasso.stats, action).hunt(); Bitmap bitmap = result.getBitmap(); if (bitmap != null && shouldWriteToMemoryCache(request.memoryPolicy)) { picasso.cache.set(request.key, bitmap); } return bitmap; }
@Test public void quickMemoryCheckReturnsBitmapIfInCache() { cache.set(URI_KEY_1, bitmap); Bitmap cached = picasso.quickMemoryCacheCheck(URI_KEY_1); assertThat(cached).isEqualTo(bitmap); verify(stats).dispatchCacheHit(); }
@Test public void overMaxSizeRemovesExisting() { PlatformLruCache cache = new PlatformLruCache(20); Bitmap size4 = Bitmap.createBitmap(2, 2, ALPHA_8); Bitmap size16 = Bitmap.createBitmap(4, 4, ALPHA_8); Bitmap size25 = Bitmap.createBitmap(5, 5, ALPHA_8); cache.set("small", size4); expectedPutCount++; assertHit(cache, "small", size4); cache.set("big", size16); expectedPutCount++; assertHit(cache, "small", size4); assertHit(cache, "big", size16); cache.set("big", size25); assertHit(cache, "small", size4); assertMiss(cache, "big"); assertThat(cache.size()).isEqualTo(4); }
@Test public void overMaxSizeDoesNotClear() { PlatformLruCache cache = new PlatformLruCache(16); Bitmap size4 = Bitmap.createBitmap(2, 2, ALPHA_8); Bitmap size16 = Bitmap.createBitmap(4, 4, ALPHA_8); Bitmap size25 = Bitmap.createBitmap(5, 5, ALPHA_8); cache.set("4", size4); expectedPutCount++; assertHit(cache, "4", size4); cache.set("16", size16); expectedPutCount++; expectedEvictionCount++; // size4 was evicted. assertMiss(cache, "4"); assertHit(cache, "16", size16); cache.set("25", size25); assertHit(cache, "16", size16); assertMiss(cache, "25"); assertThat(cache.size()).isEqualTo(16); }
@Test public void invalidateFile() { Request request = new Request.Builder(Uri.fromFile(new File("/foo/bar/baz"))).build(); cache.set(request.key, makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.invalidate(new File("/foo/bar/baz")); assertThat(cache.size()).isEqualTo(0); }
@Test public void huntReturnsWhenResultInCache() throws Exception { cache.set(URI_KEY_1 + KEY_SEPARATOR, bitmap); Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); TestableBitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, bitmap); RequestHandler.Result result = hunter.hunt(); assertThat(cache.hitCount()).isEqualTo(1); Request request = action.request; verify(hunter.requestHandler, never()) .load(eq(picasso), eq(request), any(RequestHandler.Callback.class)); assertThat(result.getBitmap()).isEqualTo(bitmap); }
@Test public void shutdown() { cache.set("key", makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.shutdown(); assertThat(cache.size()).isEqualTo(0); verify(stats).shutdown(); verify(dispatcher).shutdown(); assertThat(picasso.shutdown).isTrue(); }
@Test public void shutdownTwice() { cache.set("key", makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.shutdown(); picasso.shutdown(); assertThat(cache.size()).isEqualTo(0); verify(stats).shutdown(); verify(dispatcher).shutdown(); assertThat(picasso.shutdown).isTrue(); }
@Test public void invalidateString() { Request request = new Request.Builder(Uri.parse("https://example.com")).build(); cache.set(request.key, makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.invalidate("https://example.com"); assertThat(cache.size()).isEqualTo(0); }
@Test public void evictAll() { Picasso picasso = new Picasso.Builder(RuntimeEnvironment.application).indicatorsEnabled(true).build(); picasso.cache.set("key", Bitmap.createBitmap(1, 1, ALPHA_8)); assertThat(picasso.cache.size()).isEqualTo(1); picasso.evictAll(); assertThat(picasso.cache.size()).isEqualTo(0); }
@Test public void invalidate() { PlatformLruCache cache = new PlatformLruCache(3); cache.set("Hello\nAlice!", A); assertThat(cache.size()).isEqualTo(1); cache.clearKeyUri("Hello"); assertThat(cache.size()).isEqualTo(0); }
@Test public void invalidateUri() { Request request = new Request.Builder(URI_1).build(); cache.set(request.key, makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.invalidate(URI_1); assertThat(cache.size()).isEqualTo(0); }
@Test public void resumeActionImmediatelyCompletesCachedRequest() { cache.set(URI_KEY_1, bitmap); Request request = new Request.Builder(URI_1, 0, ARGB_8888).build(); Action action = new Action(mockPicasso(), request) { @Override void complete(RequestHandler.Result result) { assertThat(result.getBitmap()).isEqualTo(bitmap); assertThat(result.getLoadedFrom()).isEqualTo(MEMORY); } @Override void error(Exception e) { fail("Reading from memory cache should not throw an exception"); } @NonNull @Override Object getTarget() { return this; } }; picasso.resumeAction(action); }