@Test public void performRetryDoesNotMarkForReplayIfNotSupported() { NetworkInfo networkInfo = mockNetworkInfo(true); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), mockAction(URI_KEY_1, URI_1)); when(hunter.supportsReplay()).thenReturn(false); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); verify(service, never()).submit(hunter); }
@Override public void load(@NonNull Picasso picasso, @NonNull Request request, @NonNull Callback callback) { Bitmap defaultResult = makeBitmap(); RequestHandler.Result result = new RequestHandler.Result(defaultResult, MEMORY); callback.onSuccess(result); } };
@NonNull @Override public RequestHandler.Result transform(@NonNull RequestHandler.Result source) { Bitmap bitmap = source.getBitmap(); if (bitmap == null) { return source; } bitmap = transformResult(data, bitmap, source.getExifRotation()); return new RequestHandler.Result(bitmap, source.getLoadedFrom(), source.getExifRotation()); }
@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 performErrorCleansUpAndDoesNotPostToMainIfCancelled() { BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY)); when(hunter.isCancelled()).thenReturn(true); dispatcher.hunterMap.put(hunter.getKey(), hunter); dispatcher.performError(hunter); assertThat(dispatcher.hunterMap).isEmpty(); // TODO verify no main thread interactions. }
@Test public void performCancelMultipleRequestsDetachesOnly() { Action action1 = mockAction(URI_KEY_1, URI_1); Action action2 = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = mockHunter(URI_KEY_1 + KEY_SEPARATOR, new RequestHandler.Result(bitmap1, MEMORY)); hunter.attach(action1); hunter.attach(action2); dispatcher.hunterMap.put(URI_KEY_1 + KEY_SEPARATOR, hunter); dispatcher.performCancel(action1); verify(hunter).detach(action1); verify(hunter).cancel(); assertThat(dispatcher.hunterMap).hasSize(1); }
@Test public void performSubmitWithShutdownAttachesRequest() { BitmapHunter hunter = mockHunter(URI_KEY_1 + KEY_SEPARATOR, new RequestHandler.Result(bitmap1, MEMORY)); dispatcher.hunterMap.put(URI_KEY_1 + KEY_SEPARATOR, hunter); when(service.isShutdown()).thenReturn(true); Action action = mockAction(URI_KEY_1, URI_1); dispatcher.performSubmit(action); assertThat(dispatcher.hunterMap).hasSize(1); verify(hunter).attach(action); verify(service, never()).submit(any(BitmapHunter.class)); }
@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 completeWithReplayDoesNotRemove() { Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); action.willReplay = true; BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap, MEMORY)); when(hunter.getAction()).thenReturn(action); picasso.enqueueAndSubmit(action); assertThat(picasso.targetToAction).hasSize(1); picasso.complete(hunter); assertThat(picasso.targetToAction).hasSize(1); verifyActionComplete(action); }
@Test public void performRetrySkipIfServiceShutdown() { when(service.isShutdown()).thenReturn(true); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY)); dispatcher.performRetry(hunter); verify(service, never()).submit(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); }
@Test public void completeSetsBitmapOnRemoteViews() { Callback callback = mockCallback(); Bitmap bitmap = makeBitmap(); RemoteViewsAction action = createAction(callback); action.complete(new RequestHandler.Result(bitmap, NETWORK)); verify(remoteViews).setImageViewBitmap(1, bitmap); verify(callback).onSuccess(); }
@Test public void performRetrySkipsIfHunterIsCancelled() { BitmapHunter hunter = mockHunter(URI_KEY_2, new RequestHandler.Result(bitmap1, MEMORY)); when(hunter.isCancelled()).thenReturn(true); dispatcher.performRetry(hunter); verifyZeroInteractions(service); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); }
@Test public void performErrorCleansUpAndPostsToMain() { BitmapHunter hunter = mockHunter(URI_KEY_1 + KEY_SEPARATOR, new RequestHandler.Result(bitmap1, MEMORY)); dispatcher.hunterMap.put(hunter.getKey(), hunter); dispatcher.performError(hunter); assertThat(dispatcher.hunterMap).isEmpty(); // TODO verify post to main thread. }
@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 performRetryRetriesIfNoNetworkScanning() { BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), mockAction(URI_KEY_1, URI_1)); when(hunter.shouldRetry(anyBoolean(), isNull(NetworkInfo.class))).thenReturn(true); Dispatcher dispatcher = createDispatcher(false); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); verify(service).submit(hunter); }
@Test public void performRetryDoesNotMarkForReplayIfNoNetworkScanning() { BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), mockAction(URI_KEY_1, URI_1)); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false); when(hunter.supportsReplay()).thenReturn(true); Dispatcher dispatcher = createDispatcher(false); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); verify(service, never()).submit(hunter); }
@Test public void completeSkipsIfNoActions() { BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap, MEMORY)); picasso.complete(hunter); verify(hunter).getAction(); verify(hunter).getActions(); verifyNoMoreInteractions(hunter); }
@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); }