Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert ImageView was loaded with specific drawable resource ID

I'm writing a Robolectric unit test and I need to make an assertion that an ImageView had setImageResource(int) called on it with a certain resource ID. I'm using fest-android for assertions but it doesn't appear to contain this assertion.

I also tried to get the ShadowImageView from Robolectric for the ImageView because I know it used to give you access to this, but it's now gone.

Lastly, I tried to call setImageDrawable in my code instead of setImageResource, then in my test assert like this:

assertThat(imageView).hasDrawable(resources.getDrawable(R.drawable.some_drawable));

but this also fails, even though the failure message clearly shows it's the same Drawable being loaded.

like image 869
Christopher Perry Avatar asked Aug 02 '13 02:08

Christopher Perry


1 Answers

For Background

ImageView imageView = (ImageView) activity.findViewById(R.id.imageview);
assertEquals(R.drawable.expected, Robolectric.shadowOf(imageView.getBackground()).getCreatedFromResId());

For Drawable

ImageView imageView = (ImageView) activity.findViewById(R.id.imageview);
assertEquals(R.drawable.expected, Robolectric.shadowOf(imageView.getDrawable()).getCreatedFromResId());
like image 61
Manuel Avatar answered Sep 20 '22 16:09

Manuel