Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] improved render timeouts in LocationComponent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Paczos committed Sep 12, 2018
1 parent 229e35e commit 079ba02
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.content.Context
import android.graphics.Color
import android.graphics.RectF
import android.location.Location
import android.os.Bundle
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.IdlingRegistry
import android.support.test.espresso.UiController
Expand Down Expand Up @@ -219,8 +218,7 @@ class LocationComponentTest : BaseActivityTest() {

component.forceLocationUpdate(location)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER)
uiController.loopMainThreadForAtLeast(200)
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)
uiController.loopMainThreadForAtLeast(500)

mapboxMap.querySourceFeatures(LOCATION_SOURCE).also {
it.forEach {
Expand Down Expand Up @@ -325,10 +323,12 @@ class LocationComponentTest : BaseActivityTest() {
assertThat(foregroundId, `is`(equalTo("custom-gps-bitmap")))

component.applyStyle(LocationComponentOptions.builder(context).build())
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)

val revertedForegroundId = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getStringProperty(PROPERTY_FOREGROUND_ICON)
assertThat(revertedForegroundId, `is`(equalTo(FOREGROUND_ICON)))
val renderCheck = {
mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getStringProperty(PROPERTY_FOREGROUND_ICON) == FOREGROUND_ICON
}
waitForRenderResult(uiController, renderCheck, true)
assertThat(renderCheck.invoke(), `is`(true))
}
}

Expand Down Expand Up @@ -356,10 +356,12 @@ class LocationComponentTest : BaseActivityTest() {
assertThat(foregroundId, `is`(equalTo("custom-gps-bitmap")))

component.renderMode = RenderMode.NORMAL
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)

val revertedForegroundId = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getStringProperty(PROPERTY_FOREGROUND_ICON)
assertThat(revertedForegroundId, `is`(equalTo(FOREGROUND_ICON)))
val renderCheck = {
mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getStringProperty(PROPERTY_FOREGROUND_ICON) == FOREGROUND_ICON
}
waitForRenderResult(uiController, renderCheck, true)
assertThat(renderCheck.invoke(), `is`(true))
}
}

Expand All @@ -381,8 +383,12 @@ class LocationComponentTest : BaseActivityTest() {
component.forceLocationUpdate(location)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER)
uiController.loopMainThreadForAtLeast(250) // engaging stale state
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)
assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(true))

val renderCheck = {
mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE)
}
waitForRenderResult(uiController, renderCheck, true)
assertThat(renderCheck.invoke(), `is`(true))

component.onStop()
component.onStart()
Expand All @@ -409,7 +415,7 @@ class LocationComponentTest : BaseActivityTest() {

component.onStop()
component.onStart()
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER)

assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(false))
assertThat(mapboxMap.isLayerVisible(ACCURACY_LAYER), `is`(true))
Expand Down Expand Up @@ -483,7 +489,7 @@ class LocationComponentTest : BaseActivityTest() {
assertEquals(point.longitude(), location.longitude, 0.1)

component.isLocationComponentEnabled = false
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER, true)
assertThat(mapboxMap.queryRenderedFeatures(location, FOREGROUND_LAYER).isEmpty(), `is`(true))
}
}
Expand Down Expand Up @@ -935,7 +941,6 @@ class LocationComponentTest : BaseActivityTest() {
component.isLocationComponentEnabled = true

component.cameraMode = CameraMode.TRACKING
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)
val zoom = mapboxMap.cameraPosition.zoom

component.onStop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import android.support.test.runner.AndroidJUnit4
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
import com.mapbox.mapboxsdk.constants.Style
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.location.LocationComponentConstants.*
import com.mapbox.mapboxsdk.location.modes.RenderMode
import com.mapbox.mapboxsdk.location.utils.*
import com.mapbox.mapboxsdk.location.utils.MapboxTestingUtils.Companion.MAPBOX_HEAVY_STYLE
import com.mapbox.mapboxsdk.location.utils.MapboxTestingUtils.Companion.MAP_CONNECTION_DELAY
import com.mapbox.mapboxsdk.location.utils.MapboxTestingUtils.Companion.MAP_RENDER_DELAY
import com.mapbox.mapboxsdk.location.utils.MapboxTestingUtils.Companion.pushSourceUpdates
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest
import com.mapbox.mapboxsdk.testapp.activity.SingleActivity
Expand Down Expand Up @@ -74,7 +74,7 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun renderModeNormal_sourceDoesGetAdded() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.renderMode = RenderMode.NORMAL
Expand All @@ -93,7 +93,7 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun renderModeNormal_trackingNormalLayersDoGetAdded() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.renderMode = RenderMode.NORMAL
Expand All @@ -113,7 +113,7 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun renderModeCompass_bearingLayersDoGetAdded() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.renderMode = RenderMode.COMPASS
Expand All @@ -133,7 +133,7 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun renderModeGps_navigationLayersDoGetAdded() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.renderMode = RenderMode.GPS
Expand All @@ -153,15 +153,21 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun dontShowPuckWhenRenderModeSetAndComponentDisabled() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.forceLocationUpdate(location)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER)
component.isLocationComponentEnabled = false
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER, shouldDisappear = true)
component.renderMode = RenderMode.GPS
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)

// waiting for layer to appear or timing out
val renderCheck = {
mapboxMap.isLayerVisible(FOREGROUND_LAYER)
}
waitForRenderResult(uiController, renderCheck, true)

assertThat(mapboxMap.isLayerVisible(FOREGROUND_LAYER), `is`(false))
assertThat(mapboxMap.isLayerVisible(BACKGROUND_LAYER), `is`(false))
assertThat(mapboxMap.isLayerVisible(SHADOW_LAYER), `is`(false))
Expand All @@ -176,15 +182,14 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun whenLocationComponentDisabled_doesSetAllLayersToVisibilityNone() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.renderMode = RenderMode.NORMAL
component.forceLocationUpdate(location)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER)
component.isLocationComponentEnabled = false
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER, shouldDisappear = true)
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)

