Skip to content

Commit 14571b9

Browse files
committed
add suspend version of stopRecording method
1 parent a8e23f7 commit 14571b9

File tree

5 files changed

+37
-61
lines changed

5 files changed

+37
-61
lines changed

instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/core/CoreRerouteTest.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import com.mapbox.navigation.instrumentation_tests.utils.coroutines.routesUpdate
3939
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.sdkTest
4040
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.setNavigationRoutesAndWaitForAlternativesUpdate
4141
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.setNavigationRoutesAndWaitForUpdate
42+
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.stopRecording
4243
import com.mapbox.navigation.instrumentation_tests.utils.history.MapboxHistoryTestRule
4344
import com.mapbox.navigation.instrumentation_tests.utils.http.MockDirectionsRefreshHandler
4445
import com.mapbox.navigation.instrumentation_tests.utils.http.MockDirectionsRequestHandler
@@ -53,16 +54,17 @@ import com.mapbox.navigation.testing.ui.utils.getMapboxAccessTokenFromResources
5354
import com.mapbox.navigation.testing.ui.utils.runOnMainSync
5455
import com.mapbox.navigation.utils.internal.logE
5556
import kotlinx.coroutines.CompletableDeferred
57+
import kotlinx.coroutines.Dispatchers
5658
import kotlinx.coroutines.delay
5759
import kotlinx.coroutines.flow.filter
5860
import kotlinx.coroutines.flow.filterNot
5961
import kotlinx.coroutines.flow.first
62+
import kotlinx.coroutines.runBlocking
6063
import org.junit.Assert.assertEquals
6164
import org.junit.Before
6265
import org.junit.Rule
6366
import org.junit.Test
6467
import java.net.URI
65-
import java.util.concurrent.CountDownLatch
6668
import java.util.concurrent.TimeUnit
6769

6870
class CoreRerouteTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.java) {
@@ -192,14 +194,10 @@ class CoreRerouteTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.jav
192194
}
193195
locationTrackingIdlingResource.unregister()
194196

195-
val countDownLatch = CountDownLatch(1)
196-
runOnMainSync {
197-
mapboxNavigation.historyRecorder.stopRecording {
198-
logE("history path=$it", "DEBUG")
199-
countDownLatch.countDown()
200-
}
197+
runBlocking(Dispatchers.Main) {
198+
val historyPath = mapboxNavigation.historyRecorder.stopRecording()
199+
logE("history path=$historyPath", "DEBUG")
201200
}
202-
countDownLatch.await()
203201

204202
// assert results
205203
expectedStates.assert()

instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/core/MapboxHistoryTest.kt

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import com.mapbox.navigation.core.history.model.HistoryEventSetRoute
2323
import com.mapbox.navigation.core.history.model.HistoryEventUpdateLocation
2424
import com.mapbox.navigation.instrumentation_tests.activity.EmptyTestActivity
2525
import com.mapbox.navigation.instrumentation_tests.utils.MapboxNavigationRule
26+
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.sdkTest
27+
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.stopRecording
2628
import com.mapbox.navigation.instrumentation_tests.utils.idling.RouteProgressStateIdlingResource
2729
import com.mapbox.navigation.instrumentation_tests.utils.location.MockLocationReplayerRule
2830
import com.mapbox.navigation.instrumentation_tests.utils.routes.MockRoute
@@ -33,6 +35,7 @@ import com.mapbox.navigation.testing.ui.utils.runOnMainSync
3335
import junit.framework.TestCase.assertEquals
3436
import junit.framework.TestCase.assertNull
3537
import junit.framework.TestCase.assertTrue
38+
import kotlinx.coroutines.runBlocking
3639
import org.junit.After
3740
import org.junit.Assert.assertFalse
3841
import org.junit.Assert.assertNotNull
@@ -41,7 +44,6 @@ import org.junit.Rule
4144
import org.junit.Test
4245
import java.io.File
4346
import java.io.InputStream
44-
import java.util.concurrent.CountDownLatch
4547

4648
class MapboxHistoryTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.java) {
4749

@@ -143,13 +145,7 @@ class MapboxHistoryTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.j
143145
Espresso.onIdle()
144146
routeCompleteIdlingResource.unregister()
145147

146-
var filePath: String? = null
147-
val countDownLatch = CountDownLatch(1)
148-
mapboxNavigation.historyRecorder.stopRecording { filePathFromNative ->
149-
filePath = filePathFromNative
150-
countDownLatch.countDown()
151-
}
152-
countDownLatch.await()
148+
val filePath = runBlocking { mapboxNavigation.historyRecorder.stopRecording() }
153149

154150
assertNotNull(filePath)
155151
verifyHistoryEvents(filePath!!, mockRoute, routeOptions)
@@ -208,13 +204,7 @@ class MapboxHistoryTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.j
208204
Espresso.onIdle()
209205
routeCompleteIdlingResource.unregister()
210206

211-
var filePath: String? = null
212-
val countDownLatch = CountDownLatch(1)
213-
mapboxNavigation.historyRecorder.stopRecording { filePathFromNative ->
214-
filePath = filePathFromNative
215-
countDownLatch.countDown()
216-
}
217-
countDownLatch.await()
207+
val filePath = runBlocking { mapboxNavigation.historyRecorder.stopRecording() }
218208

219209
assertNotNull(filePath)
220210
verifyHistoryEvents(filePath!!, mockRoute, routeOptions)
@@ -336,7 +326,7 @@ class MapboxHistoryTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.j
336326
}
337327

