@@ -8,6 +8,9 @@ import com.mapbox.navigation.core.TripSessionResetCallback
88import com.mapbox.navigation.core.directions.session.RoutesObserver
99import com.mapbox.navigation.core.history.MapboxHistoryReader
1010import com.mapbox.navigation.core.history.MapboxHistoryReaderProvider
11+ import com.mapbox.navigation.core.history.model.HistoryEvent
12+ import com.mapbox.navigation.core.history.model.HistoryEventSetRoute
13+ import com.mapbox.navigation.core.history.model.HistoryEventUpdateLocation
1114import com.mapbox.navigation.core.replay.MapboxReplayer
1215import com.mapbox.navigation.core.trip.session.RouteProgressObserver
1316import com.mapbox.navigation.testing.LoggingFrontendTestRule
@@ -21,6 +24,7 @@ import io.mockk.unmockkAll
2124import io.mockk.verify
2225import io.mockk.verifyOrder
2326import org.junit.After
27+ import org.junit.Assert.assertEquals
2428import org.junit.Before
2529import org.junit.Rule
2630import org.junit.Test
@@ -141,6 +145,86 @@ class ReplayHistorySessionTest {
141145 }
142146 }
143147
148+ @Test
149+ fun `should push events from history file` () {
150+ val mapboxNavigation = mockMapboxNavigation()
151+ val eventCount = 100
152+ every { historyReader.hasNext() } returnsMany (0 .. eventCount)
153+ .map { it != eventCount }
154+ every { historyReader.next() } returnsMany (1 .. eventCount)
155+ .map { value ->
156+ mockk<HistoryEventUpdateLocation > {
157+ every { eventTimestamp } returns value.toDouble()
158+ every { location } returns mockk()
159+ }
160+ }
161+ val eventObserver = slot<ReplayEventsObserver >()
162+ every { replayer.registerObserver(capture(eventObserver)) } just runs
163+ val eventSlot = mutableListOf<List <ReplayEventBase >>()
164+ every { replayer.pushEvents(capture(eventSlot)) } answers {
165+ eventObserver.captured.replayEvents(firstArg())
166+ replayer
167+ }
168+
169+ sut.setOptions(mockOptions())
170+ sut.onAttached(mapboxNavigation)
171+
172+ val capturedEvents = eventSlot.flatten()
173+ assertEquals(100 , capturedEvents.size)
174+ }
175+
176+ @Test
177+ fun `should setNavigationRoutes from history file when option is enabled` () {
178+ val mapboxNavigation = mockMapboxNavigation()
179+ val options = mockOptions()
180+ every { options.enableSetRoute } returns true
181+ every { historyReader.hasNext() } returnsMany listOf (true , false )
182+ every { historyReader.next() } returnsMany listOf (
183+ mockk<HistoryEventSetRoute > {
184+ every { eventTimestamp } returns 11.0
185+ every { navigationRoute } returns mockk()
186+ }
187+ )
188+ val eventObserver = slot<ReplayEventsObserver >()
189+ every { replayer.registerObserver(capture(eventObserver)) } just runs
190+ val eventSlot = mutableListOf<List <ReplayEventBase >>()
191+ every { replayer.pushEvents(capture(eventSlot)) } answers {
192+ eventObserver.captured.replayEvents(firstArg())
193+ replayer
194+ }
195+
196+ sut.setOptions(options)
197+ sut.onAttached(mapboxNavigation)
198+
199+ verify { mapboxNavigation.setNavigationRoutes(any()) }
200+ }
201+
202+ @Test
203+ fun `should not setNavigationRoutes from history file when option is disabled` () {
204+ val mapboxNavigation = mockMapboxNavigation()
205+ val options = mockOptions()
206+ every { options.enableSetRoute } returns false
207+ every { historyReader.hasNext() } returnsMany listOf (true , false )
208+ every { historyReader.next() } returnsMany listOf (
209+ mockk<HistoryEventSetRoute > {
210+ every { eventTimestamp } returns 11.0
211+ every { navigationRoute } returns mockk()
212+ }
213+ )
214+ val eventObserver = slot<ReplayEventsObserver >()
215+ every { replayer.registerObserver(capture(eventObserver)) } just runs
216+ val eventSlot = mutableListOf<List <ReplayEventBase >>()
217+ every { replayer.pushEvents(capture(eventSlot)) } answers {
218+ eventObserver.captured.replayEvents(firstArg())
219+ replayer
220+ }
221+
222+ sut.setOptions(options)
223+ sut.onAttached(mapboxNavigation)
224+
225+ verify { mapboxNavigation.setNavigationRoutes(any()) }
226+ }
227+
144228 private fun mockMapboxNavigation (): MapboxNavigation {
145229 val context: Context = mockk(relaxed = true )
146230 val options: NavigationOptions = mockk {
@@ -158,4 +242,16 @@ class ReplayHistorySessionTest {
158242 }
159243 }
160244 }
245+
246+ private fun mockOptions (): ReplayHistorySessionOptions = mockk {
247+ every { filePath } returns " test_file_path"
248+ every { replayHistoryMapper } returns mockk {
249+ every { mapToReplayEvent(any()) } answers {
250+ mockk {
251+ every { eventTimestamp } returns firstArg<HistoryEvent >().eventTimestamp
252+ }
253+ }
254+ }
255+ every { enableSetRoute } returns true
256+ }
161257}
0 commit comments