Library screen: an "All music" card with Shuffle all, which builds a local queue from every cached song so playback does not need a mood session. Player bar: a sleep timer with 15/30/45/60/90 minute presets. The shared store shows the countdown and pauses playback; on Android the pause is also scheduled inside the media service through a custom session command, so it still fires when the app is backgrounded or the task is swiped away. Playback errors from the platform player now surface in the UI instead of failing silently (for example when no audio device is available). Tests cover shuffle-all, the empty library, timer expiry and cancelling it.
This commit is contained in:
1 parent
f8953fb200
commit
c7c1d59b70
10 files changed
+478
-42
No files matched your search
@@ -93,22 +93,4 @@ class ApiClientTest {
|
||||
assertEquals("2026-11-14T00:00:00Z", loaded.revision)
|
||||
assertEquals("alice", loaded.userName)
|
||||
}
|
||||
|
||||
private class MemoryStorage : AppStorage {
|
||||
private val files = mutableMapOf<String, String>()
|
||||
override fun read(name: String): String? = files[name]
|
||||
override fun write(name: String, text: String) {
|
||||
files[name] = text
|
||||
}
|
||||
|
||||
override fun delete(name: String) {
|
||||
files.remove(name)
|
||||
}
|
||||
|
||||
override fun saveDownload(songId: String, bytes: ByteArray): String? = null
|
||||
override fun downloadPath(songId: String): String? = null
|
||||
override fun listDownloads(): Map<String, Long> = emptyMap()
|
||||
override fun deleteDownload(songId: String) = Unit
|
||||
override fun storageUsedBytes(): Long = 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import io.ktor.client.engine.mock.MockEngine
|
||||
import io.ktor.client.engine.mock.respondError
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.advanceTimeBy
|
||||
import kotlinx.coroutines.test.runCurrent
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
import win.taptap.musicmanager.client.api.ApiClient
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.api.configuredHttpClient
|
||||
import win.taptap.musicmanager.client.data.CachedState
|
||||
import win.taptap.musicmanager.client.data.LibraryCache
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
import win.taptap.musicmanager.client.store.AppStore
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class AppStoreTest {
|
||||
|
||||
@Test
|
||||
fun `play all queues the whole library and starts playback`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = LibraryCache(storage)
|
||||
val songs = listOf(song("a"), song("b"), song("c"))
|
||||
cache.save(CachedState(serverUrl = "http://music", songs = songs))
|
||||
val player = FakePlayer()
|
||||
val store = store(storage, cache, player)
|
||||
runCurrent()
|
||||
|
||||
store.playAll()
|
||||
runCurrent()
|
||||
|
||||
val state = store.state.value
|
||||
val current = assertNotNull(state.current)
|
||||
assertTrue(state.playing)
|
||||
assertEquals(listOf(current.id), player.played)
|
||||
assertEquals(2, state.queue.size)
|
||||
assertEquals(songs.map { it.id }.toSet(), (state.queue + current).map { it.id }.toSet())
|
||||
store.release()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `play all reports an empty library instead of starting playback`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = LibraryCache(storage)
|
||||
val player = FakePlayer()
|
||||
val store = store(storage, cache, player)
|
||||
runCurrent()
|
||||
|
||||
store.playAll()
|
||||
runCurrent()
|
||||
|
||||
assertNull(store.state.value.current)
|
||||
assertTrue(player.played.isEmpty())
|
||||
assertTrue(store.state.value.messageIsError)
|
||||
store.release()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sleep timer pauses playback when it expires`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = LibraryCache(storage)
|
||||
val player = FakePlayer()
|
||||
val store = store(storage, cache, player)
|
||||
runCurrent()
|
||||
|
||||
store.playSong(song("a"))
|
||||
runCurrent()
|
||||
assertTrue(store.state.value.playing)
|
||||
|
||||
store.setSleepTimer(1)
|
||||
assertEquals(60L, store.state.value.sleepTimerRemainingSeconds)
|
||||
|
||||
advanceTimeBy(60_000)
|
||||
runCurrent()
|
||||
|
||||
val state = store.state.value
|
||||
assertFalse(state.playing)
|
||||
assertNull(state.sleepTimerRemainingSeconds)
|
||||
assertTrue(player.paused)
|
||||
store.release()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `turning the sleep timer off keeps playback running`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = LibraryCache(storage)
|
||||
val player = FakePlayer()
|
||||
val store = store(storage, cache, player)
|
||||
runCurrent()
|
||||
|
||||
store.playSong(song("a"))
|
||||
runCurrent()
|
||||
|
||||
store.setSleepTimer(30)
|
||||
assertEquals(1_800L, store.state.value.sleepTimerRemainingSeconds)
|
||||
store.setSleepTimer(null)
|
||||
assertNull(store.state.value.sleepTimerRemainingSeconds)
|
||||
|
||||
advanceTimeBy(120_000)
|
||||
runCurrent()
|
||||
assertTrue(store.state.value.playing)
|
||||
store.release()
|
||||
}
|
||||
|
||||
private fun kotlinx.coroutines.test.TestScope.store(
|
||||
storage: MemoryStorage,
|
||||
cache: LibraryCache,
|
||||
player: AudioPlayer,
|
||||
): AppStore = AppStore(
|
||||
scope = this,
|
||||
api = ApiClient(configuredHttpClient(MockEngine { respondError(HttpStatusCode.NotFound) })),
|
||||
storage = storage,
|
||||
player = player,
|
||||
cache = cache,
|
||||
)
|
||||
|
||||
private fun song(id: String) = Song(
|
||||
id = id,
|
||||
title = "Song $id",
|
||||
artist = "Artist",
|
||||
album = "Album",
|
||||
albumId = "al-1",
|
||||
duration = 180,
|
||||
bitRate = 320,
|
||||
size = 1_000,
|
||||
suffix = "mp3",
|
||||
folderId = 1,
|
||||
relativePath = "$id.mp3",
|
||||
updatedAt = "2026-01-01T00:00:00Z",
|
||||
)
|
||||
|
||||
private class FakePlayer : AudioPlayer {
|
||||
val played = mutableListOf<String>()
|
||||
var paused = false
|
||||
private set
|
||||
private var playing = false
|
||||
|
||||
override fun play(source: String, song: Song, local: Boolean, artworkUri: String?) {
|
||||
played += song.id
|
||||
playing = true
|
||||
paused = false
|
||||
}
|
||||
|
||||
override fun pause() {
|
||||
playing = false
|
||||
paused = true
|
||||
}
|
||||
|
||||
override fun resume() {
|
||||
playing = true
|
||||
paused = false
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
playing = false
|
||||
}
|
||||
|
||||
override fun isPlaying(): Boolean = playing
|
||||
override fun isFinished(): Boolean = false
|
||||
override fun positionSeconds(): Double = 0.0
|
||||
override fun durationSeconds(): Double = 0.0
|
||||
override val canSeek: Boolean = false
|
||||
override fun seekTo(seconds: Double) = Unit
|
||||
override fun release() {
|
||||
playing = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import win.taptap.musicmanager.client.data.AppStorage
|
||||
|
||||
/** In-memory [AppStorage] for store and cache tests. */
|
||||
internal class MemoryStorage : AppStorage {
|
||||
private val files = mutableMapOf<String, String>()
|
||||
|
||||
override fun read(name: String): String? = files[name]
|
||||
|
||||
override fun write(name: String, text: String) {
|
||||
files[name] = text
|
||||
}
|
||||
|
||||
override fun delete(name: String) {
|
||||
files.remove(name)
|
||||
}
|
||||
|
||||
override fun saveDownload(songId: String, bytes: ByteArray): String? = null
|
||||
override fun downloadPath(songId: String): String? = null
|
||||
override fun listDownloads(): Map<String, Long> = emptyMap()
|
||||
override fun deleteDownload(songId: String) = Unit
|
||||
override fun storageUsedBytes(): Long = 0
|
||||
}
|
||||
Reference in new issue
Block a user