views:

339

answers:

1

I'm trying to test an Activity in android which will show a ProgressDialog and everything works fine in the App, however when I try to use ActivityUnitTestCase and the test causes the Activity to show the dialog it fails with this error:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:429)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:178)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:392)

I was looking at the problem and it seems that the onCreateDialog method of my activity crashes when we try to create it from the test, which I assume is another context, I get that, however I wonder is any of you have been successful on trying such a scenario.

This is the code of my onCreateDialog.

    public Dialog onCreateDialog(final int id)
{
    Dialog dialog;
    switch (id)
    {
        case PROGRESS_BAR:
            loadingDialog = new ProgressDialog(this);
            loadingDialog.setMessage("searching for product...");
            loadingDialog.setIndeterminate(true);
            dialog = loadingDialog;
            break;
        case INEXISTING_PRODUCT:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Product not found");
            builder.setPositiveButton("OK", null);
            dialog = builder.create();
            break;
        case UNAVAILABLE_SERVICE:
            AlertDialog.Builder unavailableBuilder = new AlertDialog.Builder(this);
            unavailableBuilder.setMessage("Service Unavailable");
            unavailableBuilder.setPositiveButton("OK", null);
            dialog = unavailableBuilder.create();
            break;
        default:
            dialog = super.onCreateDialog(id);
    }
    return dialog;
}

Any ideas?

A: 

I find a way to do it, I believe.

The problem was that I needed to extend from ActivityInstrumentationTestCase2 and also do this to avoid problems with the GUI thread.

  final Button uButton = (Button) activity.findViewById(R.id.btnSearchProduct);
    activity.runOnUiThread(new Runnable()
    {
        public void run()
        {
            uButton.performClick();
        }
    });

My only question is how to check the results, since I need to check in which Activity I landed and it's extras?

MexicanHacker