Skip to content

Commit

Permalink
Add test for path hints
Browse files Browse the repository at this point in the history
  • Loading branch information
devkanro committed Oct 18, 2023
1 parent 424dab0 commit 0581bd4
Showing 1 changed file with 78 additions and 20 deletions.
98 changes: 78 additions & 20 deletions core/src/test/kotlin/org/jetbrains/jewel/PainterHintTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package org.jetbrains.jewel

import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.ui.state.ToggleableState
import org.jetbrains.jewel.painter.PainterHint
import org.jetbrains.jewel.painter.PainterPathHint
import org.jetbrains.jewel.painter.PainterResourcePathHint
import org.jetbrains.jewel.painter.hints.Dark
import org.jetbrains.jewel.painter.hints.Override
import org.jetbrains.jewel.painter.hints.Selected
import org.jetbrains.jewel.painter.hints.Size
import org.jetbrains.jewel.painter.hints.Stateful
import org.jetbrains.jewel.painter.hints.Stroke
import org.jetbrains.jewel.painter.rememberResourcePainterProvider
import org.junit.Assert
import org.junit.Test
Expand All @@ -30,35 +37,86 @@ class PainterHintTest : BasicJewelUiTest() {
awaitIdle()
}

private fun String.applyPathHints(vararg hints: PainterHint): String {
var result = this
hints.forEach {
result = when (it) {
is PainterResourcePathHint -> it.patch(result, emptyList())
is PainterPathHint -> it.patch(result)
else -> return@forEach
}
}
return result
}

@Test
fun `dark pinter hint`() = runComposeTest({
CompositionLocalProvider(LocalIsDarkTheme provides false) {
val provider = rememberResourcePainterProvider("icons/github.svg", PainterHintTest::class.java)
fun `dark painter hint`() {
val basePath = "icons/github.svg"
basePath.applyPathHints(Dark(true)).let {
Assert.assertEquals("icons/github_dark.svg", it)
}
}

val painter1 by provider.getPainter()
val painter2 by provider.getPainter(Dark(true))
// must be ignored the None and hit cache
val painter3 by provider.getPainter(Dark(false))
@Test
fun `override painter hint`() {
val basePath = "icons/github.svg"
basePath.applyPathHints(Override(mapOf("icons/github.svg" to "icons/search.svg"))).let {
Assert.assertEquals("icons/search.svg", it)
}
}

Assert.assertNotEquals(painter1, painter2)
Assert.assertEquals(painter1, painter3)
@Test
fun `selected painter hint`() {
val basePath = "icons/checkbox.svg"
basePath.applyPathHints(Selected(true)).let {
Assert.assertEquals("icons/checkboxSelected.svg", it)
}
}) {
awaitIdle()
}

@Test
fun `override pinter hint`() = runComposeTest({
CompositionLocalProvider(LocalIsDarkTheme provides false) {
val provider = rememberResourcePainterProvider("icons/github.svg", PainterHintTest::class.java)
val overrideHint = Override(mapOf("icons/github.svg" to "icons/search.svg"))
fun `size painter hint`() {
val basePath = "icons/github.svg"
basePath.applyPathHints(Size("20x20")).let {
Assert.assertEquals("icons/[email protected]", it)
}
basePath.applyPathHints(Size(20, 20)).let {
Assert.assertEquals("icons/[email protected]", it)
}
}

val painter1 by provider.getPainter()
val painter2 by provider.getPainter(overrideHint)
@Test
fun `stateful painter hint`() {
val basePath = "icons/checkbox.svg"
val state = CheckboxState.of(toggleableState = ToggleableState.Off)
basePath.applyPathHints(Stateful(state.copy(enabled = false))).let {
Assert.assertEquals("icons/checkboxDisabled.svg", it)
}
basePath.applyPathHints(Stateful(state.copy(enabled = false, pressed = true, hovered = true, focused = true)))
.let {
Assert.assertEquals("icons/checkboxDisabled.svg", it)
}
basePath.applyPathHints(Stateful(state.copy(focused = true))).let {
Assert.assertEquals("icons/checkboxFocused.svg", it)
}
basePath.applyPathHints(Stateful(state.copy(pressed = true, hovered = true, focused = true))).let {
Assert.assertEquals("icons/checkboxFocused.svg", it)
}
basePath.applyPathHints(Stateful(state.copy(pressed = true))).let {
Assert.assertEquals("icons/checkboxPressed.svg", it)
}
basePath.applyPathHints(Stateful(state.copy(pressed = true, hovered = true))).let {
Assert.assertEquals("icons/checkboxPressed.svg", it)
}
basePath.applyPathHints(Stateful(state.copy(hovered = true))).let {
Assert.assertEquals("icons/checkboxHovered.svg", it)
}
}

Assert.assertNotEquals(painter1, painter2)
@Test
fun `stroke painter hint`() {
val basePath = "icons/rerun.svg"
basePath.applyPathHints(Stroke(true)).let {
Assert.assertEquals("icons/rerun_stroke.svg", it)
}
}) {
awaitIdle()
}
}

0 comments on commit 0581bd4

Please sign in to comment.