Skip to content

Commit 85b3180

Browse files
committed
NAVAND-713: simplify API
1 parent 22210f3 commit 85b3180

File tree

13 files changed

+108
-243
lines changed

13 files changed

+108
-243
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Mapbox welcomes participation and contributions from everyone.
55
## Unreleased
66
#### Features
77
- Added support for EV route refresh. [#6511](https://github.com/mapbox/mapbox-navigation-android/pull/6511)
8+
- Added `MapboxNavigation#onEVDataUpdated` method to be invoked by the end user to notify Navigation SDK of EV data changes so that it can be used in refresh requests. [#6511](https://github.com/mapbox/mapbox-navigation-android/pull/6511)
89
#### Bug fixes and improvements
910
- Improved experience in tunnels and reduced the likelihood of losing road edge matching candidates. [#6510](https://github.com/mapbox/mapbox-navigation-android/pull/6510)
1011
- Fixed an issue with `NavigationView` that caused road label position to not update in some cases. [#6508](https://github.com/mapbox/mapbox-navigation-android/pull/6508)

instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/core/EVRouteRefreshTest.kt

+49-92
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import com.mapbox.navigation.base.options.NavigationOptions
1313
import com.mapbox.navigation.base.options.RoutingTilesOptions
1414
import com.mapbox.navigation.base.route.NavigationRoute
1515
import com.mapbox.navigation.base.route.RouteRefreshOptions
16-
import com.mapbox.navigation.core.EVDataObserver
17-
import com.mapbox.navigation.core.EVDataUpdater
1816
import com.mapbox.navigation.core.MapboxNavigation
1917
import com.mapbox.navigation.core.MapboxNavigationProvider
2018
import com.mapbox.navigation.core.directions.session.RoutesExtra
@@ -47,7 +45,6 @@ import org.junit.Before
4745
import org.junit.Rule
4846
import org.junit.Test
4947
import java.net.URI
50-
import java.util.concurrent.CopyOnWriteArraySet
5148
import java.util.concurrent.TimeUnit
5249

5350
private const val KEY_ENGINE = "engine"
@@ -78,8 +75,6 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
7875
Point.fromLngLat(11.5852259, 48.1760993),
7976
Point.fromLngLat(10.3406374, 49.16479)
8077
)
81-
private lateinit var routeHandler: MockDirectionsRequestHandler
82-
private val evDataUpdater = TestEVDataUpdater()
8378

8479
override fun setupMockLocation(): Location = mockLocationUpdatesRule.generateLocationUpdate {
8580
latitude = twoCoordinates[0].latitude()
@@ -110,7 +105,7 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
110105
.build()
111106
)
112107
mockWebServerRule.requestHandlers.clear()
113-
routeHandler = MockDirectionsRequestHandler(
108+
val routeHandler = MockDirectionsRequestHandler(
114109
"driving-traffic",
115110
readRawFileText(activity, R.raw.ev_route_response_for_refresh),
116111
twoCoordinates,
@@ -128,8 +123,7 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
128123
)
129124
val requestedRoutes = requestRoutes(twoCoordinates, electric = false)
130125

131-
mapboxNavigation.setEVDataUpdater(evDataUpdater)
132-
evDataUpdater.updateData(
126+
mapboxNavigation.onEVDataUpdated(
133127
mapOf(
134128
KEY_ENERGY_CONSUMPTION_CURVE to "0,300;20,120;40,150",
135129
KEY_EV_INITIAL_CHARGE to "80",
@@ -183,14 +177,13 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
183177
val initialCharge = "80"
184178
val preconditioningTime = "10"
185179
val auxiliaryConsumption = "300"
186-
mapboxNavigation.setEVDataUpdater(evDataUpdater)
187180
val evData = mapOf(
188181
KEY_ENERGY_CONSUMPTION_CURVE to consumptionCurve,
189182
KEY_EV_INITIAL_CHARGE to initialCharge,
190183
KEY_EV_PRECONDITIONING_TIME to preconditioningTime,
191184
KEY_AUXILIARY_CONSUMPTION to auxiliaryConsumption
192185
)
193-
evDataUpdater.updateData(evData)
186+
mapboxNavigation.onEVDataUpdated(evData)
194187

195188
mapboxNavigation.setNavigationRoutes(requestedRoutes)
196189
mapboxNavigation.startTripSession()
@@ -231,8 +224,7 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
231224
KEY_EV_PRECONDITIONING_TIME to preconditioningTime,
232225
KEY_AUXILIARY_CONSUMPTION to auxiliaryConsumption
233226
)
234-
mapboxNavigation.setEVDataUpdater(evDataUpdater)
235-
evDataUpdater.updateData(firstEvData)
227+
mapboxNavigation.onEVDataUpdated(firstEvData)
236228
waitUntilNewRefresh()
237229

238230
checkHasParameters(
@@ -241,7 +233,7 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
241233
)
242234

243235
val newInitialCharge = "60"
244-
evDataUpdater.updateData(
236+
mapboxNavigation.onEVDataUpdated(
245237
mapOf(
246238
KEY_EV_INITIAL_CHARGE to newInitialCharge,
247239
KEY_EV_PRECONDITIONING_TIME to null,
@@ -261,34 +253,7 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
261253
)
262254
checkDoesNotHaveParameters(urlWithTwiceUpdatedData, setOf(KEY_EV_PRECONDITIONING_TIME))
263255

264-
mapboxNavigation.setEVDataUpdater(null)
265-
waitUntilNewRefresh()
266-
267-
val removedUpdaterRefreshUrl = refreshHandler.handledRequests.last().requestUrl!!
268-
checkHasParameters(
269-
removedUpdaterRefreshUrl,
270-
mapOf(
271-
KEY_ENGINE to VALUE_ELECTRIC,
272-
KEY_ENERGY_CONSUMPTION_CURVE to consumptionCurve,
273-
KEY_EV_INITIAL_CHARGE to newInitialCharge,
274-
KEY_AUXILIARY_CONSUMPTION to auxiliaryConsumption
275-
)
276-
)
277-
checkDoesNotHaveParameters(removedUpdaterRefreshUrl, setOf(KEY_EV_PRECONDITIONING_TIME))
278-
279-
val newUpdater = TestEVDataUpdater()
280-
mapboxNavigation.setEVDataUpdater(newUpdater)
281-
val newUpdaterCharge = "45"
282-
evDataUpdater.updateData(mapOf(KEY_EV_INITIAL_CHARGE to "50"))
283-
newUpdater.updateData(mapOf(KEY_EV_INITIAL_CHARGE to newUpdaterCharge))
284-
waitUntilNewRefresh()
285-
286-
checkHasParameters(
287-
refreshHandler.handledRequests.last().requestUrl!!,
288-
mapOf(KEY_EV_INITIAL_CHARGE to newUpdaterCharge)
289-
)
290-
291-
newUpdater.updateData(emptyMap())
256+
mapboxNavigation.onEVDataUpdated(emptyMap())
292257
waitUntilNewRefresh()
293258

294259
val urlAfterEmptyUpdate = refreshHandler.handledRequests.last().requestUrl!!
@@ -297,7 +262,7 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
297262
mapOf(
298263
KEY_ENGINE to VALUE_ELECTRIC,
299264
KEY_ENERGY_CONSUMPTION_CURVE to consumptionCurve,
300-
KEY_EV_INITIAL_CHARGE to newUpdaterCharge,
265+
KEY_EV_INITIAL_CHARGE to newInitialCharge,
301266
KEY_AUXILIARY_CONSUMPTION to auxiliaryConsumption
302267
)
303268
)
@@ -311,14 +276,13 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
311276
acceptedGeometryIndex = 0
312277
)
313278
val requestedRoutes = requestRoutes(twoCoordinates, electric = true)
314-
mapboxNavigation.setEVDataUpdater(evDataUpdater)
315279
val evData = mapOf(
316280
KEY_ENERGY_CONSUMPTION_CURVE to "0,300;20,160;80,140;120,180",
317281
KEY_EV_INITIAL_CHARGE to "17000",
318282
KEY_EV_PRECONDITIONING_TIME to "10",
319283
KEY_AUXILIARY_CONSUMPTION to "300"
320284
)
321-
evDataUpdater.updateData(evData)
285+
mapboxNavigation.onEVDataUpdated(evData)
322286

323287
mapboxNavigation.setNavigationRoutesAndWaitForUpdate(requestedRoutes)
324288
mapboxNavigation.startTripSession()
@@ -361,14 +325,13 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
361325
geometryIndex
362326
)
363327
val requestedRoutes = requestRoutes(twoCoordinates, electric = true)
364-
mapboxNavigation.setEVDataUpdater(evDataUpdater)
365328
val evData = mapOf(
366329
KEY_ENERGY_CONSUMPTION_CURVE to "0,300;20,160;80,140;120,180",
367330
KEY_EV_INITIAL_CHARGE to "17000",
368331
KEY_EV_PRECONDITIONING_TIME to "10",
369332
KEY_AUXILIARY_CONSUMPTION to "300"
370333
)
371-
evDataUpdater.updateData(evData)
334+
mapboxNavigation.onEVDataUpdated(evData)
372335
mapboxNavigation.setNavigationRoutes(requestedRoutes)
373336
// corresponds to currentRouteGeometryIndex = 384
374337
stayOnPosition(48.209765, 11.478632)
@@ -413,14 +376,13 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
413376
acceptedGeometryIndex = 0
414377
)
415378
val requestedRoutes = requestRoutes(twoCoordinates, electric = true)
416-
mapboxNavigation.setEVDataUpdater(evDataUpdater)
417379
val evData = mapOf(
418380
KEY_ENERGY_CONSUMPTION_CURVE to "0,300;20,160;80,140;120,180",
419381
KEY_EV_INITIAL_CHARGE to "17000",
420382
KEY_EV_PRECONDITIONING_TIME to "10",
421383
KEY_AUXILIARY_CONSUMPTION to "300"
422384
)
423-
evDataUpdater.updateData(evData)
385+
mapboxNavigation.onEVDataUpdated(evData)
424386
mapboxNavigation.setNavigationRoutes(requestedRoutes)
425387
stayOnInitialPosition()
426388
mapboxNavigation.startTripSession()
@@ -456,24 +418,25 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
456418

457419
@Test
458420
fun ev_route_refresh_updates_ev_annotations_and_waypoints_for_second_leg() = sdkTest {
459-
val routeGeometryIndex = 1050
460-
val legGeometryIndex = 300
421+
val routeGeometryIndex = 774
422+
val legGeometryIndex = 26
423+
replaceOriginalResponseHandler(R.raw.ev_route_response_for_refresh_with_2_waypoints)
461424
addRefreshRequestHandler(
462425
R.raw.ev_route_refresh_response_for_second_leg,
463-
acceptedGeometryIndex = routeGeometryIndex
426+
acceptedGeometryIndex = routeGeometryIndex,
427+
testUuid = "ev_route_response_for_refresh_with_2_waypoints"
464428
)
465-
val requestedRoutes = requestRoutes(twoCoordinates, electric = true)
466-
mapboxNavigation.setEVDataUpdater(evDataUpdater)
429+
val requestedRoutes = requestRoutes(twoCoordinates, electric = true, minChargeAtDestination = 35000)
467430
val evData = mapOf(
468431
KEY_ENERGY_CONSUMPTION_CURVE to "0,300;20,160;80,140;120,180",
469-
KEY_EV_INITIAL_CHARGE to "17000",
432+
KEY_EV_INITIAL_CHARGE to "30000",
470433
KEY_EV_PRECONDITIONING_TIME to "10",
471434
KEY_AUXILIARY_CONSUMPTION to "300"
472435
)
473-
evDataUpdater.updateData(evData)
436+
mapboxNavigation.onEVDataUpdated(evData)
474437
mapboxNavigation.setNavigationRoutes(requestedRoutes, initialLegIndex = 1)
475-
// corresponds to currentRouteGeometryIndex = 1050
476-
stayOnPosition(48.435946, 10.86999)
438+
// corresponds to currentRouteGeometryIndex = 774
439+
stayOnPosition(48.391238, 11.064252, 90f)
477440
mapboxNavigation.startTripSession()
478441
mapboxNavigation.routeProgressUpdates().filter { progress ->
479442
progress.currentRouteGeometryIndex == routeGeometryIndex
@@ -486,11 +449,11 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
486449
requestedRoutes[0].getSocAnnotationsFromLeg(0)!!.firstLastAnd()
487450
)
488451
assertEquals(
489-
listOf(43, 38, 10),
452+
listOf(39, 39, 10),
490453
requestedRoutes[0].getSocAnnotationsFromLeg(1)!!.firstLastAnd(legGeometryIndex)
491454
)
492455
assertEquals(
493-
listOf(null, 8097, null),
456+
listOf(null, 7911, 6000, null),
494457
requestedRoutes[0].directionsResponse.waypoints()?.extractChargeAtArrival()
495458
)
496459

@@ -499,11 +462,11 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
499462
updatedRoutes[0].getSocAnnotationsFromLeg(0)!!.firstLastAnd()
500463
)
501464
assertEquals(
502-
listOf(43, 28, 1),
465+
listOf(39, 49, 21),
503466
updatedRoutes[0].getSocAnnotationsFromLeg(1)!!.firstLastAnd(legGeometryIndex)
504467
)
505468
assertEquals(
506-
listOf(null, 8097, null),
469+
listOf(null, 7911, 12845, null),
507470
updatedRoutes[0].directionsResponse.waypoints()?.extractChargeAtArrival()
508471
)
509472
}
@@ -512,18 +475,22 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
512475
stayOnPosition(twoCoordinates[0].latitude(), twoCoordinates[0].longitude())
513476
}
514477

515-
private fun stayOnPosition(latitude: Double, longitude: Double) {
478+
private fun stayOnPosition(latitude: Double, longitude: Double, bearing: Float = 190f) {
516479
mockLocationReplayerRule.loopUpdate(
517480
mockLocationUpdatesRule.generateLocationUpdate {
518481
this.latitude = latitude
519482
this.longitude = longitude
520-
bearing = 190f
483+
this.bearing = bearing
521484
},
522485
times = 120
523486
)
524487
}
525488

526-
private fun generateRouteOptions(coordinates: List<Point>, electric: Boolean): RouteOptions {
489+
private fun generateRouteOptions(
490+
coordinates: List<Point>,
491+
electric: Boolean,
492+
minChargeAtDestination: Int,
493+
): RouteOptions {
527494
return RouteOptions.builder().applyDefaultNavigationOptions()
528495
.profile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC)
529496
.alternatives(true)
@@ -540,7 +507,7 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
540507
KEY_ENERGY_CONSUMPTION_CURVE to "0,300;20,160;80,140;120,180",
541508
KEY_EV_PRECONDITIONING_TIME to "10",
542509
"ev_min_charge_at_charging_station" to "6000",
543-
"ev_min_charge_at_destination" to "6000",
510+
"ev_min_charge_at_destination" to "$minChargeAtDestination",
544511
"ev_max_charge" to "60000",
545512
"ev_connector_types" to "ccs_combo_type1,ccs_combo_type2",
546513
"energy_consumption_curve" to "0,300;20,160;80,140;120,180",
@@ -572,9 +539,12 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
572539

573540
private suspend fun requestRoutes(
574541
coordinates: List<Point>,
575-
electric: Boolean
542+
electric: Boolean,
543+
minChargeAtDestination: Int = 6000
576544
): List<NavigationRoute> {
577-
return mapboxNavigation.requestRoutes(generateRouteOptions(coordinates, electric))
545+
return mapboxNavigation.requestRoutes(
546+
generateRouteOptions(coordinates, electric, minChargeAtDestination)
547+
)
578548
.getSuccessfulResultOrThrowException()
579549
.routes
580550
}
@@ -615,22 +585,27 @@ class EVRouteRefreshTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.
615585

616586
private fun addRefreshRequestHandler(
617587
@IdRes fileId: Int,
618-
acceptedGeometryIndex: Int
588+
acceptedGeometryIndex: Int,
589+
testUuid: String = responseTestUuid,
619590
): MockDirectionsRefreshHandler {
620591
return MockDirectionsRefreshHandler(
621-
responseTestUuid,
592+
testUuid,
622593
readRawFileText(activity, fileId),
623594
acceptedGeometryIndex = acceptedGeometryIndex
624595
).also {
625596
mockWebServerRule.requestHandlers.add(FailByRequestMockRequestHandler(it))
626597
}
627598
}
628599

629-
private fun getOffRouteLocation(originLocation: Point): Location =
630-
mockLocationUpdatesRule.generateLocationUpdate {
631-
latitude = originLocation.latitude() + 0.002
632-
longitude = originLocation.longitude()
633-
}
600+
private fun replaceOriginalResponseHandler(@IdRes fileId: Int) {
601+
val routeHandler = MockDirectionsRequestHandler(
602+
"driving-traffic",
603+
readRawFileText(activity, fileId),
604+
twoCoordinates,
605+
relaxedExpectedCoordinates = true
606+
)
607+
mockWebServerRule.requestHandlers.add(0, routeHandler)
608+
}
634609
}
635610

636611
private class DynamicResponseModifier : (String) -> String {
@@ -668,21 +643,3 @@ private class DynamicResponseModifier : (String) -> String {
668643
.toJson()
669644
}
670645
}
671-
672-
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
673-
private class TestEVDataUpdater : EVDataUpdater {
674-
675-
private val observers = CopyOnWriteArraySet<EVDataObserver>()
676-
677-
override fun registerEVDataObserver(observer: EVDataObserver) {
678-
observers.add(observer)
679-
}
680-
681-
override fun unregisterEVDataObserver(observer: EVDataObserver) {
682-
observers.remove(observer)
683-
}
684-
685-
fun updateData(data: Map<String, String?>) {
686-
observers.forEach { it.onEVDataUpdated(data) }
687-
}
688-
}

instrumentation-tests/src/main/res/raw/ev_route_refresh_response_for_second_leg.json

+1-1
Large diffs are not rendered by default.

instrumentation-tests/src/main/res/raw/ev_route_response_for_refresh_with_2_waypoints.json

+1
Large diffs are not rendered by default.

libnavigation-base/src/main/java/com/mapbox/navigation/base/internal/utils/Constants.java

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.mapbox.navigation.base.internal.utils
2+
3+
class Constants {
4+
object RouteResponse {
5+
const val KEY_WAYPOINTS = "waypoints"
6+
}
7+
}

0 commit comments

Comments
 (0)