|
| 1 | +package com.mapbox.navigation.instrumentation_tests.core |
| 2 | + |
| 3 | +import android.location.Location |
| 4 | +import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI |
| 5 | +import com.mapbox.navigation.base.options.NavigationOptions |
| 6 | +import com.mapbox.navigation.base.route.NavigationRoute |
| 7 | +import com.mapbox.navigation.base.trip.model.RouteProgressState |
| 8 | +import com.mapbox.navigation.core.MapboxNavigation |
| 9 | +import com.mapbox.navigation.core.MapboxNavigationProvider |
| 10 | +import com.mapbox.navigation.core.directions.session.RoutesExtra |
| 11 | +import com.mapbox.navigation.core.directions.session.RoutesUpdatedResult |
| 12 | +import com.mapbox.navigation.core.preview.RoutesPreviewExtra |
| 13 | +import com.mapbox.navigation.core.preview.RoutesPreviewUpdate |
| 14 | +import com.mapbox.navigation.instrumentation_tests.activity.EmptyTestActivity |
| 15 | +import com.mapbox.navigation.instrumentation_tests.utils.MapboxNavigationRule |
| 16 | +import com.mapbox.navigation.instrumentation_tests.utils.coroutines.routeProgressUpdates |
| 17 | +import com.mapbox.navigation.instrumentation_tests.utils.coroutines.routesPreviewUpdates |
| 18 | +import com.mapbox.navigation.instrumentation_tests.utils.coroutines.routesUpdates |
| 19 | +import com.mapbox.navigation.instrumentation_tests.utils.coroutines.sdkTest |
| 20 | +import com.mapbox.navigation.instrumentation_tests.utils.routes.RoutesProvider |
| 21 | +import com.mapbox.navigation.instrumentation_tests.utils.routes.RoutesProvider.toNavigationRoutes |
| 22 | +import com.mapbox.navigation.testing.ui.BaseTest |
| 23 | +import com.mapbox.navigation.testing.ui.utils.getMapboxAccessTokenFromResources |
| 24 | +import com.mapbox.navigation.testing.ui.utils.runOnMainSync |
| 25 | +import kotlinx.coroutines.flow.first |
| 26 | +import org.junit.Assert.assertEquals |
| 27 | +import org.junit.Assert.assertNotEquals |
| 28 | +import org.junit.Assert.assertNull |
| 29 | +import org.junit.Before |
| 30 | +import org.junit.Rule |
| 31 | +import org.junit.Test |
| 32 | + |
| 33 | +@OptIn(ExperimentalPreviewMapboxNavigationAPI::class) |
| 34 | +class RoutesPreviewTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.java) { |
| 35 | + |
| 36 | + override fun setupMockLocation(): Location = mockLocationUpdatesRule.generateLocationUpdate { |
| 37 | + latitude = 38.894721 |
| 38 | + longitude = -77.031991 |
| 39 | + } |
| 40 | + |
| 41 | + @get:Rule |
| 42 | + val mapboxNavigationRule = MapboxNavigationRule() |
| 43 | + private lateinit var mapboxNavigation: MapboxNavigation |
| 44 | + |
| 45 | + @Before |
| 46 | + fun setUp() { |
| 47 | + runOnMainSync { |
| 48 | + mapboxNavigation = MapboxNavigationProvider.create( |
| 49 | + NavigationOptions.Builder(activity) |
| 50 | + .accessToken(getMapboxAccessTokenFromResources(activity)) |
| 51 | + .build() |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun transitions_free_drive_to_preview_to_active_guidance_to_free_drive() = sdkTest { |
| 58 | + var currentRoutesPreview: RoutesPreviewUpdate? = null |
| 59 | + mapboxNavigation.registerRoutesPreviewObserver { update -> |
| 60 | + currentRoutesPreview = update |
| 61 | + } |
| 62 | + var currentRoutes: RoutesUpdatedResult? = null |
| 63 | + mapboxNavigation.registerRoutesObserver { update -> |
| 64 | + currentRoutes = update |
| 65 | + } |
| 66 | + // initial free drive |
| 67 | + mapboxNavigation.startTripSession() |
| 68 | + assertNull(currentRoutesPreview) |
| 69 | + assertNull(currentRoutes) |
| 70 | + // set routes preview |
| 71 | + val routes = RoutesProvider.dc_very_short(activity).toNavigationRoutes() |
| 72 | + mapboxNavigation.setRoutesPreview(routes) |
| 73 | + mapboxNavigation.routesPreviewUpdates() |
| 74 | + .first { it.reason == RoutesPreviewExtra.PREVIEW_NEW } |
| 75 | + assertEquals(RoutesPreviewExtra.PREVIEW_NEW, currentRoutesPreview?.reason) |
| 76 | + assertEquals(routes, currentRoutesPreview!!.routesPreview!!.routesList) |
| 77 | + assertNull(currentRoutes) |
| 78 | + // start active guidance |
| 79 | + mapboxNavigation.setNavigationRoutes(currentRoutesPreview!!.routesPreview!!.routesList) |
| 80 | + mapboxNavigation.setRoutesPreview(emptyList()) |
| 81 | + mapboxNavigation.routesUpdates() |
| 82 | + .first { it.reason == RoutesExtra.ROUTES_UPDATE_REASON_NEW } |
| 83 | + mapboxNavigation.routeProgressUpdates() |
| 84 | + .first { it.currentState == RouteProgressState.TRACKING } |
| 85 | + mapboxNavigation.routesPreviewUpdates() |
| 86 | + .first { it.reason == RoutesPreviewExtra.PREVIEW_CLEAN_UP } |
| 87 | + assertEquals(RoutesPreviewExtra.PREVIEW_CLEAN_UP, currentRoutesPreview?.reason) |
| 88 | + assertNull(currentRoutesPreview!!.routesPreview) |
| 89 | + assertEquals(RoutesExtra.ROUTES_UPDATE_REASON_NEW, currentRoutes!!.reason) |
| 90 | + assertEquals(routes, currentRoutes!!.navigationRoutes) |
| 91 | + // back to free drive |
| 92 | + mapboxNavigation.setNavigationRoutes(emptyList()) |
| 93 | + mapboxNavigation.routesUpdates() |
| 94 | + .first { it.reason == RoutesExtra.ROUTES_UPDATE_REASON_CLEAN_UP } |
| 95 | + assertEquals(RoutesPreviewExtra.PREVIEW_CLEAN_UP, currentRoutesPreview?.reason) |
| 96 | + assertNull(currentRoutesPreview!!.routesPreview) |
| 97 | + assertEquals(RoutesExtra.ROUTES_UPDATE_REASON_CLEAN_UP, currentRoutes!!.reason) |
| 98 | + assertEquals(emptyList<NavigationRoute>(), currentRoutes!!.navigationRoutes) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun route_preview_in_parallel_to_active_guidance() = sdkTest { |
| 103 | + var currentRoutesPreview: RoutesPreviewUpdate? = null |
| 104 | + mapboxNavigation.registerRoutesPreviewObserver { update -> |
| 105 | + currentRoutesPreview = update |
| 106 | + } |
| 107 | + var currentRoutes: RoutesUpdatedResult? = null |
| 108 | + mapboxNavigation.registerRoutesObserver { update -> |
| 109 | + currentRoutes = update |
| 110 | + } |
| 111 | + // initial free drive |
| 112 | + mapboxNavigation.startTripSession() |
| 113 | + assertNull(currentRoutesPreview) |
| 114 | + assertNull(currentRoutes) |
| 115 | + // set routes preview |
| 116 | + val initialRoutes = RoutesProvider.dc_very_short(activity).toNavigationRoutes() |
| 117 | + mapboxNavigation.setRoutesPreview(initialRoutes) |
| 118 | + mapboxNavigation.routesPreviewUpdates() |
| 119 | + .first { it.reason == RoutesPreviewExtra.PREVIEW_NEW } |
| 120 | + // start active guidance |
| 121 | + mapboxNavigation.setNavigationRoutes(currentRoutesPreview!!.routesPreview!!.routesList) |
| 122 | + mapboxNavigation.setRoutesPreview(emptyList()) |
| 123 | + mapboxNavigation.routeProgressUpdates() |
| 124 | + .first { it.currentState == RouteProgressState.TRACKING } |
| 125 | + // preview a different route not leaving action guidance |
| 126 | + val updatedRoutes = RoutesProvider.dc_very_short_two_legs(activity).toNavigationRoutes() |
| 127 | + mapboxNavigation.setRoutesPreview(updatedRoutes) |
| 128 | + mapboxNavigation.routesPreviewUpdates() |
| 129 | + .first { it.routesPreview?.routesList == updatedRoutes } |
| 130 | + assertEquals( |
| 131 | + "active guidance should track initial routes", |
| 132 | + initialRoutes, |
| 133 | + currentRoutes?.navigationRoutes |
| 134 | + ) |
| 135 | + // user decided to switch to previewed routes |
| 136 | + mapboxNavigation.setNavigationRoutes(currentRoutesPreview!!.routesPreview!!.routesList) |
| 137 | + mapboxNavigation.setRoutesPreview(emptyList()) |
| 138 | + mapboxNavigation.routeProgressUpdates().first { |
| 139 | + it.navigationRoute == updatedRoutes[0] |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun start_active_guidance_from_previewed_alternative_route() = sdkTest { |
| 145 | + // set routes preview |
| 146 | + val routes = RoutesProvider.dc_short_with_alternative(activity).toNavigationRoutes() |
| 147 | + mapboxNavigation.setRoutesPreview(routes) |
| 148 | + val preview = mapboxNavigation.routesPreviewUpdates() |
| 149 | + .first { it.reason == RoutesPreviewExtra.PREVIEW_NEW } |
| 150 | + // switch to alternative route |
| 151 | + mapboxNavigation.changeRoutesPreviewPrimaryRoute( |
| 152 | + preview.routesPreview!!.originalRoutesList[1] |
| 153 | + ) |
| 154 | + val updatedPreview = mapboxNavigation.routesPreviewUpdates() |
| 155 | + .first { it != preview } |
| 156 | + // start active guidance |
| 157 | + mapboxNavigation.setNavigationRoutes(updatedPreview.routesPreview!!.routesList) |
| 158 | + mapboxNavigation.setRoutesPreview(emptyList()) |
| 159 | + val routesUpdate = mapboxNavigation.routesUpdates() |
| 160 | + .first { it.reason == RoutesExtra.ROUTES_UPDATE_REASON_NEW } |
| 161 | + assertEquals( |
| 162 | + listOf( |
| 163 | + routes[1], |
| 164 | + routes[0] |
| 165 | + ), |
| 166 | + routesUpdate.navigationRoutes |
| 167 | + ) |
| 168 | + val previewAlternativeMetadata = updatedPreview.routesPreview!!.alternativesMetadata.first() |
| 169 | + val activeGuidanceAlternativeMetadata = mapboxNavigation |
| 170 | + .getAlternativeMetadataFor(routes[0])!! |
| 171 | + assertEquals( |
| 172 | + 0, |
| 173 | + previewAlternativeMetadata.alternativeId |
| 174 | + ) |
| 175 | + assertNotEquals( |
| 176 | + previewAlternativeMetadata.alternativeId, |
| 177 | + activeGuidanceAlternativeMetadata.alternativeId |
| 178 | + ) |
| 179 | + assertEquals( |
| 180 | + previewAlternativeMetadata.infoFromStartOfPrimary, |
| 181 | + activeGuidanceAlternativeMetadata.infoFromStartOfPrimary |
| 182 | + ) |
| 183 | + assertEquals( |
| 184 | + previewAlternativeMetadata.forkIntersectionOfAlternativeRoute, |
| 185 | + activeGuidanceAlternativeMetadata.forkIntersectionOfAlternativeRoute |
| 186 | + ) |
| 187 | + assertEquals( |
| 188 | + previewAlternativeMetadata.infoFromFork, |
| 189 | + activeGuidanceAlternativeMetadata.infoFromFork |
| 190 | + ) |
| 191 | + assertEquals( |
| 192 | + previewAlternativeMetadata.forkIntersectionOfPrimaryRoute, |
| 193 | + activeGuidanceAlternativeMetadata.forkIntersectionOfPrimaryRoute |
| 194 | + ) |
| 195 | + } |
| 196 | +} |
0 commit comments