android / android-test

An extensive framework for testing Android apps
https://android.github.io/android-test
Apache License 2.0
1.16k stars 314 forks source link

Espresso does not wait for androidx.loader.content.ModernAsyncTask #255

Open jameswald opened 5 years ago

jameswald commented 5 years ago

Description

After migrating to AndroidX, test execution became much less reliable. It appears that Espresso is no longer waiting for androidx.loader.content.AsyncTaskLoader to emit updated data to onLoadFinished() when tests use the content resolver to insert new data. I believe this problem may have at least two causes.

  1. androidx.loader.app.LoaderManagerImpl.LoaderViewModel.hasRunningLoaders() only seems to return true up until the first value is emitted by a loader. Subsequent content change notifications aren't considered.
  2. androidx.loader.content.ModernAsyncTask doesn't appear to be monitored by Espresso. THREAD_POOL_EXECUTOR from the support library version is still referenced here.

Steps to Reproduce

Expected Results

The data should be shown and Espresso checks should pass.

Actual Results

Espresso usually passes but in rare instances it fails. Espresso waits for loaders to emit at least one value, even an initial null, but it continues immediately thereafter. This seems to be true even when there are pending content change notifications.

AndroidX Test and Android OS Versions

androidx.test.espresso: 3.1.1 Android API: 28

Link to a public git repo demonstrating the problem:

May be able to provide if needed.

jameswald commented 5 years ago

I believe the cause of the issues I'm seeing is due to Espresso failing to wait for database binder events. When I set a breakpoint in android.database.ContentObserver.dispatchChange() the failures that used to occur once in a while suddenly become instant failures even though the ContentObserver is using a Handler with the main looper. Instead, I would expect Espresso to fail after 40+ seconds waiting for resources to become idle whenever this breakpoint is active, not fail instantly. This implies that Espresso is not monitoring these Binder threads.

"Binder:27050_2"@12,700 in group "main": RUNNING
dispatchChange:198, ContentObserver {android.database}
access$000:27, ContentObserver {android.database}
onChange:231, ContentObserver$Transport {android.database}
onTransact:63, IContentObserver$Stub {android.database}
execTransact:731, Binder {android.os}
mhussien86 commented 5 years ago

Any updates on this?