views:

108

answers:

2

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.

+3  A: 

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
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
+2  A: 

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
Thanks for that lordfinga. Sounds like a neater solution than I described.
Adrian