338328
@Test
339-
fun verify_identifying_events_can_be_found() {
329+
fun verify_identifying_events_can_be_found() = sdkTest {
340330
mapboxNavigation.startTripSession()
341331
val firstJson = """{"identifier":"first"}"""
342332
val secondJson = """{"identifier":"second"}"""
@@ -354,17 +344,10 @@ class MapboxHistoryTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.j
354344
assertEquals(thirdJson, thirdFile.findCustomEvent().properties)
355345
}
356346

357-
private fun startAndStopRecording(eventJson: String): String {
347+
private suspend fun startAndStopRecording(eventJson: String): String {
358348
mapboxNavigation.historyRecorder.startRecording()
359349
mapboxNavigation.historyRecorder.pushHistory(CUSTOM_EVENT_TYPE, eventJson)
360-
var filePath: String? = null
361-
val countDownLatch = CountDownLatch(1)
362-
mapboxNavigation.historyRecorder.stopRecording { filePathFromNative ->
363-
filePath = filePathFromNative
364-
countDownLatch.countDown()
365-
}
366-
countDownLatch.await()
367-
return filePath!!
350+
return mapboxNavigation.historyRecorder.stopRecording()!!
368351
}
369352

370353
private fun historyReaderFromAssetFile(

instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/core/RouteAlternativesTest.kt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.mapbox.navigation.core.routealternatives.RouteAlternativesError
2020
import com.mapbox.navigation.instrumentation_tests.R
2121
import com.mapbox.navigation.instrumentation_tests.activity.EmptyTestActivity
2222
import com.mapbox.navigation.instrumentation_tests.utils.MapboxNavigationRule
23+
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.stopRecording
2324
import com.mapbox.navigation.instrumentation_tests.utils.history.MapboxHistoryTestRule
2425
import com.mapbox.navigation.instrumentation_tests.utils.http.MockDirectionsRequestHandler
2526
import com.mapbox.navigation.instrumentation_tests.utils.idling.FirstLocationIdlingResource
@@ -31,6 +32,8 @@ import com.mapbox.navigation.testing.ui.BaseTest
3132
import com.mapbox.navigation.testing.ui.utils.getMapboxAccessTokenFromResources
3233
import com.mapbox.navigation.testing.ui.utils.runOnMainSync
3334
import com.mapbox.navigation.utils.internal.logE
35+
import kotlinx.coroutines.Dispatchers
36+
import kotlinx.coroutines.runBlocking
3437
import org.junit.Assert.assertEquals
3538
import org.junit.Assert.assertFalse
3639
import org.junit.Assert.assertNotEquals
@@ -39,7 +42,6 @@ import org.junit.Assert.assertTrue
3942
import org.junit.Before
4043
import org.junit.Rule
4144
import org.junit.Test
42-
import java.util.concurrent.CountDownLatch
4345
import java.util.concurrent.TimeUnit
4446

4547
/**
@@ -117,14 +119,10 @@ class RouteAlternativesTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::cla
117119
firstAlternative.unregister()
118120
assertTrue(firstAlternative.calledOnMainThread)
119121

120-
val countDownLatch = CountDownLatch(1)
121-
runOnMainSync {
122-
mapboxNavigation.historyRecorder.stopRecording {
123-
logE("history path=$it", LOG_CATEGORY)
124-
countDownLatch.countDown()
125-
}
122+
runBlocking(Dispatchers.Main) {
123+
val historyPath = mapboxNavigation.historyRecorder.stopRecording()
124+
logE("history path=$historyPath", LOG_CATEGORY)
126125
}
127-
countDownLatch.await()
128126

129127
// Verify alternative routes events were triggered.
130128
assertEquals(2, routes.size)
@@ -171,14 +169,10 @@ class RouteAlternativesTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::cla
171169

172170
assertTrue(firstAlternative.calledOnMainThread)
173171

174-
val countDownLatch = CountDownLatch(1)
175-
runOnMainSync {
176-
mapboxNavigation.historyRecorder.stopRecording {
177-
logE("history path=$it", LOG_CATEGORY)
178-
countDownLatch.countDown()
179-
}
172+
runBlocking(Dispatchers.Main) {
173+
val historyPath = mapboxNavigation.historyRecorder.stopRecording()
174+
logE("history path=$historyPath", LOG_CATEGORY)
180175
}
181-
countDownLatch.await()
182176

183177
// Verify alternative routes events were triggered.
184178
assertEquals(2, routes.size)
@@ -278,14 +272,11 @@ class RouteAlternativesTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::cla
278272
}
279273
nextAlternativeObserver.unregister()
280274

281-
val countDownLatch = CountDownLatch(1)
282-
runOnMainSync {
275+
runBlocking(Dispatchers.Main) {
283276
mapboxNavigation.historyRecorder.stopRecording {
284277
logE("history path=$it", LOG_CATEGORY)
285-
countDownLatch.countDown()
286278
}
287279
}
288-
countDownLatch.await()
289280

290281
// Verify alternative routes events were triggered.
291282
firstAlternative.verifyOnRouteAlternativesAndProgressReceived()

instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/utils/coroutines/Adapters.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.mapbox.navigation.core.RoutesSetError
1919
import com.mapbox.navigation.core.RoutesSetSuccess
2020
import com.mapbox.navigation.core.directions.session.RoutesObserver
2121
import com.mapbox.navigation.core.directions.session.RoutesUpdatedResult
22+
import com.mapbox.navigation.core.history.MapboxHistoryRecorder
2223
import com.mapbox.navigation.core.preview.RoutesPreviewObserver
2324
import com.mapbox.navigation.core.preview.RoutesPreviewUpdate
2425
import com.mapbox.navigation.core.trip.session.BannerInstructionsObserver
@@ -180,3 +181,9 @@ suspend fun MapboxNavigation.voiceInstructions(): Flow<VoiceInstructions> {
180181
}
181182
}
182183
}
184+
185+
suspend fun MapboxHistoryRecorder.stopRecording(): String? = suspendCoroutine { cont ->
186+
stopRecording { path ->
187+
cont.resume(path)
188+
}
189+
}

instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/utils/history/MapboxHistoryTestRule.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import android.provider.MediaStore
88
import androidx.annotation.RequiresApi
99
import androidx.test.platform.app.InstrumentationRegistry
1010
import com.mapbox.navigation.core.history.MapboxHistoryRecorder
11-
import com.mapbox.navigation.testing.ui.utils.runOnMainSync
11+
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.stopRecording
1212
import com.mapbox.navigation.utils.internal.logE
13+
import kotlinx.coroutines.Dispatchers
14+
import kotlinx.coroutines.runBlocking
1315
import org.junit.rules.TestWatcher
1416
import org.junit.runner.Description
1517
import java.io.File
16-
import java.util.concurrent.CountDownLatch
1718

1819
/**
1920
* Add this TestRule to your test and the directory will be saved
@@ -70,14 +71,10 @@ class MapboxHistoryTestRule : TestWatcher() {
7071
try {
7172
runner()
7273
} catch (t: Throwable) {
73-
val countDownLatch = CountDownLatch(1)
74-
runOnMainSync {
75-
historyRecorder.stopRecording {
76-
logE("$message history path=$it", "DEBUG")
77-
countDownLatch.countDown()
78-
}
74+
runBlocking(Dispatchers.Main) {
75+
val path = historyRecorder.stopRecording()
76+
logE("$message history path=$path", "DEBUG")
7977
}
80-
countDownLatch.await()
8178
throw t
8279
}
8380
}

0 commit comments

Comments
 (0)