static BitmapHunter forRequest(Picasso picasso, Dispatcher dispatcher, PlatformLruCache cache, Stats stats, Action action) { Request request = action.request; List<RequestHandler> requestHandlers = picasso.getRequestHandlers(); // Index-based loop to avoid allocating an iterator. //noinspection ForLoopReplaceableByForEach for (int i = 0, count = requestHandlers.size(); i < count; i++) { RequestHandler requestHandler = requestHandlers.get(i); if (requestHandler.canHandleRequest(request)) { return new BitmapHunter(picasso, dispatcher, cache, stats, action, requestHandler); } } return new BitmapHunter(picasso, dispatcher, cache, stats, action, ERRORING_HANDLER); }
@Test public void performRetryForContentLengthResetsNetworkPolicy() { NetworkInfo networkInfo = mockNetworkInfo(true); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, mockAction(URI_KEY_2, URI_2), RETRYING_REQUEST_HANDLER); hunter.exception = new ContentLengthException("304 error"); dispatcher.performRetry(hunter); assertThat(NetworkPolicy.shouldReadFromDiskCache(hunter.data.networkPolicy)).isFalse(); }
@Test public void performCompleteCleansUpAndDoesNotPostToMainIfCancelled() { Request data = new Request.Builder(URI_1).build(); Action action = noopAction(data); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, action, EMPTY_REQUEST_HANDLER); hunter.result = new RequestHandler.Result(bitmap1, MEMORY); hunter.future = new FutureTask<>(mock(Runnable.class), null); hunter.future.cancel(false); dispatcher.performComplete(hunter); assertThat(dispatcher.hunterMap).isEmpty(); // TODO verify no main thread interactions. }
@Test public void shouldRetryTwiceWithAirplaneModeOffAndNoNetworkInfo() { Action action = TestUtils.mockAction(URI_KEY_1, URI_1); PlatformLruCache cache = new PlatformLruCache(0); BitmapHunter hunter = new BitmapHunter(picasso, dispatcher, cache, stats, action, networkHandler); assertThat(hunter.shouldRetry(false, null)).isTrue(); assertThat(hunter.shouldRetry(false, null)).isTrue(); assertThat(hunter.shouldRetry(false, null)).isFalse(); }
@Test public void performCompleteCleansUpAndPostsToMain() { Request data = new Request.Builder(URI_1).build(); Action action = noopAction(data); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, action, EMPTY_REQUEST_HANDLER); hunter.result = new RequestHandler.Result(bitmap1, MEMORY); dispatcher.performComplete(hunter); assertThat(dispatcher.hunterMap).isEmpty(); // TODO verify post to main thread. }
@Test public void performCompleteWithNoStoreMemoryPolicy() { Request data = new Request.Builder(URI_1).memoryPolicy(MemoryPolicy.NO_STORE).build(); Action action = noopAction(data); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, action, EMPTY_REQUEST_HANDLER); hunter.result = new RequestHandler.Result(bitmap1, MEMORY); dispatcher.performComplete(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(cache.size()).isEqualTo(0); }
@Test public void performCompleteSetsResultInCache() { Request data = new Request.Builder(URI_1).build(); Action action = noopAction(data); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, action, EMPTY_REQUEST_HANDLER); hunter.result = new RequestHandler.Result(bitmap1, MEMORY); dispatcher.performComplete(hunter); assertThat(cache.get(hunter.getKey())).isSameAs(hunter.result.getBitmap()); }
@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()); }