|
| 1 | +package com.mapbox.navigation.core.trip |
| 2 | + |
| 3 | +import com.mapbox.android.core.permissions.PermissionsManager |
| 4 | +import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI |
| 5 | +import com.mapbox.navigation.core.MapboxNavigation |
| 6 | +import com.mapbox.navigation.core.trip.session.TripSessionState |
| 7 | +import io.mockk.every |
| 8 | +import io.mockk.mockk |
| 9 | +import io.mockk.mockkStatic |
| 10 | +import io.mockk.verify |
| 11 | +import org.junit.Assert.assertFalse |
| 12 | +import org.junit.Assert.assertTrue |
| 13 | +import org.junit.Before |
| 14 | +import org.junit.Test |
| 15 | + |
| 16 | +@OptIn(ExperimentalPreviewMapboxNavigationAPI::class) |
| 17 | +class MapboxTripStarterTest { |
| 18 | + |
| 19 | + private val sut = MapboxTripStarter.create() |
| 20 | + |
| 21 | + @Before |
| 22 | + fun setup() { |
| 23 | + mockkStatic(PermissionsManager::class) |
| 24 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns true |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `onAttached will update location permission options`() { |
| 29 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns true |
| 30 | + |
| 31 | + val initialOptions = sut.getOptions() |
| 32 | + sut.onAttached(mockk(relaxed = true)) |
| 33 | + |
| 34 | + assertFalse(initialOptions.isLocationPermissionGranted) |
| 35 | + assertTrue(sut.getOptions().isLocationPermissionGranted) |
| 36 | + } |
| 37 | + |
| 38 | + @Test(expected = IllegalStateException::class) |
| 39 | + fun `setOptions will crash if used before onAttached`() { |
| 40 | + val options = MapboxTripStarterOptions.Builder().isLocationPermissionGranted(true).build() |
| 41 | + sut.setOptions(options) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `onAttached will startTripSession when location permissions are granted`() { |
| 46 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns true |
| 47 | + |
| 48 | + val mapboxNavigation = mockMapboxNavigation() |
| 49 | + sut.onAttached(mapboxNavigation) |
| 50 | + |
| 51 | + verify(exactly = 1) { mapboxNavigation.startTripSession() } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `onAttached will not startTripSession when location permissions are disabled`() { |
| 56 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns false |
| 57 | + |
| 58 | + val mapboxNavigation = mockMapboxNavigation() |
| 59 | + sut.onAttached(mapboxNavigation) |
| 60 | + |
| 61 | + verify(exactly = 0) { mapboxNavigation.startTripSession() } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `update will startTripSession when location permissions are disabled`() { |
| 66 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns false |
| 67 | + |
| 68 | + val mapboxNavigation = mockMapboxNavigation() |
| 69 | + sut.onAttached(mapboxNavigation) |
| 70 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns true |
| 71 | + sut.update { it.isLocationPermissionGranted(true) } |
| 72 | + |
| 73 | + verify(exactly = 1) { mapboxNavigation.startTripSession() } |
| 74 | + } |
| 75 | + |
| 76 | + @Test(expected = IllegalStateException::class) |
| 77 | + fun `cannot accept location permissions when they are not granted`() { |
| 78 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns false |
| 79 | + |
| 80 | + val mapboxNavigation = mockMapboxNavigation() |
| 81 | + sut.onAttached(mapboxNavigation) |
| 82 | + |
| 83 | + // This throws an error because areLocationPermissionsGranted is false |
| 84 | + sut.update { it.isLocationPermissionGranted(true) } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `update can be used to enable replay route without location permissions`() { |
| 89 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns false |
| 90 | + |
| 91 | + val mapboxNavigation = mockMapboxNavigation() |
| 92 | + sut.onAttached(mapboxNavigation) |
| 93 | + sut.update { it.tripType(MapboxTripStarterExtra.MAPBOX_TRIP_STARTER_REPLAY_ROUTE) } |
| 94 | + |
| 95 | + verify(exactly = 1) { mapboxNavigation.startReplayTripSession() } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun `update will not restart startReplayTripSession after changes`() { |
| 100 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns false |
| 101 | + |
| 102 | + val mapboxNavigation = mockMapboxNavigation() |
| 103 | + sut.onAttached(mapboxNavigation) |
| 104 | + sut.update { it.tripType(MapboxTripStarterExtra.MAPBOX_TRIP_STARTER_REPLAY_ROUTE) } |
| 105 | + every { PermissionsManager.areLocationPermissionsGranted(any()) } returns true |
| 106 | + sut.update { it.isLocationPermissionGranted(true) } |
| 107 | + |
| 108 | + verify(exactly = 1) { mapboxNavigation.startReplayTripSession() } |
| 109 | + } |
| 110 | + |
| 111 | + private fun mockMapboxNavigation(): MapboxNavigation { |
| 112 | + val mapboxNavigation = mockk<MapboxNavigation>(relaxed = true) |
| 113 | + every { mapboxNavigation.getTripSessionState() } returns TripSessionState.STOPPED |
| 114 | + every { mapboxNavigation.startReplayTripSession() } answers { |
| 115 | + every { mapboxNavigation.isReplayEnabled() } returns true |
| 116 | + every { mapboxNavigation.getTripSessionState() } returns TripSessionState.STARTED |
| 117 | + } |
| 118 | + every { mapboxNavigation.startTripSession() } answers { |
| 119 | + every { mapboxNavigation.isReplayEnabled() } returns false |
| 120 | + every { mapboxNavigation.getTripSessionState() } returns TripSessionState.STARTED |
| 121 | + } |
| 122 | + every { mapboxNavigation.stopTripSession() } answers { |
| 123 | + every { mapboxNavigation.isReplayEnabled() } returns false |
| 124 | + every { mapboxNavigation.getTripSessionState() } returns TripSessionState.STOPPED |
| 125 | + } |
| 126 | + return mapboxNavigation |
| 127 | + } |
| 128 | +} |
0 commit comments