I am looking for some advices in order to create an idling resource to make my espresso tests wait when the map is not idling. During tests, we have a lot of camera movements, zooms etc so a lot of tiles are downloaded and old devices are quite solicited. Since our backend can be slow sometimes, some of our espresso tests are failing.
Our project is using mapbox 5.1.3
(we can't upgrade actually) so i have based my approach on the addOnMapChangedListener
callbacks but it does not seem to work properly. I can still see that espresso is continuing the tests while the map is not yet ready...
So, here is what i have implemented so far in order to handle map/tiles loadings :
class MapboxIdlingResource(mapview: MapView) : IdlingResource {
private var mapbox: MapboxMap? = null
private var resourceCallback: IdlingResource.ResourceCallback? = null
private var isIdle = true
init {
mapview.getMapAsync { mapboxMap ->
this.mapbox = mapboxMap
mapview.addOnMapChangedListener { change ->
when (change) {
MapView.DID_FAIL_LOADING_MAP,
MapView.DID_FINISH_LOADING_MAP,
MapView.DID_FINISH_LOADING_STYLE,
MapView.DID_FINISH_RENDERING_FRAME,
MapView.DID_FINISH_RENDERING_FRAME_FULLY_RENDERED,
MapView.DID_FINISH_RENDERING_MAP,
MapView.DID_FINISH_RENDERING_MAP_FULLY_RENDERED,
MapView.SOURCE_DID_CHANGE,
MapView.REGION_DID_CHANGE,
MapView.REGION_DID_CHANGE_ANIMATED -> setIdle()
MapView.REGION_IS_CHANGING,
MapView.REGION_WILL_CHANGE,
MapView.REGION_WILL_CHANGE_ANIMATED,
MapView.WILL_START_LOADING_MAP,
MapView.WILL_START_RENDERING_FRAME,
MapView.WILL_START_RENDERING_MAP -> waitForSomething()
}
}
}
}
override fun getName(): String {
return javaClass.simpleName
}
override fun isIdleNow(): Boolean {
return isIdle
}
override fun registerIdleTransitionCallback(resourceCallback: IdlingResource.ResourceCallback) {
this.resourceCallback = resourceCallback
}
private fun waitForSomething(mapEvent: Int) {
isIdle = false
}
private fun setIdle(mapEvent: Int) {
isIdle = true
resourceCallback?.onTransitionToIdle()
}
}
Could you tell me if the approach is right ? Am I missing something ? Thank you for your help.