From 9c32dbba585b096d79e1e7fcb51650f025c69ce9 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 13 Aug 2025 10:49:05 +0200 Subject: [PATCH] Inject a StringProvider instead of the context, and update tests. Also remove the other StringProvider we had, it was not used anymore --- appnav/build.gradle.kts | 1 + .../android/appnav/RootPresenterTest.kt | 9 ++++-- .../designsystem/utils/StringProvider.kt | 15 ---------- .../apperror/api/AppErrorStateService.kt | 1 - services/apperror/impl/build.gradle.kts | 2 ++ .../impl/DefaultAppErrorStateService.kt | 9 +++--- .../impl/DefaultAppErrorStateServiceTest.kt | 28 +++++++++++++++---- 7 files changed, 36 insertions(+), 29 deletions(-) delete mode 100644 libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/utils/StringProvider.kt diff --git a/appnav/build.gradle.kts b/appnav/build.gradle.kts index e6b50eaaf6..bc0fa405a7 100644 --- a/appnav/build.gradle.kts +++ b/appnav/build.gradle.kts @@ -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) } diff --git a/appnav/src/test/kotlin/io/element/android/appnav/RootPresenterTest.kt b/appnav/src/test/kotlin/io/element/android/appnav/RootPresenterTest.kt index 4c8b2a803d..2a343a1592 100644 --- a/appnav/src/test/kotlin/io/element/android/appnav/RootPresenterTest.kt +++ b/appnav/src/test/kotlin/io/element/android/appnav/RootPresenterTest.kt @@ -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() }, diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/utils/StringProvider.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/utils/StringProvider.kt deleted file mode 100644 index 74c11e58c0..0000000000 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/utils/StringProvider.kt +++ /dev/null @@ -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) : PreviewParameterProvider { - override val values: Sequence - get() = strings.asSequence() -} diff --git a/services/apperror/api/src/main/kotlin/io/element/android/services/apperror/api/AppErrorStateService.kt b/services/apperror/api/src/main/kotlin/io/element/android/services/apperror/api/AppErrorStateService.kt index 6921ba1958..8070673804 100644 --- a/services/apperror/api/src/main/kotlin/io/element/android/services/apperror/api/AppErrorStateService.kt +++ b/services/apperror/api/src/main/kotlin/io/element/android/services/apperror/api/AppErrorStateService.kt @@ -16,5 +16,4 @@ interface AppErrorStateService { fun showError(title: String, body: String) fun showError(@StringRes titleRes: Int, @StringRes bodyRes: Int) - } diff --git a/services/apperror/impl/build.gradle.kts b/services/apperror/impl/build.gradle.kts index a3d17a14cd..01163d240c 100644 --- a/services/apperror/impl/build.gradle.kts +++ b/services/apperror/impl/build.gradle.kts @@ -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) } diff --git a/services/apperror/impl/src/main/kotlin/io/element/android/services/apperror/impl/DefaultAppErrorStateService.kt b/services/apperror/impl/src/main/kotlin/io/element/android/services/apperror/impl/DefaultAppErrorStateService.kt index ca6e6d34cf..9f9ede50ee 100644 --- a/services/apperror/impl/src/main/kotlin/io/element/android/services/apperror/impl/DefaultAppErrorStateService.kt +++ b/services/apperror/impl/src/main/kotlin/io/element/android/services/apperror/impl/DefaultAppErrorStateService.kt @@ -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.NoError) override val appErrorStateFlow: StateFlow = 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) } } diff --git a/services/apperror/impl/src/test/kotlin/io/element/android/services/apperror/impl/DefaultAppErrorStateServiceTest.kt b/services/apperror/impl/src/test/kotlin/io/element/android/services/apperror/impl/DefaultAppErrorStateServiceTest.kt index 86f918c264..6eb26c9c50 100644 --- a/services/apperror/impl/src/test/kotlin/io/element/android/services/apperror/impl/DefaultAppErrorStateServiceTest.kt +++ b/services/apperror/impl/src/test/kotlin/io/element/android/services/apperror/impl/DefaultAppErrorStateServiceTest.kt @@ -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(), + ) }