Skip to content

Native Android client for Spotify, built with Jetpack Compose

Tests: wait for the bridge to listen before posting, and for the element read to be taken in before a late refusal

kota-zo4bf9a3715278
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
index a1cd9024..3d2f0fbf 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
@@ -282,7 +282,7 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
* still while the state is that device's. Judged on the element too, as the active flag is read
* every few seconds and lags a transfer to this phone (whose element then moves).
*/
- private val remoteActive: Boolean get() = lastMedia?.let { it.active == false && it.paused } == true
+ internal val remoteActive: Boolean get() = lastMedia?.let { it.active == false && it.paused } == true
/** Loads the song after the current one in advance (see "player.preloadNext"); on by default. */
@Volatile var preloadNext = true
diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PickDuringAdTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PickDuringAdTest.kt
index 1e19fd5a..e54146f8 100644
--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PickDuringAdTest.kt
+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PickDuringAdTest.kt
@@ -43,6 +43,8 @@ class PickDuringAdTest {
scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
host = FakeHost()
player = SpotifyPlayer(Bridge(host, scope), scope)
+ // A message posted before the bridge listens is lost (a slow CI machine): wait for it.
+ waitFor("bridge listening") { host.messages.subscriptionCount.value > 0 }
}
@AfterEach fun down() {
diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt
index 2ffed7a2..1d5448ab 100644
--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt
+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt
@@ -48,6 +48,8 @@ class PlaybackFollowUpTest {
scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
host = FakeHost()
player = SpotifyPlayer(Bridge(host, scope), scope)
+ // A message posted before the bridge listens is lost (a slow CI machine): wait for it.
+ waitFor("bridge listening") { host.messages.subscriptionCount.value > 0 }
}
@AfterEach fun down() {
@@ -125,8 +127,10 @@ class PlaybackFollowUpTest {
host.post("""{"type":"ready"}""")
host.post(state(track("A")))
player.play("spotify:playlist:C", trackUri = "spotify:track:P")
- // The watcher reads the element within its tick.
- waitFor("element read") { called("media.state") > 0 }
+ // The watcher, idle until a song plays, reads the element at its first tick (up to
+ // WATCH_IDLE_TICK_MS after it started, 4 s): the wait is a bound, not a sleep. The read
+ // must also be taken in (the call alone is not): another device is known to be active.
+ waitFor("another device seen", ms = 10_000) { player.remoteActive }
host.post("""{"type":"refused","status":402,"late":true}""")
delay(300)
assertEquals(1, host.calls.count { it.first == "player.play" }, "no fallback play on another device: ${host.calls.map { it.first }}")