From 9a3935433238bbcb623ea32352d9675e8858d3a3 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Mon, 20 Jun 2022 15:23:37 +0200 Subject: [PATCH] Adding use case to retrieve flow on live summaries given a list of event ids --- .../GetLiveLocationShareSummariesUseCase.kt | 38 ++++++++ ...etLiveLocationShareSummariesUseCaseTest.kt | 86 +++++++++++++++++++ .../GetListOfUserLiveLocationUseCaseTest.kt | 20 ++--- .../test/fakes/FakeLocationSharingService.kt | 14 ++- 4 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/location/live/GetLiveLocationShareSummariesUseCase.kt create mode 100644 vector/src/test/java/im/vector/app/features/location/live/GetLiveLocationShareSummariesUseCaseTest.kt diff --git a/vector/src/main/java/im/vector/app/features/location/live/GetLiveLocationShareSummariesUseCase.kt b/vector/src/main/java/im/vector/app/features/location/live/GetLiveLocationShareSummariesUseCase.kt new file mode 100644 index 0000000000..0753f093e2 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/location/live/GetLiveLocationShareSummariesUseCase.kt @@ -0,0 +1,38 @@ +/* + * 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 + +import androidx.lifecycle.asFlow +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.emptyFlow +import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.session.getRoom +import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary +import javax.inject.Inject + +class GetLiveLocationShareSummariesUseCase @Inject constructor( + private val session: Session, +) { + + fun execute(roomId: String, eventIds: List): Flow> { + return session.getRoom(roomId) + ?.locationSharingService() + ?.getLiveLocationShareSummaries(eventIds) + ?.asFlow() + ?: emptyFlow() + } +} diff --git a/vector/src/test/java/im/vector/app/features/location/live/GetLiveLocationShareSummariesUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/location/live/GetLiveLocationShareSummariesUseCaseTest.kt new file mode 100644 index 0000000000..e0ee2c8039 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/location/live/GetLiveLocationShareSummariesUseCaseTest.kt @@ -0,0 +1,86 @@ +/* + * 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 + +import androidx.lifecycle.asFlow +import im.vector.app.test.fakes.FakeSession +import io.mockk.every +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flowOf +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.room.model.livelocation.LiveLocationShareAggregatedSummary +import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconLocationDataContent + +private const val A_ROOM_ID = "room_id" + +class GetLiveLocationShareSummariesUseCaseTest { + + private val fakeSession = FakeSession() + + private val getLiveLocationShareSummariesUseCase = GetLiveLocationShareSummariesUseCase( + session = fakeSession + ) + + @Before + fun setUp() { + mockkStatic("androidx.lifecycle.FlowLiveDataConversions") + } + + @After + fun tearDown() { + unmockkAll() + } + + @Test + fun `given a room id when calling use case then the current live is stopped with success`() = runTest { + val eventIds = listOf("event_id_1", "event_id_2", "event_id_3") + 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(A_ROOM_ID) + .locationSharingService() + .givenLiveLocationShareSummaries(eventIds, summaries) + every { liveData.asFlow() } returns flowOf(summaries) + + val result = getLiveLocationShareSummariesUseCase.execute(A_ROOM_ID, eventIds).first() + + result shouldBeEqualTo listOf(summary1, summary2, summary3) + } +} 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 index 3835006712..b747b72cb1 100644 --- 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 @@ -30,6 +30,7 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest import org.amshove.kluent.internal.assertEquals +import org.amshove.kluent.shouldBeEqualTo import org.junit.After import org.junit.Before import org.junit.Rule @@ -38,16 +39,17 @@ import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationSh import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconLocationDataContent import org.matrix.android.sdk.api.util.MatrixItem +private const val A_ROOM_ID = "room_id" + class GetListOfUserLiveLocationUseCaseTest { - @get:Rule - val mvRxTestRule = MvRxTestRule() - private val fakeSession = FakeSession() - private val viewStateMapper = mockk() - private val getListOfUserLiveLocationUseCase = GetListOfUserLiveLocationUseCase(fakeSession, viewStateMapper) + private val getListOfUserLiveLocationUseCase = GetListOfUserLiveLocationUseCase( + session = fakeSession, + userLiveLocationViewStateMapper = viewStateMapper + ) @Before fun setUp() { @@ -61,8 +63,6 @@ class GetListOfUserLiveLocationUseCaseTest { @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, @@ -83,7 +83,7 @@ class GetListOfUserLiveLocationUseCaseTest { ) val summaries = listOf(summary1, summary2, summary3) val liveData = fakeSession.roomService() - .getRoom(roomId) + .getRoom(A_ROOM_ID) .locationSharingService() .givenRunningLiveLocationShareSummaries(summaries) @@ -109,8 +109,8 @@ class GetListOfUserLiveLocationUseCaseTest { coEvery { viewStateMapper.map(summary2) } returns viewState2 coEvery { viewStateMapper.map(summary3) } returns null - val viewStates = getListOfUserLiveLocationUseCase.execute(roomId).first() + val viewStates = getListOfUserLiveLocationUseCase.execute(A_ROOM_ID).first() - assertEquals(listOf(viewState1, viewState2), viewStates) + viewStates shouldBeEqualTo listOf(viewState1, viewState2) } } 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 index 0c105a588a..8ad7004c58 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeLocationSharingService.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeLocationSharingService.kt @@ -27,13 +27,23 @@ import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationSh class FakeLocationSharingService : LocationSharingService by mockk() { - fun givenRunningLiveLocationShareSummaries(summaries: List): - LiveData> { + fun givenRunningLiveLocationShareSummaries( + summaries: List + ): LiveData> { return MutableLiveData(summaries).also { every { getRunningLiveLocationShareSummaries() } returns it } } + fun givenLiveLocationShareSummaries( + eventIds: List, + summaries: List + ): LiveData> { + return MutableLiveData(summaries).also { + every { getLiveLocationShareSummaries(eventIds) } returns it + } + } + fun givenStopLiveLocationShareReturns(result: UpdateLiveLocationShareResult) { coEvery { stopLiveLocationShare() } returns result }