Improving view state mapper

This commit is contained in:
Maxime NATUREL 2022-05-30 10:37:58 +02:00
parent 066c540eb7
commit 7f2279c8a8
1 changed files with 26 additions and 39 deletions

View File

@ -18,37 +18,29 @@ package im.vector.app.features.location.live.map
import android.graphics.drawable.Drawable
import im.vector.app.features.location.LocationData
import im.vector.app.features.location.toLocationData
import im.vector.app.test.fakes.FakeLocationPinProvider
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.internal.assertEquals
import org.junit.After
import org.junit.Before
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary
import org.matrix.android.sdk.api.session.room.model.message.LocationInfo
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconLocationDataContent
private const val A_USER_ID = "aUserId"
private const val A_IS_ACTIVE = true
private const val A_END_OF_LIVE_TIMESTAMP = 123L
private const val A_LATITUDE = 40.05
private const val A_LONGITUDE = 29.24
private const val A_UNCERTAINTY = 30.0
private const val A_GEO_URI = "geo:$A_LATITUDE,$A_LONGITUDE;$A_UNCERTAINTY"
class UserLiveLocationViewStateMapperTest {
private val locationPinProvider = FakeLocationPinProvider()
private val userLiveLocationViewStateMapper = UserLiveLocationViewStateMapper(locationPinProvider.instance)
@Before
fun setUp() {
mockkStatic("im.vector.app.features.location.LocationDataKt")
}
@After
fun tearDown() {
unmockkStatic("im.vector.app.features.location.LocationDataKt")
}
@Test
fun `given a summary with invalid data then result is null`() = runTest {
val summary1 = LiveLocationShareAggregatedSummary(
@ -58,49 +50,44 @@ class UserLiveLocationViewStateMapperTest {
lastLocationDataContent = null,
)
val summary2 = summary1.copy(userId = "")
val summaryWithoutLocation = summary1.copy(userId = "userId")
val summaryWithoutLocation = summary1.copy(userId = A_USER_ID)
val viewState1 = userLiveLocationViewStateMapper.map(summary1)
val viewState2 = userLiveLocationViewStateMapper.map(summary2)
val viewState3 = userLiveLocationViewStateMapper.map(summaryWithoutLocation)
assertEquals(null, viewState1)
assertEquals(null, viewState2)
assertEquals(null, viewState3)
viewState1 shouldBeEqualTo null
viewState2 shouldBeEqualTo null
viewState3 shouldBeEqualTo null
}
@Test
fun `given a summary with valid data then result is correctly mapped`() = runTest {
val geoUri = "geoUri"
val userId = "userId"
val pinDrawable = mockk<Drawable>()
val endOfLiveTimestampMillis = 123L
val locationDataContent = MessageBeaconLocationDataContent(
locationInfo = LocationInfo(geoUri = geoUri)
locationInfo = LocationInfo(geoUri = A_GEO_URI)
)
val summary = LiveLocationShareAggregatedSummary(
userId = userId,
isActive = true,
endOfLiveTimestampMillis = endOfLiveTimestampMillis,
userId = A_USER_ID,
isActive = A_IS_ACTIVE,
endOfLiveTimestampMillis = A_END_OF_LIVE_TIMESTAMP,
lastLocationDataContent = locationDataContent,
)
val locationData = LocationData(
latitude = 1.0,
longitude = 2.0,
uncertainty = null
)
every { geoUri.toLocationData() } returns locationData
locationPinProvider.givenCreateForUserId(userId, pinDrawable)
locationPinProvider.givenCreateForUserId(A_USER_ID, pinDrawable)
val viewState = userLiveLocationViewStateMapper.map(summary)
val expectedViewState = UserLiveLocationViewState(
userId = userId,
userId = A_USER_ID,
pinDrawable = pinDrawable,
locationData = locationData,
endOfLiveTimestampMillis = endOfLiveTimestampMillis
locationData = LocationData(
latitude = A_LATITUDE,
longitude = A_LONGITUDE,
uncertainty = A_UNCERTAINTY
),
endOfLiveTimestampMillis = A_END_OF_LIVE_TIMESTAMP
)
assertEquals(expectedViewState, viewState)
viewState shouldBeEqualTo expectedViewState
}
}