Inject a StringProvider instead of the context, and update tests.

Also remove the other StringProvider we had, it was not used anymore
This commit is contained in:
Benoit Marty
2025-08-13 10:49:05 +02:00
committed by Benoit Marty
parent 5bbfaad03b
commit 9c32dbba58
7 changed files with 36 additions and 29 deletions

View File

@@ -65,6 +65,7 @@ dependencies {
testImplementation(projects.features.rageshake.test)
testImplementation(projects.services.appnavstate.test)
testImplementation(projects.services.analytics.test)
testImplementation(projects.services.toolbox.test)
testImplementation(libs.test.appyx.junit)
testImplementation(libs.test.arch.core)
}

View File

@@ -19,6 +19,7 @@ import io.element.android.services.analytics.test.FakeAnalyticsService
import io.element.android.services.apperror.api.AppErrorState
import io.element.android.services.apperror.api.AppErrorStateService
import io.element.android.services.apperror.impl.DefaultAppErrorStateService
import io.element.android.services.toolbox.test.strings.FakeStringProvider
import io.element.android.tests.testutils.WarmUpRule
import kotlinx.coroutines.test.runTest
import org.junit.Rule
@@ -42,7 +43,9 @@ class RootPresenterTest {
@Test
fun `present - passes app error state`() = runTest {
val presenter = createRootPresenter(
appErrorService = DefaultAppErrorStateService().apply {
appErrorService = DefaultAppErrorStateService(
stringProvider = FakeStringProvider(),
).apply {
showError("Bad news", "Something bad happened")
}
)
@@ -61,7 +64,9 @@ class RootPresenterTest {
}
private fun createRootPresenter(
appErrorService: AppErrorStateService = DefaultAppErrorStateService(),
appErrorService: AppErrorStateService = DefaultAppErrorStateService(
stringProvider = FakeStringProvider(),
),
): RootPresenter {
return RootPresenter(
crashDetectionPresenter = { aCrashDetectionState() },

View File

@@ -1,15 +0,0 @@
/*
* Copyright 2023, 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.designsystem.utils
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
open class StringProvider(val strings: List<String>) : PreviewParameterProvider<String> {
override val values: Sequence<String>
get() = strings.asSequence()
}

View File

@@ -16,5 +16,4 @@ interface AppErrorStateService {
fun showError(title: String, body: String)
fun showError(@StringRes titleRes: Int, @StringRes bodyRes: Int)
}

View File

@@ -23,6 +23,7 @@ dependencies {
implementation(projects.libraries.di)
implementation(projects.libraries.designsystem)
implementation(projects.libraries.uiStrings)
implementation(projects.services.toolbox.api)
implementation(projects.anvilannotations)
implementation(libs.coroutines.core)
@@ -34,4 +35,5 @@ dependencies {
testImplementation(libs.coroutines.test)
testImplementation(libs.test.turbine)
testImplementation(libs.test.truth)
testImplementation(projects.services.toolbox.test)
}

View File

@@ -7,13 +7,12 @@
package io.element.android.services.apperror.impl
import android.content.Context
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.di.SingleIn
import io.element.android.services.apperror.api.AppErrorState
import io.element.android.services.apperror.api.AppErrorStateService
import io.element.android.services.toolbox.api.strings.StringProvider
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject
@@ -21,7 +20,7 @@ import javax.inject.Inject
@ContributesBinding(AppScope::class)
@SingleIn(AppScope::class)
class DefaultAppErrorStateService @Inject constructor(
@ApplicationContext private val context: Context,
private val stringProvider: StringProvider,
) : AppErrorStateService {
private val currentAppErrorState = MutableStateFlow<AppErrorState>(AppErrorState.NoError)
override val appErrorStateFlow: StateFlow<AppErrorState> = currentAppErrorState
@@ -37,8 +36,8 @@ class DefaultAppErrorStateService @Inject constructor(
}
override fun showError(titleRes: Int, bodyRes: Int) {
val title = context.getString(titleRes)
val body = context.getString(bodyRes)
val title = stringProvider.getString(titleRes)
val body = stringProvider.getString(bodyRes)
showError(title, body)
}
}

View File

@@ -10,14 +10,14 @@ package io.element.android.services.apperror.impl
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import io.element.android.services.apperror.api.AppErrorState
import io.element.android.services.toolbox.test.strings.FakeStringProvider
import kotlinx.coroutines.test.runTest
import org.junit.Test
internal class DefaultAppErrorStateServiceTest {
@Test
fun `initial value is no error`() = runTest {
val service = DefaultAppErrorStateService()
val service = createDefaultAppErrorStateService()
service.appErrorStateFlow.test {
val state = awaitItem()
assertThat(state).isInstanceOf(AppErrorState.NoError::class.java)
@@ -26,8 +26,7 @@ internal class DefaultAppErrorStateServiceTest {
@Test
fun `showError - emits value`() = runTest {
val service = DefaultAppErrorStateService()
val service = createDefaultAppErrorStateService()
service.appErrorStateFlow.test {
skipItems(1)
@@ -42,9 +41,22 @@ internal class DefaultAppErrorStateServiceTest {
}
@Test
fun `dismiss - clears value`() = runTest {
val service = DefaultAppErrorStateService()
fun `showError - emits value from ids`() = runTest {
val service = createDefaultAppErrorStateService()
service.appErrorStateFlow.test {
skipItems(1)
service.showError(1, 2)
val state = awaitItem()
assertThat(state).isInstanceOf(AppErrorState.Error::class.java)
val errorState = state as AppErrorState.Error
assertThat(errorState.title).isEqualTo("A string")
assertThat(errorState.body).isEqualTo("A string")
}
}
@Test
fun `dismiss - clears value`() = runTest {
val service = createDefaultAppErrorStateService()
service.appErrorStateFlow.test {
skipItems(1)
@@ -58,4 +70,8 @@ internal class DefaultAppErrorStateServiceTest {
assertThat(awaitItem()).isInstanceOf(AppErrorState.NoError::class.java)
}
}
private fun createDefaultAppErrorStateService() = DefaultAppErrorStateService(
stringProvider = FakeStringProvider(),
)
}