Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android testing: how to check a dialog is displayed on screen? (using ActivityInstrumentationTestCase2)

I'm trying to finally add UI tests to my Android application, to increase coverage (all my other layers are properly tested, hence all my bugs now come from the UI...) I started to use ActivityInstrumentationTestCase2 as my base class for emulator unit-tests, and simple things are easy to check and work nice.

But now, I'm trying to check a Dialog appears as expected, and I don't know how to do that.

My test:

public void testOpensAboutDialogWhenAboutButtonClicked() {
    final MyActivity activity = getActivity();
    final Instrumentation instrumentation = getInstrumentation();

    final Button aboutButton = (Button) activity.findViewById(R.id.about);
    TouchUtils.clickView(this, aboutButton);

    // how to test for the AboutDialog?
}

Now my dialog doesn't have an id, so I can't get a pointer to it using findViewById. It has been created using the builder classes available:

final AlertDialog about = new AlertDialog.Builder(parent)
            .setTitle(parent.getString(R.string.about_title))
            .setCancelable(true)
            .setIcon(R.drawable.skull)
            ....

Any ideas, or pointers to tutorials?

EDIT: To answer Jens comment, I'm not using managed dialogs, just creating the AlertDialog and showing it with .show()

like image 504
Guillaume Avatar asked Nov 03 '11 12:11

Guillaume


People also ask

How do I know if my android has dialog?

Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().

What method of alert dialog is used to show the dialog box on the screen?

Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is use to set the icon on Alert dialog box.

Is a dialog an activity android?

We have discussed about how to create a dialog in one of our articles Dialog. In this tutorial we would be learning how to create an activity dialog in android. As shown in the below figure, an Activity Dialog is like an Activity floating over another Activity(parent activity).


2 Answers

Since you're already using ActivityInstrumentationTestCase2 you should start using Robotium - it will simplify your testing a lot.

For your case it's as easy as this (if you know the expected title or something else vaguely unique about your dialog):

public void testSomeRandomSentence() {
    Solo solo = new Solo(getInstrumentation(), getActivity());
    getInstrumentation().waitForIdleSync();
    // Now do whatever you need to do to trigger your dialog.

    // Let's assume a properly lame dialog title.
    assertTrue("Could not find the dialog!", solo.searchText("My Dialog Title"));
}
like image 138
Jens Avatar answered Sep 19 '22 12:09

Jens


after assigning id to Toast in setUp() by

toast = (Toast)activity.findViewById(..........);

create testcase() {

ViewAsserts.assertOnScreen(toasts.getRootView(), toast.getRootView());
//pass if toast is visible on screen

}

like image 37
Shailendra Singh Rajawat Avatar answered Sep 20 '22 12:09

Shailendra Singh Rajawat