Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Animators may only be run on Looper threads" during on-device intrumentation testing

This is my test code:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class WelcomeActivityTests extends BaseTest {

    ApplicationController applicationController;

    @Rule
    public ActivityTestRule<WelcomeActivity> activityTestRule = new ActivityTestRule<>(WelcomeActivity.class);

    ArgumentCaptor<Callback> argumentCaptor;

    @Before
    @Override public void setUp() {
        applicationController = (ApplicationController) InstrumentationRegistry.getTargetContext().getApplicationContext();
        applicationController.setMockMode(true);
        argumentCaptor = ArgumentCaptor.forClass(Callback.class);
        super.setUp();
    }

    @Test
    public void testLogin() throws InterruptedException {

        onView(withId(R.id.btnLogInW)).perform(click());
        onView(withId(R.id.email)).perform(typeText("[email protected]"));
        onView(withId(R.id.passL)).perform(typeText("strong.password"));
        onView(withId(R.id.btnLogInL)).perform(click());

        User user = new User();
        user.first_name = "Fake name";
        user.last_name = "Fake name";
        user.id = 1;
        user.email = "[email protected]";

        AuthResponse authResponse = new AuthResponse();
        authResponse.api_key = "fake_api_key";
        authResponse.status = "ok";
        authResponse.user = user;

        Mockito.verify(api).login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), argumentCaptor.capture());
        argumentCaptor.getValue().success(authResponse, null);

        assert true;
    }

    @After
    public void release() {
        applicationController.setMockMode(false);
    }

}

Buttons I'm clicking using espresso are based on material theme and they probably have some animations underneath. The outcome is that when I try to run some intrumentation tests app crashes every time i perform a click. I tried turn off animation in system developers options but it didn't help.

android.util.AndroidRuntimeException: Animators may only be run on Looper threads at android.animation.ValueAnimator.start(ValueAnimator.java:1002) at android.animation.ValueAnimator.start(ValueAnimator.java:1050) at android.animation.ObjectAnimator.start(ObjectAnimator.java:829) at android.animation.AnimatorSet.start(AnimatorSet.java:585) at android.animation.StateListAnimator.start(StateListAnimator.java:187) at android.animation.StateListAnimator.setState(StateListAnimator.java:180) at android.view.View.drawableStateChanged(View.java:15988) at android.widget.TextView.drawableStateChanged(TextView.java:3659) at android.view.View.refreshDrawableState(View.java:16032) at android.view.View.setEnabled(View.java:6724) at android.widget.TextView.setEnabled(TextView.java:1446) at my.app.ui.fragments.welcome.LoginFragment.unlock(LoginFragment.java:263) at my.app.ui.fragments.welcome.LoginFragment$4.success(LoginFragment.java:224) at my.app.ui.fragments.welcome.LoginFragment$4.success(LoginFragment.java:222) at my.app.WelcomeActivityTests.testRate(WelcomeActivityTests.java:84) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257) at org.junit.rules.RunRules.evaluate(RunRules.java:18) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at org.junit.runner.JUnitCore.run(JUnitCore.java:136) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)

This is the line, that crashes my app:

btnFacebook.setEnabled(false);

Edit: I found proper solution, look for accepted answer.

like image 888
pawelo Avatar asked May 14 '15 15:05

pawelo


1 Answers

Ok, I found a proper solution! When dealing with libraries and apis that use Handlers you need to annotate your test cases with @UiThreadTest. Also, every asynchronouos callback you are stubbing should be invoked using runOnMainSync(Runnable r) method of Instrumentation. Example:

    @Test
    @UiThreadTest
    public void testLoginSuccess() {

        Instrumentation.ActivityMonitor monitor = InstrumentationRegistry.getInstrumentation().addMonitor(EventsListActivity.class.getName(), null, true);

        onView(withId(R.id.btnLogInW)).perform(click());
        onView(withId(R.id.email)).perform(typeText("[email protected]"));
        onView(withId(R.id.passL)).perform(typeText("strong.password"));
        onView(withId(R.id.btnLogInL)).perform(click());

        User user = new User();
        user.first_name = "Fake name";
        user.last_name = "Fake name";
        user.id = 1;
        user.email = "[email protected]";

        final AuthResponse authResponse = new AuthResponse();
        authResponse.api_key = "fake_api_key";
        authResponse.status = "ok";
        authResponse.user = user;

        Mockito.verify(api).login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), argumentCaptor.capture());
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override public void run() {
                argumentCaptor.getValue().success(authResponse, null);
            }
        });

        assertThat(1, equalTo(monitor.getHits()));
        InstrumentationRegistry.getInstrumentation().removeMonitor(monitor);
    }
like image 182
pawelo Avatar answered Nov 15 '22 14:11

pawelo