Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Testing: Dialog. Check it isShowing()

Here is my method, it works fine and shows the Dialog.

public void showDialog(){
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.mylayout);
    dialog.show();
}

I have a test project and I would like to test that the Dialog is showing up. I would like to apply the .isShowing() method. Something like this...

assertTrue(dialog.isShowing());

But I don't know how to get to the dialog variable within my test.

I am not using Robotium (this isn't an option for me). I'm currently using the ActivityUnitTestCase to test with. If any more information is required please don't hesitate to ask.

EDIT

I have attempted to use the answer below by making the Dialog public

public Dialog getDiag(){
    return dialog;
}

Using this answer: I have a new problem when I run showDialog() in the test, it breaks when it hits: dialog.show();

android.view.WindowManager$BadTokenException: * Unable to add window -- token null

like image 959
Jake Graham Arnold Avatar asked Nov 12 '12 18:11

Jake Graham Arnold


1 Answers

Declare Dialog outside showDialog function and then implement a method which returns this Dialog instance.

public Dialog getDiag(){
    return dialog;
}

and then do something like this

assertTrue(new YourClassName().getDialog().isShowing());
like image 137
Robin Chander Avatar answered Sep 28 '22 06:09

Robin Chander