88package io.element.android.features.roomcall.impl
99
1010import com.google.common.truth.Truth.assertThat
11+ import io.element.android.features.call.api.CurrentCall
12+ import io.element.android.features.call.api.CurrentCallObserver
13+ import io.element.android.features.call.test.FakeCurrentCallObserver
1114import io.element.android.features.roomcall.api.RoomCallState
1215import io.element.android.libraries.matrix.api.room.MatrixRoom
1316import io.element.android.libraries.matrix.test.room.FakeMatrixRoom
@@ -17,30 +20,180 @@ import kotlinx.coroutines.test.runTest
1720import org.junit.Test
1821
1922class RoomCallStatePresenterTest {
23+ @Test
24+ fun `present - initial state` () = runTest {
25+ val room = FakeMatrixRoom (
26+ canUserJoinCallResult = { Result .success(false ) },
27+ )
28+ val presenter = createRoomCallStatePresenter(matrixRoom = room)
29+ presenter.test {
30+ val initialState = awaitItem()
31+ assertThat(initialState).isEqualTo(
32+ RoomCallState .StandBy (
33+ canStartCall = false ,
34+ )
35+ )
36+ }
37+ }
38+
39+ @Test
40+ fun `present - initial state - user can join call` () = runTest {
41+ val room = FakeMatrixRoom (
42+ canUserJoinCallResult = { Result .success(true ) },
43+ )
44+ val presenter = createRoomCallStatePresenter(matrixRoom = room)
45+ presenter.test {
46+ skipItems(1 )
47+ val initialState = awaitItem()
48+ assertThat(initialState).isEqualTo(
49+ RoomCallState .StandBy (
50+ canStartCall = true ,
51+ )
52+ )
53+ }
54+ }
55+
2056 @Test
2157 fun `present - call is disabled if user cannot join it even if there is an ongoing call` () = runTest {
2258 val room = FakeMatrixRoom (
2359 canUserJoinCallResult = { Result .success(false ) },
24- canUserSendMessageResult = { _, _ -> Result .success(true ) },
25- canRedactOwnResult = { Result .success(true ) },
26- canRedactOtherResult = { Result .success(true ) },
27- typingNoticeResult = { Result .success(Unit ) },
28- canUserPinUnpinResult = { Result .success(true ) },
2960 ).apply {
3061 givenRoomInfo(aRoomInfo(hasRoomCall = true ))
3162 }
3263 val presenter = createRoomCallStatePresenter(matrixRoom = room)
3364 presenter.test {
34- val initialState = awaitItem()
35- assertThat(initialState).isEqualTo(RoomCallState .OnGoing (canJoinCall = false ))
65+ skipItems(1 )
66+ assertThat(awaitItem()).isEqualTo(
67+ RoomCallState .OnGoing (
68+ canJoinCall = false ,
69+ isUserInTheCall = false ,
70+ isUserLocallyInTheCall = false ,
71+ )
72+ )
73+ }
74+ }
75+
76+ @Test
77+ fun `present - user has joined the call on another session` () = runTest {
78+ val room = FakeMatrixRoom (
79+ canUserJoinCallResult = { Result .success(true ) },
80+ ).apply {
81+ givenRoomInfo(
82+ aRoomInfo(
83+ hasRoomCall = true ,
84+ activeRoomCallParticipants = listOf (sessionId),
85+ )
86+ )
87+ }
88+ val presenter = createRoomCallStatePresenter(matrixRoom = room)
89+ presenter.test {
90+ skipItems(1 )
91+ assertThat(awaitItem()).isEqualTo(
92+ RoomCallState .OnGoing (
93+ canJoinCall = true ,
94+ isUserInTheCall = true ,
95+ isUserLocallyInTheCall = false ,
96+ )
97+ )
98+ }
99+ }
100+
101+ @Test
102+ fun `present - user has joined the call locally` () = runTest {
103+ val room = FakeMatrixRoom (
104+ canUserJoinCallResult = { Result .success(true ) },
105+ ).apply {
106+ givenRoomInfo(
107+ aRoomInfo(
108+ hasRoomCall = true ,
109+ activeRoomCallParticipants = listOf (sessionId),
110+ )
111+ )
112+ }
113+ val presenter = createRoomCallStatePresenter(
114+ matrixRoom = room,
115+ currentCallObserver = FakeCurrentCallObserver (initialValue = CurrentCall .RoomCall (room.roomId)),
116+ )
117+ presenter.test {
118+ skipItems(1 )
119+ assertThat(awaitItem()).isEqualTo(
120+ RoomCallState .OnGoing (
121+ canJoinCall = true ,
122+ isUserInTheCall = true ,
123+ isUserLocallyInTheCall = true ,
124+ )
125+ )
126+ }
127+ }
128+
129+ @Test
130+ fun `present - user leaves the call` () = runTest {
131+ val room = FakeMatrixRoom (
132+ canUserJoinCallResult = { Result .success(true ) },
133+ ).apply {
134+ givenRoomInfo(
135+ aRoomInfo(
136+ hasRoomCall = true ,
137+ activeRoomCallParticipants = listOf (sessionId),
138+ )
139+ )
140+ }
141+ val currentCallObserver = FakeCurrentCallObserver (initialValue = CurrentCall .RoomCall (room.roomId))
142+ val presenter = createRoomCallStatePresenter(
143+ matrixRoom = room,
144+ currentCallObserver = currentCallObserver
145+ )
146+ presenter.test {
147+ skipItems(1 )
148+ assertThat(awaitItem()).isEqualTo(
149+ RoomCallState .OnGoing (
150+ canJoinCall = true ,
151+ isUserInTheCall = true ,
152+ isUserLocallyInTheCall = true ,
153+ )
154+ )
155+ currentCallObserver.setCurrentCall(CurrentCall .None )
156+ assertThat(awaitItem()).isEqualTo(
157+ RoomCallState .OnGoing (
158+ canJoinCall = true ,
159+ isUserInTheCall = true ,
160+ isUserLocallyInTheCall = false ,
161+ )
162+ )
163+ room.givenRoomInfo(
164+ aRoomInfo(
165+ hasRoomCall = true ,
166+ activeRoomCallParticipants = emptyList(),
167+ )
168+ )
169+ assertThat(awaitItem()).isEqualTo(
170+ RoomCallState .OnGoing (
171+ canJoinCall = true ,
172+ isUserInTheCall = false ,
173+ isUserLocallyInTheCall = false ,
174+ )
175+ )
176+ room.givenRoomInfo(
177+ aRoomInfo(
178+ hasRoomCall = false ,
179+ activeRoomCallParticipants = emptyList(),
180+ )
181+ )
182+ assertThat(awaitItem()).isEqualTo(
183+ RoomCallState .StandBy (
184+ canStartCall = true ,
185+ )
186+ )
36187 }
37188 }
38189
39190 private fun createRoomCallStatePresenter (
40- matrixRoom : MatrixRoom
191+ matrixRoom : MatrixRoom ,
192+ currentCallObserver : CurrentCallObserver = FakeCurrentCallObserver (),
41193 ): RoomCallStatePresenter {
42194 return RoomCallStatePresenter (
43195 room = matrixRoom,
196+ currentCallObserver = currentCallObserver,
44197 )
45198 }
46199}
0 commit comments