Skip to content

Instantly share code, notes, and snippets.

@iRYO400
Last active February 4, 2019 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iRYO400/d2b5517851f9f3a4373d91f1e18f5de8 to your computer and use it in GitHub Desktop.
Save iRYO400/d2b5517851f9f3a4373d91f1e18f5de8 to your computer and use it in GitHub Desktop.
#robolectric #robolectric4
// Backgrounds of views
Drawable currentDrawable = textView.getBackground();
Drawable expectedDrawable = getApplicationContext().getDrawable(R.drawable.background_item_chip_yellow);
assertThat(shadowOf(currentDrawable).getCreatedFromResId()).isEqualTo(shadowOf(expectedDrawable).getCreatedFromResId());
// Colors of TextView
int expectedColor = ContextCompat.getColor(getApplicationContext(), R.color.text_blue);
assertThat(textView.getCurrentTextColor()).isEqualTo(expectedColor);
// Colors of Drawable backgrounds
Drawable drawable = view.getBackground();
int currentColor = ((ColorDrawable) drawable).getColor();
int expectedColor = ContextCompat.getColor(getApplicationContext(), R.color.report_blue);
assertThat(currentColor).isEqualTo(expectedColor);
// ImageView#setImageResource(int id) - imageView.setImageResource(iconResId);
Drawable expected = getApplicationContext().getDrawable(R.drawable.ic_plus_blue);
assertThat(shadowOf(imageView.getDrawable()).getCreatedFromResId()).isEqualTo(shadowOf(expected).getCreatedFromResId());
// In case if you get exception when initializing view
// java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
// Check this https://stackoverflow.com/questions/32346748/robolectric-illegalstateexception-you-need-to-use-a-theme-appcompat-theme-or
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment