Skip to content

Commit c2936a0

Browse files
committed
wip
1 parent 1f91ae4 commit c2936a0

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.mapbox.navigation.instrumentation_tests.core
2+
3+
import android.location.Location
4+
import com.google.gson.Gson
5+
import com.mapbox.api.directions.v5.models.DirectionsResponse
6+
import com.mapbox.api.directions.v5.models.DirectionsRoute
7+
import com.mapbox.api.directions.v5.models.RouteOptions
8+
import com.mapbox.geojson.Point
9+
import com.mapbox.navigation.base.extensions.applyDefaultNavigationOptions
10+
import com.mapbox.navigation.base.internal.route.createNavigationRoutes
11+
import com.mapbox.navigation.base.internal.utils._waypoints
12+
import com.mapbox.navigation.base.route.NavigationRoute
13+
import com.mapbox.navigation.base.route.RouterOrigin
14+
import com.mapbox.navigation.instrumentation_tests.R
15+
import com.mapbox.navigation.instrumentation_tests.activity.EmptyTestActivity
16+
import com.mapbox.navigation.instrumentation_tests.utils.readRawFileText
17+
import com.mapbox.navigation.testing.ui.BaseTest
18+
import junit.framework.Assert.assertTrue
19+
import org.junit.Assert
20+
import org.junit.Test
21+
22+
class NavigationRouteTests : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.java) {
23+
24+
override fun setupMockLocation(): Location = mockLocationUpdatesRule.generateLocationUpdate {
25+
latitude = 0.0
26+
longitude = 0.0
27+
}
28+
29+
@Test
30+
fun mapDirectionsResponseToNavigationRoutes() {
31+
val jsonResponse = readRawFileText(activity, R.raw.route_response_dc_very_short)
32+
33+
val navRoutes = NavigationRoute.create(
34+
DirectionsResponse.fromJson(jsonResponse), provideRouteOptions(), RouterOrigin.Onboard
35+
)
36+
37+
assertTrue(navRoutes.isNotEmpty())
38+
navRoutes.forEach {
39+
assertTrue(it._waypoints().isNotEmpty())
40+
}
41+
}
42+
43+
@Test
44+
fun mapDirectionsRoutesToNavigationRoutes() {
45+
val directionsRoute = DirectionsRoute.fromJson(readRawFileText(activity, R.raw.short_route))
46+
47+
val navRoutes = createNavigationRoutes(
48+
listOf(directionsRoute),
49+
provideRouteOptions(),
50+
RouterOrigin.Onboard,
51+
)
52+
53+
assertTrue(navRoutes.isNotEmpty())
54+
}
55+
56+
private fun provideRouteOptions(): RouteOptions =
57+
RouteOptions.builder().applyDefaultNavigationOptions()
58+
.coordinatesList(
59+
listOf(
60+
Point.fromLngLat(1.1, 2.2),
61+
Point.fromLngLat(3.3, 4.4),
62+
)
63+
)
64+
.build()
65+
}

libnavigation-base/src/main/java/com/mapbox/navigation/base/internal/route/NavigationRouteEx.kt

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.mapbox.api.directions.v5.models.DirectionsRoute
88
import com.mapbox.api.directions.v5.models.Incident
99
import com.mapbox.api.directions.v5.models.LegAnnotation
1010
import com.mapbox.api.directions.v5.models.RouteOptions
11+
import com.mapbox.navigation.base.internal.NativeRouteParserWrapper
1112
import com.mapbox.navigation.base.internal.SDKRouteParser
1213
import com.mapbox.navigation.base.route.NavigationRoute
1314
import com.mapbox.navigation.base.route.toNavigationRoute
@@ -87,6 +88,20 @@ fun createNavigationRoutes(
8788
): List<NavigationRoute> =
8889
NavigationRoute.create(directionsResponse, routeOptions, routeParser, routerOrigin)
8990

