Add unit tests for use cases
This commit is contained in:
parent
e4a52e1d5e
commit
1bc894712e
|
@ -0,0 +1,129 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 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.voicebroadcast.usecase
|
||||||
|
|
||||||
|
import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO
|
||||||
|
import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent
|
||||||
|
import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState
|
||||||
|
import im.vector.app.test.fakes.FakeRoom
|
||||||
|
import im.vector.app.test.fakes.FakeRoomService
|
||||||
|
import im.vector.app.test.fakes.FakeSession
|
||||||
|
import io.mockk.clearAllMocks
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.slot
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.amshove.kluent.shouldBe
|
||||||
|
import org.junit.Test
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Content
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Event
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.RelationType
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||||
|
import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent
|
||||||
|
|
||||||
|
private const val A_ROOM_ID = "room_id"
|
||||||
|
private const val AN_EVENT_ID = "event_id"
|
||||||
|
private const val A_STARTED_VOICE_BROADCAST_EVENT_ID = "a_started_voice_broadcast_event_id"
|
||||||
|
|
||||||
|
class PauseVoiceBroadcastUseCaseTest {
|
||||||
|
|
||||||
|
private val fakeRoom = FakeRoom()
|
||||||
|
private val fakeSession = FakeSession(fakeRoomService = FakeRoomService(fakeRoom))
|
||||||
|
private val pauseVoiceBroadcastUseCase = PauseVoiceBroadcastUseCase(fakeSession)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a room id with a potential existing voice broadcast state when calling execute then the voice broadcast is paused or not`() = runTest {
|
||||||
|
val cases = listOf<VoiceBroadcastState?>(null).plus(VoiceBroadcastState.values()).map {
|
||||||
|
when (it) {
|
||||||
|
VoiceBroadcastState.STARTED,
|
||||||
|
VoiceBroadcastState.RESUMED -> Case(it, true)
|
||||||
|
VoiceBroadcastState.STOPPED,
|
||||||
|
VoiceBroadcastState.PAUSED,
|
||||||
|
null -> Case(it, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cases.forEach { case ->
|
||||||
|
if (case.canPauseVoiceBroadcast) {
|
||||||
|
testVoiceBroadcastPaused(case.previousState)
|
||||||
|
} else {
|
||||||
|
testVoiceBroadcastNotPaused(case.previousState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun testVoiceBroadcastPaused(previousState: VoiceBroadcastState?) {
|
||||||
|
// Given
|
||||||
|
clearAllMocks()
|
||||||
|
givenAVoiceBroadcastState(previousState)
|
||||||
|
val voiceBroadcastInfoContentInterceptor = slot<Content>()
|
||||||
|
coEvery { fakeRoom.stateService().sendStateEvent(any(), any(), capture(voiceBroadcastInfoContentInterceptor)) } coAnswers { AN_EVENT_ID }
|
||||||
|
|
||||||
|
// When
|
||||||
|
pauseVoiceBroadcastUseCase.execute(A_ROOM_ID)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
coVerify {
|
||||||
|
fakeRoom.stateService().sendStateEvent(
|
||||||
|
eventType = STATE_ROOM_VOICE_BROADCAST_INFO,
|
||||||
|
stateKey = fakeSession.myUserId,
|
||||||
|
body = any(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val voiceBroadcastInfoContent = voiceBroadcastInfoContentInterceptor.captured.toModel<MessageVoiceBroadcastInfoContent>()
|
||||||
|
voiceBroadcastInfoContent?.voiceBroadcastState shouldBe VoiceBroadcastState.PAUSED
|
||||||
|
voiceBroadcastInfoContent?.relatesTo?.type shouldBe RelationType.REFERENCE
|
||||||
|
voiceBroadcastInfoContent?.relatesTo?.eventId shouldBe A_STARTED_VOICE_BROADCAST_EVENT_ID
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun testVoiceBroadcastNotPaused(previousState: VoiceBroadcastState?) {
|
||||||
|
// Given
|
||||||
|
clearAllMocks()
|
||||||
|
givenAVoiceBroadcastState(previousState)
|
||||||
|
|
||||||
|
// When
|
||||||
|
pauseVoiceBroadcastUseCase.execute(A_ROOM_ID)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
coVerify(exactly = 0) { fakeRoom.stateService().sendStateEvent(any(), any(), any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun givenAVoiceBroadcastState(state: VoiceBroadcastState?) {
|
||||||
|
val relatesTo = when (state) {
|
||||||
|
VoiceBroadcastState.STARTED,
|
||||||
|
null -> null
|
||||||
|
VoiceBroadcastState.PAUSED,
|
||||||
|
VoiceBroadcastState.RESUMED,
|
||||||
|
VoiceBroadcastState.STOPPED -> RelationDefaultContent(RelationType.REFERENCE, A_STARTED_VOICE_BROADCAST_EVENT_ID)
|
||||||
|
}
|
||||||
|
val event = state?.let {
|
||||||
|
Event(
|
||||||
|
eventId = if (state == VoiceBroadcastState.STARTED) A_STARTED_VOICE_BROADCAST_EVENT_ID else AN_EVENT_ID,
|
||||||
|
type = STATE_ROOM_VOICE_BROADCAST_INFO,
|
||||||
|
stateKey = fakeSession.myUserId,
|
||||||
|
content = MessageVoiceBroadcastInfoContent(
|
||||||
|
voiceBroadcastStateStr = state.value,
|
||||||
|
relatesTo = relatesTo
|
||||||
|
).toContent()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
fakeRoom.stateService().givenGetStateEvent(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class Case(val previousState: VoiceBroadcastState?, val canPauseVoiceBroadcast: Boolean)
|
||||||
|
}
|
|
@ -0,0 +1,129 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 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.voicebroadcast.usecase
|
||||||
|
|
||||||
|
import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO
|
||||||
|
import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent
|
||||||
|
import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState
|
||||||
|
import im.vector.app.test.fakes.FakeRoom
|
||||||
|
import im.vector.app.test.fakes.FakeRoomService
|
||||||
|
import im.vector.app.test.fakes.FakeSession
|
||||||
|
import io.mockk.clearAllMocks
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.slot
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.amshove.kluent.shouldBe
|
||||||
|
import org.junit.Test
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Content
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Event
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.RelationType
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||||
|
import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent
|
||||||
|
|
||||||
|
private const val A_ROOM_ID = "room_id"
|
||||||
|
private const val AN_EVENT_ID = "event_id"
|
||||||
|
private const val A_STARTED_VOICE_BROADCAST_EVENT_ID = "a_started_voice_broadcast_event_id"
|
||||||
|
|
||||||
|
class ResumeVoiceBroadcastUseCaseTest {
|
||||||
|
|
||||||
|
private val fakeRoom = FakeRoom()
|
||||||
|
private val fakeSession = FakeSession(fakeRoomService = FakeRoomService(fakeRoom))
|
||||||
|
private val resumeVoiceBroadcastUseCase = ResumeVoiceBroadcastUseCase(fakeSession)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a room id with a potential existing voice broadcast state when calling execute then the voice broadcast is resumed or not`() = runTest {
|
||||||
|
val cases = listOf<VoiceBroadcastState?>(null).plus(VoiceBroadcastState.values()).map {
|
||||||
|
when (it) {
|
||||||
|
VoiceBroadcastState.PAUSED -> Case(it, true)
|
||||||
|
VoiceBroadcastState.STARTED,
|
||||||
|
VoiceBroadcastState.RESUMED,
|
||||||
|
VoiceBroadcastState.STOPPED,
|
||||||
|
null -> Case(it, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cases.forEach { case ->
|
||||||
|
if (case.canResumeVoiceBroadcast) {
|
||||||
|
testVoiceBroadcastResumed(case.previousState)
|
||||||
|
} else {
|
||||||
|
testVoiceBroadcastNotResumed(case.previousState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun testVoiceBroadcastResumed(previousState: VoiceBroadcastState?) {
|
||||||
|
// Given
|
||||||
|
clearAllMocks()
|
||||||
|
givenAVoiceBroadcastState(previousState)
|
||||||
|
val voiceBroadcastInfoContentInterceptor = slot<Content>()
|
||||||
|
coEvery { fakeRoom.stateService().sendStateEvent(any(), any(), capture(voiceBroadcastInfoContentInterceptor)) } coAnswers { AN_EVENT_ID }
|
||||||
|
|
||||||
|
// When
|
||||||
|
resumeVoiceBroadcastUseCase.execute(A_ROOM_ID)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
coVerify {
|
||||||
|
fakeRoom.stateService().sendStateEvent(
|
||||||
|
eventType = STATE_ROOM_VOICE_BROADCAST_INFO,
|
||||||
|
stateKey = fakeSession.myUserId,
|
||||||
|
body = any(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val voiceBroadcastInfoContent = voiceBroadcastInfoContentInterceptor.captured.toModel<MessageVoiceBroadcastInfoContent>()
|
||||||
|
voiceBroadcastInfoContent?.voiceBroadcastState shouldBe VoiceBroadcastState.RESUMED
|
||||||
|
voiceBroadcastInfoContent?.relatesTo?.type shouldBe RelationType.REFERENCE
|
||||||
|
voiceBroadcastInfoContent?.relatesTo?.eventId shouldBe A_STARTED_VOICE_BROADCAST_EVENT_ID
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun testVoiceBroadcastNotResumed(previousState: VoiceBroadcastState?) {
|
||||||
|
// Given
|
||||||
|
clearAllMocks()
|
||||||
|
givenAVoiceBroadcastState(previousState)
|
||||||
|
|
||||||
|
// When
|
||||||
|
resumeVoiceBroadcastUseCase.execute(A_ROOM_ID)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
coVerify(exactly = 0) { fakeRoom.stateService().sendStateEvent(any(), any(), any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun givenAVoiceBroadcastState(state: VoiceBroadcastState?) {
|
||||||
|
val relatesTo = when (state) {
|
||||||
|
VoiceBroadcastState.STARTED,
|
||||||
|
null -> null
|
||||||
|
VoiceBroadcastState.PAUSED,
|
||||||
|
VoiceBroadcastState.RESUMED,
|
||||||
|
VoiceBroadcastState.STOPPED -> RelationDefaultContent(RelationType.REFERENCE, A_STARTED_VOICE_BROADCAST_EVENT_ID)
|
||||||
|
}
|
||||||
|
val event = state?.let {
|
||||||
|
Event(
|
||||||
|
eventId = if (state == VoiceBroadcastState.STARTED) A_STARTED_VOICE_BROADCAST_EVENT_ID else AN_EVENT_ID,
|
||||||
|
type = STATE_ROOM_VOICE_BROADCAST_INFO,
|
||||||
|
stateKey = fakeSession.myUserId,
|
||||||
|
content = MessageVoiceBroadcastInfoContent(
|
||||||
|
voiceBroadcastStateStr = state.value,
|
||||||
|
relatesTo = relatesTo
|
||||||
|
).toContent()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
fakeRoom.stateService().givenGetStateEvent(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class Case(val previousState: VoiceBroadcastState?, val canResumeVoiceBroadcast: Boolean)
|
||||||
|
}
|
|
@ -0,0 +1,117 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 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.voicebroadcast.usecase
|
||||||
|
|
||||||
|
import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO
|
||||||
|
import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent
|
||||||
|
import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState
|
||||||
|
import im.vector.app.test.fakes.FakeRoom
|
||||||
|
import im.vector.app.test.fakes.FakeRoomService
|
||||||
|
import im.vector.app.test.fakes.FakeSession
|
||||||
|
import io.mockk.clearAllMocks
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.slot
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.amshove.kluent.shouldBe
|
||||||
|
import org.amshove.kluent.shouldBeNull
|
||||||
|
import org.junit.Test
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Content
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Event
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||||
|
|
||||||
|
private const val A_ROOM_ID = "room_id"
|
||||||
|
private const val AN_EVENT_ID = "event_id"
|
||||||
|
|
||||||
|
class StartVoiceBroadcastUseCaseTest {
|
||||||
|
|
||||||
|
private val fakeRoom = FakeRoom()
|
||||||
|
private val fakeSession = FakeSession(fakeRoomService = FakeRoomService(fakeRoom))
|
||||||
|
private val startVoiceBroadcastUseCase = StartVoiceBroadcastUseCase(fakeSession)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a room id with a potential existing voice broadcast state when calling execute then the voice broadcast is started or not`() = runTest {
|
||||||
|
val cases = listOf<VoiceBroadcastState?>(null).plus(VoiceBroadcastState.values()).map {
|
||||||
|
when (it) {
|
||||||
|
VoiceBroadcastState.STARTED,
|
||||||
|
VoiceBroadcastState.PAUSED,
|
||||||
|
VoiceBroadcastState.RESUMED -> Case(it, false)
|
||||||
|
VoiceBroadcastState.STOPPED,
|
||||||
|
null -> Case(it, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cases.forEach { case ->
|
||||||
|
if (case.canStartVoiceBroadcast) {
|
||||||
|
testVoiceBroadcastStarted(case.previousState)
|
||||||
|
} else {
|
||||||
|
testVoiceBroadcastNotStarted(case.previousState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun testVoiceBroadcastStarted(previousState: VoiceBroadcastState?) {
|
||||||
|
// Given
|
||||||
|
clearAllMocks()
|
||||||
|
givenAVoiceBroadcastState(previousState)
|
||||||
|
val voiceBroadcastInfoContentInterceptor = slot<Content>()
|
||||||
|
coEvery { fakeRoom.stateService().sendStateEvent(any(), any(), capture(voiceBroadcastInfoContentInterceptor)) } coAnswers { AN_EVENT_ID }
|
||||||
|
|
||||||
|
// When
|
||||||
|
startVoiceBroadcastUseCase.execute(A_ROOM_ID)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
coVerify {
|
||||||
|
fakeRoom.stateService().sendStateEvent(
|
||||||
|
eventType = STATE_ROOM_VOICE_BROADCAST_INFO,
|
||||||
|
stateKey = fakeSession.myUserId,
|
||||||
|
body = any(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val voiceBroadcastInfoContent = voiceBroadcastInfoContentInterceptor.captured.toModel<MessageVoiceBroadcastInfoContent>()
|
||||||
|
voiceBroadcastInfoContent?.voiceBroadcastState shouldBe VoiceBroadcastState.STARTED
|
||||||
|
voiceBroadcastInfoContent?.relatesTo.shouldBeNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun testVoiceBroadcastNotStarted(previousState: VoiceBroadcastState?) {
|
||||||
|
// Given
|
||||||
|
clearAllMocks()
|
||||||
|
givenAVoiceBroadcastState(previousState)
|
||||||
|
|
||||||
|
// When
|
||||||
|
startVoiceBroadcastUseCase.execute(A_ROOM_ID)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
coVerify(exactly = 0) { fakeRoom.stateService().sendStateEvent(any(), any(), any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun givenAVoiceBroadcastState(state: VoiceBroadcastState?) {
|
||||||
|
val event = state?.let {
|
||||||
|
Event(
|
||||||
|
type = STATE_ROOM_VOICE_BROADCAST_INFO,
|
||||||
|
stateKey = fakeSession.myUserId,
|
||||||
|
content = MessageVoiceBroadcastInfoContent(
|
||||||
|
voiceBroadcastStateStr = state.value
|
||||||
|
).toContent()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
fakeRoom.stateService().givenGetStateEvent(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class Case(val previousState: VoiceBroadcastState?, val canStartVoiceBroadcast: Boolean)
|
||||||
|
}
|
|
@ -0,0 +1,129 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 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.voicebroadcast.usecase
|
||||||
|
|
||||||
|
import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO
|
||||||
|
import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent
|
||||||
|
import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState
|
||||||
|
import im.vector.app.test.fakes.FakeRoom
|
||||||
|
import im.vector.app.test.fakes.FakeRoomService
|
||||||
|
import im.vector.app.test.fakes.FakeSession
|
||||||
|
import io.mockk.clearAllMocks
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.slot
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.amshove.kluent.shouldBe
|
||||||
|
import org.junit.Test
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Content
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Event
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.RelationType
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||||
|
import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent
|
||||||
|
|
||||||
|
private const val A_ROOM_ID = "room_id"
|
||||||
|
private const val AN_EVENT_ID = "event_id"
|
||||||
|
private const val A_STARTED_VOICE_BROADCAST_EVENT_ID = "a_started_voice_broadcast_event_id"
|
||||||
|
|
||||||
|
class StopVoiceBroadcastUseCaseTest {
|
||||||
|
|
||||||
|
private val fakeRoom = FakeRoom()
|
||||||
|
private val fakeSession = FakeSession(fakeRoomService = FakeRoomService(fakeRoom))
|
||||||
|
private val stopVoiceBroadcastUseCase = StopVoiceBroadcastUseCase(fakeSession)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a room id with a potential existing voice broadcast state when calling execute then the voice broadcast is stopped or not`() = runTest {
|
||||||
|
val cases = listOf<VoiceBroadcastState?>(null).plus(VoiceBroadcastState.values()).map {
|
||||||
|
when (it) {
|
||||||
|
VoiceBroadcastState.STARTED,
|
||||||
|
VoiceBroadcastState.RESUMED,
|
||||||
|
VoiceBroadcastState.PAUSED -> Case(it, true)
|
||||||
|
VoiceBroadcastState.STOPPED,
|
||||||
|
null -> Case(it, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cases.forEach { case ->
|
||||||
|
if (case.canStopVoiceBroadcast) {
|
||||||
|
testVoiceBroadcastStopped(case.previousState)
|
||||||
|
} else {
|
||||||
|
testVoiceBroadcastNotStopped(case.previousState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun testVoiceBroadcastStopped(previousState: VoiceBroadcastState?) {
|
||||||
|
// Given
|
||||||
|
clearAllMocks()
|
||||||
|
givenAVoiceBroadcastState(previousState)
|
||||||
|
val voiceBroadcastInfoContentInterceptor = slot<Content>()
|
||||||
|
coEvery { fakeRoom.stateService().sendStateEvent(any(), any(), capture(voiceBroadcastInfoContentInterceptor)) } coAnswers { AN_EVENT_ID }
|
||||||
|
|
||||||
|
// When
|
||||||
|
stopVoiceBroadcastUseCase.execute(A_ROOM_ID)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
coVerify {
|
||||||
|
fakeRoom.stateService().sendStateEvent(
|
||||||
|
eventType = STATE_ROOM_VOICE_BROADCAST_INFO,
|
||||||
|
stateKey = fakeSession.myUserId,
|
||||||
|
body = any(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val voiceBroadcastInfoContent = voiceBroadcastInfoContentInterceptor.captured.toModel<MessageVoiceBroadcastInfoContent>()
|
||||||
|
voiceBroadcastInfoContent?.voiceBroadcastState shouldBe VoiceBroadcastState.STOPPED
|
||||||
|
voiceBroadcastInfoContent?.relatesTo?.type shouldBe RelationType.REFERENCE
|
||||||
|
voiceBroadcastInfoContent?.relatesTo?.eventId shouldBe A_STARTED_VOICE_BROADCAST_EVENT_ID
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun testVoiceBroadcastNotStopped(previousState: VoiceBroadcastState?) {
|
||||||
|
// Given
|
||||||
|
clearAllMocks()
|
||||||
|
givenAVoiceBroadcastState(previousState)
|
||||||
|
|
||||||
|
// When
|
||||||
|
stopVoiceBroadcastUseCase.execute(A_ROOM_ID)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
coVerify(exactly = 0) { fakeRoom.stateService().sendStateEvent(any(), any(), any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun givenAVoiceBroadcastState(state: VoiceBroadcastState?) {
|
||||||
|
val relatesTo = when (state) {
|
||||||
|
VoiceBroadcastState.STARTED,
|
||||||
|
null -> null
|
||||||
|
VoiceBroadcastState.PAUSED,
|
||||||
|
VoiceBroadcastState.RESUMED,
|
||||||
|
VoiceBroadcastState.STOPPED -> RelationDefaultContent(RelationType.REFERENCE, A_STARTED_VOICE_BROADCAST_EVENT_ID)
|
||||||
|
}
|
||||||
|
val event = state?.let {
|
||||||
|
Event(
|
||||||
|
eventId = if (state == VoiceBroadcastState.STARTED) A_STARTED_VOICE_BROADCAST_EVENT_ID else AN_EVENT_ID,
|
||||||
|
type = STATE_ROOM_VOICE_BROADCAST_INFO,
|
||||||
|
stateKey = fakeSession.myUserId,
|
||||||
|
content = MessageVoiceBroadcastInfoContent(
|
||||||
|
voiceBroadcastStateStr = state.value,
|
||||||
|
relatesTo = relatesTo
|
||||||
|
).toContent()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
fakeRoom.stateService().givenGetStateEvent(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class Case(val previousState: VoiceBroadcastState?, val canStopVoiceBroadcast: Boolean)
|
||||||
|
}
|
|
@ -24,6 +24,7 @@ class FakeRoom(
|
||||||
private val fakeSendService: FakeSendService = FakeSendService(),
|
private val fakeSendService: FakeSendService = FakeSendService(),
|
||||||
private val fakeTimelineService: FakeTimelineService = FakeTimelineService(),
|
private val fakeTimelineService: FakeTimelineService = FakeTimelineService(),
|
||||||
private val fakeRelationService: FakeRelationService = FakeRelationService(),
|
private val fakeRelationService: FakeRelationService = FakeRelationService(),
|
||||||
|
private val fakeStateService: FakeStateService = FakeStateService(),
|
||||||
) : Room by mockk() {
|
) : Room by mockk() {
|
||||||
|
|
||||||
override fun locationSharingService() = fakeLocationSharingService
|
override fun locationSharingService() = fakeLocationSharingService
|
||||||
|
@ -33,4 +34,6 @@ class FakeRoom(
|
||||||
override fun timelineService() = fakeTimelineService
|
override fun timelineService() = fakeTimelineService
|
||||||
|
|
||||||
override fun relationService() = fakeRelationService
|
override fun relationService() = fakeRelationService
|
||||||
|
|
||||||
|
override fun stateService() = fakeStateService
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021 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 io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Event
|
||||||
|
import org.matrix.android.sdk.api.session.room.state.StateService
|
||||||
|
|
||||||
|
class FakeStateService : StateService by mockk(relaxed = true) {
|
||||||
|
|
||||||
|
fun givenGetStateEvent(event: Event?) {
|
||||||
|
every { getStateEvent(any(), any()) } returns event
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue