@Before public void setUp() { initMocks(this); picasso = new Picasso(context, dispatcher, UNUSED_CALL_FACTORY, null, cache, listener, NO_TRANSFORMERS, NO_HANDLERS, stats, ARGB_8888, false, false); }
private Picasso createPicasso() { Dispatcher dispatcher = mock(Dispatcher.class); PlatformLruCache cache = new PlatformLruCache(0); return new Picasso(RuntimeEnvironment.application, dispatcher, UNUSED_CALL_FACTORY, null, cache, null, NO_TRANSFORMERS, NO_HANDLERS, mock(Stats.class), ARGB_8888, false, false); }
@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 intoImageViewSetsPlaceholderDrawable() { 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)); ImageView target = mockImageViewTarget(); Drawable placeHolderDrawable = mock(Drawable.class); new RequestCreator(picasso, URI_1, 0).placeholder(placeHolderDrawable).into(target); verify(target).setImageDrawable(placeHolderDrawable); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue()).isInstanceOf(ImageViewAction.class); }
@Test public void intoImageViewNoPlaceholderDrawable() { 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)); ImageView target = mockImageViewTarget(); new RequestCreator(picasso, URI_1, 0).noPlaceholder().into(target); verifyNoMoreInteractions(target); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue()).isInstanceOf(ImageViewAction.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 intoImageViewSetsPlaceholderWithResourceId() { 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)); ImageView target = mockImageViewTarget(); new RequestCreator(picasso, URI_1, 0).placeholder(android.R.drawable.picture_frame) .into(target); ArgumentCaptor<Drawable> drawableCaptor = ArgumentCaptor.forClass(Drawable.class); verify(target).setImageDrawable(drawableCaptor.capture()); assertThat(shadowOf(drawableCaptor.getValue()).getCreatedFromResId()) .isEqualTo(android.R.drawable.picture_frame); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue()).isInstanceOf(ImageViewAction.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); }
/** Create the {@link Picasso} instance. */ @NonNull public Picasso build() { Context context = this.context; okhttp3.Cache unsharedCache = null; if (callFactory == null) { File cacheDir = createDefaultCacheDir(context); long maxSize = calculateDiskCacheSize(cacheDir); unsharedCache = new okhttp3.Cache(cacheDir, maxSize); callFactory = new OkHttpClient.Builder() .cache(unsharedCache) .build(); } if (cache == null) { cache = new PlatformLruCache(Utils.calculateMemoryCacheSize(context)); } if (service == null) { service = new PicassoExecutorService(new PicassoThreadFactory()); } Stats stats = new Stats(cache); Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, cache, stats); return new Picasso(context, dispatcher, callFactory, unsharedCache, cache, listener, requestTransformers, requestHandlers, stats, defaultBitmapConfig, indicatorsEnabled, loggingEnabled); } }
@Test public void invokesTargetAndCallbackSuccessIfTargetIsNotNull() { Bitmap bitmap = makeBitmap(); Dispatcher dispatcher = mock(Dispatcher.class); PlatformLruCache cache = new PlatformLruCache(0); Picasso picasso = new Picasso(RuntimeEnvironment.application, dispatcher, UNUSED_CALL_FACTORY, null, cache, null, NO_TRANSFORMERS, NO_HANDLERS, mock(Stats.class), Bitmap.Config.ARGB_8888, false, false); ImageView target = mockImageViewTarget(); Callback callback = mockCallback(); ImageViewAction request = new ImageViewAction(picasso, target, null, null, 0, false, callback); request.complete(new RequestHandler.Result(bitmap, MEMORY)); verify(target).setImageDrawable(any(PicassoDrawable.class)); verify(callback).onSuccess(); }
@Test public void throwWhenTransformRequestReturnsNull() { RequestTransformer brokenTransformer = new RequestTransformer() { @Override public Request transformRequest(Request request) { return null; } }; Picasso picasso = new Picasso(context, dispatcher, UNUSED_CALL_FACTORY, null, cache, listener, Collections.singletonList(brokenTransformer), NO_HANDLERS, stats, ARGB_8888, false, false); Request request = new Request.Builder(URI_1).build(); try { picasso.transformRequest(request); fail("Returning null from transformRequest() should throw"); } catch (IllegalStateException expected) { assertThat(expected).hasMessageThat() .isEqualTo("Request transformer " + brokenTransformer.getClass().getCanonicalName() + " returned null for " + request); } }
@Test public void shutdownClosesUnsharedCache() { okhttp3.Cache cache = new okhttp3.Cache(temporaryFolder.getRoot(), 100); Picasso picasso = new Picasso(context, dispatcher, UNUSED_CALL_FACTORY, cache, this.cache, listener, NO_TRANSFORMERS, NO_HANDLERS, stats, ARGB_8888, false, false); picasso.shutdown(); assertThat(cache.isClosed()).isTrue(); }