Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VoiceRecorderLTests by faking OggOpusEncoder #7226

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ ext.libs = [
'flipperNetworkPlugin' : "com.facebook.flipper:flipper-network-plugin:$flipper",
],
element : [
'opusencoder' : "io.element.android:opusencoder:1.0.4",
'opusencoder' : "io.element.android:opusencoder:1.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any notable changes with the dependency update?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OggOpusEncoder is now an interface to make mocking it easier. Also, we had to separate the old external fun setBitrate into fun setBitrate and external fun setEncoderBitrate as mockk can't mock external funs.

],
squareup : [
'moshi' : "com.squareup.moshi:moshi:$moshi",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package im.vector.app.features.voice
import android.Manifest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import io.mockk.spyk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.amshove.kluent.shouldBeNull
Expand All @@ -36,7 +35,7 @@ class VoiceRecorderLTests {
val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(Manifest.permission.RECORD_AUDIO)

private val context = InstrumentationRegistry.getInstrumentation().targetContext
private val recorder = spyk(VoiceRecorderL(context, Dispatchers.IO))
private val recorder = VoiceRecorderL(context, Dispatchers.IO, createFakeOpusEncoder())

@Test
fun startRecordCreatesOggFile() = with(recorder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package im.vector.app.features.voice

import im.vector.app.core.utils.waitUntil
import im.vector.app.test.fakes.FakeOggOpusEncoder
import org.amshove.kluent.shouldExist
import org.amshove.kluent.shouldNotBeNull
import java.io.File
Expand All @@ -34,3 +35,5 @@ suspend fun VoiceRecorder.waitUntilRecordingFileExists(timeout: Duration = 1.sec
}
return getVoiceMessageFile()
}

internal fun createFakeOpusEncoder() = FakeOggOpusEncoder().apply { createEmptyFileOnInit() }
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import java.io.File
class VoiceRecorderTests {

private val context = InstrumentationRegistry.getInstrumentation().targetContext
private val voiceRecorder = VoiceRecorderL(context, Dispatchers.IO)
private val voiceRecorder = VoiceRecorderL(context, Dispatchers.IO, createFakeOpusEncoder())
private val audioDirectory = File(context.cacheDir, "voice_records")

@After
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.test.fakes

import io.element.android.opusencoder.OggOpusEncoder
import io.mockk.every
import io.mockk.mockk
import java.io.File

class FakeOggOpusEncoder : OggOpusEncoder by mockk() {

init {
every { init(any(), any()) } returns 0
every { setBitrate(any()) } returns 0
every { encode(any(), any()) } returns 0
every { release() } answers {}
}

fun createEmptyFileOnInit() {
every { init(any(), any()) } answers {
val filePath = arg<String>(0)
if (File(filePath).createNewFile()) 0 else 1
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import kotlin.coroutines.CoroutineContext
class VoiceRecorderL(
context: Context,
coroutineContext: CoroutineContext,
private val codec: OggOpusEncoder,
) : VoiceRecorder {

companion object {
Expand All @@ -58,7 +59,6 @@ class VoiceRecorderL(
private var audioRecorder: AudioRecord? = null
private var noiseSuppressor: NoiseSuppressor? = null
private var automaticGainControl: AutomaticGainControl? = null
private val codec = OggOpusEncoder()

// Size of the audio buffer for Short values
private var bufferSizeInShorts = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.os.Build
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.VisibleForTesting
import im.vector.app.features.VectorFeatures
import io.element.android.opusencoder.OggOpusEncoder
import kotlinx.coroutines.Dispatchers
import org.matrix.android.sdk.api.util.BuildVersionSdkIntProvider
import javax.inject.Inject
Expand All @@ -36,7 +37,7 @@ class VoiceRecorderProvider @Inject constructor(
return if (useNativeRecorder()) {
VoiceRecorderQ(context)
} else {
VoiceRecorderL(context, Dispatchers.IO)
VoiceRecorderL(context, Dispatchers.IO, OggOpusEncoder.create())
}
}

Expand Down