mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-24 10:25:51 +03:00
untracking activities during espresso runs as soon as they're idle
- Sometimes activities are instantly idle without going through the lifecycleChanged callback which means we were registering callsbacks which were duplicated and triggering for later waits
This commit is contained in:
parent
d127e0c39d
commit
19a46afe7b
1 changed files with 25 additions and 18 deletions
|
@ -160,43 +160,50 @@ fun initialSyncIdlingResource(session: Session): IdlingResource {
|
|||
}
|
||||
|
||||
fun activityIdlingResource(activityClass: Class<*>): IdlingResource {
|
||||
val lifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance()
|
||||
|
||||
val res = object : IdlingResource, ActivityLifecycleCallback {
|
||||
private var callback: IdlingResource.ResourceCallback? = null
|
||||
private var resumedActivity: Activity? = null
|
||||
private val uniqTS = System.currentTimeMillis()
|
||||
|
||||
var hasResumed = false
|
||||
private var currentActivity: Activity? = null
|
||||
|
||||
val uniqTS = System.currentTimeMillis()
|
||||
override fun getName() = "activityIdlingResource_${activityClass.name}_$uniqTS"
|
||||
|
||||
override fun isIdleNow(): Boolean {
|
||||
val currentActivity = currentActivity ?: ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED).elementAtOrNull(0)
|
||||
val activity = resumedActivity ?: ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED).firstOrNull {
|
||||
activityClass == it.javaClass
|
||||
}
|
||||
|
||||
val isIdle = hasResumed || currentActivity?.javaClass?.let { activityClass.isAssignableFrom(it) } ?: false
|
||||
println("*** [$name] isIdleNow activityIdlingResource $currentActivity isIdle:$isIdle")
|
||||
val isIdle = activity != null
|
||||
if (isIdle) {
|
||||
unregister()
|
||||
}
|
||||
return isIdle
|
||||
}
|
||||
|
||||
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
|
||||
println("*** [$name] registerIdleTransitionCallback $callback")
|
||||
this.callback = callback
|
||||
// if (hasResumed) callback?.onTransitionToIdle()
|
||||
}
|
||||
|
||||
override fun onActivityLifecycleChanged(activity: Activity?, stage: Stage?) {
|
||||
println("*** [$name] onActivityLifecycleChanged $activity $stage")
|
||||
currentActivity = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED).elementAtOrNull(0)
|
||||
val isIdle = currentActivity?.javaClass?.let { activityClass.isAssignableFrom(it) } ?: false
|
||||
println("*** [$name] onActivityLifecycleChanged $currentActivity isIdle:$isIdle")
|
||||
if (isIdle) {
|
||||
hasResumed = true
|
||||
if (activityClass == activity?.javaClass) {
|
||||
when (stage) {
|
||||
Stage.RESUMED -> {
|
||||
unregister()
|
||||
resumedActivity = activity
|
||||
println("*** [$name] onActivityLifecycleChanged callback: $callback")
|
||||
callback?.onTransitionToIdle()
|
||||
ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(res)
|
||||
}
|
||||
|
||||
private fun unregister() {
|
||||
lifecycleMonitor.removeLifecycleCallback(this)
|
||||
}
|
||||
}
|
||||
lifecycleMonitor.addLifecycleCallback(res)
|
||||
return res
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue