From 39b7735118627581b07369e216907b7836abedcc Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 10 Jul 2024 17:39:42 +0200 Subject: [PATCH] Properly setup hilt hierarchy for SttInputDeviceWrapper --- .../screenshot/FakeSttInputDeviceWrapper.kt | 33 +++---------------- .../dicio/screenshot/ScreenshotTakerTest.kt | 15 +++------ .../stypox/dicio/di/SttInputDeviceWrapper.kt | 23 ++++++++----- 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/app/src/androidTest/kotlin/org/stypox/dicio/screenshot/FakeSttInputDeviceWrapper.kt b/app/src/androidTest/kotlin/org/stypox/dicio/screenshot/FakeSttInputDeviceWrapper.kt index 902f4f75..5db295b8 100644 --- a/app/src/androidTest/kotlin/org/stypox/dicio/screenshot/FakeSttInputDeviceWrapper.kt +++ b/app/src/androidTest/kotlin/org/stypox/dicio/screenshot/FakeSttInputDeviceWrapper.kt @@ -1,39 +1,16 @@ package org.stypox.dicio.screenshot -import android.content.Context -import androidx.datastore.core.DataStore -import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow -import okhttp3.OkHttpClient -import org.stypox.dicio.di.LocaleManager import org.stypox.dicio.di.SttInputDeviceWrapper import org.stypox.dicio.io.input.InputEvent -import org.stypox.dicio.io.input.SttInputDevice -import org.stypox.dicio.settings.datastore.InputDevice -import org.stypox.dicio.settings.datastore.UserSettings import org.stypox.dicio.ui.home.SttState -class FakeSttInputDeviceWrapper( - @ApplicationContext appContext: Context, - dataStore: DataStore, - localeManager: LocaleManager, - okHttpClient: OkHttpClient -) : SttInputDeviceWrapper(appContext, dataStore, localeManager, okHttpClient) { - val fakeUiState: MutableStateFlow = MutableStateFlow(SttState.NotInitialized) +class FakeSttInputDeviceWrapper : SttInputDeviceWrapper { + override val uiState: MutableStateFlow = MutableStateFlow(SttState.NotInitialized) - override fun buildInputDevice(setting: InputDevice): SttInputDevice { - return object : SttInputDevice { - override val uiState: StateFlow get() = fakeUiState - - override fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) { - } - - override fun onClick(eventListener: (InputEvent) -> Unit) { - } + override fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) { + } - override suspend fun destroy() { - } - } + override fun onClick(eventListener: (InputEvent) -> Unit) { } } diff --git a/app/src/androidTest/kotlin/org/stypox/dicio/screenshot/ScreenshotTakerTest.kt b/app/src/androidTest/kotlin/org/stypox/dicio/screenshot/ScreenshotTakerTest.kt index 41fb1a89..e29be284 100644 --- a/app/src/androidTest/kotlin/org/stypox/dicio/screenshot/ScreenshotTakerTest.kt +++ b/app/src/androidTest/kotlin/org/stypox/dicio/screenshot/ScreenshotTakerTest.kt @@ -64,13 +64,8 @@ class ScreenshotTakerTest { class FakeSttInputDeviceWrapperModule { @Provides @Singleton - fun provideInputDeviceWrapper( - @ApplicationContext appContext: Context, - dataStore: DataStore, - localeManager: LocaleManager, - okHttpClient: OkHttpClient, - ): SttInputDeviceWrapper { - return FakeSttInputDeviceWrapper(appContext, dataStore, localeManager, okHttpClient) + fun provideInputDeviceWrapper(): SttInputDeviceWrapper { + return FakeSttInputDeviceWrapper() } } @@ -137,12 +132,12 @@ class ScreenshotTakerTest { // screenshot 0: home screen with "Here is what I can do" and STT listening dataStore.updateData { it.copy { theme = Theme.THEME_DARK } } - fakeSttInputDeviceWrapper.fakeUiState.emit(SttState.Listening) + fakeSttInputDeviceWrapper.uiState.emit(SttState.Listening) composeRule.takeScreenshot("en-US", "0") // screenshot 1: home screen with interactions with weather, timer and lyrics skills dataStore.updateData { it.copy { theme = Theme.THEME_LIGHT } } - fakeSttInputDeviceWrapper.fakeUiState.emit(SttState.Loaded) + fakeSttInputDeviceWrapper.uiState.emit(SttState.Loaded) coilEventListener.resetStartedImages() fakeSkillEvaluator.state.value = screenshot2InteractionLog composeRule.onNodeWithTag("interaction_list") @@ -152,7 +147,7 @@ class ScreenshotTakerTest { // screenshot 2: home screen with interactions with calculator, telephone and search skills dataStore.updateData { it.copy { theme = Theme.THEME_BLACK } } - fakeSttInputDeviceWrapper.fakeUiState.emit(SttState.Loaded) + fakeSttInputDeviceWrapper.uiState.emit(SttState.Loaded) coilEventListener.resetStartedImages() fakeSkillEvaluator.state.value = screenshot3InteractionLog composeRule.onNodeWithTag("interaction_list") diff --git a/app/src/main/kotlin/org/stypox/dicio/di/SttInputDeviceWrapper.kt b/app/src/main/kotlin/org/stypox/dicio/di/SttInputDeviceWrapper.kt index 1a9acaf9..d063b748 100644 --- a/app/src/main/kotlin/org/stypox/dicio/di/SttInputDeviceWrapper.kt +++ b/app/src/main/kotlin/org/stypox/dicio/di/SttInputDeviceWrapper.kt @@ -28,19 +28,27 @@ import org.stypox.dicio.ui.home.SttState import org.stypox.dicio.util.distinctUntilChangedBlockingFirst import javax.inject.Singleton -open class SttInputDeviceWrapper( +interface SttInputDeviceWrapper { + val uiState: StateFlow + + fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) + + fun onClick(eventListener: (InputEvent) -> Unit) +} + +class SttInputDeviceWrapperImpl( @ApplicationContext private val appContext: Context, dataStore: DataStore, private val localeManager: LocaleManager, private val okHttpClient: OkHttpClient, -) { +) : SttInputDeviceWrapper { private val scope = CoroutineScope(Dispatchers.Default) private var sttInputDevice: SttInputDevice? = null // null means that the user has not enabled any STT input device private val _uiState: MutableStateFlow = MutableStateFlow(null) - val uiState: StateFlow = _uiState + override val uiState: StateFlow = _uiState private var uiStateJob: Job? = null @@ -66,8 +74,7 @@ open class SttInputDeviceWrapper( } } - // overridden in tests (TODO is there a better solution?) - protected open fun buildInputDevice(setting: InputDevice): SttInputDevice? { + private fun buildInputDevice(setting: InputDevice): SttInputDevice? { return when (setting) { UNRECOGNIZED, INPUT_DEVICE_UNSET, @@ -91,11 +98,11 @@ open class SttInputDeviceWrapper( } - fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) { + override fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) { sttInputDevice?.tryLoad(thenStartListeningEventListener) } - fun onClick(eventListener: (InputEvent) -> Unit) { + override fun onClick(eventListener: (InputEvent) -> Unit) { sttInputDevice?.onClick(eventListener) } } @@ -111,6 +118,6 @@ class SttInputDeviceWrapperModule { localeManager: LocaleManager, okHttpClient: OkHttpClient, ): SttInputDeviceWrapper { - return SttInputDeviceWrapper(appContext, dataStore, localeManager, okHttpClient) + return SttInputDeviceWrapperImpl(appContext, dataStore, localeManager, okHttpClient) } }