add tests for CallRecordingViewModel

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2023-01-30 15:28:33 +01:00
parent 5f8e52b312
commit 869dc86757
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
2 changed files with 53 additions and 10 deletions

View File

@ -20,6 +20,7 @@
package com.nextcloud.talk.viewmodels
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
@ -103,11 +104,12 @@ class CallRecordingViewModel @Inject constructor(private val repository: CallRec
this.roomToken = roomToken
}
// https://nextcloud-talk.readthedocs.io/en/latest/constants/#call-recording-status
fun setRecordingState(state: Int) {
when (state) {
0 -> _viewState.value = RecordingStoppedState
1 -> _viewState.value = RecordingStartedState
2 -> _viewState.value = RecordingStartedState
RECORDING_STOPPED_CODE -> _viewState.value = RecordingStoppedState
RECORDING_STARTED_VIDEO_CODE -> _viewState.value = RecordingStartedState
RECORDING_STARTED_AUDIO_CODE -> _viewState.value = RecordingStartedState
else -> {}
}
}
@ -122,7 +124,7 @@ class CallRecordingViewModel @Inject constructor(private val repository: CallRec
}
override fun onError(e: Throwable) {
// Log.e(TAG, "failure in CallStartRecordingObserver", e)
Log.e(TAG, "failure in CallStartRecordingObserver", e)
_viewState.value = RecordingErrorState
}
@ -143,7 +145,7 @@ class CallRecordingViewModel @Inject constructor(private val repository: CallRec
}
override fun onError(e: Throwable) {
// Log.e(TAG, "failure in CallStopRecordingObserver", e)
Log.e(TAG, "failure in CallStopRecordingObserver", e)
_viewState.value = RecordingErrorState
}
@ -154,5 +156,8 @@ class CallRecordingViewModel @Inject constructor(private val repository: CallRec
companion object {
private val TAG = CallRecordingViewModel::class.java.simpleName
const val RECORDING_STOPPED_CODE = 0
const val RECORDING_STARTED_VIDEO_CODE = 1
const val RECORDING_STARTED_AUDIO_CODE = 2
}
}

View File

@ -16,15 +16,53 @@ class CallRecordingViewModelTest : AbstractViewModelTest() {
}
@Test
fun testCallRecordingViewModel_startRecord() {
fun testCallRecordingViewModel_clickStartRecord() {
val viewModel = CallRecordingViewModel(repository)
viewModel.setData("foo")
viewModel.clickRecordButton()
// implement extension function for liveData to await value?!
Assert.equals(CallRecordingViewModel.RecordingStartLoadingState, viewModel.viewState.value)
}
@Test
fun testCallRecordingViewModel_clickStopRecord() {
val viewModel = CallRecordingViewModel(repository)
viewModel.setData("foo")
viewModel.setRecordingState(CallRecordingViewModel.RECORDING_STARTED_VIDEO_CODE)
viewModel.clickRecordButton()
Assert.equals(CallRecordingViewModel.RecordingConfirmStopState, viewModel.viewState.value)
viewModel.stopRecording()
Assert.equals(CallRecordingViewModel.RecordingStopLoadingState, viewModel.viewState.value)
}
@Test
fun testCallRecordingViewModel_keepConfirmState() {
val viewModel = CallRecordingViewModel(repository)
viewModel.setData("foo")
viewModel.setRecordingState(CallRecordingViewModel.RECORDING_STARTED_VIDEO_CODE)
viewModel.clickRecordButton()
Assert.equals(CallRecordingViewModel.RecordingConfirmStopState, viewModel.viewState.value)
viewModel.clickRecordButton()
Assert.equals(CallRecordingViewModel.RecordingConfirmStopState, viewModel.viewState.value)
}
@Test
fun testCallRecordingViewModel_continueRecordingWhenDismissStopDialog() {
val viewModel = CallRecordingViewModel(repository)
viewModel.setData("foo")
viewModel.setRecordingState(CallRecordingViewModel.RECORDING_STARTED_VIDEO_CODE)
viewModel.clickRecordButton()
Assert.equals(CallRecordingViewModel.RecordingConfirmStopState, viewModel.viewState.value)
viewModel.dismissStopRecording()
Assert.equals(CallRecordingViewModel.RecordingStartedState, viewModel.viewState.value)
}
}