Clean up and add unit test on Theme.isDark()
This commit is contained in:
parent
8e88c20a1d
commit
25c3fed1b1
2 changed files with 74 additions and 2 deletions
|
|
@ -18,8 +18,6 @@ enum class Theme {
|
||||||
Light,
|
Light,
|
||||||
}
|
}
|
||||||
|
|
||||||
val themes = listOf(Theme.System, Theme.Dark, Theme.Light)
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Theme.isDark(): Boolean {
|
fun Theme.isDark(): Boolean {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue