Skip to content

Commit 4b8df1d

Browse files
committed
improve tests
1 parent 8859993 commit 4b8df1d

File tree

3 files changed

+111
-6
lines changed

3 files changed

+111
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.mapbox.navigation.core.history
2+
3+
import androidx.annotation.VisibleForTesting
4+
5+
@VisibleForTesting
6+
internal object MapboxHistoryReaderProvider {
7+
fun create(filePath: String) = MapboxHistoryReader(filePath)
8+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mapbox.navigation.core.replay.history
33
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
44
import com.mapbox.navigation.core.MapboxNavigation
55
import com.mapbox.navigation.core.history.MapboxHistoryReader
6+
import com.mapbox.navigation.core.history.MapboxHistoryReaderProvider
67
import com.mapbox.navigation.core.history.MapboxHistoryRecorder
78
import com.mapbox.navigation.core.history.model.HistoryEvent
89
import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver
@@ -71,8 +72,9 @@ class ReplayHistorySession : MapboxNavigationObserver {
7172
this.mapboxNavigation = mapboxNavigation
7273
this.lastHistoryEvent = null
7374
mapboxNavigation.startReplayTripSession()
74-
observeStateFlow(mapboxNavigation).launchIn(coroutineScope)
75+
mapboxNavigation.mapboxReplayer.stop()
7576
mapboxNavigation.mapboxReplayer.registerObserver(replayEventsObserver)
77+
observeStateFlow(mapboxNavigation).launchIn(coroutineScope)
7678
}
7779

7880
/**
@@ -113,7 +115,7 @@ class ReplayHistorySession : MapboxNavigationObserver {
113115
return optionsFlow.mapDistinct { it.filePath }.onEach { historyFile ->
114116
mapboxNavigation.mapboxReplayer.clearEvents()
115117
mapboxNavigation.setNavigationRoutes(emptyList())
116-
mapboxHistoryReader = historyFile?.let { MapboxHistoryReader(it) }
118+
mapboxHistoryReader = historyFile?.let { MapboxHistoryReaderProvider.create(it) }
117119
mapboxNavigation.resetTripSession {
118120
mapboxNavigation.mapboxReplayer.play()
119121
pushMorePoints()

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

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ import com.mapbox.navigation.base.options.NavigationOptions
66
import com.mapbox.navigation.core.MapboxNavigation
77
import com.mapbox.navigation.core.TripSessionResetCallback
88
import com.mapbox.navigation.core.directions.session.RoutesObserver
9+
import com.mapbox.navigation.core.history.MapboxHistoryReader
10+
import com.mapbox.navigation.core.history.MapboxHistoryReaderProvider
911
import com.mapbox.navigation.core.replay.MapboxReplayer
1012
import com.mapbox.navigation.core.trip.session.RouteProgressObserver
1113
import com.mapbox.navigation.testing.LoggingFrontendTestRule
1214
import io.mockk.every
1315
import io.mockk.just
1416
import io.mockk.mockk
17+
import io.mockk.mockkObject
1518
import io.mockk.runs
1619
import io.mockk.slot
20+
import io.mockk.unmockkAll
1721
import io.mockk.verify
22+
import io.mockk.verifyOrder
23+
import org.junit.After
24+
import org.junit.Before
1825
import org.junit.Rule
1926
import org.junit.Test
2027

@@ -25,25 +32,113 @@ class ReplayHistorySessionTest {
2532
val loggerRule = LoggingFrontendTestRule()
2633

2734
private val replayer: MapboxReplayer = mockk(relaxed = true)
35+
private val historyReader: MapboxHistoryReader = mockk(relaxed = true)
2836

2937
private val sut = ReplayHistorySession()
3038

39+
@Before
40+
fun setup() {
41+
mockkObject(MapboxHistoryReaderProvider)
42+
every { MapboxHistoryReaderProvider.create(any()) } returns historyReader
43+
}
44+
45+
@After
46+
fun teardown() {
47+
unmockkAll()
48+
}
49+
50+
@Test
51+
fun `onAttached will call startReplayTripSession`() {
52+
val mapboxNavigation = mockMapboxNavigation()
53+
54+
sut.onAttached(mapboxNavigation)
55+
56+
verify(exactly = 1) { mapboxNavigation.startReplayTripSession() }
57+
}
58+
3159
@Test
32-
fun `onAttached with startReplayTripSession`() {
60+
fun `onAttached will call MapboxReplayer#play`() {
3361
val mapboxNavigation = mockMapboxNavigation()
3462

3563
sut.onAttached(mapboxNavigation)
3664

37-
verify { mapboxNavigation.startReplayTripSession() }
65+
verify(exactly = 1) { replayer.play() }
3866
}
3967

4068
@Test
41-
fun `onAttached with call MapboxReplayer#play`() {
69+
fun `setHistoryFile after onAttached will clear events and reset the trip`() {
4270
val mapboxNavigation = mockMapboxNavigation()
4371

4472
sut.onAttached(mapboxNavigation)
73+
sut.setHistoryFile("test_file_path")
74+
75+
verifyOrder {
76+
mapboxNavigation.startReplayTripSession()
77+
replayer.clearEvents()
78+
mapboxNavigation.setNavigationRoutes(emptyList())
79+
mapboxNavigation.resetTripSession(any())
80+
replayer.play()
81+
// setHistoryFile was called
82+
replayer.clearEvents()
83+
mapboxNavigation.setNavigationRoutes(emptyList())
84+
MapboxHistoryReaderProvider.create("test_file_path")
85+
mapboxNavigation.resetTripSession(any())
86+
replayer.play()
87+
}
88+
verify(exactly = 1) {
89+
mapboxNavigation.startReplayTripSession()
90+
MapboxHistoryReaderProvider.create(any())
91+
}
92+
}
4593

46-
verify { replayer.play() }
94+
@Test
95+
fun `setHistoryFile before onAttached will initialize once`() {
96+
val mapboxNavigation = mockMapboxNavigation()
97+
98+
sut.setHistoryFile("test_file_path")
99+
sut.onAttached(mapboxNavigation)
100+
101+
verifyOrder {
102+
mapboxNavigation.startReplayTripSession()
103+
replayer.clearEvents()
104+
mapboxNavigation.setNavigationRoutes(emptyList())
105+
MapboxHistoryReaderProvider.create("test_file_path")
106+
mapboxNavigation.resetTripSession(any())
107+
replayer.play()
108+
}
109+
verify(exactly = 1) {
110+
mapboxNavigation.startReplayTripSession()
111+
replayer.clearEvents()
112+
mapboxNavigation.setNavigationRoutes(any())
113+
MapboxHistoryReaderProvider.create(any())
114+
mapboxNavigation.resetTripSession(any())
115+
replayer.play()
116+
}
117+
}
118+
119+
@Test
120+
fun `onDetached will clean up but will not stopTripSession`() {
121+
val mapboxNavigation = mockMapboxNavigation()
122+
123+
sut.onAttached(mapboxNavigation)
124+
sut.onDetached(mapboxNavigation)
125+
126+
verifyOrder {
127+
mapboxNavigation.startReplayTripSession()
128+
replayer.stop()
129+
replayer.registerObserver(any())
130+
replayer.clearEvents()
131+
mapboxNavigation.setNavigationRoutes(emptyList())
132+
mapboxNavigation.resetTripSession(any())
133+
replayer.play()
134+
// onDetached called
135+
replayer.unregisterObserver(any())
136+
replayer.stop()
137+
replayer.clearEvents()
138+
}
139+
verify(exactly = 1) {
140+
replayer.play()
141+
}
47142
}
48143

49144
private fun mockMapboxNavigation(): MapboxNavigation {

0 commit comments

Comments
 (0)