// Check that all layers visibilities are set to none
assertThat(mapboxMap.isLayerVisible(FOREGROUND_LAYER), `is`(false))
Expand All @@ -201,7 +206,7 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun onMapChange_locationComponentLayersDoGetRedrawn() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.renderMode = RenderMode.NORMAL
Expand Down Expand Up @@ -233,16 +238,20 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun whenStyleChanged_continuesUsingStaleIcons() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.applyStyle(LocationComponentOptions.builder(context).staleStateTimeout(100).build())
component.forceLocationUpdate(location)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER)
uiController.loopMainThreadForAtLeast(150)
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)

assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(true))
val renderCheck = {
mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE)
}
waitForRenderResult(uiController, renderCheck, true)

assertThat(renderCheck.invoke(), `is`(true))

mapboxMap.setStyleUrl(Style.LIGHT)
uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY)
Expand All @@ -258,7 +267,7 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun whenStyleChanged_staleStateChanges() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.applyStyle(LocationComponentOptions.builder(context).staleStateTimeout(1).build())
Expand All @@ -278,7 +287,7 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun whenStyleChanged_layerVisibilityUpdates() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
styleChangeIdlingResource.waitForStyle((rule.activity as SingleActivity).mapView, mapboxMap, MAPBOX_HEAVY_STYLE)
var show = true
component.activateLocationComponent(context, false)
Expand All @@ -300,7 +309,7 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun accuracy_visibleWithNewLocation() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(location), 16.0))
Expand All @@ -320,17 +329,31 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun accuracy_visibleWhenCameraEased() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
component.forceLocationUpdate(location)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER)
mapboxMap.easeCamera(CameraUpdateFactory.newLatLngZoom(LatLng(location), 16.0), 300)
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY + 300)

assertEquals(Utils.calculateZoomLevelRadius(mapboxMap, location) /*meters projected to radius on zoom 16*/,
mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0]
.getNumberProperty(PROPERTY_ACCURACY_RADIUS).toFloat(), 0.1f)
val target = LatLng(location)
val zoom = 16.0
mapboxMap.easeCamera(CameraUpdateFactory.newLatLngZoom(target, zoom), 300)
uiController.loopMainThreadForAtLeast(300)

val cameraCheck = {
Math.abs(zoom - mapboxMap.cameraPosition.zoom) < 0.1
&& Math.abs(target.latitude - mapboxMap.cameraPosition.target.latitude) < 0.1
&& Math.abs(target.longitude - mapboxMap.cameraPosition.target.longitude) < 0.1
}
waitForRenderResult(uiController, cameraCheck, true)

val expectedRadius = Utils.calculateZoomLevelRadius(mapboxMap, location) /*meters projected to radius on zoom 16*/
val renderCheck = {
Math.abs(expectedRadius - mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getNumberProperty(PROPERTY_ACCURACY_RADIUS).toFloat()) < 0.1
}
waitForRenderResult(uiController, renderCheck, true)

assertThat(renderCheck.invoke(), `is`(true))
}
}
executeComponentTest(componentAction)
Expand All @@ -340,18 +363,30 @@ class LocationLayerControllerTest : BaseActivityTest() {
fun accuracy_visibleWhenCameraMoved() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(component: LocationComponent, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
uiController: UiController, context: Context) {
component.activateLocationComponent(context, false)
component.isLocationComponentEnabled = true
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)
component.forceLocationUpdate(location)
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY)
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(location), 16.0))
uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY + 300)
mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER)

assertEquals(Utils.calculateZoomLevelRadius(mapboxMap, location) /*meters projected to radius on zoom 16*/,
mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0]
.getNumberProperty(PROPERTY_ACCURACY_RADIUS).toFloat(), 0.1f)
val target = LatLng(location)
val zoom = 16.0
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(target, zoom))

val cameraCheck = {
Math.abs(zoom - mapboxMap.cameraPosition.zoom) < 0.1
&& Math.abs(target.latitude - mapboxMap.cameraPosition.target.latitude) < 0.1
&& Math.abs(target.longitude - mapboxMap.cameraPosition.target.longitude) < 0.1
}
waitForRenderResult(uiController, cameraCheck, true)

val expectedRadius = Utils.calculateZoomLevelRadius(mapboxMap, location) /*meters projected to radius on zoom 16*/
val renderCheck = {
Math.abs(expectedRadius - mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getNumberProperty(PROPERTY_ACCURACY_RADIUS).toFloat()) < 0.1
}
waitForRenderResult(uiController, renderCheck, true)

assertThat(renderCheck.invoke(), `is`(true))
}
}
executeComponentTest(componentAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ fun MapboxMap.waitForLayer(uiController: UiController, location: Location, layer
}
}

inline fun waitForRenderResult(uiController: UiController, checkFunction: () -> Boolean, expectedResult: Boolean) {
var counter = 0
val delay = MapboxTestingUtils.MAP_RENDER_DELAY
while (checkFunction.invoke() != expectedResult && delay * counter < MapboxTestingUtils.RENDER_TIMEOUT) {
uiController.loopMainThreadForAtLeast(delay)
counter++
}
}

class MapboxTestingUtils {
companion object {

const val MAP_RENDER_DELAY = 250L
const val MAP_CONNECTION_DELAY = 1000L
const val RENDER_TIMEOUT = 3_000L
const val RENDER_TIMEOUT = 2_500L

/**
* Used to increase style load time for stress testing.
Expand Down

0 comments on commit 079ba02

Please sign in to comment.