Robolectric: Comparing drawables results in assertion failure

Created on 24 Oct 2018  路  2Comments  路  Source: robolectric/robolectric

Description

If one does an assertEquals() for drawables, an assertion failure, drawables don't match is returned.
This is working in 3.8, but 4.0 onwards has stopped working.

Steps to Reproduce

Write a robolectric test, and test drawables like this:

assertEquals(activity.findViewById<ImageView>(R.id.imageOne).background, ContextCompat.getDrawable(mainActivity.baseContext,
            R.drawable.ic_launcher_foreground))

Robolectric & Android Version

Robolectric version: 4.0-beta-1, 4.0-beta-2-SNAPSHOT
Android version: 28

Most helpful comment

Older versions of Robolectric modified the behavior of Drawable.equals() (which Android leaves undefined, so it returns true only for identity-equal objects). In Robolectric 4.0, we try to match real Android behavior as closely as possible, so this special case has been removed.

Equivalent code that should work:

assertThat(shadowOf(drawable).getCreatedFromResId())
    .isEqualTo(shadowOf(expectedDrawable).getCreatedFromResId());

All 2 comments

Older versions of Robolectric modified the behavior of Drawable.equals() (which Android leaves undefined, so it returns true only for identity-equal objects). In Robolectric 4.0, we try to match real Android behavior as closely as possible, so this special case has been removed.

Equivalent code that should work:

assertThat(shadowOf(drawable).getCreatedFromResId())
    .isEqualTo(shadowOf(expectedDrawable).getCreatedFromResId());

Older versions of Robolectric modified the behavior of Drawable.equals() (which Android leaves undefined, so it returns true only for identity-equal objects). In Robolectric 4.0, we try to match real Android behavior as closely as possible, so this special case has been removed.

Equivalent code that should work:

assertThat(shadowOf(drawable).getCreatedFromResId())
    .isEqualTo(shadowOf(expectedDrawable).getCreatedFromResId());

Thanks! Exactly what I needed!

Was this page helpful?
0 / 5 - 0 ratings