35

Would anyone know how to test for the appearance of a Toast message on an Activity?

I'm using code similar to what the OP posted on this question for testing my program flow from one activity to the next. I'd also like to be able to test for toast messages on particular activities.

Community
  • 1
  • 1
Adrian
  • 2,123
  • 3
  • 18
  • 24

9 Answers9

21

Would anyone know how to test for the appearance of a Toast message on an Activity?

You can't -- sorry. By which, I mean there is no way to ask Android "hey, is a Toast showing? and what does it look like?".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
15

We actually can now test for toast messages using robolectric. The example below is how our team is doing this for now:

    @Test
    public void ccButtonDisplaysToast() throws NullPointerException {
        Button ccRedButton = (Button) findViewById(R.id.cc_red);
        cc_red.performClick(); --> this calls the actual onClickListener implementation which has the toast.
        ShadowLooper.idleMainLooper(YOUR_TIME_HERE); --> This may help you.
        assertThat(ShadowToast.getTextOfLatestToast().toString(), equalTo("TEST_YOUR_TEXT_HERE"));
    }

Hope this helps

user2511882
  • 9,022
  • 10
  • 51
  • 59
  • It seems to me that between the time you call `performClick()` and do the assert that it is possible that the toast hasn't appeared yet and will appear after the assert finishes. How do we account for this possibility? – Code-Apprentice Jun 16 '15 at 14:54
  • Robolectric has a shadowHandler.idleMainLooper which has been deprecated. There is however a ShadowLooper.idleMainLooper() which has multiple implementations. One of them takes the "time" as a parameters to trigger the executions.. – user2511882 Jun 16 '15 at 16:09
12

Hm, actually there is a possibility to test the appearance of a toast. Simply create a subclass of Toast (e.g. MyOwnToast) and use this one in your program instead of Toast. In this subclass you can overwrite the show() method to notify you, that the Toast is being shown.

Additionally you can store the Toast within the show() method in kind of a ToastDatabase singleton from where you can access the Toast and it's view also after it has been shown and destroyed (haven't tested this with Toasts, but I often do that with the result intents of activities to keep them available for further tests after they have been destroyed - so it should be no problem to implement this with Toasts).

Beware: maybe you have to clone the Toast object or it's corresponding view for the ToastDatabase because probably it will be null after the Toast has been destroyed. Hope this helps!

ubuntudroid
  • 3,680
  • 6
  • 36
  • 60
  • Thanks for that lordfinga. Sounds like a neater solution than I described. – Adrian Mar 29 '10 at 19:53
  • 2
    Good idea - but be careful when adding code to production code just for test purposes. I would recommend a method to disable this "test code" in the final production code, possibly by checking for the debugging flag in the manifest http://stackoverflow.com/a/4277868/383414 – Richard Le Mesurier Apr 26 '12 at 13:53
6

I check, the following works:

if(someToast == null)
    someToast = Toast.makeText(this, "sdfdsf", Toast.LENGTH_LONG);
boolean isShown = someToast.getView().isShown();
Oleksii Kolotylenko
  • 1,021
  • 1
  • 8
  • 9
  • 1
    If we are indeed asking "how can an ActivityUnitTestCase assert that production code fired a Toast with such-and-so contents", then such a test should not depend on extra production code. If, for example, you refactored that code, you wouldn't want the test to break, when in production a real Toast still displays. – Phlip Sep 29 '13 at 00:56
5

You could check that the toast was shown by message

ShadowToast.showedToast("expected message")

If you are using a custom toast

ShadowToast.showedToast("expected message", R.id.yourToastId)
Kevin Crain
  • 1,905
  • 1
  • 19
  • 28
4

you can choose Robolectric test framework.For checking toast, you can use it as below:

assertTrue(ShadowToast.showedCustomToast("message", R.id.message)); //R.id.message: textView ID
panda
  • 456
  • 4
  • 12
0

For those now using the AndroidX Test API in 2019 and using a custom layout for toasts, try this (Kotlin):

@RunWith(AndroidJUnit4:class)
class ActivityUnitTest {
    private lateinit var scenario: ActivityScenario<MainActivity>

    @Before fun setUp() {
        scenario = ActivityScenario.launch(MainActivity::class.java)
    }

    @Test fun shouldDisplayToastErrorMessageIfSearchFieldIsEmpty() {
        scenario.onActivity { activity ->
            activity.id_of_button.performClick()
            assertThat(
                ShadowToast.getLatestToast().view.id_of_textview.text.toString(),
                equalTo("Text to be tested")
            )
        }
    }
}
OzzyTheGiant
  • 711
  • 8
  • 21
0

What about (Kotlin):

onView(withText("Text of the toast"))
        .inRoot(withDecorView(not(`is`(window.decorView))))
        .check(matches(isDisplayed()))
Jan Kubovy
  • 421
  • 5
  • 11
-4

I am using it like:

wait_for_text("Notification message to be verified", timeout: 30)

This is an alternate way which partially serves the purpose.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364