Android Testing: Dialog. Check it isShowing()
android, dialog, java, reflection, unit-testing
Solution
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());
Problem
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