Lynal
Lynal

Reputation: 97

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 ?

Upvotes: 8

Views: 18592

Answers (4)

Muhammad Hamza Shahid
Muhammad Hamza Shahid

Reputation: 956

To click AlertDialog buttons use Espresso

to click positive button

onView(withId(android.R.id.button1)).perform(click());

and to click negative button

onView(withId(android.R.id.button2)).perform(click());

for more info visit Espresso Examples Dialog Tests

Upvotes: 2

Vikas
Vikas

Reputation: 4301

In your original activity you can create a simple method to return the instance of last AlertDialog.

public AlertDialog getDialog(){
    return alertDialog;
}

In the test activity you can access the alert dialog and click the button using the following code.

ActivityMonitor monitor =  getInstrumentation().addMonitor(MyActivity.class.getName(), null, false);
MyActivity myActivity = (MyActivity) monitor.waitForActivity();
getInstrumentation().waitForIdleSync();    

// access the alert dialog using the getDialog() method created in the activity
AlertDialog dialog = myActivity.getDialog();

// access the button
Button okBtn = (Button) dialog.findViewById(R.id.button_ok);
TouchUtils.clickView(this, okBtn);
getInstrumentation().removeMonitor(monitor);

Upvotes: 4

Nick Dong
Nick Dong

Reputation: 3736

You can use Espresso for now with code

onView(withText("South China Sea"))
  .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
  .perform(click());

See EspressoSamples

Upvotes: 0

SBotirov
SBotirov

Reputation: 14148

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

Upvotes: 5

Related Questions