Yokan / rebuild
Course 1 8624561

SourceLibraryHidingTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/browse/SourceLibraryHidingTest.kt

test / fixture153 lines
1package eu.friedcutlet.yokan.ui.screens.browse23import androidx.lifecycle.ViewModelProvider4import androidx.lifecycle.ViewModelStore5import androidx.lifecycle.ViewModelStoreOwner6import androidx.lifecycle.viewmodel.initializer7import androidx.lifecycle.viewmodel.viewModelFactory8import eu.friedcutlet.yokan.data.DefaultYokanRepository9import eu.friedcutlet.yokan.data.SettingsStore10import eu.friedcutlet.yokan.data.YokanRepository11import eu.friedcutlet.yokan.database.DbBrowsePreferences12import eu.friedcutlet.yokan.database.DbSource13import eu.friedcutlet.yokan.database.InMemoryYokanDatabase14import eu.friedcutlet.yokan.database.seedDatabaseSnapshot15import eu.friedcutlet.yokan.domain.ItemType16import eu.friedcutlet.yokan.services.extensions.RemoteManga17import eu.friedcutlet.yokan.services.extensions.SourceListing18import kotlin.test.AfterTest19import kotlin.test.BeforeTest20import kotlin.test.Test21import kotlin.test.assertEquals22import kotlin.test.assertFalse23import kotlin.test.assertTrue24import kotlinx.coroutines.Dispatchers25import kotlinx.coroutines.ExperimentalCoroutinesApi26import kotlinx.coroutines.flow.first27import kotlinx.coroutines.test.UnconfinedTestDispatcher28import kotlinx.coroutines.test.advanceUntilIdle29import kotlinx.coroutines.test.resetMain30import kotlinx.coroutines.test.runTest31import kotlinx.coroutines.test.setMain3233@OptIn(ExperimentalCoroutinesApi::class)34class SourceLibraryHidingTest {35    private val dispatcher = UnconfinedTestDispatcher()3637    @BeforeTest38    fun setUp() = Dispatchers.setMain(dispatcher)3940    @AfterTest41    fun tearDown() = Dispatchers.resetMain()4243    private class Entry : ViewModelStoreOwner {44        override val viewModelStore = ViewModelStore()45    }4647    private fun repository(): YokanRepository {48        val database = InMemoryYokanDatabase(seedDatabaseSnapshot())49        database.upsertSource(DbSource(id = SourceId, name = "Src", lang = "en", isAdded = true))50        return DefaultYokanRepository(database)51    }5253    private fun hide(settings: SettingsStore, type: ItemType, value: Boolean) = settings.updateDb {54        it.copy(features = it.features.copy(browse = it.features.browse.withHideLibrary(type, value)))55    }5657    /** Pages up to [hiddenPages] only contain "/fav"; later pages contain "/p<page>" and "/fav". */58    private fun Entry.sourceHome(repository: YokanRepository, settings: SettingsStore, requests: MutableList<Int>, hiddenPages: Int) =59        ViewModelProvider.create(this, viewModelFactory {60            initializer {61                SourceHomeViewModel(62                    sourceId = SourceId, genre = null, repository = repository, settings = settings, healPayload = {},63                    cache = SourceHomeCache(capacity = 5),64                    loadListing = { _, _, _, page, _ ->65                        requests += page66                        val items = if (page <= hiddenPages) listOf(RemoteManga("Fav", "/fav"))67                            else listOf(RemoteManga("P$page", "/p$page"), RemoteManga("Fav", "/fav"))68                        RuntimeLoad(SourceListing(items, hasNextPage = true))69                    },70                    loadFilters = { emptyList() },71                    loadLatestSupport = { true },72                    workDispatcher = dispatcher,73                )74            }75        })[SourceHomeViewModel::class]7677    @Test78    fun filterIsPerMediaTypeAndKeepsNonLibraryEntries() {79        val items = listOf(RemoteManga("A", "/a"), RemoteManga("B", "/b"))80        assertEquals(items, items.withoutLibrary(hide = false, libraryLinks = setOf("/a")))81        assertEquals(listOf("/b"), items.withoutLibrary(hide = true, libraryLinks = setOf("/a")).map { it.url })82        assertEquals(items, items.withoutLibrary(hide = true, libraryLinks = emptySet()))8384        val prefs = DbBrowsePreferences().withHideLibrary(ItemType.Anime, true)85        assertTrue(prefs.hidesLibrary(ItemType.Anime))86        assertFalse(prefs.hidesLibrary(ItemType.Manga))87        assertFalse(prefs.hidesLibrary(ItemType.Novel))88    }8990    @Test91    fun togglingAndLibraryChangesRefilterWithoutRefetching() = runTest(dispatcher) {92        val repository = repository()93        val settings = SettingsStore(repository, backgroundScope) {}94        val requests = mutableListOf<Int>()95        val home = Entry().sourceHome(repository, settings, requests, hiddenPages = 0)96        home.state.first { it.loadedKey != null }97        repository.upsertRemoteTitle(repository.source(SourceId)!!, RemoteManga("Fav", "/fav"), favorite = true)98        home.state.first { "/fav" in it.libraryLinks }99        assertEquals(listOf("/p1", "/fav"), home.state.value.visibleItems.map { it.url })100101        home.setHideLibrary(true)102        assertEquals(listOf("/p1"), home.state.first { it.hideLibrary }.visibleItems.map { it.url })103        // Another media type's preference leaves this manga source alone.104        hide(settings, ItemType.Anime, false)105        assertTrue(settings.db.value.features.browse.hideLibraryManga)106107        repository.upsertRemoteTitle(repository.source(SourceId)!!, RemoteManga("Fav", "/fav"), favorite = false)108        assertEquals(listOf("/p1", "/fav"), home.state.first { "/fav" !in it.libraryLinks }.visibleItems.map { it.url })109110        hide(settings, ItemType.Manga, false)111        home.state.first { !it.hideLibrary }112        advanceUntilIdle()113        assertEquals(listOf(1), requests)114    }115116    @Test117    fun allHiddenPageLoadsTheNextPage() = runTest(dispatcher) {118        val repository = repository()119        val settings = SettingsStore(repository, backgroundScope) {}120        repository.upsertRemoteTitle(repository.source(SourceId)!!, RemoteManga("Fav", "/fav"), favorite = true)121        hide(settings, ItemType.Manga, true)122        val requests = mutableListOf<Int>()123        val home = Entry().sourceHome(repository, settings, requests, hiddenPages = 1)124125        val shown = home.state.first { it.visibleItems.isNotEmpty() }126        advanceUntilIdle()127        assertEquals(listOf("/p2"), shown.visibleItems.map { it.url })128        assertEquals(listOf(1, 2), requests)129    }130131    @Test132    fun automaticPageFillsAreBounded() = runTest(dispatcher) {133        val repository = repository()134        val settings = SettingsStore(repository, backgroundScope) {}135        repository.upsertRemoteTitle(repository.source(SourceId)!!, RemoteManga("Fav", "/fav"), favorite = true)136        hide(settings, ItemType.Manga, true)137        val requests = mutableListOf<Int>()138        val home = Entry().sourceHome(repository, settings, requests, hiddenPages = Int.MAX_VALUE)139140        home.state.first { it.loadedKey != null }141        advanceUntilIdle()142        assertEquals((1..1 + MaxHiddenPageFills).toList(), requests)143        assertTrue(home.state.value.visibleItems.isEmpty())144        // The manual Load more tile still works after the bound.145        home.loadMore()146        advanceUntilIdle()147        assertEquals(2 + MaxHiddenPageFills, requests.size)148    }149150    private companion object {151        const val SourceId = 77L152    }153}154

Search chapters, reference and source filenames.