@@ -15,7 +15,8 @@ import io.mockk.every
15
15
import io.mockk.mockk
16
16
import io.mockk.verify
17
17
import kotlinx.coroutines.ExperimentalCoroutinesApi
18
- import kotlinx.coroutines.flow.collect
18
+ import kotlinx.coroutines.delay
19
+ import kotlinx.coroutines.launch
19
20
import org.junit.Assert.assertEquals
20
21
import org.junit.Rule
21
22
import org.junit.Test
@@ -27,28 +28,28 @@ class MapboxAudioGuidanceVoiceTest {
27
28
val coroutineRule = MainCoroutineRule ()
28
29
29
30
private val speechApi = mockk<MapboxSpeechApi >(relaxUnitFun = true )
30
- private val voiceInstructionsPlayer = mockk<MapboxVoiceInstructionsPlayer >(relaxUnitFun = true )
31
- private val carAppAudioGuidanceVoice = MapboxAudioGuidanceVoice (
31
+ private val voiceInstructionsPlayer = mockk<MapboxVoiceInstructionsPlayer >(relaxed = true )
32
+ private val sut = MapboxAudioGuidanceVoice (
32
33
speechApi,
33
34
voiceInstructionsPlayer
34
35
)
35
36
36
37
@Test
37
- fun `voice instruction should be played as SpeechAnnouncement` () = coroutineRule.runBlockingTest {
38
- mockSuccessfulSpeechApi()
39
- mockSuccessfulVoiceInstructionsPlayer()
38
+ fun `voice instruction should be played as SpeechAnnouncement` () =
39
+ coroutineRule.runBlockingTest {
40
+ mockSuccessfulSpeechApi()
41
+ mockSuccessfulVoiceInstructionsPlayer()
40
42
41
- val voiceInstructions = mockk<VoiceInstructions > {
42
- every { announcement() } returns " Turn right on Market"
43
- }
44
- carAppAudioGuidanceVoice .speak(voiceInstructions).collect { speechAnnouncement ->
43
+ val voiceInstructions = mockk<VoiceInstructions > {
44
+ every { announcement() } returns " Turn right on Market"
45
+ }
46
+ val speechAnnouncement = sut .speak(voiceInstructions)
45
47
assertEquals(" Turn right on Market" , speechAnnouncement!! .announcement)
46
48
}
47
- }
48
49
49
50
@Test
50
51
fun `null should clean up the api and player` () = coroutineRule.runBlockingTest {
51
- carAppAudioGuidanceVoice .speak(null ).collect( )
52
+ sut .speak(null )
52
53
53
54
verify { speechApi.cancel() }
54
55
verify { voiceInstructionsPlayer.clear() }
@@ -70,18 +71,44 @@ class MapboxAudioGuidanceVoiceTest {
70
71
val voiceInstructions = mockk<VoiceInstructions > {
71
72
every { announcement() } returns " This message fails"
72
73
}
73
- carAppAudioGuidanceVoice.speak(voiceInstructions).collect { speechAnnouncement ->
74
- assertEquals(" Turn right on Market" , speechAnnouncement!! .announcement)
75
- }
74
+ val speechAnnouncement = sut.speak(voiceInstructions)
75
+ assertEquals(" Turn right on Market" , speechAnnouncement!! .announcement)
76
76
}
77
77
78
+ @Test
79
+ fun `should wait until previous instruction finishes playback before playing next one` () =
80
+ coroutineRule.runBlockingTest {
81
+ mockSuccessfulSpeechApi()
82
+ every { voiceInstructionsPlayer.play(any(), any()) } answers {
83
+ launch {
84
+ val speechAnnouncement = firstArg<SpeechAnnouncement >()
85
+ delay(1000 ) // simulate 1 second announcement playback duration
86
+ secondArg<MapboxNavigationConsumer <SpeechAnnouncement >>()
87
+ .accept(speechAnnouncement)
88
+ }
89
+ Unit
90
+ }
91
+
92
+ val played = mutableListOf<SpeechAnnouncement ?>()
93
+ launch {
94
+ listOf (
95
+ VoiceInstructions .builder().announcement(" A" ).build(),
96
+ VoiceInstructions .builder().announcement(" B" ).build()
97
+ ).forEach {
98
+ val announcement = sut.speak(it) // suspend until playback finishes
99
+ played.add(announcement)
100
+ }
101
+ }
102
+ advanceTimeBy(1500 ) // advance time to 50% of announcement B playback time
103
+
104
+ assertEquals(1 , played.size)
105
+ }
106
+
78
107
private fun mockSuccessfulSpeechApi () {
79
108
every { speechApi.generate(any(), any()) } answers {
80
109
val announcementArg = firstArg<VoiceInstructions >().announcement()
81
110
val speechValue = mockk<SpeechValue > {
82
- every { announcement } returns mockk {
83
- every { announcement } returns announcementArg!!
84
- }
111
+ every { announcement } returns SpeechAnnouncement .Builder (announcementArg!! ).build()
85
112
}
86
113
val consumer = secondArg<MapboxNavigationConsumer <Expected <SpeechError , SpeechValue >>>()
87
114
consumer.accept(ExpectedFactory .createValue(speechValue))
0 commit comments