Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing dialog in Android's ActivityUnitTestCase

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?

like image 690
MexicanHacker Avatar asked Mar 02 '10 18:03

MexicanHacker


People also ask

How can write unit test cases for activity in Android?

unit tests can be written under test java package, and instrumental tests can be written under androidTest package. You can then access UI widgets using Espresso ViewMatchers and then you can apply actions on them using ViewActions . Check documentation for further help. Show activity on this post.

What is unit test case in Android?

Unit tests or small tests only verify a very small portion of the app, such as a method or class. End-to-end tests or big tests verify larger parts of the app at the same time, such as a whole screen or user flow. Medium tests are in between and check the integration between two or more units.

How do you test activity?

To test an activity, you use the ActivityTestRule class provided by the Android Testing Support Library. This rule provides functional testing of a single activity. The activity under test will be launched before each test annotated with @Test and before any method annotated with @Before.

How do I run test cases on Android?

To run all tests in a class or a specific method, open the test file in the Code Editor and do either of the following: Press the Run test icon in the gutter. Right-click on the test class or method and click Run . Select the test class or method and use shortcut Control+Shift+R .


1 Answers

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?

like image 151
MexicanHacker Avatar answered Nov 15 '22 20:11

MexicanHacker