91+
@VisibleForTesting
92+
fun createNavigationRoutes(
93+
directionsRoutes: List<DirectionsRoute>,
94+
routeOptions: RouteOptions,
95+
routerOrigin: com.mapbox.navigation.base.route.RouterOrigin,
96+
routeParser: SDKRouteParser = NativeRouteParserWrapper
97+
): List<NavigationRoute> =
98+
NavigationRoute.create(
99+
directionsRoutes,
100+
routeOptions,
101+
routerOrigin,
102+
routeParser,
103+
)
104+
90105
/**
91106
* Internal API to create a new [NavigationRoute] from a native peer.
92107
*/

libnavigation-base/src/main/java/com/mapbox/navigation/base/route/NavigationRoute.kt

+44-1
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ import com.mapbox.navigation.base.internal.SDKRouteParser
1818
import com.mapbox.navigation.base.internal.factory.RoadObjectFactory.toUpcomingRoadObjects
1919
import com.mapbox.navigation.base.internal.route.RouteCompatibilityCache
2020
import com.mapbox.navigation.base.internal.route.Waypoint
21+
import com.mapbox.navigation.base.internal.route.toNavigationRoute
2122
import com.mapbox.navigation.base.internal.utils.mapToSdkRouteOrigin
22-
import com.mapbox.navigation.base.trip.model.roadobject.UpcomingRoadObject
2323
import com.mapbox.navigation.base.internal.utils.mapToSkd
24+
import com.mapbox.navigation.base.trip.model.roadobject.UpcomingRoadObject
2425
import com.mapbox.navigation.utils.internal.ThreadController
2526
import com.mapbox.navigation.utils.internal.logD
2627
import com.mapbox.navigation.utils.internal.logE
2728
import com.mapbox.navigation.utils.internal.logI
2829
import com.mapbox.navigator.RouteInterface
2930
import kotlinx.coroutines.async
3031
import kotlinx.coroutines.coroutineScope
32+
import org.json.JSONArray
3133
import java.net.URL
3234

3335
/**
@@ -205,6 +207,34 @@ class NavigationRoute internal constructor(
205207
}
206208
}
207209

210+
internal fun create(
211+
directionsRoutes: List<DirectionsRoute>,
212+
routeOptions: RouteOptions,
213+
routerOrigin: RouterOrigin,
214+
routeParser: SDKRouteParser = NativeRouteParserWrapper
215+
): List<NavigationRoute> {
216+
val directionsRoutesJson = JSONArray(directionsRoutes.map { it.toJson() }).toString()
217+
return create(
218+
directionsRoutesJson,
219+
routeOptions.toUrl("").toString(),
220+
routerOrigin,
221+
routeParser
222+
)
223+
}
224+
225+
private fun create(
226+
directionsRoutesJson: String,
227+
routeOptionsUrlString: String,
228+
routerOrigin: RouterOrigin,
229+
routeParser: SDKRouteParser = NativeRouteParserWrapper
230+
): List<NavigationRoute> {
231+
return routeParser.parseDirectionsRoutes(
232+
directionsRoutesJson, routeOptionsUrlString, routerOrigin
233+
).run {
234+
create(this)
235+
}
236+
}
237+
208238
internal fun create(
209239
directionsResponse: DirectionsResponse,
210240
routeOptions: RouteOptions,
@@ -255,6 +285,19 @@ class NavigationRoute internal constructor(
255285
)
256286
}.cache()
257287
}
288+
289+
private fun create(
290+
expected: Expected<String, List<RouteInterface>>
291+
): List<NavigationRoute> {
292+
return expected.fold({ error ->
293+
logE("NavigationRoute", "Failed to parse a route. Reason: $error")
294+
listOf()
295+
}, { value ->
296+
value
297+
}).map { routeInterface ->
298+
routeInterface.toNavigationRoute()
299+
}.cache()
300+
}
258301
}
259302

260303
/**

0 commit comments

Comments
 (0)