Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Madsen committed Mar 18, 2020
1 parent 803b309 commit 531b0a0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MapboxOffboardRouter(
) {
mapboxDirections = RouteBuilderProvider.getBuilder(accessToken, context, skuTokenProvider)
.routeOptions(routeOptions)
.enableRefresh(routeOptions.isRefreshEnabled())
.build()
mapboxDirections?.enqueueCall(object : Callback<DirectionsResponse> {

Expand Down Expand Up @@ -69,3 +70,7 @@ class MapboxOffboardRouter(
mapboxDirections = null
}
}

private fun RouteOptions.isRefreshEnabled(): Boolean {
return profile().contains(other = "traffic", ignoreCase = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MapboxOffboardRouterTest : BaseTest() {
every { mockSkuTokenProvider.obtainUrlWithSkuToken("/mock", 1) } returns ("/mock&sku=102jaksdhfj")
every { RouteBuilderProvider.getBuilder(accessToken, context, mockSkuTokenProvider) } returns mapboxDirectionsBuilder
every { mapboxDirectionsBuilder.interceptor(any()) } returns mapboxDirectionsBuilder
every { mapboxDirectionsBuilder.enableRefresh(any()) } returns mapboxDirectionsBuilder
every { mapboxDirectionsBuilder.build() } returns mapboxDirections
every { mapboxDirections.enqueueCall(capture(listener)) } answers {
callback = listener.captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import com.mapbox.navigation.base.trip.model.RouteProgress
internal class RouteRefreshApi(
private val routeRefreshRetrofit: RouteRefreshRetrofit
) {
fun supportsRefresh(route: DirectionsRoute?): Boolean {
val isTrafficProfile = route?.routeOptions()
?.profile()?.contains(other = "traffic", ignoreCase = true)
return isTrafficProfile == true
}

fun refreshRoute(
accessToken: String,
route: DirectionsRoute?,
Expand All @@ -17,10 +23,17 @@ internal class RouteRefreshApi(
if (accessToken.isNotEmpty()) {
refreshBuilder.accessToken(accessToken)
}

val originalRoute: DirectionsRoute = route ?: run {
callback.onError(RouteRefreshError("No DirectionsRoute to refresh"))
return
}

if (!supportsRefresh(originalRoute)) {
callback.onError(RouteRefreshError("Unsupported profile ${originalRoute.routeOptions()?.profile()}"))
return
}

originalRoute.routeOptions()?.requestUuid()?.let {
refreshBuilder.requestId(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Job

/**
* This class is responsible for refreshing route traffic at a specified [intervalSeconds].
* This class is responsible for refreshing the current direction route's traffic at a
* specified [intervalSeconds]. This does not support alternative routes.
*
* If the route is route is successfully refreshed, this class will update the [TripSession.route]
*
* Callers are free to call [start] and [stop] as whenever. Make sure there is at
* least one call to [stop].
* [start] and [stop] are attached to the application lifecycle. Observing routes that
* can be refreshed are handled by this class. Calling [start] will restart the refresh timer.
*/
internal class RouteRefreshController(
private var accessToken: String,
Expand All @@ -32,11 +33,13 @@ internal class RouteRefreshController(
fun start(): Job {
stop()
return routerRefreshTimer.startTimer {
routeRefreshApi.refreshRoute(
accessToken,
tripSession.route,
tripSession.getRouteProgress(),
routeRefreshCallback)
if (routeRefreshApi.supportsRefresh(tripSession.route)) {
routeRefreshApi.refreshRoute(
accessToken,
tripSession.route,
tripSession.getRouteProgress(),
routeRefreshCallback)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.navigation.core.routerefresh

import com.mapbox.api.directions.v5.DirectionsCriteria
import com.mapbox.api.directions.v5.models.DirectionsRoute
import com.mapbox.navigation.base.trip.model.RouteProgress
import io.mockk.every
Expand All @@ -21,6 +22,7 @@ class RouteRefreshApiTest {
val directionsRoute: DirectionsRoute? = mockk {
every { routeOptions() } returns mockk {
every { requestUuid() } returns "test_request_id"
every { profile() } returns DirectionsCriteria.PROFILE_DRIVING_TRAFFIC
}
every { routeIndex() } returns "2"
}
Expand Down Expand Up @@ -72,6 +74,7 @@ class RouteRefreshApiTest {
val directionsRoute: DirectionsRoute? = mockk {
every { routeOptions() } returns mockk {
every { requestUuid() } returns ""
every { profile() } returns DirectionsCriteria.PROFILE_DRIVING_TRAFFIC
}
}
val routeProgress: RouteProgress = mockk(relaxed = true)
Expand All @@ -84,4 +87,26 @@ class RouteRefreshApiTest {
verify(exactly = 0) { callback.onRefresh(any()) }
verify(exactly = 1) { callback.onError(any()) }
}

@Test
fun `should error with non traffic profiles`() {
val accessToken = "pk.123"
val directionsRoute: DirectionsRoute? = mockk {
every { routeOptions() } returns mockk {
every { requestUuid() } returns "test_request_id"
every { profile() } returns DirectionsCriteria.PROFILE_DRIVING
}
every { routeIndex() } returns "2"
}
val routeProgress: RouteProgress = mockk(relaxed = true)
val callback: RouteRefreshCallback = mockk(relaxed = true) {
every { onError(any()) } returns Unit
}
every { routeRefreshRetrofit.enqueueCall(any(), any()) } returns Unit

routeRefreshApi.refreshRoute(accessToken, directionsRoute, routeProgress, callback)

verify(exactly = 0) { routeRefreshRetrofit.enqueueCall(any(), any()) }
verify(exactly = 1) { callback.onError(any()) }
}
}

0 comments on commit 531b0a0

Please sign in to comment.