package eu.friedcutlet.yokan.ui.screens.detail import eu.friedcutlet.yokan.domain.Chapter import kotlin.test.Test import kotlin.test.assertEquals class ChapterSelectionTest { private val chapters = (1L..6L).map { Chapter(it, 1, "Chapter $it", "/$it", isRead = it % 2L == 0L, isBookmarked = it == 3L, downloadSize = if (it == 4L) "1 MB" else null) } @Test fun statusShortcutsAndInversionOnlyUseShownChapters() { val shown = chapters.take(4) var selection = ChapterSelectionState().select(shown, ChapterSelectionOption.Unread) assertEquals(setOf(1L, 3L), selection.ids) selection = selection.select(shown, ChapterSelectionOption.Invert) assertEquals(setOf(2L, 4L), selection.ids) selection = selection.select(shown, ChapterSelectionOption.Downloaded) assertEquals(setOf(4L), selection.ids) selection = selection.select(shown, ChapterSelectionOption.Bookmarked) assertEquals(setOf(3L), selection.ids) } @Test fun rangeFollowsDisplayOrderAndKeepsTheFirstAnchor() { val reversed = chapters.reversed() var selection = ChapterSelectionState().toggle(5).rangeTo(reversed, 2) assertEquals(setOf(5L, 4L, 3L, 2L), selection.ids) assertEquals(5L, selection.anchorId) selection = selection.rangeTo(reversed, 1) assertEquals(setOf(5L, 4L, 3L, 2L, 1L), selection.ids) } @Test fun numericRangeUsesRowsRatherThanChapterIdsAndRejectsInvalidBounds() { val selection = ChapterSelectionState().selectRange(chapters.reversed(), 2, 4) assertEquals(setOf(5L, 4L, 3L), selection.ids) assertEquals(selection, selection.selectRange(chapters, 0, 4).selectRange(chapters, 4, 2)) } @Test fun aboveAndBelowIncludeTheAnchor() { var selection = ChapterSelectionState().toggle(3).select(chapters, ChapterSelectionOption.Above) assertEquals(setOf(1L, 2L, 3L), selection.ids) selection = selection.select(chapters, ChapterSelectionOption.Below) assertEquals(chapters.map { it.id }.toSet(), selection.ids) } @Test fun filteringOrRemovalDropsInvisibleSelectionsAndAnchor() { val selection = ChapterSelectionState().select(chapters, ChapterSelectionOption.All).retainVisible(chapters.drop(1)) assertEquals(setOf(2L, 3L, 4L, 5L, 6L), selection.ids) assertEquals(null, selection.anchorId) } }