diff --git a/Makefile b/Makefile index f0db492375d..b329ff6af78 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,13 @@ check-kotlin-lint: && $(call run-gradle-tasks,$(ANDROIDAUTO_MODULES),ktlint) \ && $(call run-gradle-tasks,$(APPLICATION_MODULES),ktlint) +.PHONY: format-kotlin-lint +format-kotlin-lint: + $(call run-gradle-tasks,$(CORE_MODULES),ktlintFormat) \ + && $(call run-gradle-tasks,$(UI_MODULES),ktlintFormat) \ + && $(call run-gradle-tasks,$(ANDROIDAUTO_MODULES),ktlintFormat) \ + && $(call run-gradle-tasks,$(APPLICATION_MODULES),ktlintFormat) + .PHONY: check-android-lint check-android-lint: $(call run-gradle-tasks,$(CORE_MODULES),lint) \ diff --git a/changelog/unreleased/bugfixes/0000.md b/changelog/unreleased/bugfixes/0000.md new file mode 100644 index 00000000000..52080a8ba1d --- /dev/null +++ b/changelog/unreleased/bugfixes/0000.md @@ -0,0 +1 @@ +- Fixed an issue where native memory was not being properly released after the `MapboxNavigation` object was destroyed. \ No newline at end of file diff --git a/changelog/unreleased/features/0000.md b/changelog/unreleased/features/0000.md new file mode 100644 index 00000000000..62069bef3c8 --- /dev/null +++ b/changelog/unreleased/features/0000.md @@ -0,0 +1 @@ +- The `PredictiveCacheController(PredictiveCacheOptions)` constructor is now deprecated. Use `PredictiveCacheController(MapboxNavigation, PredictiveCacheOptions)` instead. \ No newline at end of file diff --git a/libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/ArtificialDriverTest.kt b/libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/ArtificialDriverTest.kt index e38c8450704..d009305edec 100644 --- a/libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/ArtificialDriverTest.kt +++ b/libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/ArtificialDriverTest.kt @@ -13,7 +13,6 @@ import com.mapbox.navigation.core.replay.history.mapToLocation import com.mapbox.navigation.core.replay.route.ReplayRouteMapper import com.mapbox.navigation.core.test.R import com.mapbox.navigation.navigator.internal.MapboxNativeNavigator -import com.mapbox.navigation.navigator.internal.MapboxNativeNavigatorImpl import com.mapbox.navigator.NavigationStatus import com.mapbox.navigator.NavigationStatusOrigin import com.mapbox.navigator.NavigatorObserver @@ -43,19 +42,20 @@ class ArtificialDriverTest { @Ignore("test sometimes fails because of https://mapbox.atlassian.net/browse/NN-418") fun nativeNavigatorFollowsArtificialDriverWithoutReroutes() = runBlocking(Dispatchers.Main) { - withNavigators { mapboxNavigation, nativeNavigator -> + withNavigators { mapboxNavigation -> mapboxNavigation.historyRecorder.startRecording() val testRoute = getTestRoute() val events = createArtificialLocationUpdates(testRoute) - val setRoutesResult = - nativeNavigator.setRoutes(testRoute, reason = SetRoutesReason.NEW_ROUTE) + val setRoutesResult = mapboxNavigation.navigator + .setRoutes(testRoute, reason = SetRoutesReason.NEW_ROUTE) assertTrue("result is $setRoutesResult", setRoutesResult.isValue) val statusesTracking = async> { - nativeNavigator.collectStatuses(untilRouteState = RouteState.COMPLETE) + mapboxNavigation.navigator + .collectStatuses(untilRouteState = RouteState.COMPLETE) } for (location in events.map { it.location.mapToLocation() }) { - assertTrue(nativeNavigator.updateLocation(location.toFixLocation())) + assertTrue(mapboxNavigation.navigator.updateLocation(location.toFixLocation())) } val states = statusesTracking.await() @@ -113,7 +113,7 @@ fun MapboxNativeNavigator.statusUpdates(): Flow { } private suspend fun withNavigators( - block: suspend (MapboxNavigation, MapboxNativeNavigator) -> Unit + block: suspend (MapboxNavigation) -> Unit ) { val context = InstrumentationRegistry.getInstrumentation().targetContext val mapboxNavigation = MapboxNavigationProvider.create( @@ -122,7 +122,7 @@ private suspend fun withNavigators( .build() ) try { - block(mapboxNavigation, MapboxNativeNavigatorImpl) + block(mapboxNavigation) } finally { mapboxNavigation.onDestroy() } diff --git a/libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/NativeNavigatorCallbackOrderTest.kt b/libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/NativeNavigatorCallbackOrderTest.kt index 61cf2216b50..f9e4ccbddf4 100644 --- a/libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/NativeNavigatorCallbackOrderTest.kt +++ b/libnavigation-core/src/androidTest/java/com/mapbox/navigation/core/trip/service/NativeNavigatorCallbackOrderTest.kt @@ -16,7 +16,6 @@ import com.mapbox.navigation.core.MapboxNavigation import com.mapbox.navigation.core.MapboxNavigationProvider import com.mapbox.navigation.core.test.R import com.mapbox.navigation.core.tests.activity.TripServiceActivity -import com.mapbox.navigation.navigator.internal.MapboxNativeNavigatorImpl import com.mapbox.navigation.testing.ui.BaseTest import com.mapbox.navigation.testing.ui.utils.runOnMainSync import com.mapbox.navigator.FixLocation @@ -72,11 +71,7 @@ internal class NativeNavigatorCallbackOrderTest : ) // starts raw location updates - otherwise we don't get onStatus calls mapboxNavigation.startTripSession() - val nativeNavigatorField = mapboxNavigation.javaClass.getDeclaredField("navigator") - nativeNavigatorField.isAccessible = true - val nativeNavigatorImpl = - nativeNavigatorField.get(mapboxNavigation) as MapboxNativeNavigatorImpl - nativeNavigatorField.isAccessible = false + val nativeNavigatorImpl = mapboxNavigation.navigator val navigatorField = nativeNavigatorImpl.javaClass.getDeclaredField("navigator") navigatorField.isAccessible = true navigator = navigatorField.get(nativeNavigatorImpl) as Navigator diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/MapboxNavigation.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/MapboxNavigation.kt index ef8e5e3df4b..f6a2ea375ad 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/MapboxNavigation.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/MapboxNavigation.kt @@ -266,7 +266,6 @@ class MapboxNavigation @VisibleForTesting internal constructor( private val mainJobController = threadController.getMainScopeAndRootJob() private val directionsSession: DirectionsSession - private var navigator: MapboxNativeNavigator private var historyRecorderHandles: NavigatorLoader.HistoryRecorderHandles private val tripService: TripService private val tripSession: TripSession @@ -348,6 +347,11 @@ class MapboxNavigation @VisibleForTesting internal constructor( private var rerouteController: InternalRerouteController? private val defaultRerouteController: InternalRerouteController + private var _navigator: MapboxNativeNavigator? + + internal val navigator: MapboxNativeNavigator + get() = _navigator ?: error("MapboxNavigation is destroyed") + /** * [NavigationVersionSwitchObserver] is notified when navigation switches tiles version. */ @@ -492,7 +496,7 @@ class MapboxNavigation @VisibleForTesting internal constructor( is NavigationRouter -> LegacyNavigationRouterAdapter(result) else -> LegacyNavigationRouterAdapter(LegacyRouterAdapter(result)) } - navigator = NavigationComponentProvider.createNativeNavigator( + _navigator = NavigationComponentProvider.createNativeNavigator( cacheHandle, config, historyRecorderHandles.composite, @@ -1299,6 +1303,8 @@ class MapboxNavigation @VisibleForTesting internal constructor( } resetAdasisMessageObserver() + _navigator = null + isDestroyed = true hasInstance = false } diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/NavigationComponentProvider.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/NavigationComponentProvider.kt index fed76a39c49..4dc6b727777 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/NavigationComponentProvider.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/NavigationComponentProvider.kt @@ -43,7 +43,7 @@ internal object NavigationComponentProvider { historyRecorderComposite: HistoryRecorderHandle?, accessToken: String, router: RouterInterface?, - ): MapboxNativeNavigator = MapboxNativeNavigatorImpl.create( + ): MapboxNativeNavigator = MapboxNativeNavigatorImpl( cacheHandle, config, historyRecorderComposite, diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/internal/PredictiveCache.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/internal/PredictiveCache.kt index 707e6725396..b3491ba2a0e 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/internal/PredictiveCache.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/internal/PredictiveCache.kt @@ -3,10 +3,10 @@ package com.mapbox.navigation.core.internal import com.mapbox.common.TileStore import com.mapbox.common.TilesetDescriptor import com.mapbox.navigation.base.options.PredictiveCacheLocationOptions -import com.mapbox.navigation.navigator.internal.MapboxNativeNavigatorImpl +import com.mapbox.navigation.core.MapboxNavigation import com.mapbox.navigator.PredictiveCacheController -object PredictiveCache { +class PredictiveCache(private val mapboxNavigation: MapboxNavigation) { internal val cachedNavigationPredictiveCacheControllers = mutableSetOf() @@ -22,9 +22,9 @@ object PredictiveCache { internal val mapsPredictiveCacheLocationOptionsTileVariant = mutableMapOf>>() - fun init() { + init { // recreate controllers with the same options but with a new navigator instance - MapboxNativeNavigatorImpl.setNativeNavigatorRecreationObserver { + mapboxNavigation.navigator.setNativeNavigatorRecreationObserver { val navOptions = navPredictiveCacheLocationOptions.toSet() val mapsOptions = mapsPredictiveCacheLocationOptions.toMap() val mapsOptionsTileVariant = mapsPredictiveCacheLocationOptionsTileVariant.toMap() @@ -61,7 +61,7 @@ object PredictiveCache { ) { navPredictiveCacheLocationOptions.add(predictiveCacheLocationOptions) val predictiveCacheController = - MapboxNativeNavigatorImpl.createNavigationPredictiveCacheController( + mapboxNavigation.navigator.createNavigationPredictiveCacheController( predictiveCacheLocationOptions ) cachedNavigationPredictiveCacheControllers.add(predictiveCacheController) @@ -77,7 +77,7 @@ object PredictiveCache { predictiveCacheLocationOptions: PredictiveCacheLocationOptions ) { val predictiveCacheController = - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( + mapboxNavigation.navigator.createMapsPredictiveCacheControllerTileVariant( tileStore, tileVariant, predictiveCacheLocationOptions @@ -100,7 +100,7 @@ object PredictiveCache { descriptorsAndOptions: List> ) { val descriptorsToPredictiveCacheControllers = descriptorsAndOptions.map { - it.first to MapboxNativeNavigatorImpl.createMapsPredictiveCacheController( + it.first to mapboxNavigation.navigator.createMapsPredictiveCacheController( tileStore, it.first, it.second diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/MapboxTripSession.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/MapboxTripSession.kt index fc0fb85b572..524772cd173 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/MapboxTripSession.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/MapboxTripSession.kt @@ -29,7 +29,6 @@ import com.mapbox.navigation.core.trip.service.TripService import com.mapbox.navigation.core.trip.session.eh.EHorizonObserver import com.mapbox.navigation.core.trip.session.eh.EHorizonSubscriptionManager import com.mapbox.navigation.navigator.internal.MapboxNativeNavigator -import com.mapbox.navigation.navigator.internal.MapboxNativeNavigatorImpl import com.mapbox.navigation.navigator.internal.utils.calculateRemainingWaypoints import com.mapbox.navigation.navigator.internal.utils.getCurrentLegDestination import com.mapbox.navigation.utils.internal.JobControl @@ -64,7 +63,7 @@ import java.util.concurrent.CopyOnWriteArraySet internal class MapboxTripSession( override val tripService: TripService, private val tripSessionLocationEngine: TripSessionLocationEngine, - private val navigator: MapboxNativeNavigator = MapboxNativeNavigatorImpl, + private val navigator: MapboxNativeNavigator, private val threadController: ThreadController, private val eHorizonSubscriptionManager: EHorizonSubscriptionManager ) : TripSession { diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/GraphAccessor.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/GraphAccessor.kt index 21d0a111bdb..0647f99f4d6 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/GraphAccessor.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/GraphAccessor.kt @@ -34,7 +34,7 @@ class GraphAccessor internal constructor( * @return list of Points representing edge shape */ fun getEdgeShape(edgeId: Long): List? { - return navigator.graphAccessor?.getEdgeShape(edgeId) + return navigator.graphAccessor.getEdgeShape(edgeId) } /** @@ -45,7 +45,7 @@ class GraphAccessor internal constructor( * @return EHorizonEdgeMetadata */ fun getEdgeMetadata(edgeId: Long): EHorizonEdgeMetadata? { - return navigator.graphAccessor?.getEdgeMetadata(edgeId)?.let { + return navigator.graphAccessor.getEdgeMetadata(edgeId)?.let { EHorizonFactory.buildEHorizonEdgeMetadata(it) } } @@ -55,7 +55,7 @@ class GraphAccessor internal constructor( * If any of path edges is not accessible, returns null. */ fun getPathShape(graphPath: EHorizonGraphPath): List? { - return navigator.graphAccessor?.getPathShape( + return navigator.graphAccessor.getPathShape( EHorizonFactory.buildNativeGraphPath(graphPath) ) } @@ -65,7 +65,7 @@ class GraphAccessor internal constructor( * If position's edge is not accessible, returns null. */ fun getGraphPositionCoordinate(graphPosition: EHorizonGraphPosition): Point? { - return navigator.graphAccessor?.getPositionCoordinate( + return navigator.graphAccessor.getPositionCoordinate( EHorizonFactory.buildNativeGraphPosition(graphPosition) ) } @@ -75,7 +75,7 @@ class GraphAccessor internal constructor( */ @ExperimentalPreviewMapboxNavigationAPI fun getAdasisEdgeAttributes(edgeId: Long): AdasEdgeAttributes? { - return navigator.graphAccessor?.getAdasAttributes(edgeId)?.let { + return navigator.graphAccessor.getAdasAttributes(edgeId)?.let { AdasEdgeAttributes.createFromNativeObject(it) } } diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/RoadObjectMatcher.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/RoadObjectMatcher.kt index 32ba8b198e7..6505eeecefb 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/RoadObjectMatcher.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/RoadObjectMatcher.kt @@ -36,7 +36,7 @@ class RoadObjectMatcher internal constructor( init { navigator.setNativeNavigatorRecreationObserver { if (roadObjectMatcherObservers.isNotEmpty()) { - navigator.roadObjectMatcher?.setListener(roadObjectMatcherListener) + navigator.roadObjectMatcher.setListener(roadObjectMatcherListener) } } } @@ -47,7 +47,7 @@ class RoadObjectMatcher internal constructor( */ fun registerRoadObjectMatcherObserver(roadObjectMatcherObserver: RoadObjectMatcherObserver) { if (roadObjectMatcherObservers.isEmpty()) { - navigator.roadObjectMatcher?.setListener(roadObjectMatcherListener) + navigator.roadObjectMatcher.setListener(roadObjectMatcherListener) } roadObjectMatcherObservers.add(roadObjectMatcherObserver) } @@ -110,7 +110,7 @@ class RoadObjectMatcher internal constructor( openLRLocation: String, @OpenLRStandard.Type openLRStandard: String ) { - navigator.roadObjectMatcher?.matchOpenLRs( + navigator.roadObjectMatcher.matchOpenLRs( listOf( EHorizonFactory.buildNativeMatchableOpenLr( MatchableOpenLr(roadObjectId, openLRLocation, openLRStandard) @@ -137,7 +137,7 @@ class RoadObjectMatcher internal constructor( matchableOpenLrs: List, useOnlyPreloadedTiles: Boolean = false ) { - navigator.roadObjectMatcher?.matchOpenLRs( + navigator.roadObjectMatcher.matchOpenLRs( matchableOpenLrs.map { EHorizonFactory.buildNativeMatchableOpenLr(it) }, @@ -167,7 +167,7 @@ class RoadObjectMatcher internal constructor( ) ) fun matchPolylineObject(roadObjectId: String, polyline: List) { - navigator.roadObjectMatcher?.matchPolylines( + navigator.roadObjectMatcher.matchPolylines( listOf( EHorizonFactory.buildNativeMatchableGeometry( MatchableGeometry(roadObjectId, polyline) @@ -199,7 +199,7 @@ class RoadObjectMatcher internal constructor( matchableGeometries: List, useOnlyPreloadedTiles: Boolean = false ) { - navigator.roadObjectMatcher?.matchPolylines( + navigator.roadObjectMatcher.matchPolylines( matchableGeometries.map { EHorizonFactory.buildNativeMatchableGeometry(it) }, @@ -229,7 +229,7 @@ class RoadObjectMatcher internal constructor( ) ) fun matchPolygonObject(roadObjectId: String, polygon: List) { - navigator.roadObjectMatcher?.matchPolygons( + navigator.roadObjectMatcher.matchPolygons( listOf( EHorizonFactory.buildNativeMatchableGeometry( MatchableGeometry(roadObjectId, polygon) @@ -261,7 +261,7 @@ class RoadObjectMatcher internal constructor( matchableGeometries: List, useOnlyPreloadedTiles: Boolean = false ) { - navigator.roadObjectMatcher?.matchPolygons( + navigator.roadObjectMatcher.matchPolygons( matchableGeometries.map { EHorizonFactory.buildNativeMatchableGeometry(it) }, @@ -291,7 +291,7 @@ class RoadObjectMatcher internal constructor( ) ) fun matchGantryObject(roadObjectId: String, gantry: List) { - navigator.roadObjectMatcher?.matchGantries( + navigator.roadObjectMatcher.matchGantries( listOf( EHorizonFactory.buildNativeMatchableGeometry( MatchableGeometry(roadObjectId, gantry) @@ -323,7 +323,7 @@ class RoadObjectMatcher internal constructor( matchableGeometries: List, useOnlyPreloadedTiles: Boolean = false ) { - navigator.roadObjectMatcher?.matchGantries( + navigator.roadObjectMatcher.matchGantries( matchableGeometries.map { EHorizonFactory.buildNativeMatchableGeometry(it) }, @@ -351,7 +351,7 @@ class RoadObjectMatcher internal constructor( ) ) fun matchPointObject(roadObjectId: String, point: Point) { - navigator.roadObjectMatcher?.matchPoints( + navigator.roadObjectMatcher.matchPoints( listOf( EHorizonFactory.buildNativeMatchablePoint( MatchablePoint(roadObjectId, point) @@ -381,7 +381,7 @@ class RoadObjectMatcher internal constructor( matchablePoints: List, useOnlyPreloadedTiles: Boolean = false ) { - navigator.roadObjectMatcher?.matchPoints( + navigator.roadObjectMatcher.matchPoints( matchablePoints.map { EHorizonFactory.buildNativeMatchablePoint(it) }, @@ -399,13 +399,13 @@ class RoadObjectMatcher internal constructor( * @param roadObjectIds list of object ids to cancel matching */ fun cancel(roadObjectIds: List) { - navigator.roadObjectMatcher?.cancel(roadObjectIds) + navigator.roadObjectMatcher.cancel(roadObjectIds) } /** * Cancel all road objects matching */ fun cancelAll() { - navigator.roadObjectMatcher?.cancelAll() + navigator.roadObjectMatcher.cancelAll() } } diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/RoadObjectsStore.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/RoadObjectsStore.kt index 8c42c0e7d2d..8f1dae35e32 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/RoadObjectsStore.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/eh/RoadObjectsStore.kt @@ -24,7 +24,7 @@ class RoadObjectsStore internal constructor( */ fun getRoadObjectsOnTheEdge(edgeId: Long): Map { val roadObjects = mutableMapOf() - navigator.roadObjectsStore?.get(edgeId)?.forEach { (objectId, objectEdgeLocation) -> + navigator.roadObjectsStore.get(edgeId).forEach { (objectId, objectEdgeLocation) -> roadObjects[objectId] = EHorizonFactory.buildRoadObjectEdgeLocation(objectEdgeLocation) } @@ -37,7 +37,7 @@ class RoadObjectsStore internal constructor( * @param roadObjectId id of the road object */ fun getRoadObject(roadObjectId: String): RoadObject? { - return navigator.roadObjectsStore?.getRoadObject(roadObjectId)?.let { + return navigator.roadObjectsStore.getRoadObject(roadObjectId)?.let { RoadObjectFactory.buildRoadObject(it) } } @@ -49,7 +49,7 @@ class RoadObjectsStore internal constructor( * @return list of road object ids */ fun getRoadObjectIdsByEdgeIds(edgeIds: List): List { - return navigator.roadObjectsStore?.getRoadObjectIdsByEdgeIds(edgeIds) ?: emptyList() + return navigator.roadObjectsStore.getRoadObjectIdsByEdgeIds(edgeIds) } /** @@ -59,7 +59,7 @@ class RoadObjectsStore internal constructor( */ fun addCustomRoadObject(roadObject: RoadObject) { val nativeRoadObject = RoadObjectFactory.buildNativeRoadObject(roadObject) - navigator.roadObjectsStore?.addCustomRoadObject(nativeRoadObject) + navigator.roadObjectsStore.addCustomRoadObject(nativeRoadObject) } /** @@ -67,14 +67,14 @@ class RoadObjectsStore internal constructor( * @param roadObjectId id of the road object */ fun removeCustomRoadObject(roadObjectId: String) { - navigator.roadObjectsStore?.removeCustomRoadObject(roadObjectId) + navigator.roadObjectsStore.removeCustomRoadObject(roadObjectId) } /** * Removes all custom road objects (i.e. stops tracking them in electronic horizon) */ fun removeAllCustomRoadObjects() { - navigator.roadObjectsStore?.removeAllCustomRoadObjects() + navigator.roadObjectsStore.removeAllCustomRoadObjects() } /** diff --git a/libnavigation-core/src/test/java/com/mapbox/navigation/core/MapboxNavigationBaseTest.kt b/libnavigation-core/src/test/java/com/mapbox/navigation/core/MapboxNavigationBaseTest.kt index d8e91c02961..755e225f420 100644 --- a/libnavigation-core/src/test/java/com/mapbox/navigation/core/MapboxNavigationBaseTest.kt +++ b/libnavigation-core/src/test/java/com/mapbox/navigation/core/MapboxNavigationBaseTest.kt @@ -230,15 +230,6 @@ internal open class MapboxNavigationBaseTest { } returns routesPreviewController every { NavigationComponentProvider.createRoutesCacheClearer() } returns routesCacheClearer - every { - navigator.create( - any(), - any(), - any(), - any(), - any(), - ) - } returns navigator mockkObject(TelemetryUtilsDelegate) every { TelemetryUtilsDelegate.getEventsCollectionState() } returns true } diff --git a/libnavigation-core/src/test/java/com/mapbox/navigation/core/internal/cache/PredictiveCacheTests.kt b/libnavigation-core/src/test/java/com/mapbox/navigation/core/internal/cache/PredictiveCacheTests.kt index 4bfb9950a97..1297819b1b6 100644 --- a/libnavigation-core/src/test/java/com/mapbox/navigation/core/internal/cache/PredictiveCacheTests.kt +++ b/libnavigation-core/src/test/java/com/mapbox/navigation/core/internal/cache/PredictiveCacheTests.kt @@ -3,18 +3,16 @@ package com.mapbox.navigation.core.internal.cache import com.mapbox.common.TileStore import com.mapbox.common.TilesetDescriptor import com.mapbox.navigation.base.options.PredictiveCacheLocationOptions +import com.mapbox.navigation.core.MapboxNavigation import com.mapbox.navigation.core.internal.PredictiveCache -import com.mapbox.navigation.navigator.internal.MapboxNativeNavigatorImpl +import com.mapbox.navigation.navigator.internal.MapboxNativeNavigator import com.mapbox.navigation.navigator.internal.NativeNavigatorRecreationObserver import io.mockk.Runs import io.mockk.every import io.mockk.just import io.mockk.mockk -import io.mockk.mockkObject import io.mockk.slot -import io.mockk.unmockkObject import io.mockk.verify -import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Before @@ -25,37 +23,20 @@ class PredictiveCacheTests { private val tileStore: TileStore = mockk() private val navigatorRecreationCallbackSlot = slot() + private val navigator: MapboxNativeNavigator = mockk(relaxed = true) + private lateinit var predictiveCache: PredictiveCache + @Before fun setUp() { - mockkObject(MapboxNativeNavigatorImpl) - - every { - MapboxNativeNavigatorImpl.createNavigationPredictiveCacheController(any()) - } returns mockk() - - every { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( - any(), - any(), - any() - ) - } returns mockk() - every { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheController(any(), any(), any()) - } returns mockk() - every { - MapboxNativeNavigatorImpl.setNativeNavigatorRecreationObserver( - capture(navigatorRecreationCallbackSlot) + navigator.setNativeNavigatorRecreationObserver( + capture(navigatorRecreationCallbackSlot), ) } just Runs - PredictiveCache.clean() - } - - @After - fun cleanUp() { - unmockkObject(MapboxNativeNavigatorImpl) + val mapboxNavigation = mockk(relaxed = true) + every { mapboxNavigation.navigator } returns navigator + predictiveCache = PredictiveCache(mapboxNavigation) } @Test @@ -63,19 +44,19 @@ class PredictiveCacheTests { val map1 = mockk() val map2 = mockk() - PredictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_1, mockk()) - PredictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_2, mockk()) - PredictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_3, mockk()) + predictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_1, mockk()) + predictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_2, mockk()) + predictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_3, mockk()) - PredictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_4, mockk()) - PredictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_5, mockk()) - PredictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_6, mockk()) - PredictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_7, mockk()) + predictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_4, mockk()) + predictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_5, mockk()) + predictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_6, mockk()) + predictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_7, mockk()) - assertEquals(3, PredictiveCache.currentMapsPredictiveCacheControllers(map1).size) - assertEquals(4, PredictiveCache.currentMapsPredictiveCacheControllers(map2).size) - assertNull(PredictiveCache.cachedMapsPredictiveCacheControllers[map1]) - assertNull(PredictiveCache.cachedMapsPredictiveCacheControllers[map2]) + assertEquals(3, predictiveCache.currentMapsPredictiveCacheControllers(map1).size) + assertEquals(4, predictiveCache.currentMapsPredictiveCacheControllers(map2).size) + assertNull(predictiveCache.cachedMapsPredictiveCacheControllers[map1]) + assertNull(predictiveCache.cachedMapsPredictiveCacheControllers[map2]) } @Test @@ -87,78 +68,78 @@ class PredictiveCacheTests { val tilesetDescriptor1 = mockk() val tilesetDescriptor2 = mockk() - PredictiveCache.createMapsControllers( + predictiveCache.createMapsControllers( map1, tileStore, listOf(tilesetDescriptor1 to options1, tilesetDescriptor2 to options2) ) - PredictiveCache.createMapsControllers( + predictiveCache.createMapsControllers( map2, tileStore, listOf(tilesetDescriptor2 to options2) ) - assertEquals(2, PredictiveCache.cachedMapsPredictiveCacheControllers[map1]!!.size) - assertEquals(1, PredictiveCache.cachedMapsPredictiveCacheControllers[map2]!!.size) - assertEquals(0, PredictiveCache.currentMapsPredictiveCacheControllers(map1).size) - assertEquals(0, PredictiveCache.currentMapsPredictiveCacheControllers(map2).size) - assertNull(PredictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map1]) - assertNull(PredictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map2]) - assertNull(PredictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map1]) - assertNull(PredictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map2]) + assertEquals(2, predictiveCache.cachedMapsPredictiveCacheControllers[map1]!!.size) + assertEquals(1, predictiveCache.cachedMapsPredictiveCacheControllers[map2]!!.size) + assertEquals(0, predictiveCache.currentMapsPredictiveCacheControllers(map1).size) + assertEquals(0, predictiveCache.currentMapsPredictiveCacheControllers(map2).size) + assertNull(predictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map1]) + assertNull(predictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map2]) + assertNull(predictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map1]) + assertNull(predictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map2]) - PredictiveCache.createMapsControllers( + predictiveCache.createMapsControllers( map1, tileStore, listOf(tilesetDescriptor2 to options2) ) - PredictiveCache.createMapsControllers( + predictiveCache.createMapsControllers( map2, tileStore, listOf(tilesetDescriptor1 to options1, tilesetDescriptor2 to options2) ) - assertEquals(1, PredictiveCache.cachedMapsPredictiveCacheControllers[map1]!!.size) - assertEquals(2, PredictiveCache.cachedMapsPredictiveCacheControllers[map2]!!.size) - assertEquals(0, PredictiveCache.currentMapsPredictiveCacheControllers(map1).size) - assertEquals(0, PredictiveCache.currentMapsPredictiveCacheControllers(map2).size) - assertNull(PredictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map1]) - assertNull(PredictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map2]) - assertNull(PredictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map1]) - assertNull(PredictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map2]) + assertEquals(1, predictiveCache.cachedMapsPredictiveCacheControllers[map1]!!.size) + assertEquals(2, predictiveCache.cachedMapsPredictiveCacheControllers[map2]!!.size) + assertEquals(0, predictiveCache.currentMapsPredictiveCacheControllers(map1).size) + assertEquals(0, predictiveCache.currentMapsPredictiveCacheControllers(map2).size) + assertNull(predictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map1]) + assertNull(predictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map2]) + assertNull(predictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map1]) + assertNull(predictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map2]) } @Test fun `size of deprecated map controllers is correct after removing`() { val map = mockk() - PredictiveCache.createMapsController(map, tileStore, TILE_VARIANT_1, mockk()) - PredictiveCache.createMapsController(map, tileStore, TILE_VARIANT_2, mockk()) - PredictiveCache.createMapsController(map, tileStore, TILE_VARIANT_3, mockk()) - PredictiveCache.createMapsController(map, tileStore, TILE_VARIANT_4, mockk()) + predictiveCache.createMapsController(map, tileStore, TILE_VARIANT_1, mockk()) + predictiveCache.createMapsController(map, tileStore, TILE_VARIANT_2, mockk()) + predictiveCache.createMapsController(map, tileStore, TILE_VARIANT_3, mockk()) + predictiveCache.createMapsController(map, tileStore, TILE_VARIANT_4, mockk()) - PredictiveCache.removeMapControllers(map, TILE_VARIANT_2) - PredictiveCache.removeMapControllers(map, TILE_VARIANT_1) - PredictiveCache.removeMapControllers(map, TILE_VARIANT_4) + predictiveCache.removeMapControllers(map, TILE_VARIANT_2) + predictiveCache.removeMapControllers(map, TILE_VARIANT_1) + predictiveCache.removeMapControllers(map, TILE_VARIANT_4) - assertEquals(1, PredictiveCache.currentMapsPredictiveCacheControllers(map).size) - assertEquals(1, PredictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map]?.size) - assertEquals(TILE_VARIANT_3, PredictiveCache.currentMapsPredictiveCacheControllers(map)[0]) + assertEquals(1, predictiveCache.currentMapsPredictiveCacheControllers(map).size) + assertEquals(1, predictiveCache.cachedMapsPredictiveCacheControllersTileVariant[map]?.size) + assertEquals(TILE_VARIANT_3, predictiveCache.currentMapsPredictiveCacheControllers(map)[0]) } @Test fun `deprecated map controllers are empty after removing all`() { val map = mockk() - PredictiveCache.createMapsController(map, tileStore, TILE_VARIANT_1, mockk()) - PredictiveCache.createMapsController(map, tileStore, TILE_VARIANT_2, mockk()) - PredictiveCache.createMapsController(map, tileStore, TILE_VARIANT_3, mockk()) + predictiveCache.createMapsController(map, tileStore, TILE_VARIANT_1, mockk()) + predictiveCache.createMapsController(map, tileStore, TILE_VARIANT_2, mockk()) + predictiveCache.createMapsController(map, tileStore, TILE_VARIANT_3, mockk()) - PredictiveCache.removeAllMapControllersFromTileVariants(map) + predictiveCache.removeAllMapControllersFromTileVariants(map) - assertEquals(0, PredictiveCache.currentMapsPredictiveCacheControllers(map).size) - assertEquals(null, PredictiveCache.mapsPredictiveCacheLocationOptions[map]) - assertEquals(null, PredictiveCache.cachedMapsPredictiveCacheControllers[map]) + assertEquals(0, predictiveCache.currentMapsPredictiveCacheControllers(map).size) + assertEquals(null, predictiveCache.mapsPredictiveCacheLocationOptions[map]) + assertEquals(null, predictiveCache.cachedMapsPredictiveCacheControllers[map]) } @Test @@ -170,17 +151,17 @@ class PredictiveCacheTests { val tilesetDescriptor1 = mockk() val tilesetDescriptor2 = mockk() - PredictiveCache.createMapsControllers( + predictiveCache.createMapsControllers( map, tileStore, listOf(tilesetDescriptor1 to options1, tilesetDescriptor2 to options2) ) - PredictiveCache.removeAllMapControllersFromDescriptors(map) + predictiveCache.removeAllMapControllersFromDescriptors(map) - assertEquals(0, PredictiveCache.currentMapsPredictiveCacheControllers(map).size) - assertEquals(null, PredictiveCache.mapsPredictiveCacheLocationOptions[map]) - assertEquals(null, PredictiveCache.cachedMapsPredictiveCacheControllers[map]) + assertEquals(0, predictiveCache.currentMapsPredictiveCacheControllers(map).size) + assertEquals(null, predictiveCache.mapsPredictiveCacheLocationOptions[map]) + assertEquals(null, predictiveCache.cachedMapsPredictiveCacheControllers[map]) } @Test @@ -192,33 +173,19 @@ class PredictiveCacheTests { val tilesetDescriptor2 = mockk() val tilesetDescriptor3 = mockk() - every { - MapboxNativeNavigatorImpl.createNavigationPredictiveCacheController(navLocationOptions1) - } returns mockk() - - every { - MapboxNativeNavigatorImpl.createNavigationPredictiveCacheController(navLocationOptions2) - } returns mockk() - - every { - MapboxNativeNavigatorImpl.createNavigationPredictiveCacheController(navLocationOptions3) - } returns mockk() - val map1 = mockk() val map2 = mockk() - PredictiveCache.init() - - PredictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_1, mockk()) - PredictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_2, mockk()) - PredictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_3, mockk()) + predictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_1, mockk()) + predictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_2, mockk()) + predictiveCache.createMapsController(map1, tileStore, TILE_VARIANT_3, mockk()) - PredictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_4, mockk()) - PredictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_5, mockk()) - PredictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_6, mockk()) - PredictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_7, mockk()) + predictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_4, mockk()) + predictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_5, mockk()) + predictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_6, mockk()) + predictiveCache.createMapsController(map2, tileStore, TILE_VARIANT_7, mockk()) - PredictiveCache.createMapsControllers( + predictiveCache.createMapsControllers( map1, tileStore, listOf( @@ -226,36 +193,36 @@ class PredictiveCacheTests { tilesetDescriptor2 to navLocationOptions2 ) ) - PredictiveCache.createMapsControllers( + predictiveCache.createMapsControllers( map2, tileStore, listOf(tilesetDescriptor3 to navLocationOptions3) ) - PredictiveCache.createNavigationController(navLocationOptions1) - PredictiveCache.createNavigationController(navLocationOptions2) + predictiveCache.createNavigationController(navLocationOptions1) + predictiveCache.createNavigationController(navLocationOptions2) val callback = navigatorRecreationCallbackSlot.captured callback.onNativeNavigatorRecreated() - assertEquals(2, PredictiveCache.cachedMapsPredictiveCacheControllersTileVariant.size) - assertEquals(3, PredictiveCache.currentMapsPredictiveCacheControllers(map1).size) - assertEquals(4, PredictiveCache.currentMapsPredictiveCacheControllers(map2).size) + assertEquals(2, predictiveCache.cachedMapsPredictiveCacheControllersTileVariant.size) + assertEquals(3, predictiveCache.currentMapsPredictiveCacheControllers(map1).size) + assertEquals(4, predictiveCache.currentMapsPredictiveCacheControllers(map2).size) - assertEquals(2, PredictiveCache.mapsPredictiveCacheLocationOptionsTileVariant.size) - assertEquals(3, PredictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map1]?.size) - assertEquals(4, PredictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map2]?.size) + assertEquals(2, predictiveCache.mapsPredictiveCacheLocationOptionsTileVariant.size) + assertEquals(3, predictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map1]?.size) + assertEquals(4, predictiveCache.mapsPredictiveCacheLocationOptionsTileVariant[map2]?.size) - assertEquals(2, PredictiveCache.cachedMapsPredictiveCacheControllers.size) - assertEquals(2, PredictiveCache.cachedMapsPredictiveCacheControllers[map1]!!.size) - assertEquals(2, PredictiveCache.mapsPredictiveCacheLocationOptions.size) - assertEquals(1, PredictiveCache.cachedMapsPredictiveCacheControllers[map2]!!.size) + assertEquals(2, predictiveCache.cachedMapsPredictiveCacheControllers.size) + assertEquals(2, predictiveCache.cachedMapsPredictiveCacheControllers[map1]!!.size) + assertEquals(2, predictiveCache.mapsPredictiveCacheLocationOptions.size) + assertEquals(1, predictiveCache.cachedMapsPredictiveCacheControllers[map2]!!.size) - assertEquals(2, PredictiveCache.cachedNavigationPredictiveCacheControllers.size) - assertEquals(2, PredictiveCache.navPredictiveCacheLocationOptions.size) + assertEquals(2, predictiveCache.cachedNavigationPredictiveCacheControllers.size) + assertEquals(2, predictiveCache.navPredictiveCacheLocationOptions.size) verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( + navigator.createMapsPredictiveCacheControllerTileVariant( tileStore, TILE_VARIANT_1, any() @@ -263,7 +230,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( + navigator.createMapsPredictiveCacheControllerTileVariant( tileStore, TILE_VARIANT_2, any() @@ -271,7 +238,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( + navigator.createMapsPredictiveCacheControllerTileVariant( tileStore, TILE_VARIANT_3, any() @@ -279,7 +246,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( + navigator.createMapsPredictiveCacheControllerTileVariant( tileStore, TILE_VARIANT_4, any() @@ -287,7 +254,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( + navigator.createMapsPredictiveCacheControllerTileVariant( tileStore, TILE_VARIANT_5, any() @@ -295,7 +262,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( + navigator.createMapsPredictiveCacheControllerTileVariant( tileStore, TILE_VARIANT_6, any() @@ -303,7 +270,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheControllerTileVariant( + navigator.createMapsPredictiveCacheControllerTileVariant( tileStore, TILE_VARIANT_7, any() @@ -311,7 +278,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheController( + navigator.createMapsPredictiveCacheController( tileStore, tilesetDescriptor1, navLocationOptions1 @@ -319,7 +286,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheController( + navigator.createMapsPredictiveCacheController( tileStore, tilesetDescriptor2, navLocationOptions2 @@ -327,7 +294,7 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createMapsPredictiveCacheController( + navigator.createMapsPredictiveCacheController( tileStore, tilesetDescriptor3, navLocationOptions3 @@ -335,27 +302,27 @@ class PredictiveCacheTests { } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createNavigationPredictiveCacheController(navLocationOptions1) + navigator.createNavigationPredictiveCacheController(navLocationOptions1) } verify(exactly = 2) { - MapboxNativeNavigatorImpl.createNavigationPredictiveCacheController(navLocationOptions2) + navigator.createNavigationPredictiveCacheController(navLocationOptions2) } } @Test fun `caches are empty after clean`() { - PredictiveCache.createMapsController(mockk(), tileStore, TILE_VARIANT_1, mockk()) - PredictiveCache.createNavigationController(mockk()) + predictiveCache.createMapsController(mockk(), tileStore, TILE_VARIANT_1, mockk()) + predictiveCache.createNavigationController(mockk()) - PredictiveCache.clean() + predictiveCache.clean() - assertEquals(0, PredictiveCache.cachedNavigationPredictiveCacheControllers.size) - assertEquals(0, PredictiveCache.cachedMapsPredictiveCacheControllers.size) - assertEquals(0, PredictiveCache.cachedMapsPredictiveCacheControllersTileVariant.size) - assertEquals(0, PredictiveCache.navPredictiveCacheLocationOptions.size) - assertEquals(0, PredictiveCache.mapsPredictiveCacheLocationOptions.size) - assertEquals(0, PredictiveCache.mapsPredictiveCacheLocationOptionsTileVariant.size) + assertEquals(0, predictiveCache.cachedNavigationPredictiveCacheControllers.size) + assertEquals(0, predictiveCache.cachedMapsPredictiveCacheControllers.size) + assertEquals(0, predictiveCache.cachedMapsPredictiveCacheControllersTileVariant.size) + assertEquals(0, predictiveCache.navPredictiveCacheLocationOptions.size) + assertEquals(0, predictiveCache.mapsPredictiveCacheLocationOptions.size) + assertEquals(0, predictiveCache.mapsPredictiveCacheLocationOptionsTileVariant.size) } private companion object { diff --git a/libnavigation-core/src/test/java/com/mapbox/navigation/core/trip/session/MapboxTripSessionTest.kt b/libnavigation-core/src/test/java/com/mapbox/navigation/core/trip/session/MapboxTripSessionTest.kt index 50dbfbdaa77..1194665e249 100644 --- a/libnavigation-core/src/test/java/com/mapbox/navigation/core/trip/session/MapboxTripSessionTest.kt +++ b/libnavigation-core/src/test/java/com/mapbox/navigation/core/trip/session/MapboxTripSessionTest.kt @@ -28,7 +28,6 @@ import com.mapbox.navigation.core.trip.service.TripService import com.mapbox.navigation.core.trip.session.eh.EHorizonObserver import com.mapbox.navigation.core.trip.session.eh.EHorizonSubscriptionManager import com.mapbox.navigation.navigator.internal.MapboxNativeNavigator -import com.mapbox.navigation.navigator.internal.MapboxNativeNavigatorImpl import com.mapbox.navigation.navigator.internal.NativeNavigatorRecreationObserver import com.mapbox.navigation.navigator.internal.TripStatus import com.mapbox.navigation.navigator.internal.utils.calculateRemainingWaypoints @@ -150,7 +149,6 @@ class MapboxTripSessionTest { @Before fun setUp() { - mockkObject(MapboxNativeNavigatorImpl) mockkStatic("com.mapbox.navigation.core.navigator.NavigatorMapper") mockkStatic("com.mapbox.navigation.core.navigator.LocationEx") mockkStatic("com.mapbox.navigation.navigator.internal.utils.TripStatusEx") @@ -1879,7 +1877,6 @@ class MapboxTripSessionTest { @After fun cleanUp() { - unmockkObject(MapboxNativeNavigatorImpl) unmockkStatic("com.mapbox.navigation.core.navigator.NavigatorMapper") unmockkStatic("com.mapbox.navigation.core.navigator.LocationEx") unmockkStatic("com.mapbox.navigation.navigator.internal.utils.TripStatusEx") diff --git a/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigator.kt b/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigator.kt index f96fa8d624a..d1cde637814 100644 --- a/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigator.kt +++ b/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigator.kt @@ -37,17 +37,6 @@ import com.mapbox.navigator.UpdateExternalSensorDataCallback */ interface MapboxNativeNavigator { - /** - * Initialize the navigator with a device profile - */ - fun create( - cacheHandle: CacheHandle, - config: ConfigHandle, - historyRecorderComposite: HistoryRecorderHandle?, - accessToken: String, - router: RouterInterface?, - ): MapboxNativeNavigator - /** * Reinitialize the navigator with a device profile */ @@ -205,13 +194,13 @@ interface MapboxNativeNavigator { val routeAlternativesController: RouteAlternativesControllerInterface - val graphAccessor: GraphAccessor? + val graphAccessor: GraphAccessor - val roadObjectsStore: RoadObjectsStore? + val roadObjectsStore: RoadObjectsStore val cache: CacheHandle - val roadObjectMatcher: RoadObjectMatcher? + val roadObjectMatcher: RoadObjectMatcher val experimental: Experimental } diff --git a/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt b/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt index 171f5eac9b3..719c161dbf8 100644 --- a/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt +++ b/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt @@ -56,17 +56,18 @@ import kotlin.coroutines.resume /** * Default implementation of [MapboxNativeNavigator] interface. */ -object MapboxNativeNavigatorImpl : MapboxNativeNavigator { - - private const val LOG_CATEGORY = "MapboxNativeNavigatorImpl" - - // TODO: What should be the default value? Should we expose it publicly? - private const val MAX_NUMBER_TILES_LOAD_PARALLEL_REQUESTS = 2 - - private var navigator: Navigator? = null - override var graphAccessor: GraphAccessor? = null - override var roadObjectMatcher: RoadObjectMatcher? = null - override var roadObjectsStore: RoadObjectsStore? = null +class MapboxNativeNavigatorImpl( + cacheHandle: CacheHandle, + config: ConfigHandle, + historyRecorderComposite: HistoryRecorderHandle?, + accessToken: String, + router: RouterInterface?, +) : MapboxNativeNavigator { + + private lateinit var navigator: Navigator + override lateinit var graphAccessor: GraphAccessor + override lateinit var roadObjectMatcher: RoadObjectMatcher + override lateinit var roadObjectsStore: RoadObjectsStore override lateinit var experimental: Experimental override lateinit var cache: CacheHandle override lateinit var routeAlternativesController: RouteAlternativesControllerInterface @@ -74,21 +75,21 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { CopyOnWriteArraySet() private lateinit var accessToken: String - // Route following + init { + init(cacheHandle, config, historyRecorderComposite, accessToken, router) + } /** * Create or reset resources. This must be called before calling any * functions within [MapboxNativeNavigatorImpl] */ - override fun create( + private fun init( cacheHandle: CacheHandle, config: ConfigHandle, historyRecorderComposite: HistoryRecorderHandle?, accessToken: String, router: RouterInterface?, - ): MapboxNativeNavigator { - navigator?.shutdown() - + ) { val nativeComponents = NavigatorLoader.createNavigator( cacheHandle, config, @@ -103,7 +104,6 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { cache = nativeComponents.cache routeAlternativesController = nativeComponents.routeAlternativesController this.accessToken = accessToken - return this } /** @@ -116,26 +116,29 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { accessToken: String, router: RouterInterface ) { - val storeNavSessionState = navigator!!.storeNavigationSession() - create(cacheHandle, config, historyRecorderComposite, accessToken, router) - navigator!!.restoreNavigationSession(storeNavSessionState) + val storeNavSessionState = navigator.storeNavigationSession() + + navigator.shutdown() + + init(cacheHandle, config, historyRecorderComposite, accessToken, router) + navigator.restoreNavigationSession(storeNavSessionState) nativeNavigatorRecreationObservers.forEach { it.onNativeNavigatorRecreated() } } override suspend fun resetRideSession() = suspendCancellableCoroutine { - navigator!!.reset { + navigator.reset { it.resume(Unit) } } override fun startNavigationSession() { - navigator!!.startNavigationSession() + navigator.startNavigationSession() } override fun stopNavigationSession() { - navigator!!.stopNavigationSession() + navigator.stopNavigationSession() } /** @@ -147,16 +150,16 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { */ override suspend fun updateLocation(rawLocation: FixLocation): Boolean = suspendCancellableCoroutine { continuation -> - navigator!!.updateLocation(rawLocation) { + navigator.updateLocation(rawLocation) { continuation.resume(it) } } override fun addNavigatorObserver(navigatorObserver: NavigatorObserver) = - navigator!!.addObserver(navigatorObserver) + navigator.addObserver(navigatorObserver) override fun removeNavigatorObserver(navigatorObserver: NavigatorObserver) = - navigator!!.removeObserver(navigatorObserver) + navigator.removeObserver(navigatorObserver) // Routing @@ -166,7 +169,7 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { alternatives: List, reason: SetRoutesReason, ): Expected = suspendCancellableCoroutine { continuation -> - navigator!!.setRoutes( + navigator.setRoutes( primaryRoute?.let { route -> SetRoutesParams( route.nativeRoute(), @@ -190,7 +193,7 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { override suspend fun setAlternativeRoutes( routes: List ): List = suspendCancellableCoroutine { continuation -> - navigator!!.setAlternativeRoutes( + navigator.setAlternativeRoutes( routes.map { it.nativeRoute() } ) { result -> result.onError { @@ -279,7 +282,7 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { "with generated refresh response: $refreshResponseJson", LOG_CATEGORY ) - navigator!!.refreshRoute( + navigator.refreshRoute( refreshResponseJson, route.nativeRoute().routeId ) { callback(continuation, it) } @@ -297,7 +300,7 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { */ override suspend fun updateLegIndex(legIndex: Int): Boolean = suspendCancellableCoroutine { continuation -> - navigator!!.changeLeg(legIndex) { + navigator.changeLeg(legIndex) { continuation.resume(it) } } @@ -310,21 +313,21 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { * @param eHorizonObserver */ override fun setElectronicHorizonObserver(eHorizonObserver: ElectronicHorizonObserver?) { - navigator!!.setElectronicHorizonObserver(eHorizonObserver) + navigator.setElectronicHorizonObserver(eHorizonObserver) } override fun addRoadObjectsStoreObserver(roadObjectsStoreObserver: RoadObjectsStoreObserver) { - roadObjectsStore?.addObserver(roadObjectsStoreObserver) + roadObjectsStore.addObserver(roadObjectsStoreObserver) } override fun removeRoadObjectsStoreObserver( roadObjectsStoreObserver: RoadObjectsStoreObserver ) { - roadObjectsStore?.removeObserver(roadObjectsStoreObserver) + roadObjectsStore.removeObserver(roadObjectsStoreObserver) } override fun setFallbackVersionsObserver(fallbackVersionsObserver: FallbackVersionsObserver?) { - navigator!!.setFallbackVersionsObserver(fallbackVersionsObserver) + navigator.setFallbackVersionsObserver(fallbackVersionsObserver) } override fun setNativeNavigatorRecreationObserver( @@ -334,9 +337,9 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { } override fun unregisterAllObservers() { - navigator!!.setElectronicHorizonObserver(null) - navigator!!.setFallbackVersionsObserver(null) - roadObjectsStore?.removeAllCustomRoadObjects() + navigator.setElectronicHorizonObserver(null) + navigator.setFallbackVersionsObserver(null) + roadObjectsStore.removeAllCustomRoadObjects() nativeNavigatorRecreationObservers.clear() } @@ -359,7 +362,7 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { tileVariant: String, predictiveCacheLocationOptions: PredictiveCacheLocationOptions ): PredictiveCacheController = - navigator!!.createPredictiveCacheController( + navigator.createPredictiveCacheController( tileStore, createDefaultMapsPredictiveCacheControllerOptions(tileVariant), predictiveCacheLocationOptions.toPredictiveLocationTrackerOptions() @@ -379,7 +382,7 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { tilesetDescriptor: TilesetDescriptor, predictiveCacheLocationOptions: PredictiveCacheLocationOptions ): PredictiveCacheController = - navigator!!.createPredictiveCacheController( + navigator.createPredictiveCacheController( tileStore, listOf(tilesetDescriptor), predictiveCacheLocationOptions.toPredictiveLocationTrackerOptions() @@ -396,7 +399,7 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { override fun createNavigationPredictiveCacheController( predictiveCacheLocationOptions: PredictiveCacheLocationOptions ): PredictiveCacheController = - navigator!!.createPredictiveCacheController( + navigator.createPredictiveCacheController( predictiveCacheLocationOptions.toPredictiveLocationTrackerOptions() ) @@ -420,17 +423,24 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator { data: SensorData, callback: UpdateExternalSensorDataCallback, ) { - navigator!!.updateExternalSensorData(data, callback) + navigator.updateExternalSensorData(data, callback) } override fun setAdasisMessageCallback( callback: ADASISv2MessageCallback, adasisConfig: AdasisConfig, ) { - navigator!!.setAdasisMessageCallback(callback, adasisConfig) + navigator.setAdasisMessageCallback(callback, adasisConfig) } override fun resetAdasisMessageCallback() { - navigator!!.resetAdasisMessageCallback() + navigator.resetAdasisMessageCallback() + } + + private companion object { + const val LOG_CATEGORY = "MapboxNativeNavigatorImpl" + + // TODO: What should be the default value? Should we expose it publicly? + const val MAX_NUMBER_TILES_LOAD_PARALLEL_REQUESTS = 2 } } diff --git a/libnavui-maps/api/current.txt b/libnavui-maps/api/current.txt index 7c2da0e46fa..5b15b77133f 100644 --- a/libnavui-maps/api/current.txt +++ b/libnavui-maps/api/current.txt @@ -65,7 +65,8 @@ package com.mapbox.navigation.ui.maps { } @UiThread public final class PredictiveCacheController { - ctor public PredictiveCacheController(com.mapbox.navigation.base.options.PredictiveCacheOptions predictiveCacheOptions); + ctor @Deprecated public PredictiveCacheController(com.mapbox.navigation.base.options.PredictiveCacheOptions predictiveCacheOptions); + ctor public PredictiveCacheController(com.mapbox.navigation.core.MapboxNavigation mapboxNavigation, com.mapbox.navigation.base.options.PredictiveCacheOptions predictiveCacheOptions); ctor @Deprecated public PredictiveCacheController(com.mapbox.navigation.base.options.PredictiveCacheLocationOptions predictiveCacheLocationOptions = PredictiveCacheLocationOptions.().build(), com.mapbox.navigation.ui.maps.PredictiveCacheControllerErrorHandler? predictiveCacheControllerErrorHandler = null); ctor @Deprecated public PredictiveCacheController(com.mapbox.navigation.base.options.PredictiveCacheLocationOptions predictiveCacheLocationOptions = PredictiveCacheLocationOptions.().build(), com.mapbox.navigation.base.options.PredictiveCacheLocationOptions predictiveCacheGuidanceLocationOptions = PredictiveCacheLocationOptions.().build(), com.mapbox.navigation.ui.maps.PredictiveCacheControllerErrorHandler? predictiveCacheControllerErrorHandler = null); ctor @Deprecated public PredictiveCacheController(com.mapbox.navigation.base.options.PredictiveCacheLocationOptions predictiveCacheLocationOptions = PredictiveCacheLocationOptions.().build(), com.mapbox.navigation.base.options.PredictiveCacheLocationOptions predictiveCacheGuidanceLocationOptions = PredictiveCacheLocationOptions.().build()); diff --git a/libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/PredictiveCacheController.kt b/libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/PredictiveCacheController.kt index 889fa4db589..69620e918e2 100644 --- a/libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/PredictiveCacheController.kt +++ b/libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/PredictiveCacheController.kt @@ -1,6 +1,7 @@ package com.mapbox.navigation.ui.maps import androidx.annotation.UiThread +import androidx.annotation.VisibleForTesting import com.mapbox.bindgen.Expected import com.mapbox.bindgen.Value import com.mapbox.common.TileStore @@ -15,6 +16,7 @@ import com.mapbox.navigation.base.options.PredictiveCacheNavigationOptions import com.mapbox.navigation.base.options.PredictiveCacheOptions import com.mapbox.navigation.base.options.RoutingTilesOptions import com.mapbox.navigation.core.MapboxNavigation +import com.mapbox.navigation.core.MapboxNavigationProvider import com.mapbox.navigation.core.internal.PredictiveCache import com.mapbox.navigation.ui.maps.internal.offline.OfflineManagerProvider import com.mapbox.navigation.utils.internal.logE @@ -42,6 +44,11 @@ private const val RASTER_SOURCE_TYPE = "raster" * whenever the [MapView] is destroyed to avoid leaking references or downloading unnecessary * resources. When the map instance is recreated, set it back with [createMapControllers]. * + * Also, note that [MapboxNavigation] instance should be created before [PredictiveCacheController] + * and the lifecycle of the [MapboxNavigation] instance should be longer than that of the + * [PredictiveCacheController]. Specifically, [MapboxNavigation] should not be destroyed before + * [PredictiveCacheController.onDestroy] is called. + * * The map instance has to be configured with the same [TileStore] instance that was provided to [RoutingTilesOptions.tileStore]. * You need to call [TileStore.create] with a path and pass it to [ResourceOptions.tileStore] or use the Maps SDK's tile store path XML attribute. * @@ -59,8 +66,9 @@ private const val RASTER_SOURCE_TYPE = "raster" * @param predictiveCacheOptions [PredictiveCacheOptions] options to instantiate instance of [PredictiveCacheController] */ @UiThread -class PredictiveCacheController constructor( +class PredictiveCacheController @VisibleForTesting internal constructor( private val predictiveCacheOptions: PredictiveCacheOptions, + private val predictiveCache: PredictiveCache, ) { /** @@ -72,15 +80,56 @@ class PredictiveCacheController constructor( private var mapListeners = mutableMapOf() /** - * Constructor of [PredictiveCacheController] + * Constructs a new instance of the [PredictiveCacheController] using the provided + * [PredictiveCacheOptions]. Throws [IllegalStateException] if [MapboxNavigation] + * was not instantiated before. Use [PredictiveCacheController] constructor which explicitly + * accepts [MapboxNavigation] instance. + * + * @param predictiveCacheOptions [PredictiveCacheOptions] options to instantiate instance of [PredictiveCacheController] + * @throws IllegalStateException if [MapboxNavigation] was not instantiated before + */ + @Deprecated( + "This constructor is deprecated", + ReplaceWith("PredictiveCacheController(MapboxNavigation, PredictiveCacheOptions)"), + ) + constructor( + predictiveCacheOptions: PredictiveCacheOptions, + ) : this( + predictiveCacheOptions, + PredictiveCache(tryToRetrieveMapboxNavigation()), + ) + + /** + * Constructs a new instance of the [PredictiveCacheController] using the provided + * [MapboxNavigation] and [PredictiveCacheOptions]. + * + * **Note:** The lifecycle of the [MapboxNavigation] instance should be longer than that of the + * [PredictiveCacheController]. Specifically, [MapboxNavigation] should not be destroyed before + * [PredictiveCacheController.onDestroy] is called. + * + * @param mapboxNavigation [MapboxNavigation] object which will be used as a source of active route + * @param predictiveCacheOptions [PredictiveCacheOptions] options to instantiate instance of [PredictiveCacheController] + */ + constructor( + mapboxNavigation: MapboxNavigation, + predictiveCacheOptions: PredictiveCacheOptions, + ) : this(predictiveCacheOptions, PredictiveCache(mapboxNavigation)) + + /** + * Constructs a new instance of the [PredictiveCacheController] using the provided options. + * Throws [IllegalStateException] if [MapboxNavigation] was not instantiated before. + * Use [PredictiveCacheController] constructor which explicitly + * accepts [MapboxNavigation] instance and [predictiveCacheControllerErrorHandler] instead. * * @param predictiveCacheLocationOptions [PredictiveCacheLocationOptions] location configuration for visual map predictive caching (optional) * @param predictiveCacheControllerErrorHandler [PredictiveCacheControllerErrorHandler] listener (optional) * Noting that `predictiveCacheLocationOptions` is used as `predictiveCacheGuidanceLocationOptions` when constructing `PredictiveCacheController` to retain backwards compatibility + * + * @throws IllegalStateException if [MapboxNavigation] was not instantiated before */ @Deprecated( - "Use PredictiveCacheController(PredictiveCacheOptions) and " + - "PredictiveCacheController#predictiveCacheControllerErrorHandler instead" + "This constructor is deprecated", + ReplaceWith("PredictiveCacheController(MapboxNavigation, PredictiveCacheOptions)"), ) constructor( predictiveCacheLocationOptions: PredictiveCacheLocationOptions = @@ -94,16 +143,21 @@ class PredictiveCacheController constructor( ) /** - * Constructor of [PredictiveCacheController] + * Constructs a new instance of the [PredictiveCacheController] using the provided options. + * Throws [IllegalStateException] if [MapboxNavigation] was not instantiated before. + * Use [PredictiveCacheController] constructor which explicitly + * accepts [MapboxNavigation] instance and [predictiveCacheControllerErrorHandler] instead. * * @param predictiveCacheLocationOptions [PredictiveCacheLocationOptions] location configuration for visual map predictive caching (optional) * @param predictiveCacheGuidanceLocationOptions [PredictiveCacheLocationOptions] location configuration for guidance predictive caching (optional) * @param predictiveCacheControllerErrorHandler [PredictiveCacheControllerErrorHandler] listener (optional) + * + * @throws IllegalStateException if [MapboxNavigation] was not instantiated before */ @JvmOverloads @Deprecated( - "Use PredictiveCacheController(PredictiveCacheOptions) and " + - "PredictiveCacheController#predictiveCacheControllerErrorHandler instead" + "This constructor is deprecated", + ReplaceWith("PredictiveCacheController(MapboxNavigation, PredictiveCacheOptions)"), ) constructor( predictiveCacheLocationOptions: PredictiveCacheLocationOptions = @@ -125,13 +179,13 @@ class PredictiveCacheController constructor( }.build() ) }.build(), + PredictiveCache(tryToRetrieveMapboxNavigation()) ) { this.predictiveCacheControllerErrorHandler = predictiveCacheControllerErrorHandler } init { - PredictiveCache.init() - PredictiveCache.createNavigationController( + predictiveCache.createNavigationController( predictiveCacheOptions.predictiveCacheNavigationOptions.predictiveCacheLocationOptions ) } @@ -168,7 +222,7 @@ class PredictiveCacheController constructor( } traverseMapSources(map, sourceIdsToCache) { tileVariant -> - PredictiveCache.createMapsController( + predictiveCache.createMapsController( map, tileStore, tileVariant, @@ -184,7 +238,7 @@ class PredictiveCacheController constructor( updateMapsControllers( map, currentMapSources, - PredictiveCache.currentMapsPredictiveCacheControllers(map), + predictiveCache.currentMapsPredictiveCacheControllers(map), tileStore ) } @@ -248,8 +302,8 @@ class PredictiveCacheController constructor( } mapListeners.remove(map) } - PredictiveCache.removeAllMapControllersFromTileVariants(map) - PredictiveCache.removeAllMapControllersFromDescriptors(map) + predictiveCache.removeAllMapControllersFromTileVariants(map) + predictiveCache.removeAllMapControllersFromDescriptors(map) } /** @@ -264,7 +318,7 @@ class PredictiveCacheController constructor( } } mapListeners.clear() - PredictiveCache.clean() + predictiveCache.clean() } private fun handleError(error: String?) { @@ -319,12 +373,12 @@ class PredictiveCacheController constructor( ) { attachedMapSources .filterNot { currentMapSources.contains(it) } - .forEach { PredictiveCache.removeMapControllers(map, it) } + .forEach { predictiveCache.removeMapControllers(map, it) } currentMapSources .filterNot { attachedMapSources.contains(it) } .forEach { - PredictiveCache.createMapsController( + predictiveCache.createMapsController( map, tileStore, it, @@ -362,11 +416,21 @@ class PredictiveCacheController constructor( tilesetDescriptor to options.predictiveCacheLocationOptions } - PredictiveCache.createMapsControllers( + predictiveCache.createMapsControllers( map, tileStore, descriptorsToOptions ) } } + + private companion object { + fun tryToRetrieveMapboxNavigation(): MapboxNavigation { + return if (MapboxNavigationProvider.isCreated()) { + MapboxNavigationProvider.retrieve() + } else { + error("Instantiate MapboxNavigation first") + } + } + } } diff --git a/libnavui-maps/src/test/java/com/mapbox/navigation/ui/maps/PredictiveCacheControllerTest.kt b/libnavui-maps/src/test/java/com/mapbox/navigation/ui/maps/PredictiveCacheControllerTest.kt index adc33969e1a..19f4759a13e 100644 --- a/libnavui-maps/src/test/java/com/mapbox/navigation/ui/maps/PredictiveCacheControllerTest.kt +++ b/libnavui-maps/src/test/java/com/mapbox/navigation/ui/maps/PredictiveCacheControllerTest.kt @@ -12,11 +12,11 @@ import com.mapbox.maps.TilesetDescriptorOptions import com.mapbox.maps.plugin.delegates.listeners.OnStyleLoadedListener import com.mapbox.navigation.base.options.PredictiveCacheLocationOptions import com.mapbox.navigation.base.options.PredictiveCacheMapsOptions +import com.mapbox.navigation.base.options.PredictiveCacheNavigationOptions import com.mapbox.navigation.base.options.PredictiveCacheOptions import com.mapbox.navigation.core.internal.PredictiveCache import com.mapbox.navigation.testing.LoggingFrontendTestRule import com.mapbox.navigation.ui.maps.internal.offline.OfflineManagerProvider -import io.mockk.Ordering import io.mockk.Runs import io.mockk.every import io.mockk.just @@ -24,7 +24,6 @@ import io.mockk.mockk import io.mockk.mockkObject import io.mockk.mockkStatic import io.mockk.slot -import io.mockk.unmockkObject import io.mockk.unmockkStatic import io.mockk.verify import org.junit.After @@ -44,84 +43,45 @@ class PredictiveCacheControllerTest { @get:Rule val loggerRule = LoggingFrontendTestRule() + private val predictiveCache = mockk(relaxed = true) + private val errorHandler = mockk { every { onError(any()) } just Runs } @Before fun setup() { - mockkObject(PredictiveCache) mockkStatic(TileStore::class) } @After fun teardown() { - unmockkObject(PredictiveCache) unmockkStatic(TileStore::class) } + @Test fun `sanity primary constructor`() { + val locationOptions = mockk() val predictiveCacheOptions = mockk { every { predictiveCacheNavigationOptions } returns mockk { - every { predictiveCacheLocationOptions } returns mockk() + every { predictiveCacheLocationOptions } returns locationOptions } } - every { - PredictiveCache.createNavigationController( - predictiveCacheOptions.predictiveCacheNavigationOptions - .predictiveCacheLocationOptions - ) - } just Runs - val predictiveCacheController = PredictiveCacheController(predictiveCacheOptions) + val predictiveCacheController = PredictiveCacheController( + predictiveCacheOptions, + predictiveCache + ) assertNull(predictiveCacheController.predictiveCacheControllerErrorHandler) - verify(Ordering.SEQUENCE) { - PredictiveCache.init() - PredictiveCache.createNavigationController( - predictiveCacheOptions.predictiveCacheNavigationOptions - .predictiveCacheLocationOptions - ) - } - } - - fun `sanity secondary constructor`() { - val mockedLocationOptions: PredictiveCacheLocationOptions = mockk() - every { - PredictiveCache.createNavigationController(mockedLocationOptions) - } just Runs - - val predictiveCacheController = - PredictiveCacheController(mockedLocationOptions, errorHandler) - - assertEquals(errorHandler, predictiveCacheController.predictiveCacheControllerErrorHandler) - verify(Ordering.SEQUENCE) { - PredictiveCache.init() - PredictiveCache.createNavigationController(mockedLocationOptions) - } - } - - @Test - fun `initialize creates Navigation Predictive Cache Controller`() { - val mockedLocationOptions: PredictiveCacheLocationOptions = mockk() - every { - PredictiveCache.createNavigationController(any()) - } just Runs - - PredictiveCacheController(mockedLocationOptions, errorHandler) - - verify { - PredictiveCache.createNavigationController(mockedLocationOptions) + verify(exactly = 1) { + predictiveCache.createNavigationController(locationOptions) } - verify(exactly = 0) { errorHandler.onError(any()) } } @Test fun `null tileStore creates error message and does not initialize Predictive Cache Controllers`() { val mockedLocationOptions: PredictiveCacheLocationOptions = mockk() - every { - PredictiveCache.createNavigationController(any()) - } just Runs val mockedMapboxMap = mockk(relaxed = true) every { mockedMapboxMap.getResourceOptions().tileStore @@ -143,9 +103,11 @@ class PredictiveCacheControllerTest { every { style.styleSources } returns styleSources val predictiveCacheController = PredictiveCacheController( - mockedLocationOptions, - errorHandler, - ) + buildOptions(mockedLocationOptions), + predictiveCache + ).apply { + predictiveCacheControllerErrorHandler = errorHandler + } predictiveCacheController.createMapControllers(mockedMapboxMap) @@ -155,9 +117,6 @@ class PredictiveCacheControllerTest { @Test fun `non-null tileStore initializes Maps Predictive Cache Controllers`() { val mockedLocationOptions: PredictiveCacheLocationOptions = mockk() - every { - PredictiveCache.createNavigationController(any()) - } just Runs val mockedTileStore = mockk() every { TileStore.create(any()) } returns mockedTileStore val mockedMapboxMap = mockk(relaxed = true) { @@ -200,13 +159,15 @@ class PredictiveCacheControllerTest { every { style.getStyleSourceProperties(mockedIds[2]) } returns mockedPropertiesRaster val predictiveCacheController = PredictiveCacheController( - mockedLocationOptions, - errorHandler, - ) + buildOptions(mockedLocationOptions), + predictiveCache + ).apply { + predictiveCacheControllerErrorHandler = errorHandler + } val slotIds = mutableListOf() every { - PredictiveCache.createMapsController( + predictiveCache.createMapsController( mockedMapboxMap, mockedTileStore, capture(slotIds), @@ -217,7 +178,7 @@ class PredictiveCacheControllerTest { predictiveCacheController.createMapControllers(mockedMapboxMap) verify(exactly = 2) { - PredictiveCache.createMapsController( + predictiveCache.createMapsController( mockedMapboxMap, mockedTileStore, any(), @@ -237,9 +198,6 @@ class PredictiveCacheControllerTest { @Test fun `Maps Predictive Cache Controllers initialized for passed sources`() { val mockedLocationOptions: PredictiveCacheLocationOptions = mockk() - every { - PredictiveCache.createNavigationController(any()) - } just Runs val mockedTileStore = mockk() every { TileStore.create(any()) } returns mockedTileStore val mockedMapboxMap = mockk(relaxed = true) { @@ -300,13 +258,15 @@ class PredictiveCacheControllerTest { every { style.getStyleSourceProperties(mockedIds[4]) } returns mockedPropertiesVectorThird val predictiveCacheController = PredictiveCacheController( - mockedLocationOptions, - errorHandler, - ) + buildOptions(mockedLocationOptions), + predictiveCache + ).apply { + predictiveCacheControllerErrorHandler = errorHandler + } val slotIds = mutableListOf() every { - PredictiveCache.createMapsController( + predictiveCache.createMapsController( mockedMapboxMap, mockedTileStore, capture(slotIds), @@ -323,7 +283,7 @@ class PredictiveCacheControllerTest { ) verify(exactly = 2) { - PredictiveCache.createMapsController( + predictiveCache.createMapsController( mockedMapboxMap, mockedTileStore, any(), @@ -340,9 +300,6 @@ class PredictiveCacheControllerTest { @Test fun `style change triggers Maps Predictive Cache Controllers update`() { val mockedLocationOptions: PredictiveCacheLocationOptions = mockk() - every { - PredictiveCache.createNavigationController(any()) - } just Runs val mockedTileStore = mockk() every { TileStore.create(any()) } returns mockedTileStore val mockedMapboxMap = mockk(relaxed = true) { @@ -385,12 +342,14 @@ class PredictiveCacheControllerTest { every { style.getStyleSourceProperties(mockedIds[2]) } returns mockedPropertiesRaster val predictiveCacheController = PredictiveCacheController( - mockedLocationOptions, - errorHandler, - ) + buildOptions(mockedLocationOptions), + predictiveCache + ).apply { + predictiveCacheControllerErrorHandler = errorHandler + } val slotIds = mutableListOf() every { - PredictiveCache.createMapsController( + predictiveCache.createMapsController( mockedMapboxMap, mockedTileStore, capture(slotIds), @@ -401,7 +360,7 @@ class PredictiveCacheControllerTest { predictiveCacheController.createMapControllers(mockedMapboxMap) verify(exactly = 2) { - PredictiveCache.createMapsController( + predictiveCache.createMapsController( mockedMapboxMap, mockedTileStore, any(), @@ -409,7 +368,7 @@ class PredictiveCacheControllerTest { ) } every { - PredictiveCache.currentMapsPredictiveCacheControllers(mockedMapboxMap) + predictiveCache.currentMapsPredictiveCacheControllers(mockedMapboxMap) } returns listOf("mapbox.satellite") val newStyle = mockk