mardi 5 décembre 2017

Could not launch intent Intent using IdlingResources - IdlingRegistry

I want to create instrumented unit test of some Activities that contains animations and I can't due to this error.

I have implemented the idling resources to avoid the error but it doesn't work anyway.

Android Instrumented Unit Tests: http://ift.tt/1L3U2Cy

Espresso idling resource: http://ift.tt/2zOLKF1

Activity:

private CountingIdlingResource mIdlingResource;    

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    //...
    mIdlingResource = new CountingIdlingResource("qwerty");
    mIdlingResource.increment();
}

@Override
protected void onResume() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mIdlingResource.decrement();
        }
    }, 5000); // Just to be sure, without the Handler occurs the same
    super.onResume();
}

public CountingIdlingResource getIdlingResource() {
    return mIdlingResource;
}

Test:

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MyActivityTest {

    @Rule
    public ActivityTestRule<MyActivity> mActivityTestRule =
            new ActivityTestRule<>(MyActivity.class);

    @Test
    public void selectView() {

        MyActivity activity = mActivityTestRule.getActivity();
        LogUtil.debugTest("is idle? - " + activity.getIdlingResource().isIdleNow()); // logs false

        boolean registered = IdlingRegistry.getInstance().register(activity.getIdlingResource());
        LogUtil.debugTest("is registered? - " + registered); // logs true


        onView(withId(R.id.view_id)).perform(click());
    }
}

The test fails with the next error log:

java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=xxx.MyActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1512488719461 and now the last time the queue went idle was: 1512488719461. If these numbers are the same your activity might be hogging the event queue.
    at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:419)
    at android.support.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:332)
    at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:431)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
    at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)

I have the same error on different activities, how can I solve it?

Aucun commentaire:

Enregistrer un commentaire