diff --git a/libraries/compound/src/main/kotlin/io/element/android/compound/theme/Theme.kt b/libraries/compound/src/main/kotlin/io/element/android/compound/theme/Theme.kt index c6e9b47eb7..06668e2952 100644 --- a/libraries/compound/src/main/kotlin/io/element/android/compound/theme/Theme.kt +++ b/libraries/compound/src/main/kotlin/io/element/android/compound/theme/Theme.kt @@ -18,8 +18,6 @@ enum class Theme { Light, } -val themes = listOf(Theme.System, Theme.Dark, Theme.Light) - @Composable fun Theme.isDark(): Boolean { return when (this) { diff --git a/libraries/compound/src/test/kotlin/io/element/android/compound/theme/ThemeTest.kt b/libraries/compound/src/test/kotlin/io/element/android/compound/theme/ThemeTest.kt new file mode 100644 index 0000000000..17cb2425dc --- /dev/null +++ b/libraries/compound/src/test/kotlin/io/element/android/compound/theme/ThemeTest.kt @@ -0,0 +1,74 @@ +/* + * Copyright 2025 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.compound.theme + +import android.content.res.Configuration +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.platform.LocalConfiguration +import app.cash.molecule.RecompositionMode +import app.cash.molecule.moleculeFlow +import app.cash.turbine.test +import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.test.runTest +import org.junit.Test + +class ThemeTest { + @Test + fun `isDark for System dark returns true`() { + `isDark for System`( + uiMode = Configuration.UI_MODE_NIGHT_YES, + expected = true, + ) + } + + @Test + fun `isDark for System light return false`() { + `isDark for System`( + uiMode = Configuration.UI_MODE_NIGHT_NO, + expected = false, + ) + } + + fun `isDark for System`( + uiMode: Int, + expected: Boolean, + ) = runTest { + moleculeFlow(RecompositionMode.Immediate) { + var result: Boolean? = null + CompositionLocalProvider( + // Let set the system to dark + LocalConfiguration provides Configuration().apply { + this.uiMode = uiMode + }, + ) { + result = Theme.System.isDark() + } + result + }.test { + assertThat(awaitItem()).isEqualTo(expected) + } + } + + @Test + fun `isDark for Light returns false`() = runTest { + moleculeFlow(RecompositionMode.Immediate) { + Theme.Light.isDark() + }.test { + assertThat(awaitItem()).isFalse() + } + } + + @Test + fun `isDark for Dark returns true`() = runTest { + moleculeFlow(RecompositionMode.Immediate) { + Theme.Dark.isDark() + }.test { + assertThat(awaitItem()).isTrue() + } + } +}