Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test an AlertDialog in Android?

I'm trying to test an AlertDialog with ActivityInstrumentationTestCase2.

Here is the original code :

    this.setmBtAppelerFixe(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(InterventionImmobiliereDetailsActivity.this);
            dialog.setTitle("Appel");
            dialog.setMessage("Appeler le contact ?");
            dialog.setCancelable(true);
            dialog.setNegativeButton("Non", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    InterventionImmobiliereDetailsActivity.this.lancerIntentAppel(mIntervention.getTelContact());
                }
            });

            mAdAppelerFixe = dialog.create();
            mAdAppelerFixe.show();
        }
    });

Now I can't manage to click on the Positive Button. This code doesn't seem to work :

    mActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            assertTrue(mLLAppelerFixe.performClick());

            AlertDialog mDialog = mActivity.getAdAppelerFixe();
            assertTrue(mDialog.isShowing());

            Button okButton = mDialog.getButton(AlertDialog.BUTTON_POSITIVE);

            assertTrue(okButton.performClick());
            assertTrue(mActivity.isNumeroValide());
        }
    });

First I perform a click on my layout to open the AlertDialog. Then I get the OK_BUTTON and I perform a click on it. It should set the numeroValide boolean at true. But nothing.

How can I simply test an AlertDialog with buttons ?

like image 371
Lynal Avatar asked Jul 08 '13 11:07

Lynal


People also ask

How do I use AlertDialog?

AlertDialog dialog = builder.create(); The set...Button() methods require a title for the button (supplied by a string resource) and a DialogInterface.OnClickListener that defines the action to take when the user presses the button. There are three different action buttons you can add: Positive.

What is the use of AlertDialog in Android?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

What is difference between dialog and DialogFragment in Android?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How many button options can you use in creating AlertDialog?

An alert dialog can have maximum three action buttons. If you want the user to accept the action, use Positive action button. It is normally displayed as OK/YES. If the user wants to cancel the action , then you can use Negative action button (NO).


1 Answers

This is working perfectly in my nexus 4 device:

@MediumTest
public void testStartMyActivity() {
    monitor = getInstrumentation().addMonitor(MyActivity.class.getName(), null, false);

    TouchUtils.clickView(this, startMyActivityButton);

    MyActivity myActivity = (MyActivity) monitor.waitForActivityWithTimeout(2000);
    assertNotNull("MyActivity activity not started, activity is null", myActivity);

    AlertDialog dialog = myActivity.getLastDialog(); // I create getLastDialog method in MyActivity class. Its return last created AlertDialog
    if (dialog.isShowing()) {
        try {
            performClick(dialog.getButton(DialogInterface.BUTTON_POSITIVE));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    myActivity.finish();
    getInstrumentation().removeMonitor(monitor);
}

private void performClick(final Button button) throws Throwable {
    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            button.performClick();
        }
    });
    getInstrumentation().waitForIdleSync();
}

Here example testing AlertDialog(from android google source): AlertDialogTest.java

like image 143
SBotirov Avatar answered Sep 23 '22 08:09

SBotirov