Skip to content

Commit

Permalink
Merge pull request #435 from ably/feature/fix-mapbox-start-stop-crash
Browse files Browse the repository at this point in the history
Fix crash when Mapbox is started and stopped without starting a trip
  • Loading branch information
QuintinWillison authored Sep 17, 2021
2 parents cd52f3c + 8dd9a78 commit 63062ac
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.ably.tracking.publisher

import android.app.Notification
import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.ably.tracking.connection.Authentication
import com.ably.tracking.connection.ConnectionConfiguration
import com.ably.tracking.test.android.common.NOTIFICATION_CHANNEL_ID
import com.ably.tracking.test.android.common.createNotificationChannel
import com.google.gson.Gson
import org.junit.Test
import org.junit.runner.RunWith

private const val MAPBOX_ACCESS_TOKEN = BuildConfig.MAPBOX_ACCESS_TOKEN
private const val CLIENT_ID = "IntegrationTestsClient"
private const val ABLY_API_KEY = BuildConfig.ABLY_API_KEY

@RunWith(AndroidJUnit4::class)
class MapboxTests {
private val gson = Gson()

@Test
fun shouldNotThrowErrorWhenMapboxIsStartedAndStoppedWithoutStartingTrip() {
// given
val context = InstrumentationRegistry.getInstrumentation().targetContext
createNotificationChannel(context)
val mapbox: Mapbox = createMapbox(context)

// when
mapbox.stopAndClose()

// then
}

private fun createMapbox(context: Context) =
DefaultMapbox(
context,
MapConfiguration(MAPBOX_ACCESS_TOKEN),
ConnectionConfiguration(Authentication.basic(CLIENT_ID, ABLY_API_KEY)),
LocationSourceRaw.create(getLocationData(context), null),
null,
object : PublisherNotificationProvider {
override fun getNotification(): Notification =
NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setContentTitle("TEST")
.setContentText("Test")
.setSmallIcon(R.drawable.aat_logo)
.build()
},
12345
)

private fun getLocationData(context: Context): LocationHistoryData {
val historyString = context.assets.open("location_history_small.txt").use { String(it.readBytes()) }
return gson.fromJson(historyString, LocationHistoryData::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,13 @@ internal class DefaultMapbox(
stopTripSession()
mapboxReplayer?.finish()
val tripHistoryString = retrieveHistory()
val historyEvents = ReplayHistoryMapper().mapToReplayEvents(tripHistoryString)
locationHistoryListener?.invoke(LocationHistoryData(historyEvents.toGeoJsonMessages()))
// MapBox's mapToReplayEvents method crashes if passed an empty string,
// so check it's not empty to prevent that.
// (see: https://github.com/ably/ably-asset-tracking-android/issues/434)
if (tripHistoryString.isNotEmpty()) {
val historyEvents = ReplayHistoryMapper().mapToReplayEvents(tripHistoryString)
locationHistoryListener?.invoke(LocationHistoryData(historyEvents.toGeoJsonMessages()))
}
onDestroy()
}
}
Expand Down

0 comments on commit 63062ac

Please sign in to comment.