@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 huntDecodesWhenNotInCache() throws Exception { 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.missCount()).isEqualTo(1); Request request = action.request; verify(hunter.requestHandler) .load(eq(picasso), eq(request), any(RequestHandler.Callback.class)); assertThat(result.getBitmap()).isEqualTo(bitmap); }
@Test public void runWithIoExceptionDispatchRetry() { Action action = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null, new IOException()); hunter.run(); verify(dispatcher).dispatchRetry(hunter); }
@Test public void cancelMultipleRequests() { Action action1 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); Action action2 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action1); hunter.future = new FutureTask<>(mock(Runnable.class), mock(Object.class)); hunter.attach(action2); assertThat(hunter.isCancelled()).isFalse(); assertThat(hunter.cancel()).isFalse(); hunter.detach(action1); hunter.detach(action2); assertThat(hunter.cancel()).isTrue(); assertThat(hunter.isCancelled()).isTrue(); }
@Test public void cancelSingleRequest() { Action action1 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action1); hunter.future = new FutureTask<>(mock(Runnable.class), mock(Object.class)); assertThat(hunter.isCancelled()).isFalse(); assertThat(hunter.cancel()).isFalse(); hunter.detach(action1); assertThat(hunter.cancel()).isTrue(); assertThat(hunter.isCancelled()).isTrue(); }
@Test public void attachMultipleRequests() { Action action1 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); Action action2 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action1); assertThat(hunter.actions).isNull(); hunter.attach(action2); assertThat(hunter.actions).isNotNull(); assertThat(hunter.actions).hasSize(1); }
@Test public void responseExceptionDispatchFailed() { Action action = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null, new NetworkRequestHandler.ResponseException(504, 0)); hunter.run(); verify(dispatcher).dispatchFailed(hunter); }
@Test public void runWithResultDispatchComplete() { Action action = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, bitmap); hunter.run(); verify(dispatcher).dispatchComplete(hunter); }
@Test public void runWithNoResultDispatchFailed() { Action action = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action); hunter.run(); verify(dispatcher).dispatchFailed(hunter); }
@Test public void detachSingleRequest() { Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action); assertThat(hunter.action).isNotNull(); hunter.detach(action); assertThat(hunter.action).isNull(); }
@Test public void nullDecodeResponseIsError() { Action action = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null); hunter.run(); verify(dispatcher).dispatchFailed(hunter); }
@Test public void detachMultipleRequests() { Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); Action action2 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action); hunter.attach(action2); hunter.detach(action2); assertThat(hunter.action).isNotNull(); assertThat(hunter.actions).isNotNull(); assertThat(hunter.actions).isEmpty(); hunter.detach(action); assertThat(hunter.action).isNull(); }
@Test public void attachSingleRequest() { Action action1 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action1); assertThat(hunter.action).isEqualTo(action1); hunter.detach(action1); hunter.attach(action1); assertThat(hunter.action).isEqualTo(action1); assertThat(hunter.actions).isNull(); }