package link.roadio.riff.player.auto import link.roadio.riff.indexer.core.media.MediaId import link.roadio.riff.player.storage.PlaybackCheckpoint import link.roadio.riff.player.storage.PlaybackQueueKind import link.roadio.riff.player.storage.PlaybackQueueOccurrence import link.roadio.riff.player.storage.PlaybackQueueSnapshot import link.roadio.riff.ui.BrowserItem import link.roadio.riff.ui.BrowserKind import link.roadio.riff.ui.BrowserRowKey import link.roadio.riff.ui.BrowserRows import link.roadio.riff.ui.contextualPlayProjection import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotEquals import org.junit.Assert.assertSame import org.junit.Assert.assertThrows import org.junit.Assert.assertTrue import org.junit.Test class AndroidAutoCatalogTest { private fun track( id: String, media: Long = 7, ) = BrowserItem(id, id, BrowserKind.TRACK, playable = true, mediaId = MediaId(media)) private fun category( id: String, children: List = emptyList(), ) = BrowserItem("category:$id", id, BrowserKind.CATEGORY, children = children) private fun tree( albums: List, epoch: Long = 1, queue: PlaybackQueueSnapshot? = null, playlists: List = emptyList(), ) = AndroidAutoCatalog( epoch, BrowserItem( "root", "Library", BrowserKind.ROOT, children = listOf(category("albums", albums), category("artists"), category("folders")), ), category("playlists", playlists), queue, ) { track("queue-$it") } @Test fun rootContainsOnlyFourBrowsableHostSections() { val tree = tree(emptyList()) val root = tree.children(AndroidAutoCatalog.ROOT) assertEquals(listOf("Album", "Artist", "Folder", "Queue"), root.map { it.title }) assertTrue(root.all { it.browsable && !it.playable }) assertEquals(listOf("Current queue", "Playlists"), tree.children(AndroidAutoCatalog.QUEUE).map { it.title }) } @Test fun selectedRepeatedAlbumPositionRetainsItsContextAndBinding() { val first = track("first").copy(sourceBindingId = 10) val second = track("second").copy(sourceBindingId = 20) val album = BrowserItem("album:one", "Album", BrowserKind.ALBUM, children = listOf(first, second)) val tree = tree(listOf(album)) val albumId = tree.children(AndroidAutoCatalog.ALBUM).single().id val selected = tree.children(albumId).last() val request = tree.selection(selected.id) as AutoPlayRequest.Context val projection = requireNotNull(contextualPlayProjection(request.container, request.selectedId)) assertEquals(listOf(first, second), projection.tracks) assertEquals(1, projection.selectedQueueIndex) assertEquals(20L, projection.tracks[1].sourceBindingId) } @Test fun containerPlayUsesExistingResumeProjection() { val album = BrowserItem("album:one", "Album", BrowserKind.ALBUM, children = listOf(track("one"), track("two"))) val tree = tree(listOf(album)) val albumId = tree.children(AndroidAutoCatalog.ALBUM).single().id val playAll = tree.children(albumId).first() assertEquals("Play all", playAll.title) val request = tree.selection(playAll.id) as AutoPlayRequest.Context val projection = requireNotNull(contextualPlayProjection(request.container, request.selectedId)) assertTrue(projection.resume) assertTrue(projection.albumContext) assertEquals(album.children, projection.tracks) } @Test fun changedViewRejectsOldPlayableAndBrowseTokens() { val album = BrowserItem("album:one", "Album", BrowserKind.ALBUM, children = listOf(track("one"))) val before = tree(listOf(album), epoch = 1) val node = before.children(AndroidAutoCatalog.ALBUM).single().id val playable = before.children(node).last().id val after = tree(listOf(album), epoch = 2) assertThrows(IllegalArgumentException::class.java) { after.children(node) } assertThrows(IllegalArgumentException::class.java) { after.selection(playable) } } @Test fun oversizedCatalogCreatesRangesWithoutDisplayRowMaterialization() { var rowsCreated = 0 val rows = object : BrowserRows() { override val size = 200_001 override fun key(index: Int) = BrowserRowKey("album:$index", "Album $index", BrowserKind.ALBUM) override fun get(index: Int): BrowserItem { rowsCreated++ return BrowserItem("album:$index", "Album $index", BrowserKind.ALBUM) } } val tree = tree(rows) val ranges = tree.children(AndroidAutoCatalog.ALBUM) assertTrue(ranges.size <= AndroidAutoCatalog.MAX_CHILDREN) assertEquals(0, rowsCreated) assertTrue(ranges.all { it.browsable && !it.playable }) } @Test fun rangeNavigationIncludesEveryTailEntry() { val albums = (0 until 123).map { BrowserItem("album:$it", "Album $it", BrowserKind.ALBUM) } val tree = tree(albums) val ranges = tree.children(AndroidAutoCatalog.ALBUM) val entries = ranges.flatMap { tree.children(it.id) } assertEquals(albums.map { it.label }, entries.map { it.title }) assertTrue(ranges.size <= AndroidAutoCatalog.MAX_CHILDREN) } @Test fun activeQueueUsesExactOccurrencesAndNeverRebuildsOrRotatesThem() { val occurrences = listOf(PlaybackQueueOccurrence(11, 0, 1, MediaId(7)), PlaybackQueueOccurrence(22, 1, 0, MediaId(7))) val queue = PlaybackQueueSnapshot(99, PlaybackQueueKind.MANUAL, 1, true, occurrences, PlaybackCheckpoint(22, shuffle = true)) val tree = tree(emptyList(), queue = queue) val active = tree.children(AndroidAutoCatalog.QUEUE).first() val entries = tree.children(active.id) assertNotEquals(entries[0].id, entries[1].id) assertEquals(AutoPlayRequest.Occurrence(99, 22, 1), tree.selection(entries[0].id)) assertSame(queue, tree.queue) assertSame(occurrences, tree.queue?.occurrences) } @Test fun playlistSelectionKeepsNormalPlaylistRotation() { val playlist = BrowserItem("playlist:42", "Road", BrowserKind.PLAYLIST, children = listOf(track("first"), track("second"), track("third"))) val tree = tree(emptyList(), playlists = listOf(playlist)) val playlistRoot = tree.children(AndroidAutoCatalog.QUEUE).last() val list = tree.children(playlistRoot.id).single() val second = tree.children(list.id)[2] val request = tree.selection(second.id) as AutoPlayRequest.Context val projection = requireNotNull(contextualPlayProjection(request.container, request.selectedId)) assertTrue(projection.permanentPlaylist) assertEquals(listOf("second", "third", "first"), projection.tracks.map { it.id }) assertEquals(0, projection.selectedQueueIndex) } @Test fun unavailableTrackIsVisibleButCannotBeSelected() { val album = BrowserItem("album:one", "Album", BrowserKind.ALBUM, children = listOf(track("missing").copy(playable = false))) val tree = tree(listOf(album)) val entry = tree.children(tree.children(AndroidAutoCatalog.ALBUM).single().id).last() assertFalse(entry.playable) assertThrows(IllegalArgumentException::class.java) { tree.selection(entry.id) } } @Test fun nativeQueueWindowIsBoundedAndContainsCurrentAtAllSizes() { for (size in listOf(0, 1, 40, 41, 42, 125_000, 125_001, 200_000, 200_001)) { for (current in listOf(0, size / 2, size - 1)) { val window = androidAutoQueueWindow(size, current) assertTrue(window.count() <= 41) if (size > 0) { assertTrue(current.coerceIn(0, size - 1) in window) assertTrue(window.first >= 0 && window.last < size) } else { assertTrue(window.isEmpty()) } } } } }