Fix unit tests.

This commit is contained in:
Onuray Sahin 2022-05-30 19:21:57 +03:00
parent 84b3d5520c
commit 353f290f0c
2 changed files with 33 additions and 4 deletions

View file

@ -21,7 +21,10 @@ import im.vector.app.features.location.LocationData
import im.vector.app.features.location.LocationSharingServiceConnection
import im.vector.app.test.test
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Rule
@ -60,7 +63,7 @@ class LocationLiveMapViewModelTest {
showStopSharingButton = false
)
)
every { locationServiceConnection.bind(any()) } just runs
every { getListOfUserLiveLocationUseCase.execute(fakeRoomId) } returns flowOf(userLocations)
val viewModel = createViewModel()
@ -72,5 +75,7 @@ class LocationLiveMapViewModelTest {
)
)
.finish()
verify { locationServiceConnection.bind(viewModel) }
}
}

View file

@ -21,18 +21,28 @@ import im.vector.app.features.location.LocationData
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakeLocationPinProvider
import im.vector.app.test.fakes.FakeSession
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.shouldBeEqualTo
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.matrix.android.sdk.api.session.getUser
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
import org.matrix.android.sdk.api.session.user.model.User
import org.matrix.android.sdk.api.util.MatrixItem
import org.matrix.android.sdk.api.util.toMatrixItem
private const val A_USER_ID = "@aUserId:matrix.org"
private const val A_USER_DISPLAY_NAME = "A_USER_DISPLAY_NAME"
private const val A_IS_ACTIVE = true
private const val A_END_OF_LIVE_TIMESTAMP = 123L
private const val A_LOCATION_TIMESTAMP = 122L
private const val A_LATITUDE = 40.05
private const val A_LONGITUDE = 29.24
private const val A_UNCERTAINTY = 30.0
@ -46,6 +56,19 @@ class UserLiveLocationViewStateMapperTest {
private val userLiveLocationViewStateMapper = UserLiveLocationViewStateMapper(locationPinProvider.instance, fakeActiveSessionHolder.instance)
@Before
fun setup() {
mockkStatic("org.matrix.android.sdk.api.util.MatrixItemKt")
val fakeUser = mockk<User>()
every { fakeSession.getUser(A_USER_ID) } returns fakeUser
every { fakeUser.toMatrixItem() } returns MatrixItem.UserItem(id = A_USER_ID, displayName = A_USER_DISPLAY_NAME, avatarUrl = "")
}
@After
fun tearDown() {
unmockkStatic("org.matrix.android.sdk.api.util.MatrixItemKt")
}
@Test
fun `given a summary with invalid data then result is null`() = runTest {
val summary1 = LiveLocationShareAggregatedSummary(
@ -71,7 +94,8 @@ class UserLiveLocationViewStateMapperTest {
val pinDrawable = mockk<Drawable>()
val locationDataContent = MessageBeaconLocationDataContent(
locationInfo = LocationInfo(geoUri = A_GEO_URI)
locationInfo = LocationInfo(geoUri = A_GEO_URI),
unstableTimestampMillis = A_LOCATION_TIMESTAMP
)
val summary = LiveLocationShareAggregatedSummary(
userId = A_USER_ID,
@ -84,7 +108,7 @@ class UserLiveLocationViewStateMapperTest {
val viewState = userLiveLocationViewStateMapper.map(summary)
val expectedViewState = UserLiveLocationViewState(
matrixItem = MatrixItem.UserItem(id = A_USER_ID, displayName = "User 2", avatarUrl = ""),
matrixItem = MatrixItem.UserItem(id = A_USER_ID, displayName = A_USER_DISPLAY_NAME, avatarUrl = ""),
pinDrawable = pinDrawable,
locationData = LocationData(
latitude = A_LATITUDE,
@ -92,7 +116,7 @@ class UserLiveLocationViewStateMapperTest {
uncertainty = A_UNCERTAINTY
),
endOfLiveTimestampMillis = A_END_OF_LIVE_TIMESTAMP,
locationTimestampMillis = A_END_OF_LIVE_TIMESTAMP,
locationTimestampMillis = A_LOCATION_TIMESTAMP,
showStopSharingButton = false
)
viewState shouldBeEqualTo expectedViewState