|
| 1 | +package com.mapbox.navigation.core.internal |
| 2 | + |
| 3 | +import com.mapbox.navigation.core.CopilotSessionObserver |
| 4 | +import com.mapbox.navigation.core.HistoryRecordingStateHandler |
| 5 | +import com.mapbox.navigation.core.trip.session.TripSessionState |
| 6 | +import io.mockk.clearMocks |
| 7 | +import io.mockk.mockk |
| 8 | +import io.mockk.verify |
| 9 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 10 | +import org.junit.Assert.assertTrue |
| 11 | +import org.junit.Before |
| 12 | +import org.junit.Test |
| 13 | + |
| 14 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 15 | +class HistoryRecordingStateHandlerCopilotTest { |
| 16 | + |
| 17 | + private val copilotObserver = mockk<CopilotSessionObserver>(relaxUnitFun = true) |
| 18 | + private lateinit var stateHandler: HistoryRecordingStateHandler |
| 19 | + |
| 20 | + @Before |
| 21 | + fun setUp() { |
| 22 | + stateHandler = HistoryRecordingStateHandler() |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun currentCopilotSessionShouldBeIdle() { |
| 27 | + assertTrue(stateHandler.currentCopilotSession() is HistoryRecordingSessionState.Idle) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun currentCopilotSessionShouldBeFreeDrive() { |
| 32 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 33 | + assertTrue(stateHandler.currentCopilotSession() is HistoryRecordingSessionState.FreeDrive) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun currentCopilotSessionShouldBeActiveGuidance() { |
| 38 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 39 | + stateHandler.setRoutes(listOf(mockk())) |
| 40 | + assertTrue( |
| 41 | + stateHandler.currentCopilotSession() is HistoryRecordingSessionState.ActiveGuidance |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun shouldNotifyCopilotObserverOnRegister() { |
| 47 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 48 | + |
| 49 | + verify { |
| 50 | + copilotObserver.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.Idle>()) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun shouldNotifyCopilotObserverOnChangeFromIdleToFreeDrive() { |
| 56 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 57 | + |
| 58 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 59 | + |
| 60 | + verify { |
| 61 | + copilotObserver.onCopilotSessionChanged( |
| 62 | + ofType<HistoryRecordingSessionState.FreeDrive>() |
| 63 | + ) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun shouldNotifyCopilotObserverOnChangeFromIdleToActiveGuidance() { |
| 69 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 70 | + stateHandler.setRoutes(listOf(mockk())) |
| 71 | + clearMocks(copilotObserver, answers = false) |
| 72 | + |
| 73 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 74 | + |
| 75 | + verify { |
| 76 | + copilotObserver.onCopilotSessionChanged( |
| 77 | + ofType<HistoryRecordingSessionState.ActiveGuidance>() |
| 78 | + ) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun shouldNotNotifyCopilotObserverOnChangeFromIdleToIdle() { |
| 84 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 85 | + clearMocks(copilotObserver, answers = false) |
| 86 | + |
| 87 | + stateHandler.onSessionStateChanged(TripSessionState.STOPPED) |
| 88 | + |
| 89 | + verify(exactly = 0) { copilotObserver.onCopilotSessionChanged(any()) } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun shouldNotifyCopilotObserverOnChangeFromFreeDriveToIdle() { |
| 94 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 95 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 96 | + clearMocks(copilotObserver, answers = false) |
| 97 | + |
| 98 | + stateHandler.onSessionStateChanged(TripSessionState.STOPPED) |
| 99 | + |
| 100 | + verify { |
| 101 | + copilotObserver.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.Idle>()) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun shouldNotNotifyCopilotObserverOnChangeFromFreeDriveToFreeDrive() { |
| 107 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 108 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 109 | + clearMocks(copilotObserver, answers = false) |
| 110 | + |
| 111 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 112 | + |
| 113 | + verify(exactly = 0) { copilotObserver.onCopilotSessionChanged(any()) } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun shouldNotifyCopilotObserverOnChangeFromFreeDriveToActiveGuidance() { |
| 118 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 119 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 120 | + clearMocks(copilotObserver, answers = false) |
| 121 | + |
| 122 | + stateHandler.setRoutes(listOf(mockk())) |
| 123 | + |
| 124 | + verify { |
| 125 | + copilotObserver.onCopilotSessionChanged( |
| 126 | + ofType<HistoryRecordingSessionState.ActiveGuidance>() |
| 127 | + ) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun shouldNotifyCopilotObserverOnChangeFromActiveGuidanceToIdle() { |
| 133 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 134 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 135 | + stateHandler.setRoutes(listOf(mockk())) |
| 136 | + clearMocks(copilotObserver, answers = false) |
| 137 | + |
| 138 | + stateHandler.onSessionStateChanged(TripSessionState.STOPPED) |
| 139 | + |
| 140 | + verify { |
| 141 | + copilotObserver.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.Idle>()) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun shouldNotifyCopilotObserverOnChangeFromActiveGuidanceToFreeDrive() { |
| 147 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 148 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 149 | + stateHandler.setRoutes(listOf(mockk())) |
| 150 | + clearMocks(copilotObserver, answers = false) |
| 151 | + |
| 152 | + stateHandler.setRoutes(emptyList()) |
| 153 | + |
| 154 | + verify { |
| 155 | + copilotObserver.onCopilotSessionChanged( |
| 156 | + ofType<HistoryRecordingSessionState.FreeDrive>() |
| 157 | + ) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + fun shouldNotNotifyCopilotObserverOnChangeFromActiveGuidanceToActiveGuidance() { |
| 163 | + stateHandler.registerCopilotSessionObserver(copilotObserver) |
| 164 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 165 | + stateHandler.setRoutes(listOf(mockk())) |
| 166 | + clearMocks(copilotObserver, answers = false) |
| 167 | + |
| 168 | + stateHandler.setRoutes(listOf(mockk(), mockk())) |
| 169 | + |
| 170 | + verify(exactly = 0) { copilotObserver.onCopilotSessionChanged(any()) } |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + fun copilotObserversNotification() { |
| 175 | + val observer1 = mockk<CopilotSessionObserver>(relaxUnitFun = true) |
| 176 | + val observer2 = mockk<CopilotSessionObserver>(relaxUnitFun = true) |
| 177 | + val observer3 = mockk<CopilotSessionObserver>(relaxUnitFun = true) |
| 178 | + stateHandler = HistoryRecordingStateHandler() |
| 179 | + |
| 180 | + // no observers |
| 181 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 182 | + |
| 183 | + verifyNoInteractions(observer1, observer2, observer3) |
| 184 | + |
| 185 | + // 1 observer |
| 186 | + stateHandler.registerCopilotSessionObserver(observer1) |
| 187 | + verify(exactly = 1) { |
| 188 | + observer1.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.FreeDrive>()) |
| 189 | + } |
| 190 | + stateHandler.setRoutes(listOf(mockk())) |
| 191 | + |
| 192 | + verify(exactly = 1) { |
| 193 | + observer1.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.ActiveGuidance>()) |
| 194 | + } |
| 195 | + verifyNoInteractions(observer2, observer3) |
| 196 | + |
| 197 | + // 3 observers |
| 198 | + clearMocks(observer1, observer2, observer3) |
| 199 | + |
| 200 | + stateHandler.registerCopilotSessionObserver(observer2) |
| 201 | + stateHandler.registerCopilotSessionObserver(observer3) |
| 202 | + stateHandler.onSessionStateChanged(TripSessionState.STOPPED) |
| 203 | + |
| 204 | + verify(exactly = 1) { |
| 205 | + observer1.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.Idle>()) |
| 206 | + observer2.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.Idle>()) |
| 207 | + observer3.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.Idle>()) |
| 208 | + } |
| 209 | + |
| 210 | + // 2 observers |
| 211 | + clearMocks(observer1, observer2, observer3) |
| 212 | + |
| 213 | + stateHandler.unregisterCopilotSessionObserver(observer2) |
| 214 | + stateHandler.onSessionStateChanged(TripSessionState.STARTED) |
| 215 | + stateHandler.setRoutes(listOf(mockk())) |
| 216 | + |
| 217 | + verify(exactly = 1) { |
| 218 | + observer1.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.ActiveGuidance>()) |
| 219 | + observer3.onCopilotSessionChanged(ofType<HistoryRecordingSessionState.ActiveGuidance>()) |
| 220 | + } |
| 221 | + verifyNoInteractions(observer2) |
| 222 | + |
| 223 | + // no observers |
| 224 | + clearMocks(observer1, observer2, observer3) |
| 225 | + |
| 226 | + stateHandler.unregisterAllCopilotSessionObservers() |
| 227 | + stateHandler.setRoutes(emptyList()) |
| 228 | + |
| 229 | + verifyNoInteractions(observer1, observer2, observer3) |
| 230 | + } |
| 231 | + |
| 232 | + private fun verifyNoInteractions(vararg observers: CopilotSessionObserver) { |
| 233 | + observers.forEach { |
| 234 | + verify(exactly = 0) { |
| 235 | + it.onCopilotSessionChanged(any()) |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments