Have a fragment that creates an alert dialog to ask the user if they wish to call an ambulance. How do I test that it appears , what each button does etc. I have tried
onView(withId(titleId))
.inRoot(isDialog().)
.check(matches(withText("Call Ambulance")))
.check(matches(isDisplayed()));
But this fails saying that "Call Ambulance" cannot be found in the root view hierarchy. The buttons dont have assigned views like R.id.button one so how do I test these also.
public class AmbulanceFragment extends DialogFragment {
final Context context = getActivity();
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Log.d(TAG, "Alert Dialog");
builder.setTitle("CALL AMBULANCE? ");
builder.setMessage("Do you wish to call an Ambulance?");
builder.setIcon(R.drawable.warning);
builder.setPositiveButton("CALL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent call = new Intent(Intent.ACTION_DIAL);
String number = "tel:" + getString(R.string.phone_number);
call.setData(Uri.parse(number));
startActivity(call);
Log.d(TAG, "Call button pressed and keypad launched");
}
});
builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}