Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Billing tests for multiple route scenarios #7037

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import com.mapbox.navigation.core.MapboxNavigationProvider
import com.mapbox.navigation.instrumentation_tests.activity.EmptyTestActivity
import com.mapbox.navigation.instrumentation_tests.utils.MapboxNavigationRule
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.clearNavigationRoutesAndWaitForUpdate
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.routeProgressUpdates
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.sdkTest
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.setNavigationRoutesAndWaitForUpdate
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.startTripSessionAndWaitForActiveGuidanceState
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.startTripSessionAndWaitForFreeDriveState
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.stopTripSessionAndWaitForIdleState
import com.mapbox.navigation.instrumentation_tests.utils.http.HttpServiceEvent
import com.mapbox.navigation.instrumentation_tests.utils.http.HttpServiceEventsObserver
import com.mapbox.navigation.instrumentation_tests.utils.location.MockLocationReplayerRule
import com.mapbox.navigation.instrumentation_tests.utils.parameters
import com.mapbox.navigation.instrumentation_tests.utils.routes.RoutesProvider
import com.mapbox.navigation.instrumentation_tests.utils.routes.RoutesProvider.toNavigationRoutes
Expand All @@ -24,6 +26,7 @@ import com.mapbox.navigation.testing.ui.utils.runOnMainSync
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.toList
import org.junit.After
Expand All @@ -43,9 +46,13 @@ class TripSessionsBillingTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::c
@get:Rule
val mapboxNavigationRule = MapboxNavigationRule()

@get:Rule
val mockLocationReplayerRule = MockLocationReplayerRule(mockLocationUpdatesRule)

private lateinit var mapboxNavigation: MapboxNavigation

private val route = RoutesProvider.dc_very_short(context).toNavigationRoutes()[0]
private val anotherRoute = RoutesProvider.dc_short_with_alternative(context).toNavigationRoutes()[0]

private lateinit var httpEventsObserver: HttpServiceEventsObserver

Expand All @@ -57,6 +64,20 @@ class TripSessionsBillingTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::c
bearing = 190f
}

private fun stayOnInitialPosition() {
mockLocationReplayerRule.loopUpdate(
mockLocationUpdatesRule.generateLocationUpdate {
route.routeOptions.coordinatesList().first().run {
[email protected] = latitude()
[email protected] = longitude()
}

bearing = 190f
},
times = 120
)
}

@Before
fun setup() {
httpEventsObserver = HttpServiceEventsObserver.register()
Expand Down Expand Up @@ -128,6 +149,55 @@ class TripSessionsBillingTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::c
assertSessionDuration(SESSION_DURATION_SECONDS, billingRequests.first().duration)
}

@Test
fun testBillingSessionsWhenNewRouteSet() = sdkTest {
mapboxNavigation.setNavigationRoutesAndWaitForUpdate(listOf(route))

stayOnInitialPosition()

mapboxNavigation.startTripSessionAndWaitForActiveGuidanceState()

mapboxNavigation.routeProgressUpdates().first()

mapboxNavigation.setNavigationRoutesAndWaitForUpdate(listOf(anotherRoute))

delay(TimeUnit.SECONDS.toMillis(2))

mapboxNavigation.clearNavigationRoutesAndWaitForUpdate()

val billingRequests = getAllBillingEventsWhenHasEvents(1, "Active Guidance") {
it.isActiveGuidanceSession
}

assertSessionEvents(BillingEventType.ACTIVE_GUIDANCE, billingRequests)
assertSessionDuration(3, billingRequests[0].duration)
}

@Test
fun testBillingSessionsWhenNewRouteSetImmediately() = sdkTest(timeout = Long.MAX_VALUE) {
mapboxNavigation.setNavigationRoutesAndWaitForUpdate(listOf(route))
mapboxNavigation.startTripSessionAndWaitForActiveGuidanceState()
mapboxNavigation.setNavigationRoutesAndWaitForUpdate(listOf(anotherRoute))

delay(TimeUnit.SECONDS.toMillis(SESSION_DURATION_SECONDS))

mapboxNavigation.clearNavigationRoutesAndWaitForUpdate()

val billingRequests = getAllBillingEventsWhenHasEvents(2, "Active Guidance") {
it.isActiveGuidanceSession
}

assertSessionEvents(
listOf(
BillingEventType.ACTIVE_GUIDANCE,
BillingEventType.ACTIVE_GUIDANCE
),
billingRequests
)
assertSessionDuration(0, billingRequests[0].duration)
assertSessionDuration(SESSION_DURATION_SECONDS, billingRequests[1].duration)
}

@Test
fun testBillingSessionSentForEmptyActiveGuidanceTrip() = sdkTest {
mapboxNavigation.setNavigationRoutesAndWaitForUpdate(listOf(route))
Expand Down