Fix voice recording tests

This commit is contained in:
Jorge Martín 2022-09-06 12:34:24 +02:00 committed by Jorge Martin Espinosa
parent b4fcffbdf9
commit 273999ee88
2 changed files with 15 additions and 3 deletions

View File

@ -20,6 +20,8 @@ import android.os.Build
import androidx.test.platform.app.InstrumentationRegistry
import im.vector.app.AndroidVersionTestOverrider
import im.vector.app.features.DefaultVectorFeatures
import io.mockk.every
import io.mockk.spyk
import org.amshove.kluent.shouldBeInstanceOf
import org.junit.After
import org.junit.Test
@ -27,7 +29,7 @@ import org.junit.Test
class VoiceRecorderProviderTests {
private val context = InstrumentationRegistry.getInstrumentation().targetContext
private val provider = VoiceRecorderProvider(context, DefaultVectorFeatures())
private val provider = spyk(VoiceRecorderProvider(context, DefaultVectorFeatures()))
@After
fun tearDown() {
@ -35,11 +37,19 @@ class VoiceRecorderProviderTests {
}
@Test
fun provideVoiceRecorderOnAndroidQReturnsQRecorder() {
fun provideVoiceRecorderOnAndroidQAndCodecReturnsQRecorder() {
AndroidVersionTestOverrider.override(Build.VERSION_CODES.Q)
every { provider.hasOpusEncoder() } returns true
provider.provideVoiceRecorder().shouldBeInstanceOf(VoiceRecorderQ::class)
}
@Test
fun provideVoiceRecorderOnAndroidQButNoCodecReturnsLRecorder() {
AndroidVersionTestOverrider.override(Build.VERSION_CODES.Q)
every { provider.hasOpusEncoder() } returns false
provider.provideVoiceRecorder().shouldBeInstanceOf(VoiceRecorderL::class)
}
@Test
fun provideVoiceRecorderOnOlderAndroidVersionReturnsLRecorder() {
AndroidVersionTestOverrider.override(Build.VERSION_CODES.LOLLIPOP)

View File

@ -20,6 +20,7 @@ import android.content.Context
import android.media.MediaCodecList
import android.media.MediaFormat
import android.os.Build
import androidx.annotation.VisibleForTesting
import im.vector.app.features.VectorFeatures
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject
@ -40,7 +41,8 @@ class VoiceRecorderProvider @Inject constructor(
return Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || !hasOpusEncoder() || vectorFeatures.forceUsageOfOpusEncoder()
}
private fun hasOpusEncoder(): Boolean {
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun hasOpusEncoder(): Boolean {
val codecList = MediaCodecList(MediaCodecList.ALL_CODECS)
val format = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_OPUS, 48000, 1)
return codecList.findEncoderForFormat(format) != null