diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollDataSource.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollDataSource.kt index 60b53a90b4..3a65297fde 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollDataSource.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollDataSource.kt @@ -25,7 +25,6 @@ import org.matrix.android.sdk.api.session.room.poll.PollHistoryService import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent import javax.inject.Inject -// TODO add unit tests class RoomPollDataSource @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, ) { diff --git a/vector/src/test/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollDataSourceTest.kt b/vector/src/test/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollDataSourceTest.kt new file mode 100644 index 0000000000..11006b10e8 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollDataSourceTest.kt @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2023 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.roomprofile.polls.list.data + +import im.vector.app.test.fakes.FakeActiveSessionHolder +import im.vector.app.test.fakes.FakeFlowLiveDataConversions +import im.vector.app.test.fakes.FakePollHistoryService +import im.vector.app.test.fakes.givenAsFlow +import io.mockk.unmockkAll +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.test.runTest +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Test +import org.matrix.android.sdk.api.session.room.poll.LoadedPollsStatus +import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent + +private const val A_ROOM_ID = "room-id" + +internal class RoomPollDataSourceTest { + + private val fakeActiveSessionHolder = FakeActiveSessionHolder() + + private val roomPollDataSource = RoomPollDataSource( + activeSessionHolder = fakeActiveSessionHolder.instance, + ) + + @Test + fun `given poll history service when dispose then correct method of service is called`() { + // Given + val fakePollHistoryService = givenPollHistoryService() + fakePollHistoryService.givenDispose() + + // When + roomPollDataSource.dispose(A_ROOM_ID) + + // Then + fakePollHistoryService.verifyDispose() + } + + @Test + fun `given poll history service when get polls then correct method of service is called and correct result is returned`() = runTest { + // Given + val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions() + fakeFlowLiveDataConversions.setup() + val fakePollHistoryService = givenPollHistoryService() + val pollEvents = listOf() + fakePollHistoryService + .givenGetPollsReturns(pollEvents) + .givenAsFlow() + + // When + val result = roomPollDataSource.getPolls(A_ROOM_ID).firstOrNull() + + // Then + result shouldBeEqualTo pollEvents + fakePollHistoryService.verifyGetPolls() + unmockkAll() + } + + @Test + fun `given poll history service when get loaded polls then correct method of service is called and correct result is returned`() = runTest { + // Given + val fakePollHistoryService = givenPollHistoryService() + val aLoadedPollsStatus = givenALoadedPollsStatus() + fakePollHistoryService.givenGetLoadedPollsStatusReturns(aLoadedPollsStatus) + + // When + val result = roomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) + + // Then + result shouldBeEqualTo aLoadedPollsStatus + fakePollHistoryService.verifyGetLoadedPollsStatus() + } + + @Test + fun `given poll history service when load more then correct method of service is called and correct result is returned`() = runTest { + // Given + val fakePollHistoryService = givenPollHistoryService() + val aLoadedPollsStatus = givenALoadedPollsStatus() + fakePollHistoryService.givenLoadMoreReturns(aLoadedPollsStatus) + + // When + val result = roomPollDataSource.loadMorePolls(A_ROOM_ID) + + // Then + result shouldBeEqualTo aLoadedPollsStatus + fakePollHistoryService.verifyLoadMore() + } + + @Test + fun `given poll history service when sync polls then correct method of service is called`() = runTest { + // Given + val fakePollHistoryService = givenPollHistoryService() + fakePollHistoryService.givenSyncPollsSuccess() + + // When + roomPollDataSource.syncPolls(A_ROOM_ID) + + // Then + fakePollHistoryService.verifySyncPolls() + } + + private fun givenPollHistoryService(): FakePollHistoryService { + return fakeActiveSessionHolder + .fakeSession + .fakeRoomService + .getRoom(A_ROOM_ID) + .pollHistoryService() + } + + private fun givenALoadedPollsStatus() = LoadedPollsStatus( + canLoadMore = true, + nbSyncedDays = 10, + hasCompletedASyncBackward = true, + ) +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakePollHistoryService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakePollHistoryService.kt new file mode 100644 index 0000000000..c934c3acde --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakePollHistoryService.kt @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2023 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.coEvery +import io.mockk.coJustRun +import io.mockk.coVerify +import io.mockk.every +import io.mockk.justRun +import io.mockk.mockk +import io.mockk.verify +import org.matrix.android.sdk.api.session.room.poll.LoadedPollsStatus +import org.matrix.android.sdk.api.session.room.poll.PollHistoryService +import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent + +class FakePollHistoryService : PollHistoryService by mockk() { + + fun givenDispose() { + justRun { dispose() } + } + + fun verifyDispose() { + verify { dispose() } + } + + fun givenGetPollsReturns(events: List): LiveData> { + return MutableLiveData(events).also { + every { getPollEvents() } returns it + } + } + + fun verifyGetPolls() { + verify { getPollEvents() } + } + + fun givenGetLoadedPollsStatusReturns(status: LoadedPollsStatus) { + coEvery { getLoadedPollsStatus() } returns status + } + + fun verifyGetLoadedPollsStatus() { + coVerify { getLoadedPollsStatus() } + } + + fun givenLoadMoreReturns(status: LoadedPollsStatus) { + coEvery { loadMore() } returns status + } + + fun verifyLoadMore() { + coVerify { loadMore() } + } + + fun givenSyncPollsSuccess() { + coJustRun { syncPolls() } + } + + fun verifySyncPolls() { + coVerify { syncPolls() } + } +} 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 index 7835c314ef..d3703f11c4 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeRoom.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeRoom.kt @@ -25,6 +25,7 @@ class FakeRoom( private val fakeTimelineService: FakeTimelineService = FakeTimelineService(), private val fakeRelationService: FakeRelationService = FakeRelationService(), private val fakeStateService: FakeStateService = FakeStateService(), + private val fakePollHistoryService: FakePollHistoryService = FakePollHistoryService(), ) : Room by mockk() { override fun locationSharingService() = fakeLocationSharingService @@ -36,4 +37,6 @@ class FakeRoom( override fun relationService() = fakeRelationService override fun stateService() = fakeStateService + + override fun pollHistoryService() = fakePollHistoryService }