From d041de3217065ad53b573ea5e3357bba09b998f4 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 6 Nov 2024 10:11:50 +0100 Subject: [PATCH] Update tests --- .../call/test/FakeCurrentCallObserver.kt | 22 +++ .../impl/timeline/TimelinePresenterTest.kt | 2 + features/roomcall/impl/build.gradle.kts | 1 + .../impl/RoomCallStatePresenterTest.kt | 169 +++++++++++++++++- 4 files changed, 186 insertions(+), 8 deletions(-) create mode 100644 features/call/test/src/main/kotlin/io/element/android/features/call/test/FakeCurrentCallObserver.kt diff --git a/features/call/test/src/main/kotlin/io/element/android/features/call/test/FakeCurrentCallObserver.kt b/features/call/test/src/main/kotlin/io/element/android/features/call/test/FakeCurrentCallObserver.kt new file mode 100644 index 0000000000..d153047c66 --- /dev/null +++ b/features/call/test/src/main/kotlin/io/element/android/features/call/test/FakeCurrentCallObserver.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2024 New Vector Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only + * Please see LICENSE in the repository root for full details. + */ + +package io.element.android.features.call.test + +import io.element.android.features.call.api.CurrentCall +import io.element.android.features.call.api.CurrentCallObserver +import kotlinx.coroutines.flow.MutableStateFlow + +class FakeCurrentCallObserver( + initialValue: CurrentCall = CurrentCall.None, +) : CurrentCallObserver { + override val currentCall = MutableStateFlow(initialValue) + + fun setCurrentCall(value: CurrentCall) { + currentCall.value = value + } +} diff --git a/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/TimelinePresenterTest.kt b/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/TimelinePresenterTest.kt index 74df8ee46c..d153dd5743 100644 --- a/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/TimelinePresenterTest.kt +++ b/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/TimelinePresenterTest.kt @@ -27,6 +27,7 @@ import io.element.android.features.poll.api.actions.EndPollAction import io.element.android.features.poll.api.actions.SendPollResponseAction import io.element.android.features.poll.test.actions.FakeEndPollAction import io.element.android.features.poll.test.actions.FakeSendPollResponseAction +import io.element.android.features.roomcall.api.aStandByCallState import io.element.android.libraries.matrix.api.core.EventId import io.element.android.libraries.matrix.api.core.UniqueId import io.element.android.libraries.matrix.api.room.MatrixRoomMembersState @@ -685,5 +686,6 @@ internal fun TestScope.createTimelinePresenter( timelineController = TimelineController(room), resolveVerifiedUserSendFailurePresenter = { aResolveVerifiedUserSendFailureState() }, typingNotificationPresenter = { aTypingNotificationState() }, + roomCallStatePresenter = { aStandByCallState() }, ) } diff --git a/features/roomcall/impl/build.gradle.kts b/features/roomcall/impl/build.gradle.kts index adb5b07bf2..6ac4ff934e 100644 --- a/features/roomcall/impl/build.gradle.kts +++ b/features/roomcall/impl/build.gradle.kts @@ -31,6 +31,7 @@ dependencies { testImplementation(libs.test.truth) testImplementation(libs.test.turbine) testImplementation(projects.libraries.matrix.test) + testImplementation(projects.features.call.test) testImplementation(projects.tests.testutils) testImplementation(libs.androidx.compose.ui.test.junit) testReleaseImplementation(libs.androidx.compose.ui.test.manifest) diff --git a/features/roomcall/impl/src/test/kotlin/io/element/android/features/roomcall/impl/RoomCallStatePresenterTest.kt b/features/roomcall/impl/src/test/kotlin/io/element/android/features/roomcall/impl/RoomCallStatePresenterTest.kt index ae6b062738..aa4a0eb676 100644 --- a/features/roomcall/impl/src/test/kotlin/io/element/android/features/roomcall/impl/RoomCallStatePresenterTest.kt +++ b/features/roomcall/impl/src/test/kotlin/io/element/android/features/roomcall/impl/RoomCallStatePresenterTest.kt @@ -8,6 +8,9 @@ package io.element.android.features.roomcall.impl import com.google.common.truth.Truth.assertThat +import io.element.android.features.call.api.CurrentCall +import io.element.android.features.call.api.CurrentCallObserver +import io.element.android.features.call.test.FakeCurrentCallObserver import io.element.android.features.roomcall.api.RoomCallState import io.element.android.libraries.matrix.api.room.MatrixRoom import io.element.android.libraries.matrix.test.room.FakeMatrixRoom @@ -17,30 +20,180 @@ import kotlinx.coroutines.test.runTest import org.junit.Test class RoomCallStatePresenterTest { + @Test + fun `present - initial state`() = runTest { + val room = FakeMatrixRoom( + canUserJoinCallResult = { Result.success(false) }, + ) + val presenter = createRoomCallStatePresenter(matrixRoom = room) + presenter.test { + val initialState = awaitItem() + assertThat(initialState).isEqualTo( + RoomCallState.StandBy( + canStartCall = false, + ) + ) + } + } + + @Test + fun `present - initial state - user can join call`() = runTest { + val room = FakeMatrixRoom( + canUserJoinCallResult = { Result.success(true) }, + ) + val presenter = createRoomCallStatePresenter(matrixRoom = room) + presenter.test { + skipItems(1) + val initialState = awaitItem() + assertThat(initialState).isEqualTo( + RoomCallState.StandBy( + canStartCall = true, + ) + ) + } + } + @Test fun `present - call is disabled if user cannot join it even if there is an ongoing call`() = runTest { val room = FakeMatrixRoom( canUserJoinCallResult = { Result.success(false) }, - canUserSendMessageResult = { _, _ -> Result.success(true) }, - canRedactOwnResult = { Result.success(true) }, - canRedactOtherResult = { Result.success(true) }, - typingNoticeResult = { Result.success(Unit) }, - canUserPinUnpinResult = { Result.success(true) }, ).apply { givenRoomInfo(aRoomInfo(hasRoomCall = true)) } val presenter = createRoomCallStatePresenter(matrixRoom = room) presenter.test { - val initialState = awaitItem() - assertThat(initialState).isEqualTo(RoomCallState.OnGoing(canJoinCall = false)) + skipItems(1) + assertThat(awaitItem()).isEqualTo( + RoomCallState.OnGoing( + canJoinCall = false, + isUserInTheCall = false, + isUserLocallyInTheCall = false, + ) + ) + } + } + + @Test + fun `present - user has joined the call on another session`() = runTest { + val room = FakeMatrixRoom( + canUserJoinCallResult = { Result.success(true) }, + ).apply { + givenRoomInfo( + aRoomInfo( + hasRoomCall = true, + activeRoomCallParticipants = listOf(sessionId), + ) + ) + } + val presenter = createRoomCallStatePresenter(matrixRoom = room) + presenter.test { + skipItems(1) + assertThat(awaitItem()).isEqualTo( + RoomCallState.OnGoing( + canJoinCall = true, + isUserInTheCall = true, + isUserLocallyInTheCall = false, + ) + ) + } + } + + @Test + fun `present - user has joined the call locally`() = runTest { + val room = FakeMatrixRoom( + canUserJoinCallResult = { Result.success(true) }, + ).apply { + givenRoomInfo( + aRoomInfo( + hasRoomCall = true, + activeRoomCallParticipants = listOf(sessionId), + ) + ) + } + val presenter = createRoomCallStatePresenter( + matrixRoom = room, + currentCallObserver = FakeCurrentCallObserver(initialValue = CurrentCall.RoomCall(room.roomId)), + ) + presenter.test { + skipItems(1) + assertThat(awaitItem()).isEqualTo( + RoomCallState.OnGoing( + canJoinCall = true, + isUserInTheCall = true, + isUserLocallyInTheCall = true, + ) + ) + } + } + + @Test + fun `present - user leaves the call`() = runTest { + val room = FakeMatrixRoom( + canUserJoinCallResult = { Result.success(true) }, + ).apply { + givenRoomInfo( + aRoomInfo( + hasRoomCall = true, + activeRoomCallParticipants = listOf(sessionId), + ) + ) + } + val currentCallObserver = FakeCurrentCallObserver(initialValue = CurrentCall.RoomCall(room.roomId)) + val presenter = createRoomCallStatePresenter( + matrixRoom = room, + currentCallObserver = currentCallObserver + ) + presenter.test { + skipItems(1) + assertThat(awaitItem()).isEqualTo( + RoomCallState.OnGoing( + canJoinCall = true, + isUserInTheCall = true, + isUserLocallyInTheCall = true, + ) + ) + currentCallObserver.setCurrentCall(CurrentCall.None) + assertThat(awaitItem()).isEqualTo( + RoomCallState.OnGoing( + canJoinCall = true, + isUserInTheCall = true, + isUserLocallyInTheCall = false, + ) + ) + room.givenRoomInfo( + aRoomInfo( + hasRoomCall = true, + activeRoomCallParticipants = emptyList(), + ) + ) + assertThat(awaitItem()).isEqualTo( + RoomCallState.OnGoing( + canJoinCall = true, + isUserInTheCall = false, + isUserLocallyInTheCall = false, + ) + ) + room.givenRoomInfo( + aRoomInfo( + hasRoomCall = false, + activeRoomCallParticipants = emptyList(), + ) + ) + assertThat(awaitItem()).isEqualTo( + RoomCallState.StandBy( + canStartCall = true, + ) + ) } } private fun createRoomCallStatePresenter( - matrixRoom: MatrixRoom + matrixRoom: MatrixRoom, + currentCallObserver: CurrentCallObserver = FakeCurrentCallObserver(), ): RoomCallStatePresenter { return RoomCallStatePresenter( room = matrixRoom, + currentCallObserver = currentCallObserver, ) } }