jeudi 25 juin 2015

View Exception in test setUp() method with @UiThreadTest annotation

I'm running into an issue where I'm trying to alter the UI in my setUp() method.

public class DrawerRootActivityTest extends android.test.ActivityInstrumentationTestCase2<DrawerRootActivity> {

    @UiThreadTest
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        rootActivity = (DrawerRootActivity) getActivity();
        cf = rootActivity.calculatorFragment;
        cf.changeDigitDisplay(2); // Line 29
    }

}

Here is the testing console:

Running tests
Test running started
android.view.ViewRootImpl$CalledFromWrongThreadException: 
    Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:853)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
at android.view.View.invalidate(View.java:10935)
at android.view.View.invalidate(View.java:10890)
...
com.mydomain.myapp.DrawerRootActivityTest.setUp(DrawerRootActivityTest.java:29)

I need to make some changes to my app in the setUp() method which trigger side-effects that will update the UI. I thought adding the @UiThreadTest annotation would cause it to be run on the main ui thread.

Am I supposed to do this in a different fashion?

Aucun commentaire:

Enregistrer un commentaire