@Override void error(Exception e) { if (errorResId != 0) { target.onBitmapFailed(e, ContextCompat.getDrawable(picasso.context, errorResId)); } else { target.onBitmapFailed(e, errorDrawable); } }
@Override void complete(RequestHandler.Result result) { if (result == null) { throw new AssertionError( String.format("Attempted to complete action with no result!\n%s", this)); } Bitmap bitmap = result.getBitmap(); if (bitmap != null) { target.onBitmapLoaded(bitmap, result.getLoadedFrom()); if (bitmap.isRecycled()) { throw new IllegalStateException("Target callback must not recycle bitmap!"); } } }
target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null); return; if (bitmap != null) { picasso.cancelRequest(target); target.onBitmapLoaded(bitmap, MEMORY); return; target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null);
@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 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 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 public void intoTargetNoPlaceholderCallsWithNull() { BitmapTarget target = mockTarget(); new RequestCreator(picasso, URI_1, 0).noPlaceholder().into(target); verify(target).onPrepareLoad(null); }
@Test public void invokesOnBitmapFailedIfTargetIsNotNullWithErrorResourceId() { Drawable errorDrawable = mock(Drawable.class); BitmapTarget target = mockTarget(); Context context = mock(Context.class); Dispatcher dispatcher = mock(Dispatcher.class); PlatformLruCache cache = new PlatformLruCache(0); Picasso picasso = new Picasso(context, dispatcher, UNUSED_CALL_FACTORY, null, cache, null, NO_TRANSFORMERS, NO_HANDLERS, mock(Stats.class), ARGB_8888, false, false); Resources res = mock(Resources.class); BitmapTargetAction request = new BitmapTargetAction(picasso, target, null, null, RESOURCE_ID_1); when(context.getResources()).thenReturn(res); when(res.getDrawable(RESOURCE_ID_1)).thenReturn(errorDrawable); Exception e = new RuntimeException(); request.error(e); verify(target).onBitmapFailed(e, errorDrawable); }
@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); }