From 273999ee88dd7dea690b50452d325c89cb3db008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Mart=C3=ADn?= Date: Tue, 6 Sep 2022 12:34:24 +0200 Subject: [PATCH] Fix voice recording tests --- .../features/voice/VoiceRecorderProviderTests.kt | 14 ++++++++++++-- .../app/features/voice/VoiceRecorderProvider.kt | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/vector/src/androidTest/java/im/vector/app/features/voice/VoiceRecorderProviderTests.kt b/vector/src/androidTest/java/im/vector/app/features/voice/VoiceRecorderProviderTests.kt index 61f745178c3..65f81b145bc 100644 --- a/vector/src/androidTest/java/im/vector/app/features/voice/VoiceRecorderProviderTests.kt +++ b/vector/src/androidTest/java/im/vector/app/features/voice/VoiceRecorderProviderTests.kt @@ -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) diff --git a/vector/src/main/java/im/vector/app/features/voice/VoiceRecorderProvider.kt b/vector/src/main/java/im/vector/app/features/voice/VoiceRecorderProvider.kt index 3441d0231ae..1bf289fb4cc 100644 --- a/vector/src/main/java/im/vector/app/features/voice/VoiceRecorderProvider.kt +++ b/vector/src/main/java/im/vector/app/features/voice/VoiceRecorderProvider.kt @@ -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