@Test public void cancelNotOnMainThreadCrashes() throws InterruptedException { doCallRealMethod().when(picasso).cancelRequest(any(BitmapTarget.class)); final CountDownLatch latch = new CountDownLatch(1); new Thread(new Runnable() { @Override public void run() { try { new RequestCreator(picasso, null, 0).into(mockTarget()); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ignored) { } finally { latch.countDown(); } } }).start(); latch.await(); }
@Test public void cancelExistingRequestWithNullImageView() { try { picasso.cancelRequest((ImageView) null); fail("Canceling with a null ImageView should throw exception."); } catch (NullPointerException expected) { } }
@Test public void cancelExistingRequestWithNullTarget() { try { picasso.cancelRequest((BitmapTarget) null); fail("Canceling with a null target should throw exception."); } catch (NullPointerException expected) { } }
@Test public void cancelExistingRequestWithNullRemoteViews() { try { picasso.cancelRequest(null, 0); fail("Canceling with a null RemoteViews should throw exception."); } catch (NullPointerException expected) { } }
@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 cancelExistingRequestWithDeferredImageViewTarget() { ImageView target = mockImageViewTarget(); DeferredRequestCreator deferredRequestCreator = mockDeferredRequestCreator(); picasso.targetToDeferredRequestCreator.put(target, deferredRequestCreator); picasso.cancelRequest(target); verify(deferredRequestCreator).cancel(); assertThat(picasso.targetToDeferredRequestCreator).isEmpty(); }
@Test public void cancelExistingRequestWithUnknownTarget() { ImageView target = mockImageViewTarget(); Action action = mockAction(URI_KEY_1, URI_1, target); picasso.cancelRequest(target); verifyZeroInteractions(action, dispatcher); }
@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 intoImageViewWithQuickMemoryCacheCheckDoesNotSubmit() { PlatformLruCache cache = new PlatformLruCache(0); Picasso picasso = spy(new Picasso(RuntimeEnvironment.application, mock(Dispatcher.class), UNUSED_CALL_FACTORY, null, cache, null, NO_TRANSFORMERS, NO_HANDLERS, mock(Stats.class), ARGB_8888, false, false)); doReturn(bitmap).when(picasso).quickMemoryCacheCheck(URI_KEY_1); ImageView target = mockImageViewTarget(); Callback callback = mockCallback(); new RequestCreator(picasso, URI_1, 0).into(target, callback); verify(target).setImageDrawable(any(PicassoDrawable.class)); verify(callback).onSuccess(); verify(picasso).cancelRequest(target); verify(picasso, never()).enqueueAndSubmit(any(Action.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); }
picasso.cancelRequest(target); target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null); return; Bitmap bitmap = picasso.quickMemoryCacheCheck(request.key); if (bitmap != null) { picasso.cancelRequest(target); target.onBitmapLoaded(bitmap, MEMORY); return;
@Test public void cancelExistingRequestWithImageViewTarget() { ImageView target = mockImageViewTarget(); Action action = mockAction(URI_KEY_1, URI_1, target); picasso.enqueueAndSubmit(action); assertThat(picasso.targetToAction).hasSize(1); picasso.cancelRequest(target); assertThat(picasso.targetToAction).isEmpty(); verify(action).cancel(); verify(dispatcher).dispatchCancel(action); }
@Test public void cancelExistingRequestWithTarget() { BitmapTarget target = mockTarget(); Action action = mockAction(URI_KEY_1, URI_1, target); picasso.enqueueAndSubmit(action); assertThat(picasso.targetToAction).hasSize(1); picasso.cancelRequest(target); assertThat(picasso.targetToAction).isEmpty(); verify(action).cancel(); verify(dispatcher).dispatchCancel(action); }
@Config(sdk = 16) // This test fails on 23 so restore the default level. @Test public void cancelExistingRequestWithRemoteViewTarget() { int layoutId = 0; int viewId = 1; RemoteViews remoteViews = new RemoteViews("packageName", layoutId); RemoteViewsTarget target = new RemoteViewsTarget(remoteViews, viewId); Action action = mockAction(URI_KEY_1, URI_1, target); picasso.enqueueAndSubmit(action); assertThat(picasso.targetToAction).hasSize(1); picasso.cancelRequest(remoteViews, viewId); assertThat(picasso.targetToAction).isEmpty(); verify(action).cancel(); verify(dispatcher).dispatchCancel(action); }
picasso.cancelRequest(target); if (setPlaceholder) { setPlaceholder(target, getPlaceholderDrawable()); Bitmap bitmap = picasso.quickMemoryCacheCheck(request.key); if (bitmap != null) { picasso.cancelRequest(target); RequestHandler.Result result = new RequestHandler.Result(bitmap, MEMORY); setResult(target, picasso.context, result, noFade, picasso.indicatorsEnabled);