package eu.friedcutlet.yokan.ui.screens.detail import androidx.compose.foundation.layout.Column import androidx.compose.ui.ImageComposeScene import androidx.compose.ui.geometry.Offset import androidx.compose.ui.input.pointer.PointerEventType import eu.friedcutlet.yokan.data.DefaultYokanRepository import eu.friedcutlet.yokan.database.DbManga import eu.friedcutlet.yokan.database.InMemoryYokanDatabase import eu.friedcutlet.yokan.database.emptySnapshot import eu.friedcutlet.yokan.ui.theme.YokanTheme import java.util.concurrent.FutureTask import javax.swing.SwingUtilities import kotlin.test.Test import kotlin.test.assertEquals class DetailActionRowTest { @Test fun migrationImmediatelyFollowsWebViewOnPhoneAndDesktop() { for (width in listOf(360, 1024)) checkActions(width, showWeb = true) } @Test fun migrationRemainsAvailableWhenWebViewIsHidden() { checkActions(360, showWeb = false) } private fun checkActions(width: Int, showWeb: Boolean) { val repository = DefaultYokanRepository(InMemoryYokanDatabase(emptySnapshot().copy( mangas = listOf(DbManga(id = 1, name = "Test title")), ))) val manga = checkNotNull(repository.manga(1)) val clicks = mutableListOf() val scene = onUi { ImageComposeScene(width, 140) { YokanTheme(darkTheme = false) { Column { DetailActionRow( manga = manga, trackCount = 0, showWeb = showWeb, onFavorite = { clicks += "favorite" }, onFavoriteLongClick = null, automaticUpdateDays = null, onSmartUpdate = { clicks += "updates" }, onTrack = { clicks += "track" }, onWeb = { clicks += "web" }, onMigrate = { clicks += "migrate" }, ) } } } } try { var time = 0L fun render() = onUi { time += 100_000_000L; scene.render(time).close() } repeat(4) { render() } fun click(fraction: Float) { onUi { val point = Offset(width * fraction, 24f) scene.sendPointerEvent(PointerEventType.Press, point) scene.sendPointerEvent(PointerEventType.Release, point) } render() } if (showWeb) click(.7f) // Fourth of five equal-width actions. click(if (showWeb) .9f else 5f / 6f) assertEquals(if (showWeb) listOf("web", "migrate") else listOf("migrate"), clicks) } finally { onUi { scene.close() } } } private fun onUi(block: () -> T): T { val task = FutureTask { block() } SwingUtilities.invokeAndWait(task) return task.get() } }