-
Notifications
You must be signed in to change notification settings - Fork 20
/
OngoingCallViewModelTest.kt
191 lines (167 loc) · 7.39 KB
/
OngoingCallViewModelTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.ui.calling
import androidx.lifecycle.SavedStateHandle
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.datastore.GlobalDataStore
import com.wire.android.config.NavigationTestExtension
import com.wire.android.ui.calling.model.UICallParticipant
import com.wire.android.ui.calling.ongoing.OngoingCallViewModel
import com.wire.android.ui.home.conversationslist.model.Membership
import com.wire.android.ui.navArgs
import com.wire.android.util.CurrentScreen
import com.wire.android.util.CurrentScreenManager
import com.wire.kalium.logic.data.call.Call
import com.wire.kalium.logic.data.call.CallClient
import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.logic.data.call.VideoState
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCase
import com.wire.kalium.logic.feature.call.usecase.RequestVideoStreamsUseCase
import com.wire.kalium.logic.feature.call.usecase.video.SetVideoSendStateUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.internal.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.Test
@OptIn(ExperimentalCoroutinesApi::class)
@ExtendWith(NavigationTestExtension::class)
@ExtendWith(CoroutineTestExtension::class)
class OngoingCallViewModelTest {
@MockK
private lateinit var savedStateHandle: SavedStateHandle
@MockK
private lateinit var establishedCall: ObserveEstablishedCallsUseCase
@MockK
private lateinit var requestVideoStreams: RequestVideoStreamsUseCase
@MockK
private lateinit var currentScreenManager: CurrentScreenManager
@MockK
private lateinit var setVideoSendState: SetVideoSendStateUseCase
@MockK
private lateinit var globalDataStore: GlobalDataStore
private lateinit var ongoingCallViewModel: OngoingCallViewModel
@BeforeEach
fun setup() {
MockKAnnotations.init(this)
every { savedStateHandle.navArgs<CallingNavArgs>() } returns CallingNavArgs(conversationId = conversationId)
coEvery { establishedCall.invoke() } returns flowOf(listOf(provideCall()))
coEvery { currentScreenManager.observeCurrentScreen(any()) } returns MutableStateFlow(CurrentScreen.SomeOther)
coEvery { globalDataStore.getShouldShowDoubleTapToast(any()) } returns false
coEvery { setVideoSendState.invoke(any(), any()) } returns Unit
ongoingCallViewModel = OngoingCallViewModel(
savedStateHandle = savedStateHandle,
establishedCalls = establishedCall,
requestVideoStreams = requestVideoStreams,
currentScreenManager = currentScreenManager,
currentUserId = currentUserId,
setVideoSendState = setVideoSendState,
globalDataStore = globalDataStore,
)
}
@Test
fun givenAnOngoingCall_WhenTurningOnCamera_ThenSetVideoSendStateToStarted() = runTest {
ongoingCallViewModel.startSendingVideoFeed()
coVerify(exactly = 1) { setVideoSendState.invoke(any(), VideoState.STARTED) }
}
@Test
fun givenAnOngoingCall_WhenTurningOffCamera_ThenSetVideoSendStateToStopped() = runTest {
ongoingCallViewModel.stopSendingVideoFeed()
coVerify { setVideoSendState.invoke(any(), VideoState.STOPPED) }
}
@Test
fun givenParticipantsList_WhenRequestingVideoStream_ThenRequestItForOnlyParticipantsWithVideoEnabled() = runTest {
val expectedClients = listOf(
CallClient(participant1.id.toString(), participant1.clientId),
CallClient(participant3.id.toString(), participant3.clientId)
)
coEvery { requestVideoStreams(conversationId = conversationId, expectedClients) } returns Unit
ongoingCallViewModel.requestVideoStreams(participants)
coVerify(exactly = 1) { requestVideoStreams(conversationId, expectedClients) }
}
@Test
fun givenDoubleTabIndicatorIsDisplayed_whenUserTapsOnIt_thenHideIt() = runTest {
coEvery { globalDataStore.setShouldShowDoubleTapToastStatus(currentUserId.toString(), false) } returns Unit
ongoingCallViewModel.hideDoubleTapToast()
assertEquals(false, ongoingCallViewModel.shouldShowDoubleTapToast)
coVerify(exactly = 1) { globalDataStore.setShouldShowDoubleTapToastStatus(currentUserId.toString(), false) }
}
companion object {
val conversationId = ConversationId("some-dummy-value", "some.dummy.domain")
val currentUserId = UserId("userId", "some.dummy.domain")
private val participant1 = UICallParticipant(
id = QualifiedID("value1", "domain"),
clientId = "client-id1",
name = "name1",
isMuted = false,
isSpeaking = false,
isCameraOn = true,
isSharingScreen = false,
membership = Membership.None,
hasEstablishedAudio = true
)
private val participant2 = UICallParticipant(
id = QualifiedID("value2", "domain"),
clientId = "client-id2",
name = "name2",
isMuted = false,
isSpeaking = false,
isCameraOn = false,
isSharingScreen = false,
membership = Membership.None,
hasEstablishedAudio = true
)
private val participant3 = UICallParticipant(
id = QualifiedID("value3", "domain"),
clientId = "client-id3",
name = "name3",
isMuted = false,
isSpeaking = false,
isCameraOn = true,
isSharingScreen = true,
membership = Membership.None,
hasEstablishedAudio = true
)
val participants = listOf(participant1, participant2, participant3)
}
private fun provideCall(id: ConversationId = ConversationId("some-dummy-value", "some.dummy.domain")) = Call(
conversationId = id,
status = CallStatus.ESTABLISHED,
callerId = "caller_id",
participants = listOf(),
isMuted = false,
isCameraOn = false,
maxParticipants = 0,
conversationName = "ONE_ON_ONE Name",
conversationType = Conversation.Type.ONE_ON_ONE,
callerName = "otherUsername",
callerTeamName = "team_1",
isCbrEnabled = false
)
}