|
| 1 | +package expo.modules.t3voicetranscription |
| 2 | + |
| 3 | +import android.media.AudioRecord |
| 4 | +import com.facebook.react.bridge.BridgeReactContext |
| 5 | +import expo.modules.kotlin.AppContext |
| 6 | +import expo.modules.kotlin.ModulesProvider |
| 7 | +import expo.modules.kotlin.modules.Module |
| 8 | +import java.lang.ref.WeakReference |
| 9 | +import kotlinx.coroutines.test.runTest |
| 10 | +import org.junit.Assert.assertEquals |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import org.robolectric.RobolectricTestRunner |
| 14 | +import org.robolectric.RuntimeEnvironment |
| 15 | +import org.robolectric.annotation.Config |
| 16 | +import org.robolectric.util.ReflectionHelpers |
| 17 | + |
| 18 | +@RunWith(RobolectricTestRunner::class) |
| 19 | +@Config(manifest = Config.NONE, sdk = [31]) |
| 20 | +class LiveVoiceRecorderTest { |
| 21 | + @Test fun preparingAgainReleasesTheUnusedMicrophone() = runTest { |
| 22 | + val context = RuntimeEnvironment.getApplication() |
| 23 | + val reactContext = BridgeReactContext(context) |
| 24 | + val appContext = AppContext( |
| 25 | + object : ModulesProvider { |
| 26 | + override fun getModulesMap() = emptyMap<Class<out Module>, String?>() |
| 27 | + }, |
| 28 | + expo.modules.core.ModuleRegistry(emptyList(), emptyList()), |
| 29 | + WeakReference(reactContext) |
| 30 | + ) |
| 31 | + val recorder = LiveVoiceRecorder(context, appContext, this) {} |
| 32 | + try { |
| 33 | + recorder.prepare() |
| 34 | + val previous = ReflectionHelpers.getField<AudioRecord>(recorder, "audio") |
| 35 | + assertEquals(AudioRecord.STATE_INITIALIZED, previous.state) |
| 36 | + recorder.prepare() |
| 37 | + assertEquals(AudioRecord.STATE_UNINITIALIZED, previous.state) |
| 38 | + } finally { |
| 39 | + recorder.sharedObjectDidRelease() |
| 40 | + appContext.javaClass.declaredMethods.first { |
| 41 | + it.name.startsWith("onDestroy") && it.parameterCount == 0 |
| 42 | + } |
| 43 | + .invoke(appContext) |
| 44 | + reactContext.destroy() |
| 45 | + } |
| 46 | + } |
| 47 | +} |
0 commit comments