Skip to content

Commit 5be157b

Browse files
committed
Clean up and add unit test on Theme.isDark()
1 parent 4c358ae commit 5be157b

File tree

2 files changed

+74
-2
lines changed
  • libraries/compound/src
    • main/kotlin/io/element/android/compound/theme
    • test/kotlin/io/element/android/compound/theme

2 files changed

+74
-2
lines changed

libraries/compound/src/main/kotlin/io/element/android/compound/theme/Theme.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ enum class Theme {
1818
Light,
1919
}
2020

21-
val themes = listOf(Theme.System, Theme.Dark, Theme.Light)
22-
2321
@Composable
2422
fun Theme.isDark(): Boolean {
2523
return when (this) {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
package io.element.android.compound.theme
9+
10+
import android.content.res.Configuration
11+
import androidx.compose.runtime.CompositionLocalProvider
12+
import androidx.compose.ui.platform.LocalConfiguration
13+
import app.cash.molecule.RecompositionMode
14+
import app.cash.molecule.moleculeFlow
15+
import app.cash.turbine.test
16+
import com.google.common.truth.Truth.assertThat
17+
import kotlinx.coroutines.test.runTest
18+
import org.junit.Test
19+
20+
class ThemeTest {
21+
@Test
22+
fun `isDark for System dark returns true`() {
23+
`isDark for System`(
24+
uiMode = Configuration.UI_MODE_NIGHT_YES,
25+
expected = true,
26+
)
27+
}
28+
29+
@Test
30+
fun `isDark for System light return false`() {
31+
`isDark for System`(
32+
uiMode = Configuration.UI_MODE_NIGHT_NO,
33+
expected = false,
34+
)
35+
}
36+
37+
fun `isDark for System`(
38+
uiMode: Int,
39+
expected: Boolean,
40+
) = runTest {
41+
moleculeFlow(RecompositionMode.Immediate) {
42+
var result: Boolean? = null
43+
CompositionLocalProvider(
44+
// Let set the system to dark
45+
LocalConfiguration provides Configuration().apply {
46+
this.uiMode = uiMode
47+
},
48+
) {
49+
result = Theme.System.isDark()
50+
}
51+
result
52+
}.test {
53+
assertThat(awaitItem()).isEqualTo(expected)
54+
}
55+
}
56+
57+
@Test
58+
fun `isDark for Light returns false`() = runTest {
59+
moleculeFlow(RecompositionMode.Immediate) {
60+
Theme.Light.isDark()
61+
}.test {
62+
assertThat(awaitItem()).isFalse()
63+
}
64+
}
65+
66+
@Test
67+
fun `isDark for Dark returns true`() = runTest {
68+
moleculeFlow(RecompositionMode.Immediate) {
69+
Theme.Dark.isDark()
70+
}.test {
71+
assertThat(awaitItem()).isTrue()
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)