@Test public void stopPlaceholderAnimationOnError() { Picasso picasso = mock(Picasso.class); AnimationDrawable placeholder = mock(AnimationDrawable.class); ImageView target = mockImageViewTarget(); when(target.getDrawable()).thenReturn(placeholder); ImageViewAction request = new ImageViewAction(picasso, target, null, null, 0, false, null); request.error(new RuntimeException()); verify(placeholder).stop(); } }
@Test public void intoImageViewWithNullUriAndResourceIdSkipsAndCancels() { ImageView target = mockImageViewTarget(); new RequestCreator(picasso, null, 0).into(target); verify(picasso).cancelRequest(target); verify(picasso, never()).quickMemoryCacheCheck(anyString()); verify(picasso, never()).enqueueAndSubmit(any(Action.class)); }
@Test public void invokesTargetAndCallbackErrorIfTargetIsNotNullWithErrorResourceId() { ImageView target = mockImageViewTarget(); Callback callback = mockCallback(); Picasso mock = mock(Picasso.class); ImageViewAction request = new ImageViewAction(mock, target, null, null, RESOURCE_ID_1, false, callback); Exception e = new RuntimeException(); request.error(e); verify(target).setImageResource(RESOURCE_ID_1); verify(callback).onError(e); }
@Test public void imageViewActionWithDefaultPriority() { new RequestCreator(picasso, URI_1, 0).into(mockImageViewTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().request.priority).isEqualTo(NORMAL); }
@Test public void cancelExistingRequestWithUnknownTarget() { ImageView target = mockImageViewTarget(); Action action = mockAction(URI_KEY_1, URI_1, target); picasso.cancelRequest(target); verifyZeroInteractions(action, dispatcher); }
@Test(expected = AssertionError.class) public void throwsErrorWithNullResult() { ImageView target = mockImageViewTarget(); ImageViewAction action = new ImageViewAction(mock(Picasso.class), target, null, null, 0, false, null); action.complete(null); }
@Test public void completeInvokesErrorOnAllFailedRequests() { Action action1 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); Action action2 = mockCanceledAction(); Exception exception = mock(Exception.class); BitmapHunter hunter = mockHunter(URI_KEY_1, null); when(hunter.getException()).thenReturn(exception); when(hunter.getActions()).thenReturn(Arrays.asList(action1, action2)); picasso.complete(hunter); verify(action1).error(exception); verify(action2, never()).error(exception); verify(listener).onImageLoadFailed(picasso, URI_1, exception); }
@Test public void imageViewActionWithStableKey() { new RequestCreator(picasso, URI_1, 0).stableKey(STABLE_1).into(mockImageViewTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().request.key).isEqualTo(STABLE_URI_KEY_1); }
@Test public void imageViewActionWithStableKeyNull() { new RequestCreator(picasso, URI_1, 0).stableKey(null).into(mockImageViewTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().request.key).isEqualTo(URI_KEY_1); }
@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 completeDeliversToSingle() { Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap, MEMORY)); when(hunter.getAction()).thenReturn(action); when(hunter.getActions()).thenReturn(Collections.<Action>emptyList()); picasso.complete(hunter); verifyActionComplete(action); }
@Test public void clearsCallbackOnCancel() { Picasso picasso = mock(Picasso.class); ImageView target = mockImageViewTarget(); Callback callback = mockCallback(); ImageViewAction request = new ImageViewAction(picasso, target, null, null, 0, false, callback); request.cancel(); assertThat(request.callback).isNull(); }
@Test public void deferAddsToMap() { ImageView target = mockImageViewTarget(); DeferredRequestCreator deferredRequestCreator = mockDeferredRequestCreator(); assertThat(picasso.targetToDeferredRequestCreator).isEmpty(); picasso.defer(target, deferredRequestCreator); assertThat(picasso.targetToDeferredRequestCreator).hasSize(1); }
@Test public void submitWithTargetInvokesDispatcher() { Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); assertThat(picasso.targetToAction).isEmpty(); picasso.enqueueAndSubmit(action); assertThat(picasso.targetToAction).hasSize(1); verify(dispatcher).dispatchSubmit(action); }
@Test public void clearsCallbackOnCancel() { Picasso picasso = mock(Picasso.class); ImageView target = mockImageViewTarget(); Callback callback = mockCallback(); ImageViewAction request = new ImageViewAction(picasso, target, null, null, 0, false, callback); request.cancel(); assertThat(request.callback).isNull(); }
@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 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 cancelTagAllActions() { ImageView target = mockImageViewTarget(); Action action = mockAction(URI_KEY_1, URI_1, target, "TAG"); picasso.enqueueAndSubmit(action); assertThat(picasso.targetToAction).hasSize(1); picasso.cancelTag("TAG"); assertThat(picasso.targetToAction).isEmpty(); verify(action).cancel(); }
@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 enqueueingDeferredRequestCancelsThePreviousOne() { ImageView target = mockImageViewTarget(); DeferredRequestCreator firstRequestCreator = mockDeferredRequestCreator(); picasso.defer(target, firstRequestCreator); assertThat(picasso.targetToDeferredRequestCreator).containsKey(target); DeferredRequestCreator secondRequestCreator = mockDeferredRequestCreator(); picasso.defer(target, secondRequestCreator); verify(firstRequestCreator).cancel(); assertThat(picasso.targetToDeferredRequestCreator).containsKey(target); }