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 ?
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.
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.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With