LibraryGroupingRenderTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/ui/screens/library/LibraryGroupingRenderTest.kt
1package eu.friedcutlet.yokan.ui.screens.library23import androidx.compose.foundation.layout.fillMaxSize4import androidx.compose.material3.Surface5import androidx.compose.runtime.CompositionLocalProvider6import androidx.compose.runtime.mutableStateOf7import androidx.compose.ui.ImageComposeScene8import androidx.compose.ui.Modifier9import androidx.compose.ui.use10import androidx.lifecycle.Lifecycle11import androidx.lifecycle.LifecycleOwner12import androidx.lifecycle.LifecycleRegistry13import androidx.lifecycle.compose.LocalLifecycleOwner14import eu.friedcutlet.yokan.data.LibraryIndex15import eu.friedcutlet.yokan.database.DbLibraryGrouping16import eu.friedcutlet.yokan.database.DbSettings17import eu.friedcutlet.yokan.domain.ItemType18import eu.friedcutlet.yokan.domain.Manga19import eu.friedcutlet.yokan.domain.Status20import eu.friedcutlet.yokan.ui.theme.YokanTheme21import java.io.File22import java.util.concurrent.FutureTask23import javax.swing.SwingUtilities24import kotlin.test.Test25import kotlin.test.assertEquals2627class LibraryGroupingRenderTest {28 @Test fun aThousandTagGroupsLayOutAndNavigateAtPhoneAndDesktopWidths() {29 val manga = Manga(1, "A title with many tags", link = "/title", description = "", status = Status.Ongoing,30 itemType = ItemType.Manga, source = "Source", lang = "en", dateAddedMillis = 0, lastUpdateMillis = 0,31 genres = List(1000) { "Tag ${it.toString().padStart(4, '0')}" })32 val settings = DbSettings().withLibraryGrouping(ItemType.Manga, DbLibraryGrouping.Tags)33 .withLibraryDisplay(ItemType.Manga, categoryTabs = true)34 val query = settings.libraryQuery(ItemType.Manga)35 val content = libraryContent(listOf(manga), emptyList(), LibraryIndex.Empty, query)36 assertEquals(1000, content.tabs.size)37 listOf(360 to 800, 1024 to 800).forEach { (width, height) ->38 val state = mutableStateOf(LibraryState(ItemType.Manga, query, settings.libraryDisplay(ItemType.Manga), content = content))39 val owner = object : LifecycleOwner { override val lifecycle = LifecycleRegistry.createUnsafe(this) }40 onUi { owner.lifecycle.currentState = Lifecycle.State.RESUMED }41 val scene = onUi { ImageComposeScene(width, height) {42 CompositionLocalProvider(LocalLifecycleOwner provides owner) {43 YokanTheme(darkTheme = true) {44 Surface(Modifier.fillMaxSize()) {45 LibraryScreen(state.value, compact = width < 600, onEvent = { event ->46 if (event is LibraryEvent.SelectCategory) state.value = state.value.copy(selectedCategoryId = event.categoryId)47 }, onOpenDetail = {}, onContinue = {})48 }49 }50 }51 } }52 try {53 var time = 0L54 repeat(12) { time += 100_000_000L; onUi { scene.render(time).close() } }55 assertEquals(null, state.value.selectedCategoryId)56 val last = content.tabs.last().id57 onUi { state.value = state.value.copy(selectedCategoryId = last) }58 repeat(12) { time += 100_000_000L; onUi { scene.render(time).close() } }59 assertEquals(last, state.value.selectedCategoryId)60 onUi { scene.render(time).use { image ->61 assertEquals(width, image.width)62 val output = File("build/reports/library-ui/library-1000-tags-$width.png")63 output.parentFile.mkdirs()64 checkNotNull(image.encodeToData()).use { output.writeBytes(it.bytes) }65 } }66 } finally {67 onUi { scene.close(); owner.lifecycle.currentState = Lifecycle.State.DESTROYED }68 }69 }70 }7172 private fun <T> onUi(block: () -> T): T {73 val task = FutureTask<T> { block() }74 SwingUtilities.invokeAndWait(task)75 return task.get()76 }77}78