Android & AndroidJUnit4. How to handle & test indeterminate lifecycle loop error

124 views Asked by At

Errors connected with infinite loops of view events are the topic of this question. Such loops don't throw StackOverflowError, because are executed very frequently, but not recursively. An example of that code error could be invalidate() call inside onDraw of custom view.

By default AndroidJUnit4 waits for app idle after starting a test activity and then performs verifications. But in case of infinite lifecycle loop idle won't be achieved and test will fail with error java.lang.RuntimeException: Could not launch intent Intent within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? Such error isn't so declarative and test have to wait for it so long.

For example, I should test onLayoutChildren method of LayoutManager. By default in case properly implementation this method is executed two or three times after start and that's all. And this test is passed verify(spy, atMost(3)).onLayoutChildren(any(RecyclerView.Recycler.class), any(RecyclerView.State.class)), where spy is a spy of LayoutManager. A behaviour could be corrupted and onLayoutChildren goes to infinite loop. In case of error under test it stucks.

What i am trying to achive. Possible variant is run test & application for some time (like 500ms or so) then break the app and perform verifications after that. Without any connection to idle

0

There are 0 answers