@Test public void invokesOnBitmapFailedIfTargetIsNotNullWithErrorDrawable() { Drawable errorDrawable = mock(Drawable.class); BitmapTarget target = mockTarget(); BitmapTargetAction request = new BitmapTargetAction(mock(Picasso.class), target, null, errorDrawable, 0); Exception e = new RuntimeException(); request.error(e); verify(target).onBitmapFailed(e, errorDrawable); }
@Test public void intoTargetWithQuickMemoryCacheCheckDoesNotSubmit() { when(picasso.quickMemoryCacheCheck(URI_KEY_1)).thenReturn(bitmap); BitmapTarget target = mockTarget(); new RequestCreator(picasso, URI_1, 0).into(target); verify(target).onBitmapLoaded(bitmap, MEMORY); verify(picasso).cancelRequest(target); verify(picasso, never()).enqueueAndSubmit(any(Action.class)); }
@Test(expected = AssertionError.class) public void throwsErrorWithNullResult() { BitmapTarget target = mockTarget(); BitmapTargetAction request = new BitmapTargetAction(mock(Picasso.class), target, null, null, 0); request.complete(null); }
@Test public void targetActionWithDefaultPriority() { new RequestCreator(picasso, URI_1, 0).into(mockTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().request.priority).isEqualTo(NORMAL); }
@Override public void run() { try { new RequestCreator(picasso, null, 0).into(mockTarget()); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ignored) { } finally { latch.countDown(); } } }).start();
@Test public void targetActionWithDefaultTag() { new RequestCreator(picasso, URI_1, 0).into(mockTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().getTag()).isEqualTo(actionCaptor.getValue()); }
@Test public void intoTargetAndNotInCacheSubmitsTargetRequest() { BitmapTarget target = mockTarget(); Drawable placeHolderDrawable = mock(Drawable.class); new RequestCreator(picasso, URI_1, 0).placeholder(placeHolderDrawable).into(target); verify(target).onPrepareLoad(placeHolderDrawable); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue()).isInstanceOf(BitmapTargetAction.class); }
@Test public void targetActionWithCustomPriority() { new RequestCreator(picasso, URI_1, 0).priority(HIGH).into(mockTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().request.priority).isEqualTo(HIGH); }
@Test public void intoTargetWithNullUriAndResourceIdSkipsAndCancels() { BitmapTarget target = mockTarget(); Drawable placeHolderDrawable = mock(Drawable.class); new RequestCreator(picasso, null, 0).placeholder(placeHolderDrawable).into(target); verify(picasso).cancelRequest(target); verify(target).onPrepareLoad(placeHolderDrawable); verifyNoMoreInteractions(picasso); }
@Test public void performRetryMarksForReplayIfSupportedScansNetworkChangesAndShouldNotRetry() { NetworkInfo networkInfo = mockNetworkInfo(true); Action action = mockAction(URI_KEY_1, URI_1, mockTarget()); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), action); when(hunter.supportsReplay()).thenReturn(true); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).hasSize(1); verify(service, never()).submit(hunter); }
@Test public void performPauseTagIsIdempotent() { Action action = mockAction(URI_KEY_1, URI_1, mockTarget(), "tag"); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), action); dispatcher.hunterMap.put(URI_KEY_1, hunter); dispatcher.pausedTags.add("tag"); dispatcher.performPauseTag("tag"); verify(hunter, never()).getAction(); }
@Test public void performRetryMarksForReplayIfSupportsReplayAndShouldNotRetry() { Action action = mockAction(URI_KEY_1, URI_1, mockTarget()); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), action); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false); when(hunter.supportsReplay()).thenReturn(true); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).hasSize(1); verify(service, never()).submit(hunter); }
@Test public void intoTargetWithSkipMemoryPolicy() { BitmapTarget target = mockTarget(); new RequestCreator(picasso, URI_1, 0).memoryPolicy(MemoryPolicy.NO_CACHE).into(target); verify(picasso, never()).quickMemoryCacheCheck(URI_KEY_1); }
@Test public void performCancelUnqueuesAndDetachesPausedRequest() { Action action = mockAction(URI_KEY_1, URI_1, mockTarget(), "tag"); BitmapHunter hunter = mockHunter(URI_KEY_1 + KEY_SEPARATOR, new RequestHandler.Result(bitmap1, MEMORY), action); dispatcher.hunterMap.put(URI_KEY_1 + KEY_SEPARATOR, hunter); dispatcher.pausedTags.add("tag"); dispatcher.pausedActions.put(action.getTarget(), action); dispatcher.performCancel(action); assertThat(dispatcher.pausedTags).containsExactly("tag"); assertThat(dispatcher.pausedActions).isEmpty(); verify(hunter).detach(action); }
@Test public void targetActionWithCustomTag() { new RequestCreator(picasso, URI_1, 0).tag("tag").into(mockTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().getTag()).isEqualTo("tag"); }
@Test public void performRetryRetriesIfShouldRetry() { Action action = mockAction(URI_KEY_1, URI_1, mockTarget()); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), action); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(true); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); verify(service).submit(hunter); }
@Test public void cancelExistingRequestWithTarget() { BitmapTarget target = mockTarget(); Action action = mockAction(URI_KEY_1, URI_1, target); picasso.enqueueAndSubmit(action); assertThat(picasso.targetToAction).hasSize(1); picasso.cancelRequest(target); assertThat(picasso.targetToAction).isEmpty(); verify(action).cancel(); verify(dispatcher).dispatchCancel(action); }
@Test public void invokesSuccessIfTargetIsNotNull() { Bitmap bitmap = makeBitmap(); BitmapTarget target = mockTarget(); BitmapTargetAction request = new BitmapTargetAction(mock(Picasso.class), target, null, null, 0); request.complete(new RequestHandler.Result(bitmap, MEMORY)); verify(target).onBitmapLoaded(bitmap, MEMORY); }
@Test public void intoTargetNoPlaceholderCallsWithNull() { BitmapTarget target = mockTarget(); new RequestCreator(picasso, URI_1, 0).noPlaceholder().into(target); verify(target).onPrepareLoad(null); }
@Test public void intoTargetWithFitThrows() { try { BitmapTarget target = mockTarget(); new RequestCreator(picasso, URI_1, 0).fit().into(target); fail("Calling into() target with fit() should throw exception"); } catch (IllegalStateException ignored) { } }