Yokan / rebuild
Course 1 8624561

SourceRelatedScreenRenderTest.kt

composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/ui/screens/discovery/SourceRelatedScreenRenderTest.kt

test / fixture72 lines
1package eu.friedcutlet.yokan.ui.screens.discovery23import androidx.compose.foundation.layout.fillMaxSize4import androidx.compose.material3.Surface5import androidx.compose.runtime.CompositionLocalProvider6import androidx.compose.ui.ImageComposeScene7import androidx.compose.ui.Modifier8import androidx.compose.ui.geometry.Offset9import androidx.compose.ui.input.pointer.PointerEventType10import androidx.compose.ui.use11import androidx.lifecycle.*12import androidx.lifecycle.compose.LocalLifecycleOwner13import eu.friedcutlet.yokan.services.extensions.RemoteManga14import eu.friedcutlet.yokan.services.extensions.SourceRelatedGroup15import eu.friedcutlet.yokan.ui.theme.YokanTheme16import java.io.File17import java.util.concurrent.FutureTask18import javax.swing.SwingUtilities19import kotlin.test.*2021/** Synthetic source data; renders the shared layout and exercises opening an actual row. */22class SourceRelatedScreenRenderTest {23    @Test fun groupedResultsAndRecoveryStatesRenderAtCompactAndWideSizes() {24        val owner = object : LifecycleOwner { override val lifecycle = LifecycleRegistry.createUnsafe(this) }25        onUi { owner.lifecycle.currentState = Lifecycle.State.RESUMED }26        val loaded = SourceRelatedState(title = "Sample journey", supported = true, groups = listOf(27            SourceRelatedGroup("Sequel", listOf(28                RemoteManga("Sample journey: The next season", "/next", description = "The story continues with familiar characters and a new destination."),29                RemoteManga("A related title with a longer name that should remain readable on a compact screen", "/long"),30            )),31            SourceRelatedGroup("Adaptation", listOf(RemoteManga("Sample journey — original manga", "/manga"))),32        ))33        try {34            listOf(Triple(360, 900, true), Triple(1024, 800, false)).forEach { (width, height, dark) ->35                listOf("groups" to loaded, "error" to SourceRelatedState(error = "The source could not be reached. Check your connection and try again."),36                    "unsupported" to SourceRelatedState(supported = false)).forEach { (name, state) ->37                    val opened = mutableListOf<RemoteManga>()38                    val scene = onUi { ImageComposeScene(width, height) {39                        CompositionLocalProvider(LocalLifecycleOwner provides owner) {40                            YokanTheme(darkTheme = dark) { Surface(Modifier.fillMaxSize()) {41                                SourceRelatedScreen(state, onOpen = { opened += it }, onRefresh = {}, onBack = {})42                            } }43                        }44                    } }45                    try {46                        repeat(10) { onUi { scene.render(it * 100_000_000L).close() } }47                        onUi { scene.render(1_100_000_000L).use { image ->48                            assertEquals(width, image.width)49                            val output = File("build/reports/source-related-ui/$name-$width.png")50                            output.parentFile.mkdirs()51                            checkNotNull(image.encodeToData()).use { output.writeBytes(it.bytes) }52                        } }53                        if (name == "groups") {54                            onUi {55                                scene.sendPointerEvent(PointerEventType.Press, Offset(200f, 210f))56                                scene.sendPointerEvent(PointerEventType.Release, Offset(200f, 210f))57                            }58                            assertEquals("/next", opened.single().url)59                        }60                    } finally { onUi { scene.close() } }61                }62            }63        } finally { onUi { owner.lifecycle.currentState = Lifecycle.State.DESTROYED } }64    }6566    private fun <T> onUi(block: () -> T): T {67        if (SwingUtilities.isEventDispatchThread()) return block()68        val task = FutureTask(block)69        SwingUtilities.invokeAndWait(task)70        return task.get()71    }72}73

Search chapters, reference and source filenames.