Usage tips
I have been perplexed for a long time by the duration parameter of android Toast function,
before I noticed the warning message from android studio

The right way is
1
| Toast.makeText(activity, "Email was sent successfully.", Toast.LENGTH_LONG).show();
|
or
1
| Toast.makeText(activity, "Email was sent successfully.", Toast.LENGTH_SHORT).show();
|
Test
I will demonstrate how to test toast function with Robolectric
1, For default toast message
1
2
| Toast toast = ShadowToast.getLatestToast();
String text = Robolectric.shadowOf(toast).text;
|
Also, you can use
1
| String text = ShadowToast.getTextOfLatestToast();
|
To check shown toast count
1
| int count = ShadowToast.shownToastCount();
|
To verify a toast message has been displayed
1
2
| CharSequence message = "custom message"
boolean showed = ShadowToast.showedToast(message);
|
To get all shown toasts
1
| List<Toast> toasts = Robolectric.getShadowApplication().getShownToasts();
|
To reset all shown toasts list
1
2
| ShadowToast.reset(); // or..
Robolectric.getShadowApplication().getShownToasts().clear();
|
2, For custom toasts
1
2
3
| CharSequence message = "custom message";
int layoutResourceIdToCheckForMessage = R.id.custom_toast_text_view_id;
boolean showed = ShadowToast.showedCustomToast(message, layoutResourceIdToCheckForMessage);
|
For busy developers who don’t have time to look at source codes of robolectric. :)