@Test public void performSubmitWithTwoDifferentRequestsQueuesHunters() { Action action1 = mockAction(URI_KEY_1, URI_1); Action action2 = mockAction(URI_KEY_2, URI_2); dispatcher.performSubmit(action1); dispatcher.performSubmit(action2); assertThat(dispatcher.hunterMap).hasSize(2); verify(service, times(2)).submit(any(BitmapHunter.class)); }
@Test public void performSubmitWithShutdownServiceIgnoresRequest() { when(service.isShutdown()).thenReturn(true); Action action = mockAction(URI_KEY_1, URI_1); dispatcher.performSubmit(action); assertThat(dispatcher.hunterMap).isEmpty(); verify(service, never()).submit(any(BitmapHunter.class)); }
@Test public void performSubmitWithNewRequestQueuesHunter() { Action action = mockAction(URI_KEY_1, URI_1); dispatcher.performSubmit(action); assertThat(dispatcher.hunterMap).hasSize(1); verify(service).submit(any(BitmapHunter.class)); }
@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 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); }
@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 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 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 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 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 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 forAndroidXmlResourceRequest() { Resources resources = mockResources(XML_RESOURCE_VALUE); when(context.getResources()).thenReturn(resources); Action action = mockAction(RESOURCE_ID_KEY_1, null, null, RESOURCE_ID_1); ResourceDrawableRequestHandler requestHandler = ResourceDrawableRequestHandler.create(context, makeLoaderWithDrawable(null)); BitmapHunter hunter = forRequest(mockPicasso(requestHandler), dispatcher, cache, stats, action); assertThat(hunter.requestHandler).isInstanceOf(ResourceDrawableRequestHandler.class); }
@Test public void forOverrideRequest() { Action action = mockAction(ASSET_KEY_1, ASSET_URI_1); RequestHandler handler = new AssetRequestHandler(context); List<RequestHandler> handlers = Collections.singletonList(handler); // Must use non-mock constructor because that is where Picasso's list of handlers is created. Picasso picasso = new Picasso(context, dispatcher, UNUSED_CALL_FACTORY, null, cache, null, NO_TRANSFORMERS, handlers, stats, ARGB_8888, false, false); BitmapHunter hunter = forRequest(picasso, dispatcher, cache, stats, action); assertThat(hunter.requestHandler).isEqualTo(handler); }
@Test public void forAndroidBitmapResourceUriWithId() { Action action = mockAction(RESOURCE_ID_URI_KEY, RESOURCE_ID_URI); BitmapHunter hunter = forRequest(mockPicasso(new ResourceRequestHandler(context)), dispatcher, cache, stats, action); assertThat(hunter.requestHandler).isInstanceOf(ResourceRequestHandler.class); }
@Test public void forAndroidBitmapResourceUriWithType() { Action action = mockAction(RESOURCE_TYPE_URI_KEY, RESOURCE_TYPE_URI); BitmapHunter hunter = forRequest(mockPicasso(new ResourceRequestHandler(context)), dispatcher, cache, stats, action); assertThat(hunter.requestHandler).isInstanceOf(ResourceRequestHandler.class); }
@Test public void forMediaStoreRequest() { Action action = mockAction(MEDIA_STORE_CONTENT_KEY_1, MEDIA_STORE_CONTENT_1_URL); BitmapHunter hunter = forRequest(mockPicasso(new MediaStoreRequestHandler(context)), dispatcher, cache, stats, action); assertThat(hunter.requestHandler).isInstanceOf(MediaStoreRequestHandler.class); }
@Test public void forContactsPhotoRequest() { Action action = mockAction(CONTACT_KEY_1, CONTACT_URI_1); BitmapHunter hunter = forRequest(mockPicasso(new ContactsPhotoRequestHandler(context)), dispatcher, cache, stats, action); assertThat(hunter.requestHandler).isInstanceOf(ContactsPhotoRequestHandler.class); }
@Test public void forAssetRequest() { Action action = mockAction(ASSET_KEY_1, ASSET_URI_1); BitmapHunter hunter = forRequest(mockPicasso(new AssetRequestHandler(context)), dispatcher, cache, stats, action); assertThat(hunter.requestHandler).isInstanceOf(AssetRequestHandler.class); }
@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(); }
@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); }