When a user opens an app and the initial data gets fetched from the server, a good pattern is to display a ProgressBar.
When I wanted to test the presence of this loading screen, Espresso was causing me some trouble.
This is the problematic test – looks simple:
@Test public void testShowLoadingScreen(){ onView(withId(R.id.loading)).check(matches(isDisplayed())); }
The error message is quite revealing:
java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.example.espresso/.activities.LoadingActivity (has extras) } 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 1431198543157 and now the last time the queue went idle was: 1431198543157. If these numbers are the same your activity might be hogging the event queue.
After fiddling around a while I got my tests running.
Replace all your ProgressBar views with plain views for testing. The solutions are the so called Build Types. I first heard about them when using a different Google Maps API key for release and debug. But there is also a build type called androidTest.
The solution is to copy the problematic layout files to app/src/androidTest/res/layout and replace all ProgressBar views with normal views.
Kinda dirty, but it works.
I just tried overriding the layout files in the Test layout folder. However when running the tests, the “normal” layouts are used still.
Did you do any changes to the gradle configuration or other setup?
How does the layout file look in the tree/project view in Android studio?
Have you tried app/src/test as basepath for the layout file?
Thanks for the reply.
I actually found another solution just now. Creating a “custom” Progressbar that does nothing in production code (only calling super()), but that disables all animations and sets it to GONE in test code.
I posted my solution here: https://gist.github.com/Mauin/62c24c8a53593c0a605e
I have tried to create a layout folder under androidTest but when I try to define the namespace xmlns:android=”http://schemas.android.com/apk/res/android” on the root element of my layout I get the error: URI is not registered ( Setting | Project Settings | Schemas and DTDs )
Any ideas?
Is it possible you forgot to create the res-folder?
app/src/androidTest/res/layout
Thanks for the reply, but no, I did create the res folder. My layout is exactly in the path you mentioned