How to test for the appearance of a Toast message

20,044

Solution 1

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?".

Solution 2

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

Solution 3

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!

Solution 4

I check, the following works:

if(someToast == null)
    someToast = Toast.makeText(this, "sdfdsf", Toast.LENGTH_LONG);
boolean isShown = someToast.getView().isShown();

Solution 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)
Share:
20,044
Adrian
Author by

Adrian

Updated on July 16, 2022

Comments

  • Adrian
    Adrian almost 2 years

    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.

  • Adrian
    Adrian about 14 years
    Thanks for that CommonsWare. I'll probably do something like populate a variable with the toast message when creating the toast. It'll be invisible to the user but will give me something to examine.
  • Adrian
    Adrian about 14 years
    Thanks for that lordfinga. Sounds like a neater solution than I described.
  • Richard Le Mesurier
    Richard Le Mesurier about 12 years
    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 stackoverflow.com/a/4277868/383414
  • Dzmitry Lazerka
    Dzmitry Lazerka over 11 years
    Voilà: myToast.getView().isShown().
  • Phlip
    Phlip over 10 years
    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.
  • Code-Apprentice
    Code-Apprentice almost 9 years
    What is wait_for_text()? There is no method in the Android API with this name.
  • Code-Apprentice
    Code-Apprentice almost 9 years
    If you are using a language other than Java you should specify.
  • Code-Apprentice
    Code-Apprentice almost 9 years
    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?
  • user2511882
    user2511882 almost 9 years
    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..
  • Aditya Saxena
    Aditya Saxena almost 9 years
    "wait_for_text" is a method in Calabash api in ruby
  • Dharmaraj
    Dharmaraj about 2 years
    roblectric has a class ShadowToast to do that, check below answers