StatisticsRenderTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/ui/screens/settings/statistics/StatisticsRenderTest.kt
1package eu.friedcutlet.yokan.ui.screens.settings.statistics23import androidx.compose.foundation.layout.fillMaxSize4import androidx.compose.foundation.rememberScrollState5import androidx.compose.foundation.verticalScroll6import androidx.compose.material3.Surface7import androidx.compose.ui.ImageComposeScene8import androidx.compose.ui.Modifier9import androidx.compose.ui.use10import eu.friedcutlet.yokan.domain.History11import eu.friedcutlet.yokan.domain.ItemType12import eu.friedcutlet.yokan.ui.theme.YokanTheme13import java.io.File14import java.util.concurrent.FutureTask15import javax.swing.SwingUtilities16import kotlin.test.Test1718class StatisticsRenderTest {19 @Test fun renderPhoneAndDesktopLightDarkAndEmptyStates() {20 val history = listOf("Frieren: Beyond Journey’s End", "Witch Hat Atelier", "Blue Period", "The Summer Hikaru Died", "Dungeon Meshi")21 .mapIndexed { index, title -> History(index.toLong(), index.toLong(), index.toLong(), ItemType.Manga,22 title, "Chapter ${42 - index * 3}", "Now", 39000 - index * 7000, 1790000000000L - index * 86400000L) }23 val days = List(ACTIVITY_WEEKS * 7 - 3) { i -> if ((i * 7 + i / 5) % 5 < 2) 0 else (i * 13 % 9) }24 val hourly = List(24) { h -> if (h in 2..6) 0 else ((h - 12) * (h - 12) % 11) + if (h in 20..23) 8 else 0 }25 val genres = listOf("Fantasy" to 18, "Drama" to 14, "Adventure" to 11, "Slice of Life" to 8, "Comedy" to 6,26 "Mystery" to 4, "Romance" to 3, "Horror" to 2)27 val growth = listOf(4, 5, 5, 7, 9, 12, 13, 13, 15, 19, 22, 24, 25, 28, 30, 31, 33, 34, 36, 37, 39, 40, 41, 42)28 val data = StatisticsData(42, 1280, 864, 12, 18, 12, 8, 14, 20, 156840L, 28, history, days, hourly, genres, growth)29 for (width in listOf(360, 1000)) for (dark in listOf(false, true)) for (empty in listOf(false, true)) {30 val fixture = if (empty) summarizeStatistics(emptyList(), emptyMap(), emptyList(), emptySet(), emptySet()) else data31 val scene = ui { ImageComposeScene(width, if (width == 360) 3000 else 1700) {32 YokanTheme(darkTheme = dark) {33 Surface(Modifier.fillMaxSize()) {34 StatisticsDashboard(StatisticsState(ItemType.Manga, data = fixture, downloadBytes = 8_200_000_000, localBytes = 12_600_000_000),35 Modifier.verticalScroll(rememberScrollState()))36 }37 }38 } }39 try {40 repeat(12) { frame -> ui { scene.render(frame * 100_000_000L).close() } }41 ui { scene.render(1_300_000_000L).use { image ->42 checkNotNull(image.encodeToData()).use { data ->43 File("build/reports/statistics/$width-${if (dark) "dark" else "light"}-${if (empty) "empty" else "populated"}.png")44 .apply { parentFile.mkdirs(); writeBytes(data.bytes) }45 }46 } }47 } finally { ui { scene.close() } }48 }49 }50 private fun <T> ui(block: () -> T): T {51 val task = FutureTask<T> { block() }52 SwingUtilities.invokeAndWait(task)53 return task.get()54 }55}56