package eu.friedcutlet.yokan.ui.screens.library import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Surface import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.ImageComposeScene import androidx.compose.ui.Modifier import androidx.compose.ui.use import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleRegistry import androidx.lifecycle.compose.LocalLifecycleOwner import eu.friedcutlet.yokan.data.LibraryIndex import eu.friedcutlet.yokan.database.DbLibraryGrouping import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.domain.Manga import eu.friedcutlet.yokan.domain.Status import eu.friedcutlet.yokan.ui.theme.YokanTheme import java.io.File import java.util.concurrent.FutureTask import javax.swing.SwingUtilities import kotlin.test.Test import kotlin.test.assertEquals class LibraryGroupingRenderTest { @Test fun aThousandTagGroupsLayOutAndNavigateAtPhoneAndDesktopWidths() { val manga = Manga(1, "A title with many tags", link = "/title", description = "", status = Status.Ongoing, itemType = ItemType.Manga, source = "Source", lang = "en", dateAddedMillis = 0, lastUpdateMillis = 0, genres = List(1000) { "Tag ${it.toString().padStart(4, '0')}" }) val settings = DbSettings().withLibraryGrouping(ItemType.Manga, DbLibraryGrouping.Tags) .withLibraryDisplay(ItemType.Manga, categoryTabs = true) val query = settings.libraryQuery(ItemType.Manga) val content = libraryContent(listOf(manga), emptyList(), LibraryIndex.Empty, query) assertEquals(1000, content.tabs.size) listOf(360 to 800, 1024 to 800).forEach { (width, height) -> val state = mutableStateOf(LibraryState(ItemType.Manga, query, settings.libraryDisplay(ItemType.Manga), content = content)) val owner = object : LifecycleOwner { override val lifecycle = LifecycleRegistry.createUnsafe(this) } onUi { owner.lifecycle.currentState = Lifecycle.State.RESUMED } val scene = onUi { ImageComposeScene(width, height) { CompositionLocalProvider(LocalLifecycleOwner provides owner) { YokanTheme(darkTheme = true) { Surface(Modifier.fillMaxSize()) { LibraryScreen(state.value, compact = width < 600, onEvent = { event -> if (event is LibraryEvent.SelectCategory) state.value = state.value.copy(selectedCategoryId = event.categoryId) }, onOpenDetail = {}, onContinue = {}) } } } } } try { var time = 0L repeat(12) { time += 100_000_000L; onUi { scene.render(time).close() } } assertEquals(null, state.value.selectedCategoryId) val last = content.tabs.last().id onUi { state.value = state.value.copy(selectedCategoryId = last) } repeat(12) { time += 100_000_000L; onUi { scene.render(time).close() } } assertEquals(last, state.value.selectedCategoryId) onUi { scene.render(time).use { image -> assertEquals(width, image.width) val output = File("build/reports/library-ui/library-1000-tags-$width.png") output.parentFile.mkdirs() checkNotNull(image.encodeToData()).use { output.writeBytes(it.bytes) } } } } finally { onUi { scene.close(); owner.lifecycle.currentState = Lifecycle.State.DESTROYED } } } } private fun onUi(block: () -> T): T { val task = FutureTask { block() } SwingUtilities.invokeAndWait(task) return task.get() } }