playback: first pass at the deck, a play-after region of the queue

Twelve TDD cycles plus menu wiring: base case, pointer advancement, resets on
IndexMoved/NewPlayback/QueueReordered, drag and remove boundary adjustments,
play-next interplay, the :app unit-test harness, and PlaybackViewModel routing
add-to-queue to playAfter. Region-of-heap model with a deckEnd pointer;
superseded by the ephemeral-queue model (separate deck list) in later work.
This commit is contained in:
jay
2026-11-11 21:31:06 -05:00
parent c8d3ba55fc
commit f3003f3a8a
7 changed files with 625 additions and 18 deletions
+8
View File
@@ -75,6 +75,12 @@ android {
viewBinding true viewBinding true
buildConfig true buildConfig true
} }
// Return default values from android.jar stubs in unit tests instead of throwing.
// PlaybackStateManagerImpl construction calls Progression.nil() -> SystemClock.elapsedRealtime().
testOptions {
unitTests.returnDefaultValues = true
}
} }
kotlin { kotlin {
@@ -86,6 +92,8 @@ kotlin {
} }
dependencies { dependencies {
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test:runner:1.7.0" androidTestImplementation "androidx.test:runner:1.7.0"
androidTestImplementation "androidx.test.ext:junit:1.3.0" androidTestImplementation "androidx.test.ext:junit:1.3.0"
@@ -544,63 +544,63 @@ constructor(
} }
/** /**
* Add a [Song] to the end of the queue. * Add a [Song] to the deck.
* *
* @param song The [Song] to add. * @param song The [Song] to add.
*/ */
fun addToQueue(song: Song) { fun addToQueue(song: Song) {
L.d("Adding $song to queue") L.d("Adding $song to deck")
playbackManager.addToQueue(song) playbackManager.playAfter(song)
} }
/** /**
* Add a [Album] to the end of the queue. * Add a [Album] to the deck.
* *
* @param album The [Album] to add. * @param album The [Album] to add.
*/ */
fun addToQueue(album: Album) { fun addToQueue(album: Album) {
L.d("Adding $album to queue") L.d("Adding $album to deck")
playbackManager.addToQueue(listSettings.albumSongSort.songs(album.songs)) playbackManager.playAfter(listSettings.albumSongSort.songs(album.songs))
} }
/** /**
* Add a [Artist] to the end of the queue. * Add a [Artist] to the deck.
* *
* @param artist The [Artist] to add. * @param artist The [Artist] to add.
*/ */
fun addToQueue(artist: Artist) { fun addToQueue(artist: Artist) {
L.d("Adding $artist to queue") L.d("Adding $artist to deck")
playbackManager.addToQueue(listSettings.artistSongSort.songs(artist.songs)) playbackManager.playAfter(listSettings.artistSongSort.songs(artist.songs))
} }
/** /**
* Add a [Genre] to the end of the queue. * Add a [Genre] to the deck.
* *
* @param genre The [Genre] to add. * @param genre The [Genre] to add.
*/ */
fun addToQueue(genre: Genre) { fun addToQueue(genre: Genre) {
L.d("Adding $genre to queue") L.d("Adding $genre to deck")
playbackManager.addToQueue(listSettings.genreSongSort.songs(genre.songs)) playbackManager.playAfter(listSettings.genreSongSort.songs(genre.songs))
} }
/** /**
* Add a [Playlist] to the end of the queue. * Add a [Playlist] to the deck.
* *
* @param playlist The [Playlist] to add. * @param playlist The [Playlist] to add.
*/ */
fun addToQueue(playlist: Playlist) { fun addToQueue(playlist: Playlist) {
L.d("Adding $playlist to queue") L.d("Adding $playlist to deck")
playbackManager.addToQueue(playlist.songs) playbackManager.playAfter(playlist.songs)
} }
/** /**
* Add [Song]s to the end of the queue. * Add [Song]s to the deck.
* *
* @param songs The [Song]s to add. * @param songs The [Song]s to add.
*/ */
fun addToQueue(songs: List<Song>) { fun addToQueue(songs: List<Song>) {
L.d("Adding ${songs.size} songs to queue") L.d("Adding ${songs.size} songs to deck")
playbackManager.addToQueue(songs) playbackManager.playAfter(songs)
} }
// --- STATUS FUNCTIONS --- // --- STATUS FUNCTIONS ---
@@ -353,6 +353,19 @@ class ExoPlaybackStateHolder(
deferSave() deferSave()
} }
override fun playAfter(songs: List<Song>, ack: StateAck.PlayNext) {
val indices = player.unscrambleQueueIndices()
val items = songs.map { it.buildMediaItem() }
// ack.at is a resolved-queue position; translate it to a timeline position.
if (indices.isEmpty() || ack.at >= indices.size) {
player.addMediaItems(items)
} else {
player.addMediaItems(indices[ack.at], items)
}
playbackManager.ack(this, ack)
deferSave()
}
override fun addToQueue(songs: List<Song>, ack: StateAck.AddToQueue) { override fun addToQueue(songs: List<Song>, ack: StateAck.AddToQueue) {
player.addMediaItems(songs.map { it.buildMediaItem() }) player.addMediaItems(songs.map { it.buildMediaItem() })
playbackManager.ack(this, ack) playbackManager.ack(this, ack)
@@ -96,6 +96,15 @@ interface PlaybackStateHolder {
*/ */
fun playNext(songs: List<Song>, ack: StateAck.PlayNext) fun playNext(songs: List<Song>, ack: StateAck.PlayNext)
/**
* Add songs after the last deck item in the queue.
*
* @param songs The songs to add.
* @param ack The [StateAck.PlayNext] to return to [PlaybackStateManager]. [StateAck.PlayNext.at]
* is the resolved-queue position to insert at.
*/
fun playAfter(songs: List<Song>, ack: StateAck.PlayNext)
/** /**
* Add songs to the end of the queue. * Add songs to the end of the queue.
* *
@@ -162,6 +162,21 @@ interface PlaybackStateManager {
*/ */
fun addToQueue(song: Song) = addToQueue(listOf(song)) fun addToQueue(song: Song) = addToQueue(listOf(song))
/**
* Add [Song]s to the end of the deck: directly after the current [Song] if the deck is
* empty, otherwise after the last deck [Song].
*
* @param songs The [Song]s to add.
*/
fun playAfter(songs: List<Song>)
/**
* Add a [Song] to the end of the deck.
*
* @param song The [Song] to add.
*/
fun playAfter(song: Song) = playAfter(listOf(song))
/** /**
* Move a [Song] in the queue. * Move a [Song] in the queue.
* *
@@ -362,6 +377,9 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
@Volatile private var pendingDeferredPlayback: DeferredPlayback? = null @Volatile private var pendingDeferredPlayback: DeferredPlayback? = null
@Volatile private var isInitialized = false @Volatile private var isInitialized = false
/** Resolved-queue position just past the last deck song. Null when the deck is empty. */
private var deckEnd: Int? = null
override val progression override val progression
get() = stateMirror.progression get() = stateMirror.progression
@@ -487,6 +505,9 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
val stateHolder = stateHolder ?: return val stateHolder = stateHolder ?: return
L.d("Adding ${songs.size} songs to start of queue") L.d("Adding ${songs.size} songs to start of queue")
stateHolder.playNext(songs, StateAck.PlayNext(stateMirror.index + 1, songs.size)) stateHolder.playNext(songs, StateAck.PlayNext(stateMirror.index + 1, songs.size))
// Inserting at the deck's top pushes the boundary down with the deck, starting
// one right after the current song when the deck is empty.
deckEnd = (deckEnd ?: (stateMirror.index + 1)) + songs.size
} }
} }
@@ -502,6 +523,20 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
} }
} }
@Synchronized
override fun playAfter(songs: List<Song>) {
if (currentSong == null) {
L.d("Nothing playing, short-circuiting to new playback")
play(QueueCommand(songs))
} else {
val stateHolder = stateHolder ?: return
val at = deckEnd ?: (stateMirror.index + 1)
L.d("Adding ${songs.size} songs at deck end $at")
stateHolder.playAfter(songs, StateAck.PlayNext(at, songs.size))
deckEnd = at + songs.size
}
}
private class QueueCommand(override val queue: List<Song>) : PlaybackCommand { private class QueueCommand(override val queue: List<Song>) : PlaybackCommand {
override val song: Song? = null override val song: Song? = null
override val parent: MusicParent? = null override val parent: MusicParent? = null
@@ -592,6 +627,7 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
is StateAck.IndexMoved -> { is StateAck.IndexMoved -> {
val rawQueue = stateHolder.resolveQueue() val rawQueue = stateHolder.resolveQueue()
stateMirror = stateMirror.copy(index = rawQueue.resolveIndex(), rawQueue = rawQueue) stateMirror = stateMirror.copy(index = rawQueue.resolveIndex(), rawQueue = rawQueue)
deckEnd = null
listeners.forEach { it.onIndexMoved(stateMirror.index) } listeners.forEach { it.onIndexMoved(stateMirror.index) }
} }
is StateAck.PlayNext -> { is StateAck.PlayNext -> {
@@ -629,6 +665,17 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
rawQueue = rawQueue, rawQueue = rawQueue,
) )
// The boundary compares both endpoints in pre-move coordinates: leaving the
// deck steps it down, entering the deck steps it up.
deckEnd =
deckEnd?.let { end ->
when {
ack.from < end && ack.to >= end -> end - 1
ack.from >= end && ack.to < end -> end + 1
else -> end
}
}
listeners.forEach { listeners.forEach {
it.onQueueChanged(stateMirror.queue, stateMirror.index, change) it.onQueueChanged(stateMirror.queue, stateMirror.index, change)
} }
@@ -653,6 +700,9 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
rawQueue = rawQueue, rawQueue = rawQueue,
) )
// A removal ahead of the boundary steps the boundary down with it.
deckEnd = deckEnd?.let { end -> if (ack.index < end) end - 1 else end }
listeners.forEach { listeners.forEach {
it.onQueueChanged(stateMirror.queue, stateMirror.index, change) it.onQueueChanged(stateMirror.queue, stateMirror.index, change)
} }
@@ -666,6 +716,7 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
isShuffled = rawQueue.isShuffled, isShuffled = rawQueue.isShuffled,
rawQueue = rawQueue, rawQueue = rawQueue,
) )
deckEnd = null
listeners.forEach { listeners.forEach {
it.onQueueReordered( it.onQueueReordered(
stateMirror.queue, stateMirror.queue,
@@ -684,6 +735,7 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
isShuffled = rawQueue.isShuffled, isShuffled = rawQueue.isShuffled,
rawQueue = rawQueue, rawQueue = rawQueue,
) )
deckEnd = null
listeners.forEach { listeners.forEach {
it.onNewPlayback( it.onNewPlayback(
stateMirror.parent, stateMirror.parent,
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2026 Auxio Project
* Fakes.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.playback.state
import android.net.Uri
import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Genre
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.MusicParent
import org.oxycblt.musikr.Song
import org.oxycblt.musikr.covers.Cover
import org.oxycblt.musikr.fs.Format
import org.oxycblt.musikr.fs.Path
import org.oxycblt.musikr.tag.Date
import org.oxycblt.musikr.tag.Disc
import org.oxycblt.musikr.tag.Name
import org.oxycblt.musikr.tag.ReplayGainAdjustment
/**
* A [PlaybackStateHolder] test double.
*
* Keeps a plain [RawQueue], records insertion receipts in [insertions], and returns
* [StateAck.NewPlayback] immediately from [newPlayback].
*/
class FakePlaybackStateHolder : PlaybackStateHolder {
/** The manager to return receipts to. Set before [newPlayback] is called. */
var manager: PlaybackStateManager? = null
/** Every [StateAck.PlayNext] receipt handed to this holder, in order. */
val insertions = mutableListOf<StateAck.PlayNext>()
var rawQueue = RawQueue.nil()
private set
override val progression = Progression.nil()
override val repeatMode = RepeatMode.NONE
override val parent: MusicParent? = null
override val audioSessionId = 0
override fun resolveQueue() = rawQueue
/** Advance to [index] and return the [StateAck.IndexMoved] receipt, as a track change would. */
fun moveTo(index: Int) {
rawQueue = rawQueue.copy(heapIndex = index)
manager?.ack(this, StateAck.IndexMoved)
}
override fun newPlayback(command: PlaybackCommand) {
rawQueue =
RawQueue(
heap = command.queue,
shuffledMapping = emptyList(),
heapIndex = command.song?.let(command.queue::indexOf) ?: 0,
)
manager?.ack(this, StateAck.NewPlayback)
}
override fun playNext(songs: List<Song>, ack: StateAck.PlayNext) {
insertions += ack
}
override fun playAfter(songs: List<Song>, ack: StateAck.PlayNext) {
insertions += ack
}
override fun playing(playing: Boolean) {}
override fun seekTo(positionMs: Long) {}
override fun repeatMode(repeatMode: RepeatMode) {}
override fun next() {}
override fun prev() {}
override fun goto(index: Int) {}
override fun addToQueue(songs: List<Song>, ack: StateAck.AddToQueue) {}
override fun move(from: Int, to: Int, ack: StateAck.Move) {
// Positions are resolved-queue positions; the fake is unshuffled, so they are heap
// positions.
val heap = rawQueue.heap.toMutableList()
heap.add(to, heap.removeAt(from))
rawQueue = rawQueue.copy(heap = heap)
manager?.ack(this, StateAck.Move(from, to))
}
override fun remove(at: Int, ack: StateAck.Remove) {
// Positions are resolved-queue positions; the fake is unshuffled, so they are heap
// positions.
val heap = rawQueue.heap.toMutableList()
heap.removeAt(at)
rawQueue = rawQueue.copy(heap = heap)
manager?.ack(this, StateAck.Remove(at))
}
override fun shuffled(shuffled: Boolean) {
// Apply a reversed mapping as a stand-in shuffle.
rawQueue =
rawQueue.copy(
shuffledMapping = if (shuffled) rawQueue.heap.indices.reversed().toList() else emptyList()
)
manager?.ack(this, StateAck.QueueReordered)
}
override fun handleDeferred(action: DeferredPlayback) = false
override fun applySavedState(
parent: MusicParent?,
rawQueue: RawQueue,
positionMs: Long,
repeatMode: RepeatMode,
ack: StateAck.NewPlayback?,
) {}
override fun endSession() {}
override fun reset(ack: StateAck.NewPlayback) {}
}
/**
* A [Song] whose every property throws.
*
* The manager moves songs in lists without reading them. Grow a member when a test needs it.
*/
fun fakeSong(): Song =
object : Song {
override val uid: Music.UID get() = TODO()
override val name: Name.Known get() = TODO()
override val track: Int? get() = TODO()
override val disc: Disc? get() = TODO()
override val date: Date? get() = TODO()
override val uri: Uri get() = TODO()
override val path: Path get() = TODO()
override val format: Format get() = TODO()
override val size: Long get() = TODO()
override val durationMs: Long get() = TODO()
override val bitrateKbps: Int get() = TODO()
override val sampleRateHz: Int get() = TODO()
override val replayGainAdjustment: ReplayGainAdjustment get() = TODO()
override val modifiedMs: Long get() = TODO()
override val addedMs: Long get() = TODO()
override val cover: Cover? get() = TODO()
override val album: Album get() = TODO()
override val artists: List<Artist> get() = TODO()
override val genres: List<Genre> get() = TODO()
}
@@ -0,0 +1,360 @@
/*
* Copyright (c) 2026 Auxio Project
* PlaybackStateManagerImplTest.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.playback.state
import org.junit.Assert.assertEquals
import org.junit.Test
class PlaybackStateManagerImplTest {
/**
* Interaction: playNext on an empty deck starts one — the deck is the songs the user
* manually lined up, and play-next is such an act.
*/
@Test
fun `playNext on an empty deck starts one`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue; no deck.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Play next one song: inserts at position 3 and starts the deck.
manager.playNext(listOf(fakeSong()))
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Receipts: 1 at 3 (play next), then 1 at 4 (behind the play-nexted song).
assertEquals(
listOf(StateAck.PlayNext(at = 3, size = 1), StateAck.PlayNext(at = 4, size = 1)),
holder.insertions,
)
}
/**
* Interaction: playNext inserts at the deck's top, so on a nonempty deck the boundary
* advances by the number of songs play-nexted.
*/
@Test
fun `playNext on a nonempty deck advances the boundary`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 songs occupies positions 3 and 4; boundary at 5.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// Play next one song: inserts at the deck's top, pushing the deck down one.
manager.playNext(listOf(fakeSong()))
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Receipts: 2 at 3 (deck), 1 at 3 (play next), then 1 at 6 (past the pushed deck).
assertEquals(
listOf(
StateAck.PlayNext(at = 3, size = 2),
StateAck.PlayNext(at = 3, size = 1),
StateAck.PlayNext(at = 6, size = 1),
),
holder.insertions,
)
}
/**
* Adjust: removing a song inside the deck shrinks the deck, and the boundary steps down.
*/
@Test
fun `removing a deck song shrinks the deck`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 songs occupies positions 3 and 4; boundary at 5.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// Swipe the deck's last song out of the queue.
manager.removeQueueItem(4)
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Last receipt: 1 song at position 4, past the shrunken deck.
assertEquals(StateAck.PlayNext(at = 4, size = 1), holder.insertions.last())
}
/**
* Adjust: dragging a deck song upward within the deck leaves the boundary unchanged.
* Brackets the within-deck case with the downward drag.
*/
@Test
fun `dragging a deck song to the front of the deck keeps the boundary`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 songs occupies positions 3 and 4; boundary at 5.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// Drag the deck's second song to the deck's front.
manager.moveQueueItem(4, 3)
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Last receipt: 1 song at position 5, past the unchanged deck.
assertEquals(StateAck.PlayNext(at = 5, size = 1), holder.insertions.last())
}
/**
* Adjust: dragging a deck song out past the boundary into the tail shrinks the deck, and
* the boundary steps down.
*/
@Test
fun `dragging a deck song out to the tail shrinks the deck`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 songs occupies positions 3 and 4; boundary at 5.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// Drag the deck's front song out to the tail.
manager.moveQueueItem(3, 7)
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Last receipt: 1 song at position 4, past the shrunken deck.
assertEquals(StateAck.PlayNext(at = 4, size = 1), holder.insertions.last())
}
/**
* Adjust: dragging a deck song within the deck — including to its last position — leaves
* the boundary unchanged. The deck was rearranged, not shrunk.
*/
@Test
fun `dragging a deck song to the end of the deck keeps the boundary`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 songs occupies positions 3 and 4; boundary at 5.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// Drag the deck's front song to the deck's last position.
manager.moveQueueItem(3, 4)
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Last receipt: 1 song at position 5, past the unchanged deck.
assertEquals(StateAck.PlayNext(at = 5, size = 1), holder.insertions.last())
}
/**
* Adjust: dragging a tail song into the deck grows the deck, and the boundary rides along
* with the move.
*/
@Test
fun `dragging a tail song into the deck grows it`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 songs occupies positions 3 and 4; boundary at 5.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// Drag the tail song at position 7 to the front of the deck.
manager.moveQueueItem(7, 3)
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Last receipt: 1 song at position 6, past the grown deck.
assertEquals(StateAck.PlayNext(at = 6, size = 1), holder.insertions.last())
}
/**
* Reset: when the queue is reshuffled ([StateAck.QueueReordered]), the deck is forgotten and
* the next playAfter starts a fresh deck behind the current song's new position.
*/
@Test
fun `playAfter after a reshuffle starts a fresh deck`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 songs occupies positions 3 and 4.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// The user reshuffles; the current song lands at position 7.
manager.shuffled(true)
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Last receipt: 1 song at position 8, behind the current song's new position.
assertEquals(StateAck.PlayNext(at = 8, size = 1), holder.insertions.last())
}
/**
* Reset: when new playback starts ([StateAck.NewPlayback]), the deck is forgotten and the
* next playAfter starts a fresh deck behind the new queue's current song.
*/
@Test
fun `playAfter after new playback starts a fresh deck`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 songs occupies positions 3 and 4.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// The user starts a new 5-song album from its first song.
val album = List(5) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(album[0], null, album, false))
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Last receipt: 1 song at position 1, behind the album's first song.
assertEquals(StateAck.PlayNext(at = 1, size = 1), holder.insertions.last())
}
/**
* Reset: when the current song changes ([StateAck.IndexMoved]), the deck is forgotten and
* the next playAfter starts a fresh deck behind the new current song.
*/
@Test
fun `playAfter after the current song changes starts a fresh deck`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Deck of 2 then 3 songs occupies positions 3 through 7.
manager.playAfter(listOf(fakeSong(), fakeSong()))
manager.playAfter(listOf(fakeSong(), fakeSong(), fakeSong()))
// The current song ends; index 3 begins.
holder.moveTo(3)
// Play after one song.
manager.playAfter(listOf(fakeSong()))
// Last receipt: 1 song at position 4, behind the new current song.
assertEquals(StateAck.PlayNext(at = 4, size = 1), holder.insertions.last())
}
/**
* Advancement: with a nonempty deck, playAfter inserts after the last deck song, and the
* deck end advances by the number of songs added.
*/
@Test
fun `playAfter with a nonempty deck inserts after the last deck song`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Play after two songs, then three more.
manager.playAfter(listOf(fakeSong(), fakeSong()))
manager.playAfter(listOf(fakeSong(), fakeSong(), fakeSong()))
// Two receipts: 2 songs at position 3, then 3 songs at position 5.
assertEquals(
listOf(StateAck.PlayNext(at = 3, size = 2), StateAck.PlayNext(at = 5, size = 3)),
holder.insertions,
)
}
/**
* Base case: with an empty deck, playAfter inserts immediately after the current song
* (index + 1), reported to the holder as [StateAck.PlayNext].
*/
@Test
fun `playAfter with an empty deck inserts after the current song`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue.
val queue = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(queue[2], null, queue, false))
// Play-after two songs.
manager.playAfter(listOf(fakeSong(), fakeSong()))
// One insertion receipt: 2 songs at position 3, directly after the current song.
assertEquals(listOf(StateAck.PlayNext(at = 3, size = 2)), holder.insertions)
}
}