private void markForReplay(BitmapHunter hunter) { Action action = hunter.getAction(); if (action != null) { markForReplay(action); } List<Action> joined = hunter.getActions(); if (joined != null) { //noinspection ForLoopReplaceableByForEach for (int i = 0, n = joined.size(); i < n; i++) { Action join = joined.get(i); markForReplay(join); } } }
static BitmapHunter mockHunter(String key, RequestHandler.Result result, Action action) { Request data = new Request.Builder(URI_1).build(); BitmapHunter hunter = mock(BitmapHunter.class); when(hunter.getKey()).thenReturn(key); when(hunter.getResult()).thenReturn(result); when(hunter.getData()).thenReturn(data); when(hunter.getAction()).thenReturn(action); Picasso picasso = mockPicasso(); when(hunter.getPicasso()).thenReturn(picasso); return hunter; }
static String getLogIdsForHunter(BitmapHunter hunter, String prefix) { StringBuilder builder = new StringBuilder(prefix); Action action = hunter.getAction(); if (action != null) { builder.append(action.request.logId()); } List<Action> actions = hunter.getActions(); if (actions != null) { for (int i = 0, count = actions.size(); i < count; i++) { if (i > 0 || action != null) builder.append(", "); builder.append(actions.get(i).request.logId()); } } return builder.toString(); }
@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(); }
void complete(BitmapHunter hunter) { Action single = hunter.getAction(); List<Action> joined = hunter.getActions(); boolean hasMultiple = joined != null && !joined.isEmpty(); boolean shouldDeliver = single != null || hasMultiple; if (!shouldDeliver) { return; } Uri uri = checkNotNull(hunter.getData().uri, "uri == null"); Exception exception = hunter.getException(); RequestHandler.Result result = hunter.getResult(); if (single != null) { deliverAction(result, single, exception); } if (joined != null) { //noinspection ForLoopReplaceableByForEach for (int i = 0, n = joined.size(); i < n; i++) { Action join = joined.get(i); deliverAction(result, join, exception); } } if (listener != null && exception != null) { listener.onImageLoadFailed(this, uri, exception); } }
@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 completeSkipsIfNoActions() { BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap, MEMORY)); picasso.complete(hunter); verify(hunter).getAction(); verify(hunter).getActions(); verifyNoMoreInteractions(hunter); }
boolean loggingEnabled = hunter.getPicasso().loggingEnabled; Action single = hunter.getAction(); List<Action> joined = hunter.getActions(); boolean hasMultiple = joined != null && !joined.isEmpty();
@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 completeDeliversToSingleAndMultiple() { Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); Action action2 = 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(Arrays.asList(action2)); picasso.complete(hunter); verifyActionComplete(action); verifyActionComplete(action2); }
@Test public void getPriorityAfterDetach() { Action action1 = mockAction(URI_KEY_1, URI_1, NORMAL); Action action2 = mockAction(URI_KEY_1, URI_1, HIGH); NetworkRequestHandler requestHandler = new NetworkRequestHandler(UNUSED_CALL_FACTORY, stats); BitmapHunter hunter = forRequest(mockPicasso(requestHandler), dispatcher, cache, stats, action1); hunter.attach(action2); assertThat(hunter.getAction()).isEqualTo(action1); assertThat(hunter.getActions()).containsExactly(action2); assertThat(hunter.getPriority()).isEqualTo(HIGH); hunter.detach(action2); assertThat(hunter.getAction()).isEqualTo(action1); assertThat(hunter.getActions()).isEmpty(); assertThat(hunter.getPriority()).isEqualTo(NORMAL); }
@Test public void getPriorityWithSingleRequest() { Action action = mockAction(URI_KEY_1, URI_1, HIGH); NetworkRequestHandler requestHandler = new NetworkRequestHandler(UNUSED_CALL_FACTORY, stats); BitmapHunter hunter = forRequest(mockPicasso(requestHandler), dispatcher, cache, stats, action); assertThat(hunter.getAction()).isEqualTo(action); assertThat(hunter.getActions()).isNull(); assertThat(hunter.getPriority()).isEqualTo(HIGH); }
@Test public void getPriorityWithNoRequests() { Action action = mockAction(URI_KEY_1, URI_1); NetworkRequestHandler requestHandler = new NetworkRequestHandler(UNUSED_CALL_FACTORY, stats); BitmapHunter hunter = forRequest(mockPicasso(requestHandler), dispatcher, cache, stats, action); hunter.detach(action); assertThat(hunter.getAction()).isNull(); assertThat(hunter.getActions()).isNull(); assertThat(hunter.getPriority()).isEqualTo(LOW); }
@Test public void getPriorityWithMultipleRequests() { Action action1 = mockAction(URI_KEY_1, URI_1, NORMAL); Action action2 = mockAction(URI_KEY_1, URI_1, HIGH); NetworkRequestHandler requestHandler = new NetworkRequestHandler(UNUSED_CALL_FACTORY, stats); BitmapHunter hunter = forRequest(mockPicasso(requestHandler), dispatcher, cache, stats, action1); hunter.attach(action2); assertThat(hunter.getAction()).isEqualTo(action1); assertThat(hunter.getActions()).containsExactly(action2); assertThat(hunter.getPriority()).isEqualTo(HIGH); }