ReaderPageDownloadProgressTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/reader/ReaderPageDownloadProgressTest.kt
1package eu.friedcutlet.yokan.ui.screens.reader23import kotlin.test.AfterTest4import kotlin.test.Test5import kotlin.test.assertEquals6import kotlin.test.assertNull7import kotlin.test.assertSame8import kotlinx.coroutines.Dispatchers9import kotlinx.coroutines.launch10import kotlinx.coroutines.joinAll11import kotlinx.coroutines.test.runTest12import kotlinx.coroutines.test.UnconfinedTestDispatcher13import kotlinx.coroutines.flow.toList1415class ReaderPageDownloadProgressTest {16 @Test17 fun unchangedPercentKeepsTheSameSnapshot() {18 ReaderPageDownloadProgress.report("page", 501, 1_000)19 val snapshot = ReaderPageDownloadProgress.progress.value20 ReaderPageDownloadProgress.report("page", 509, 1_000)21 assertSame(snapshot, ReaderPageDownloadProgress.progress.value)22 }2324 @Test25 fun concurrentDownloadsDoNotLoseEachOthersProgress() = runTest {26 (0 until 100).map { index ->27 launch(Dispatchers.Default) { ReaderPageDownloadProgress.report("page-$index", 50, 100) }28 }.joinAll()29 repeat(100) { assertEquals(0.5f, ReaderPageDownloadProgress.progress.value["page-$it"]) }30 }3132 @OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)33 @Test34 fun pageSubscriberDoesNotReceiveOtherPagesUpdates() = runTest {35 val values = mutableListOf<Float?>()36 backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {37 ReaderPageDownloadProgress.forUrl("watched").toList(values)38 }39 ReaderPageDownloadProgress.report("other", 10, 100)40 ReaderPageDownloadProgress.report("watched", 25, 100)41 ReaderPageDownloadProgress.report("other", 50, 100)42 ReaderPageDownloadProgress.clear("watched")43 assertEquals(listOf(null, 0.25f, null), values)44 }4546 @AfterTest47 fun tearDown() {48 ReaderPageDownloadProgress.progress.value.keys.forEach(ReaderPageDownloadProgress::clear)49 }5051 @Test52 fun reportsRatioAndClearsOnCompletion() {53 ReaderPageDownloadProgress.report("https://example/page1.jpg", receivedBytes = 250, totalBytes = 1_000)54 assertEquals(0.25f, ReaderPageDownloadProgress.progress.value["https://example/page1.jpg"])5556 ReaderPageDownloadProgress.report("https://example/page1.jpg", receivedBytes = 1_000, totalBytes = 1_000)57 assertNull(ReaderPageDownloadProgress.progress.value["https://example/page1.jpg"])58 }5960 @Test61 fun ignoresUnknownTotalsAndSupportsExplicitClear() {62 ReaderPageDownloadProgress.report("https://example/page2.jpg", receivedBytes = 10, totalBytes = null)63 ReaderPageDownloadProgress.report("https://example/page2.jpg", receivedBytes = 10, totalBytes = 0)64 assertNull(ReaderPageDownloadProgress.progress.value["https://example/page2.jpg"])6566 ReaderPageDownloadProgress.report("https://example/page3.jpg", receivedBytes = 900, totalBytes = 1_000)67 ReaderPageDownloadProgress.clear("https://example/page3.jpg")68 assertNull(ReaderPageDownloadProgress.progress.value["https://example/page3.jpg"])69 }70}71