|
| 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.home.room.list.usecase |
| 18 | + |
| 19 | +import im.vector.app.features.voicebroadcast.VoiceBroadcastConstants |
| 20 | +import im.vector.app.features.voicebroadcast.VoiceBroadcastConstants.VOICE_BROADCAST_CHUNK_KEY |
| 21 | +import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState |
| 22 | +import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent |
| 23 | +import im.vector.app.features.voicebroadcast.usecase.GetRoomLiveVoiceBroadcastsUseCase |
| 24 | +import im.vector.app.test.fakes.FakeActiveSessionHolder |
| 25 | +import im.vector.app.test.fakes.FakeRoom |
| 26 | +import io.mockk.every |
| 27 | +import io.mockk.mockk |
| 28 | +import org.amshove.kluent.shouldBe |
| 29 | +import org.amshove.kluent.shouldBeEqualTo |
| 30 | +import org.amshove.kluent.shouldBeNull |
| 31 | +import org.junit.Before |
| 32 | +import org.junit.Test |
| 33 | +import org.matrix.android.sdk.api.session.events.model.Event |
| 34 | +import org.matrix.android.sdk.api.session.events.model.EventType |
| 35 | +import org.matrix.android.sdk.api.session.events.model.RelationType |
| 36 | +import org.matrix.android.sdk.api.session.getRoom |
| 37 | +import org.matrix.android.sdk.api.session.room.model.RoomSummary |
| 38 | +import org.matrix.android.sdk.api.session.room.model.message.MessageContent |
| 39 | +import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent |
| 40 | + |
| 41 | +private const val A_ROOM_ID = "a-room-id" |
| 42 | + |
| 43 | +internal class GetLatestPreviewableEventUseCaseTest { |
| 44 | + |
| 45 | + private val fakeRoom = FakeRoom() |
| 46 | + private val fakeSessionHolder = FakeActiveSessionHolder() |
| 47 | + private val fakeRoomSummary = mockk<RoomSummary>() |
| 48 | + private val fakeGetRoomLiveVoiceBroadcastsUseCase = mockk<GetRoomLiveVoiceBroadcastsUseCase>() |
| 49 | + |
| 50 | + private val getLatestPreviewableEventUseCase = GetLatestPreviewableEventUseCase( |
| 51 | + fakeSessionHolder.instance, |
| 52 | + fakeGetRoomLiveVoiceBroadcastsUseCase, |
| 53 | + ) |
| 54 | + |
| 55 | + @Before |
| 56 | + fun setup() { |
| 57 | + every { fakeSessionHolder.instance.getSafeActiveSession()?.getRoom(A_ROOM_ID) } returns fakeRoom |
| 58 | + every { fakeRoom.roomSummary() } returns fakeRoomSummary |
| 59 | + every { fakeRoom.roomId } returns A_ROOM_ID |
| 60 | + every { fakeRoom.timelineService().getTimelineEvent(any()) } answers { |
| 61 | + mockk(relaxed = true) { |
| 62 | + every { eventId } returns firstArg() |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `given the latest event is a call invite and there is a live broadcast, when execute, returns the call event`() { |
| 69 | + // Given |
| 70 | + val aLatestPreviewableEvent = mockk<TimelineEvent> { |
| 71 | + every { root.type } returns EventType.MESSAGE |
| 72 | + every { root.getClearType() } returns EventType.CALL_INVITE |
| 73 | + } |
| 74 | + every { fakeRoomSummary.latestPreviewableEvent } returns aLatestPreviewableEvent |
| 75 | + every { fakeGetRoomLiveVoiceBroadcastsUseCase.execute(A_ROOM_ID) } returns listOf( |
| 76 | + givenAVoiceBroadcastEvent("id1", VoiceBroadcastState.STARTED, "id1"), |
| 77 | + givenAVoiceBroadcastEvent("id2", VoiceBroadcastState.RESUMED, "id1"), |
| 78 | + ).mapNotNull { it.asVoiceBroadcastEvent() } |
| 79 | + |
| 80 | + // When |
| 81 | + val result = getLatestPreviewableEventUseCase.execute(A_ROOM_ID) |
| 82 | + |
| 83 | + // Then |
| 84 | + result shouldBe aLatestPreviewableEvent |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `given the latest event is not a call invite and there is a live broadcast, when execute, returns the latest broadcast event`() { |
| 89 | + // Given |
| 90 | + val aLatestPreviewableEvent = mockk<TimelineEvent> { |
| 91 | + every { root.type } returns EventType.MESSAGE |
| 92 | + every { root.getClearType() } returns EventType.MESSAGE |
| 93 | + } |
| 94 | + every { fakeRoomSummary.latestPreviewableEvent } returns aLatestPreviewableEvent |
| 95 | + every { fakeGetRoomLiveVoiceBroadcastsUseCase.execute(A_ROOM_ID) } returns listOf( |
| 96 | + givenAVoiceBroadcastEvent("id1", VoiceBroadcastState.STARTED, "vb_id1"), |
| 97 | + givenAVoiceBroadcastEvent("id2", VoiceBroadcastState.RESUMED, "vb_id2"), |
| 98 | + ).mapNotNull { it.asVoiceBroadcastEvent() } |
| 99 | + |
| 100 | + // When |
| 101 | + val result = getLatestPreviewableEventUseCase.execute(A_ROOM_ID) |
| 102 | + |
| 103 | + // Then |
| 104 | + result?.eventId shouldBeEqualTo "vb_id2" |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `given there is no live broadcast, when execute, returns the latest event`() { |
| 109 | + // Given |
| 110 | + val aLatestPreviewableEvent = mockk<TimelineEvent> { |
| 111 | + every { root.type } returns EventType.MESSAGE |
| 112 | + every { root.getClearType() } returns EventType.MESSAGE |
| 113 | + } |
| 114 | + every { fakeRoomSummary.latestPreviewableEvent } returns aLatestPreviewableEvent |
| 115 | + every { fakeGetRoomLiveVoiceBroadcastsUseCase.execute(A_ROOM_ID) } returns emptyList() |
| 116 | + |
| 117 | + // When |
| 118 | + val result = getLatestPreviewableEventUseCase.execute(A_ROOM_ID) |
| 119 | + |
| 120 | + // Then |
| 121 | + result shouldBe aLatestPreviewableEvent |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `given there is no live broadcast and the latest event is a vb message, when execute, returns null`() { |
| 126 | + // Given |
| 127 | + val aLatestPreviewableEvent = mockk<TimelineEvent> { |
| 128 | + every { root.type } returns EventType.MESSAGE |
| 129 | + every { root.getClearType() } returns EventType.MESSAGE |
| 130 | + every { root.getClearContent() } returns mapOf( |
| 131 | + MessageContent.MSG_TYPE_JSON_KEY to "m.audio", |
| 132 | + VOICE_BROADCAST_CHUNK_KEY to "1", |
| 133 | + "body" to "", |
| 134 | + ) |
| 135 | + } |
| 136 | + every { fakeRoomSummary.latestPreviewableEvent } returns aLatestPreviewableEvent |
| 137 | + every { fakeGetRoomLiveVoiceBroadcastsUseCase.execute(A_ROOM_ID) } returns emptyList() |
| 138 | + |
| 139 | + // When |
| 140 | + val result = getLatestPreviewableEventUseCase.execute(A_ROOM_ID) |
| 141 | + |
| 142 | + // Then |
| 143 | + result.shouldBeNull() |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + fun `given the latest event is an ended vb, when execute, returns the stopped event`() { |
| 148 | + // Given |
| 149 | + val aLatestPreviewableEvent = mockk<TimelineEvent> { |
| 150 | + every { eventId } returns "id1" |
| 151 | + every { root } returns givenAVoiceBroadcastEvent("id1", VoiceBroadcastState.STOPPED, "vb_id1") |
| 152 | + } |
| 153 | + every { fakeRoomSummary.latestPreviewableEvent } returns aLatestPreviewableEvent |
| 154 | + every { fakeGetRoomLiveVoiceBroadcastsUseCase.execute(A_ROOM_ID) } returns emptyList() |
| 155 | + |
| 156 | + // When |
| 157 | + val result = getLatestPreviewableEventUseCase.execute(A_ROOM_ID) |
| 158 | + |
| 159 | + // Then |
| 160 | + result?.eventId shouldBeEqualTo "id1" |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + fun `given the latest event is a resumed vb, when execute, returns the started event`() { |
| 165 | + // Given |
| 166 | + val aLatestPreviewableEvent = mockk<TimelineEvent> { |
| 167 | + every { eventId } returns "id1" |
| 168 | + every { root } returns givenAVoiceBroadcastEvent("id1", VoiceBroadcastState.RESUMED, "vb_id1") |
| 169 | + } |
| 170 | + every { fakeRoomSummary.latestPreviewableEvent } returns aLatestPreviewableEvent |
| 171 | + every { fakeGetRoomLiveVoiceBroadcastsUseCase.execute(A_ROOM_ID) } returns emptyList() |
| 172 | + |
| 173 | + // When |
| 174 | + val result = getLatestPreviewableEventUseCase.execute(A_ROOM_ID) |
| 175 | + |
| 176 | + // Then |
| 177 | + result?.eventId shouldBeEqualTo "vb_id1" |
| 178 | + } |
| 179 | + |
| 180 | + private fun givenAVoiceBroadcastEvent( |
| 181 | + eventId: String, |
| 182 | + state: VoiceBroadcastState, |
| 183 | + voiceBroadcastId: String, |
| 184 | + ): Event = mockk { |
| 185 | + every { this@mockk.eventId } returns eventId |
| 186 | + every { getClearType() } returns VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO |
| 187 | + every { type } returns VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO |
| 188 | + every { content } returns mapOf( |
| 189 | + "state" to state.value, |
| 190 | + "m.relates_to" to mapOf( |
| 191 | + "rel_type" to RelationType.REFERENCE, |
| 192 | + "event_id" to voiceBroadcastId |
| 193 | + ) |
| 194 | + ) |
| 195 | + } |
| 196 | +} |
0 commit comments