/** * 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; }
result = hunt();
@Test public void huntUnrecognizedUri() throws Exception { Action action = mockAction(CUSTOM_URI_KEY, CUSTOM_URI); BitmapHunter hunter = forRequest(picasso, dispatcher, cache, stats, action); try { hunter.hunt(); fail("Unrecognized URI should throw exception."); } catch (IllegalStateException ignored) { } }
@Test public void huntDecodesWithRequestHandler() throws Exception { Action action = mockAction(CUSTOM_URI_KEY, CUSTOM_URI); BitmapHunter hunter = forRequest(mockPicasso(new CustomRequestHandler()), dispatcher, cache, stats, action); RequestHandler.Result result = hunter.hunt(); assertThat(result.getBitmap()).isEqualTo(bitmap); }
@Test public void withZeroRetryCountForcesLocalCacheOnly() throws Exception { responses.add(responseOf(ResponseBody.create(null, new byte[10]))); Action action = TestUtils.mockAction(URI_KEY_1, URI_1); PlatformLruCache cache = new PlatformLruCache(0); BitmapHunter hunter = new BitmapHunter(picasso, dispatcher, cache, stats, action, networkHandler); hunter.retryCount = 0; hunter.hunt(); assertThat(requests.takeFirst().cacheControl().toString()).isEqualTo(CacheControl.FORCE_CACHE.toString()); }