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()
Since you’re already using
ActivityInstrumentationTestCase2you 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):