@Test public void nullTargetsInvalid() { try { new RequestCreator().into((ImageView) null); fail("Null ImageView should throw exception."); } catch (IllegalArgumentException ignored) { } try { new RequestCreator().into((BitmapTarget) null); fail("Null Target should throw exception."); } catch (IllegalArgumentException ignored) { } }
@Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.sample_list_detail_item, parent, false); holder = new ViewHolder(); holder.image = view.findViewById(R.id.photo); holder.text = view.findViewById(R.id.url); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } // Get the image URL for the current position. String url = getItem(position); holder.text.setText(url); // Trigger the download of the URL asynchronously into the image view. PicassoProvider.get() .load(url) .placeholder(R.drawable.placeholder) .error(R.drawable.error) .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) .centerInside() .tag(context) .into(holder.image); return view; }
@Test public void fetchWithFitThrows() { try { new RequestCreator(picasso, URI_1, 0).fit().fetch(); fail("Calling fetch() with fit() should throw an exception"); } catch (IllegalStateException ignored) { } }
private void loadImage() { // Index 1 is the progress bar. Show it while we're loading the image. animator.setDisplayedChild(1); PicassoProvider.get().load(image).fit().centerInside().into(imageView, new EmptyCallback() { @Override public void onSuccess() { // Index 0 is the image view. animator.setDisplayedChild(0); } }); } }
@Override public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.sample_widget); // Load image for all appWidgetIds. Picasso picasso = PicassoProvider.get(); picasso.load(Data.URLS[new Random().nextInt(Data.URLS.length)]) // .placeholder(R.drawable.placeholder) // .error(R.drawable.error) // .transform(new GrayscaleTransformation(picasso)) // .into(updateViews, R.id.image, appWidgetIds); } }
@Override public void bindView(View view, Context context, Cursor cursor) { Uri contactUri = Contacts.getLookupUri(cursor.getLong(ContactsQuery.ID), cursor.getString(ContactsQuery.LOOKUP_KEY)); ViewHolder holder = (ViewHolder) view.getTag(); holder.text1.setText(cursor.getString(ContactsQuery.DISPLAY_NAME)); holder.icon.assignContactUri(contactUri); PicassoProvider.get() .load(contactUri) .placeholder(R.drawable.contact_picture_placeholder) .tag(context) .into(holder.icon); }
@Test public void intoRemoteViewsWidgetWithPlaceholderDrawableThrows() { try { new RequestCreator(picasso, URI_1, 0).placeholder(new ColorDrawable(0)) .into(mockRemoteViews(), 0, new int[] { 1, 2, 3 }); fail("Calling into() with placeholder drawable should throw exception"); } catch (IllegalArgumentException ignored) { } }
@Test public void intoTargetWithFitThrows() { try { BitmapTarget target = mockTarget(); new RequestCreator(picasso, URI_1, 0).fit().into(target); fail("Calling into() target with fit() should throw exception"); } catch (IllegalStateException ignored) { } }
@Test public void targetActionWithCustomTag() { new RequestCreator(picasso, URI_1, 0).tag("tag").into(mockTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().getTag()).isEqualTo("tag"); }
@Override public View getView(int position, View convertView, ViewGroup parent) { SquaredImageView view = (SquaredImageView) convertView; if (view == null) { view = new SquaredImageView(context); view.setScaleType(CENTER_CROP); } // Get the image URL for the current position. String url = getItem(position); // Trigger the download of the URL asynchronously into the image view. PicassoProvider.get() // .load(url) // .placeholder(R.drawable.placeholder) // .error(R.drawable.error) // .fit() // .tag(context) // .into(view); return view; }
@Test public void imageViewActionWithCustomPriority() { new RequestCreator(picasso, URI_1, 0).priority(HIGH).into(mockImageViewTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().request.priority).isEqualTo(HIGH); }
@Test public void intoImageViewWithFitAndResizeThrows() { try { ImageView target = mockImageViewTarget(); new RequestCreator(picasso, URI_1, 0).fit().resize(10, 10).into(target); fail("Calling into() ImageView with fit() and resize() should throw exception"); } catch (IllegalStateException ignored) { } }
@Test public void invalidCenterInside() { try { new RequestCreator().resize(10, 10).centerInside().centerCrop(); fail("Calling center inside after center crop should throw exception."); } catch (IllegalStateException ignored) { } }
@Test public void intoRemoteViewsWidgetWithErrorDrawableThrows() { try { new RequestCreator(picasso, URI_1, 0).error(new ColorDrawable(0)) .into(mockRemoteViews(), 0, new int[] { 1, 2, 3 }); fail("Calling into() with error drawable should throw exception"); } catch (IllegalArgumentException ignored) { } }
@Test public void intoTargetNoResizeWithCenterInsideOrCenterCropThrows() { try { new RequestCreator(picasso, URI_1, 0).centerInside().into(mockTarget()); fail("Center inside with unknown width should throw exception."); } catch (IllegalStateException ignored) { } try { new RequestCreator(picasso, URI_1, 0).centerCrop().into(mockTarget()); fail("Center inside with unknown height should throw exception."); } catch (IllegalStateException ignored) { } }
@Test(expected = IllegalArgumentException.class) public void nullKeyTransformationInvalid() { new RequestCreator().transform(new Transformation() { @Override public RequestHandler.Result transform(RequestHandler.Result source) { return source; } @Override public String key() { return null; } }); }
@Test public void imageViewActionWithStableKey() { new RequestCreator(picasso, URI_1, 0).stableKey(STABLE_1).into(mockImageViewTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().request.key).isEqualTo(STABLE_URI_KEY_1); }
@Test public void intoTargetNoPlaceholderCallsWithNull() { BitmapTarget target = mockTarget(); new RequestCreator(picasso, URI_1, 0).noPlaceholder().into(target); verify(target).onPrepareLoad(null); }
@Test public void intoImageViewWithSkipMemoryCachePolicy() { ImageView target = mockImageViewTarget(); new RequestCreator(picasso, URI_1, 0).memoryPolicy(MemoryPolicy.NO_CACHE).into(target); verify(picasso, never()).quickMemoryCacheCheck(URI_KEY_1); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { SampleListDetailActivity activity = (SampleListDetailActivity) getActivity(); View view = LayoutInflater.from(activity) .inflate(R.layout.sample_list_detail_detail, container, false); TextView urlView = view.findViewById(R.id.url); ImageView imageView = view.findViewById(R.id.photo); Bundle arguments = getArguments(); String url = arguments.getString(KEY_URL); urlView.setText(url); PicassoProvider.get() .load(url) .fit() .tag(activity) .into(imageView); return view; } }