Skip to content

Commit 39c0cb2

Browse files
author
Florian Renaud
committed
Add unit test
1 parent 63dccb4 commit 39c0cb2

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.features.voicebroadcast.usecase
18+
19+
import im.vector.app.features.voicebroadcast.VoiceBroadcastConstants
20+
import im.vector.app.features.voicebroadcast.model.VoiceBroadcast
21+
import im.vector.app.test.fakes.FakeSession
22+
import io.mockk.every
23+
import io.mockk.mockk
24+
import org.amshove.kluent.shouldBeEqualTo
25+
import org.amshove.kluent.shouldBeNull
26+
import org.amshove.kluent.shouldNotBeNull
27+
import org.junit.Test
28+
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
29+
30+
private const val A_ROOM_ID = "A_ROOM_ID"
31+
private const val A_VOICE_BROADCAST_ID = "A_VOICE_BROADCAST_ID"
32+
33+
internal class GetVoiceBroadcastStateEventUseCaseTest {
34+
35+
private val fakeSession = FakeSession()
36+
private val getVoiceBroadcastStateEventUseCase = GetVoiceBroadcastStateEventUseCase(fakeSession)
37+
38+
private val fakeRoom get() = fakeSession.fakeRoomService.fakeRoom
39+
40+
@Test
41+
fun `given there is no event related to the given vb, when execute, then return null`() {
42+
// Given
43+
val aVoiceBroadcast = VoiceBroadcast(A_VOICE_BROADCAST_ID, A_ROOM_ID)
44+
every { fakeRoom.fakeTimelineService.getTimelineEventsRelatedTo(any(), any()) } returns emptyList()
45+
46+
// When
47+
val result = getVoiceBroadcastStateEventUseCase.execute(aVoiceBroadcast)
48+
49+
// Then
50+
result.shouldBeNull()
51+
}
52+
53+
@Test
54+
fun `given there are several related events related to the given vb, when execute, then return the most recent one`() {
55+
// Given
56+
val aVoiceBroadcast = VoiceBroadcast(A_VOICE_BROADCAST_ID, A_ROOM_ID)
57+
val aListOfTimelineEvents = listOf<TimelineEvent>(
58+
mockk(relaxed = true) {
59+
every { root.eventId } returns "event_id_1"
60+
every { root.type } returns VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO
61+
every { root.isRedacted() } returns false
62+
every { root.originServerTs } returns 1L
63+
},
64+
mockk(relaxed = true) {
65+
every { root.eventId } returns "event_id_3"
66+
every { root.type } returns VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO
67+
every { root.isRedacted() } returns false
68+
every { root.originServerTs } returns 3L
69+
},
70+
mockk(relaxed = true) {
71+
every { root.eventId } returns "event_id_2"
72+
every { root.type } returns VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO
73+
every { root.isRedacted() } returns false
74+
every { root.originServerTs } returns 2L
75+
},
76+
)
77+
every { fakeRoom.fakeTimelineService.getTimelineEventsRelatedTo(any(), any()) } returns aListOfTimelineEvents
78+
79+
// When
80+
val result = getVoiceBroadcastStateEventUseCase.execute(aVoiceBroadcast)
81+
82+
// Then
83+
result.shouldNotBeNull()
84+
result.root.eventId shouldBeEqualTo "event_id_3"
85+
}
86+
87+
@Test
88+
fun `given there are several related events related to the given vb, when execute, then return the most recent one which is not redacted`() {
89+
// Given
90+
val aVoiceBroadcast = VoiceBroadcast(A_VOICE_BROADCAST_ID, A_ROOM_ID)
91+
val aListOfTimelineEvents = listOf<TimelineEvent>(
92+
mockk(relaxed = true) {
93+
every { root.eventId } returns "event_id_1"
94+
every { root.type } returns VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO
95+
every { root.isRedacted() } returns false
96+
every { root.originServerTs } returns 1L
97+
},
98+
mockk(relaxed = true) {
99+
every { root.eventId } returns "event_id_2"
100+
every { root.type } returns VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO
101+
every { root.isRedacted() } returns true
102+
every { root.originServerTs } returns 2L
103+
},
104+
)
105+
every { fakeRoom.fakeTimelineService.getTimelineEventsRelatedTo(any(), any()) } returns aListOfTimelineEvents
106+
107+
// When
108+
val result = getVoiceBroadcastStateEventUseCase.execute(aVoiceBroadcast)
109+
110+
// Then
111+
result.shouldNotBeNull()
112+
result.root.eventId shouldBeEqualTo "event_id_1"
113+
}
114+
}

vector/src/test/java/im/vector/app/test/fakes/FakeRoom.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.matrix.android.sdk.api.session.room.Room
2222
class FakeRoom(
2323
private val fakeLocationSharingService: FakeLocationSharingService = FakeLocationSharingService(),
2424
private val fakeSendService: FakeSendService = FakeSendService(),
25-
private val fakeTimelineService: FakeTimelineService = FakeTimelineService(),
25+
val fakeTimelineService: FakeTimelineService = FakeTimelineService(),
2626
private val fakeRelationService: FakeRelationService = FakeRelationService(),
2727
private val fakeStateService: FakeStateService = FakeStateService(),
2828
) : Room by mockk() {

vector/src/test/java/im/vector/app/test/fakes/FakeRoomService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.matrix.android.sdk.api.session.room.RoomService
2222
import org.matrix.android.sdk.api.session.room.model.RoomSummary
2323

2424
class FakeRoomService(
25-
private val fakeRoom: FakeRoom = FakeRoom()
25+
val fakeRoom: FakeRoom = FakeRoom()
2626
) : RoomService by mockk() {
2727

2828
override fun getRoom(roomId: String) = fakeRoom

0 commit comments

Comments
 (0)