-1

I have a method in my class i want to test which shows an alert dialog . When i call this method from the test class it is executed but the dialog is not shown? The same happens when i call a method, that shows some toast or other popup dialog my test class extends ActivityInstrumentationTestCase2.

public void showSaveName(String name){
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_menu_save)
        .setTitle(R.string.savePopupLabel)
        .setMessage(R.string.savePopupMessage)
        .setPositiveButton(R.string.save_yes, new alter(name))
        .setNegativeButton(R.string.save_no, null)
        .show();
    }

when i call this from my test class

getActivity().showSaveName(name);

the dialog is not shown? Can anyone help me to figure out why it is happening ?or if i am doing something wrong?

alitha
  • 325
  • 1
  • 3
  • 6

2 Answers2

0

That's normal. Your test classes are not meant to make anything show on the device. You are supposed to programmatically make sure that the dialog appears.

In your test class, once you display the dialog, keep the instance of the Dialg box and then do

assertTrue(yourDialogInstance.isShown());

And if your dialog has not appeared, your test will fail.

That should do it.

JDenais
  • 2,956
  • 2
  • 21
  • 30
0

Can you try:

public void showSaveName(String name) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setIcon(android.R.drawable.ic_menu_save)
        .setTitle(R.string.savePopupLabel)
        .setMessage(R.string.savePopupMessage)
        .setPositiveButton(R.string.save_yes, new alter(name))
        .setNegativeButton(R.string.save_no, null);
    AlertDialog dialog = builder.create();
    dialog.show();
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Actiwitty
  • 1,232
  • 2
  • 12
  • 20