diff --git a/vector/src/main/java/im/vector/app/features/location/live/map/GetListOfUserLiveLocationUseCase.kt b/vector/src/main/java/im/vector/app/features/location/live/map/GetListOfUserLiveLocationUseCase.kt index ca4f6859ee..91f6999e2c 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/map/GetListOfUserLiveLocationUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/map/GetListOfUserLiveLocationUseCase.kt @@ -29,7 +29,6 @@ class GetListOfUserLiveLocationUseCase @Inject constructor( private val userLiveLocationViewStateMapper: UserLiveLocationViewStateMapper, ) { - // TODO add unit tests fun execute(roomId: String): Flow> { return session.getRoom(roomId) ?.locationSharingService() diff --git a/vector/src/test/java/im/vector/app/features/location/live/map/GetListOfUserLiveLocationUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/location/live/map/GetListOfUserLiveLocationUseCaseTest.kt new file mode 100644 index 0000000000..765eee4937 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/location/live/map/GetListOfUserLiveLocationUseCaseTest.kt @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.location.live.map + +import androidx.lifecycle.asFlow +import com.airbnb.mvrx.test.MvRxTestRule +import im.vector.app.features.location.LocationData +import im.vector.app.test.fakes.FakeSession +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkStatic +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.amshove.kluent.internal.assertEquals +import org.junit.After +import org.junit.Before +import org.junit.Rule +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.MessageBeaconLocationDataContent + +class GetListOfUserLiveLocationUseCaseTest { + + @get:Rule + val mvRxTestRule = MvRxTestRule() + + private val fakeSession = FakeSession() + + private val viewStateMapper = mockk() + + private val getListOfUserLiveLocationUseCase = GetListOfUserLiveLocationUseCase(fakeSession, viewStateMapper) + + @Before + fun setUp() { + mockkStatic("androidx.lifecycle.FlowLiveDataConversions") + } + + @After + fun tearDown() { + unmockkStatic("androidx.lifecycle.FlowLiveDataConversions") + } + + @Test + fun `given a room id then the correct flow of view states list is collected`() = runTest { + val roomId = "roomId" + + val summary1 = LiveLocationShareAggregatedSummary( + userId = "userId1", + isActive = true, + endOfLiveTimestampMillis = 123, + lastLocationDataContent = MessageBeaconLocationDataContent() + ) + val summary2 = LiveLocationShareAggregatedSummary( + userId = "userId2", + isActive = true, + endOfLiveTimestampMillis = 1234, + lastLocationDataContent = MessageBeaconLocationDataContent() + ) + val summary3 = LiveLocationShareAggregatedSummary( + userId = "userId3", + isActive = true, + endOfLiveTimestampMillis = 1234, + lastLocationDataContent = MessageBeaconLocationDataContent() + ) + val summaries = listOf(summary1, summary2, summary3) + val liveData = fakeSession.roomService() + .getRoom(roomId) + .locationSharingService() + .givenRunningLiveLocationShareSummaries(summaries) + + every { liveData.asFlow() } returns flowOf(summaries) + + val viewState1 = UserLiveLocationViewState( + userId = "userId1", + pinDrawable = mockk(), + locationData = LocationData(latitude = 1.0, longitude = 2.0, uncertainty = null), + endOfLiveTimestampMillis = 123 + ) + val viewState2 = UserLiveLocationViewState( + userId = "userId2", + pinDrawable = mockk(), + locationData = LocationData(latitude = 1.0, longitude = 2.0, uncertainty = null), + endOfLiveTimestampMillis = 1234 + ) + coEvery { viewStateMapper.map(summary1) } returns viewState1 + coEvery { viewStateMapper.map(summary2) } returns viewState2 + coEvery { viewStateMapper.map(summary3) } returns null + + val viewStates = getListOfUserLiveLocationUseCase.execute(roomId).first() + + assertEquals(listOf(viewState1, viewState2), viewStates) + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeLocationSharingService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeLocationSharingService.kt new file mode 100644 index 0000000000..2cd98c086c --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeLocationSharingService.kt @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.test.fakes + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import io.mockk.every +import io.mockk.mockk +import org.matrix.android.sdk.api.session.room.location.LocationSharingService +import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary + +class FakeLocationSharingService : LocationSharingService by mockk() { + + fun givenRunningLiveLocationShareSummaries(summaries: List): + LiveData> { + return MutableLiveData(summaries).also { + every { getRunningLiveLocationShareSummaries() } returns it + } + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeRoom.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeRoom.kt new file mode 100644 index 0000000000..ff87ab0fde --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeRoom.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.test.fakes + +import io.mockk.mockk +import org.matrix.android.sdk.api.session.room.Room + +class FakeRoom( + private val fakeLocationSharingService: FakeLocationSharingService = FakeLocationSharingService(), +) : Room by mockk() { + + override fun locationSharingService() = fakeLocationSharingService +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeRoomService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeRoomService.kt new file mode 100644 index 0000000000..b09256f747 --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeRoomService.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.test.fakes + +import io.mockk.mockk +import org.matrix.android.sdk.api.session.room.RoomService + +class FakeRoomService( + private val fakeRoom: FakeRoom = FakeRoom() +) : RoomService by mockk() { + + override fun getRoom(roomId: String) = fakeRoom +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt index 5f02879e65..cf94493f61 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt @@ -33,7 +33,8 @@ class FakeSession( val fakeCryptoService: FakeCryptoService = FakeCryptoService(), val fakeProfileService: FakeProfileService = FakeProfileService(), val fakeHomeServerCapabilitiesService: FakeHomeServerCapabilitiesService = FakeHomeServerCapabilitiesService(), - val fakeSharedSecretStorageService: FakeSharedSecretStorageService = FakeSharedSecretStorageService() + val fakeSharedSecretStorageService: FakeSharedSecretStorageService = FakeSharedSecretStorageService(), + private val fakeRoomService: FakeRoomService = FakeRoomService(), ) : Session by mockk(relaxed = true) { init { @@ -48,6 +49,7 @@ class FakeSession( override fun profileService(): ProfileService = fakeProfileService override fun homeServerCapabilitiesService(): HomeServerCapabilitiesService = fakeHomeServerCapabilitiesService override fun sharedSecretStorageService() = fakeSharedSecretStorageService + override fun roomService() = fakeRoomService fun givenVectorStore(vectorSessionStore: VectorSessionStore) { coEvery {