static Bitmap makeBitmap() { return makeBitmap(10, 10); }
@Implementation public static Bitmap getThumbnail(ContentResolver cr, long origId, int kind, BitmapFactory.Options options) { return makeBitmap(20, 20); }
@Implementation public static Bitmap getThumbnail(ContentResolver cr, long origId, int kind, BitmapFactory.Options options) { return makeBitmap(); }
@Override public void load(@NonNull Picasso picasso, @NonNull Request request, @NonNull Callback callback) { Bitmap defaultResult = makeBitmap(); RequestHandler.Result result = new RequestHandler.Result(defaultResult, MEMORY); callback.onSuccess(result); } };
@Test public void invalidateFile() { Request request = new Request.Builder(Uri.fromFile(new File("/foo/bar/baz"))).build(); cache.set(request.key, makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.invalidate(new File("/foo/bar/baz")); assertThat(cache.size()).isEqualTo(0); }
@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); }
@Test public void shutdown() { cache.set("key", makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.shutdown(); assertThat(cache.size()).isEqualTo(0); verify(stats).shutdown(); verify(dispatcher).shutdown(); assertThat(picasso.shutdown).isTrue(); }
@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 shutdownTwice() { cache.set("key", makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.shutdown(); picasso.shutdown(); assertThat(cache.size()).isEqualTo(0); verify(stats).shutdown(); verify(dispatcher).shutdown(); assertThat(picasso.shutdown).isTrue(); }
@Test public void completeSetsBitmapOnRemoteViews() { Callback callback = mockCallback(); Bitmap bitmap = makeBitmap(); RemoteViewsAction action = createAction(callback); action.complete(new RequestHandler.Result(bitmap, NETWORK)); verify(remoteViews).setImageViewBitmap(1, bitmap); verify(callback).onSuccess(); }
@Test public void invalidateString() { Request request = new Request.Builder(Uri.parse("https://example.com")).build(); cache.set(request.key, makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.invalidate("https://example.com"); assertThat(cache.size()).isEqualTo(0); }
@Test public void recyclingInSuccessThrowsException() { BitmapTarget bad = new BitmapTarget() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { bitmap.recycle(); } @Override public void onBitmapFailed(Exception e, Drawable errorDrawable) { throw new AssertionError(); } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { throw new AssertionError(); } }; Picasso picasso = mock(Picasso.class); Bitmap bitmap = makeBitmap(); BitmapTargetAction tr = new BitmapTargetAction(picasso, bad, null, null, 0); try { tr.complete(new RequestHandler.Result(bitmap, MEMORY)); fail(); } catch (IllegalStateException ignored) { } } }
@Test public void invalidateUri() { Request request = new Request.Builder(URI_1).build(); cache.set(request.key, makeBitmap(1, 1)); assertThat(cache.size()).isEqualTo(1); picasso.invalidate(URI_1); assertThat(cache.size()).isEqualTo(0); }
@Test public void decodesVideoThumbnailWithVideoMimeType() { final Bitmap bitmap = makeBitmap(); Request request = new Request.Builder(MEDIA_STORE_CONTENT_1_URL, 0, ARGB_8888) .stableKey(MEDIA_STORE_CONTENT_KEY_1).resize(100, 100).build(); Action action = mockAction(request); MediaStoreRequestHandler requestHandler = create("video/"); requestHandler.load(null, action.request, new RequestHandler.Callback() { @Override public void onSuccess(Result result) { assertBitmapsEqual(result.getBitmap(), bitmap); } @Override public void onError(@NonNull Throwable t) { fail(t.getMessage()); } }); }
@Test public void decodesImageThumbnailWithImageMimeType() { final Bitmap bitmap = makeBitmap(20, 20); Request request = new Request.Builder(MEDIA_STORE_CONTENT_1_URL, 0, ARGB_8888) .stableKey(MEDIA_STORE_CONTENT_KEY_1).resize(100, 100).build(); Action action = mockAction(request); MediaStoreRequestHandler requestHandler = create("image/png"); requestHandler.load(null, action.request, new RequestHandler.Callback() { @Override public void onSuccess(Result result) { assertBitmapsEqual(result.getBitmap(), bitmap); } @Override public void onError(@NonNull Throwable t) { fail(t.getMessage()); } }); }