Skip to content

Commit 74eb6d3

Browse files
committed
test coverage
1 parent 92833a6 commit 74eb6d3

File tree

5 files changed

+120
-26
lines changed

5 files changed

+120
-26
lines changed

libnavigation-core/api/current.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ package com.mapbox.navigation.core.history {
270270
method public String getFilePath();
271271
method public boolean hasNext();
272272
method public com.mapbox.navigation.core.history.model.HistoryEvent next();
273-
method public java.util.List<com.mapbox.navigation.core.history.model.HistoryEvent> takeLocations(int count);
274273
property public final String filePath;
275274
}
276275

libnavigation-core/src/main/java/com/mapbox/navigation/core/history/MapboxHistoryReader.kt

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.mapbox.navigation.core.history
22

33
import com.mapbox.navigation.core.history.model.HistoryEvent
44
import com.mapbox.navigation.core.history.model.HistoryEventMapper
5-
import com.mapbox.navigation.core.history.model.HistoryEventUpdateLocation
65
import com.mapbox.navigator.HistoryReader
76

87
/**
@@ -39,26 +38,6 @@ class MapboxHistoryReader(
3938
hasNext = loadNext()
4039
}
4140

42-
/**
43-
* Loads the next [count] location events from the history file and returns all of the
44-
* [HistoryEvent] in a list. The size of the list returned will often be larger than [count]
45-
* because there are other types of events in the history file.
46-
*
47-
* @param count the maximum number of [HistoryEventUpdateLocation] to take
48-
*/
49-
fun takeLocations(count: Int): List<HistoryEvent> {
50-
val historyEvents = mutableListOf<HistoryEvent>()
51-
var locationCount = 0
52-
while (locationCount < count && hasNext) {
53-
val event = next()
54-
if (event is HistoryEventUpdateLocation) {
55-
locationCount++
56-
}
57-
historyEvents.add(event)
58-
}
59-
return historyEvents
60-
}
61-
6241
private fun loadNext(): Boolean {
6342
val historyRecord = nativeHistoryReader.next()
6443
next = if (historyRecord != null) {

libnavigation-core/src/main/java/com/mapbox/navigation/core/replay/history/ReplayHistorySession.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.mapbox.navigation.core.history.MapboxHistoryReader
66
import com.mapbox.navigation.core.history.MapboxHistoryReaderProvider
77
import com.mapbox.navigation.core.history.MapboxHistoryRecorder
88
import com.mapbox.navigation.core.history.model.HistoryEvent
9+
import com.mapbox.navigation.core.history.model.HistoryEventUpdateLocation
910
import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver
1011
import com.mapbox.navigation.core.replay.MapboxReplayer
1112
import kotlinx.coroutines.CoroutineScope
@@ -128,10 +129,9 @@ class ReplayHistorySession : MapboxNavigationObserver {
128129
): Flow<R> = map(transform).distinctUntilChanged()
129130

130131
private fun isLastEventPlayed(events: List<ReplayEventBase>): Boolean {
131-
val currentLocationEvent = events.lastOrNull { it is ReplayEventUpdateLocation }
132-
?: return false
132+
val currentEvent = events.lastOrNull() ?: return false
133133
val lastEventTimestamp = this.lastHistoryEvent?.eventTimestamp ?: 0.0
134-
return currentLocationEvent.eventTimestamp >= lastEventTimestamp
134+
return currentEvent.eventTimestamp >= lastEventTimestamp
135135
}
136136

137137
private fun pushMorePoints() {
@@ -154,6 +154,26 @@ class ReplayHistorySession : MapboxNavigationObserver {
154154
}
155155
}
156156

157+
/**
158+
* Loads the next [count] location events from the history file and returns all of the
159+
* [HistoryEvent] in a list. The size of the list returned will often be larger than [count]
160+
* because there are other types of events in the history file.
161+
*
162+
* @param count the maximum number of [HistoryEventUpdateLocation] to take
163+
*/
164+
private fun MapboxHistoryReader.takeLocations(count: Int): List<HistoryEvent> {
165+
val historyEvents = mutableListOf<HistoryEvent>()
166+
var locationCount = 0
167+
while (locationCount < count && hasNext()) {
168+
val event = next()
169+
if (event is HistoryEventUpdateLocation) {
170+
locationCount++
171+
}
172+
historyEvents.add(event)
173+
}
174+
return historyEvents
175+
}
176+
157177
private companion object {
158178

159179
/**

libnavigation-core/src/test/java/com/mapbox/navigation/core/replay/history/ReplayHistorySessionTest.kt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import com.mapbox.navigation.core.TripSessionResetCallback
88
import com.mapbox.navigation.core.directions.session.RoutesObserver
99
import com.mapbox.navigation.core.history.MapboxHistoryReader
1010
import 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
1114
import com.mapbox.navigation.core.replay.MapboxReplayer
1215
import com.mapbox.navigation.core.trip.session.RouteProgressObserver
1316
import com.mapbox.navigation.testing.LoggingFrontendTestRule
@@ -21,6 +24,7 @@ import io.mockk.unmockkAll
2124
import io.mockk.verify
2225
import io.mockk.verifyOrder
2326
import org.junit.After
27+
import org.junit.Assert.assertEquals
2428
import org.junit.Before
2529
import org.junit.Rule
2630
import 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
}

libnavigation-core/src/test/java/com/mapbox/navigation/core/trip/MapboxTripStarterTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class MapboxTripStarterTest {
263263
replayHistorySession.onDetached(mapboxNavigation)
264264
}
265265
}
266-
266+
267267
private fun mockMapboxNavigation(): MapboxNavigation {
268268
val mapboxNavigation = mockk<MapboxNavigation>(relaxed = true)
269269
every { mapboxNavigation.getTripSessionState() } returns TripSessionState.STOPPED

0 commit comments

Comments
 (0)