Skip to content

Commit 94e04b5

Browse files
committed
refactoring
1 parent a4650b6 commit 94e04b5

File tree

1 file changed

+57
-48
lines changed

1 file changed

+57
-48
lines changed

libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/ArtificialDriverTest.kt

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import android.util.Log
44
import androidx.test.ext.junit.runners.AndroidJUnit4
55
import androidx.test.platform.app.InstrumentationRegistry
66
import com.mapbox.api.directions.v5.models.DirectionsRoute
7-
import com.mapbox.common.LogConfiguration
8-
import com.mapbox.common.LoggingLevel
97
import com.mapbox.navigation.base.options.NavigationOptions
108
import com.mapbox.navigation.base.route.NavigationRoute
119
import com.mapbox.navigation.base.route.RouterOrigin
@@ -29,13 +27,10 @@ import kotlinx.coroutines.async
2927
import kotlinx.coroutines.channels.awaitClose
3028
import kotlinx.coroutines.flow.Flow
3129
import kotlinx.coroutines.flow.callbackFlow
32-
import kotlinx.coroutines.flow.collect
33-
import kotlinx.coroutines.flow.filter
34-
import kotlinx.coroutines.flow.first
3530
import kotlinx.coroutines.flow.map
36-
import kotlinx.coroutines.launch
31+
import kotlinx.coroutines.flow.takeWhile
32+
import kotlinx.coroutines.flow.toList
3733
import kotlinx.coroutines.runBlocking
38-
import org.junit.Assert.assertEquals
3934
import org.junit.Assert.assertTrue
4035
import org.junit.Test
4136
import org.junit.runner.RunWith
@@ -45,58 +40,72 @@ class ArtificialDriverTest {
4540

4641
@Test
4742
fun testFollowingRoute() = runBlocking<Unit>(Dispatchers.Main) {
48-
LogConfiguration.setLoggingLevel(LoggingLevel.DEBUG)
4943
getNativeNavigator { nativeNavigator ->
5044
val testRoute = getTestRoute()
5145
val replayRouteMapper = ReplayRouteMapper()
52-
val events = replayRouteMapper.mapDirectionsRouteGeometry(testRoute.directionsRoute)
53-
val result = nativeNavigator.setRoutes(testRoute, reason = SetRoutesReason.NEW_ROUTE)
54-
assertTrue("result is $result", result.isValue)
55-
val states = mutableListOf<NavigationStatus>()
56-
val statusTracking = launch {
57-
nativeNavigator.statusUpdates().collect {
58-
states.add(it.second)
59-
}
46+
val events = replayRouteMapper.mapDirectionsRouteGeometry(testRoute.directionsRoute).filterIsInstance<ReplayEventUpdateLocation>()
47+
val setRoutesResult = nativeNavigator.setRoutes(testRoute, reason = SetRoutesReason.NEW_ROUTE)
48+
assertTrue("result is $setRoutesResult", setRoutesResult.isValue)
49+
val statusesTracking = async<List<NavigationStatus>> {
50+
nativeNavigator.collectStatuses(untilRouteState = RouteState.COMPLETE)
6051
}
61-
for (event in events.filterIsInstance<ReplayEventUpdateLocation>()) {
52+
53+
for (event in events) {
6254
val location = event.location.mapToLocation(event.eventTimestamp)
6355
assertTrue(nativeNavigator.updateLocation(location.toFixLocation()))
6456
}
65-
statusTracking.cancel()
66-
assertTrue(states.all { it.routeState == RouteState.TRACKING || it.routeState == RouteState.COMPLETE })
57+
58+
val states = statusesTracking.await()
59+
assertTrue(states.all { it.routeState == RouteState.TRACKING })
6760
}
6861
}
62+
}
6963

70-
@OptIn(ExperimentalCoroutinesApi::class)
71-
fun MapboxNativeNavigator.statusUpdates(): Flow<Pair<NavigationStatusOrigin, NavigationStatus>> {
72-
return callbackFlow {
73-
val observer = NavigatorObserver { origin, status ->
74-
Log.d("vadzim-debug", "$origin, $status")
75-
this.trySend(Pair(origin, status))
76-
}
77-
addNavigatorObserver(observer)
78-
awaitClose {
79-
removeNavigatorObserver(observer)
80-
}
64+
private suspend fun MapboxNativeNavigator.collectStatuses(
65+
untilRouteState: RouteState
66+
): MutableList<NavigationStatus> {
67+
val statues = mutableListOf<NavigationStatus>()
68+
statusUpdates()
69+
.map { it.status }
70+
.takeWhile { it.routeState != untilRouteState }
71+
.toList(statues)
72+
return statues
73+
}
74+
75+
data class OnStatusUpdateParameters(
76+
val origin: NavigationStatusOrigin,
77+
val status: NavigationStatus
78+
)
79+
80+
@OptIn(ExperimentalCoroutinesApi::class)
81+
fun MapboxNativeNavigator.statusUpdates(): Flow<OnStatusUpdateParameters> {
82+
return callbackFlow {
83+
val observer = NavigatorObserver { origin, status ->
84+
Log.d("vadzim-debug", "$origin, $status")
85+
this.trySend(OnStatusUpdateParameters(origin, status))
86+
}
87+
addNavigatorObserver(observer)
88+
awaitClose {
89+
removeNavigatorObserver(observer)
8190
}
8291
}
92+
}
8393

84-
private suspend fun getNativeNavigator(block: suspend (MapboxNativeNavigator) -> Unit) {
85-
val context = InstrumentationRegistry.getInstrumentation().targetContext
86-
val mapboxNavigation = MapboxNavigationProvider.create(
87-
NavigationOptions.Builder(context)
88-
.accessToken(context.getString(R.string.mapbox_access_token))
89-
.build()
90-
)
91-
block(MapboxNativeNavigatorImpl)
92-
mapboxNavigation.onDestroy()
93-
}
94+
private suspend fun getNativeNavigator(block: suspend (MapboxNativeNavigator) -> Unit) {
95+
val context = InstrumentationRegistry.getInstrumentation().targetContext
96+
val mapboxNavigation = MapboxNavigationProvider.create(
97+
NavigationOptions.Builder(context)
98+
.accessToken(context.getString(R.string.mapbox_access_token))
99+
.build()
100+
)
101+
block(MapboxNativeNavigatorImpl)
102+
mapboxNavigation.onDestroy()
103+
}
94104

95-
private fun getTestRoute(): NavigationRoute {
96-
val context = InstrumentationRegistry.getInstrumentation().targetContext
97-
return DirectionsRoute.fromJson(
98-
context.resources.openRawResource(R.raw.multileg_route)
99-
.readBytes().decodeToString()
100-
).toNavigationRoute(RouterOrigin.Custom())
101-
}
102-
}
105+
private fun getTestRoute(): NavigationRoute {
106+
val context = InstrumentationRegistry.getInstrumentation().targetContext
107+
return DirectionsRoute.fromJson(
108+
context.resources.openRawResource(R.raw.multileg_route)
109+
.readBytes().decodeToString()
110+
).toNavigationRoute(RouterOrigin.Custom())
111+
}

0 commit comments

Comments
 (0)