From 847aa8daaecc60539cfec95ec4537602323701f2 Mon Sep 17 00:00:00 2001 From: kalarina Date: Thu, 12 Nov 2026 16:43:34 +0200 Subject: [PATCH] Add unit tests for the reconnect policy, the player state and the repository --- .../fogwave/data/RadioRepositoryTest.kt | 68 ++++++++++ .../fogwave/player/PlaybackPolicyTest.kt | 119 ++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 app/src/test/java/cc/skysparkle/fogwave/data/RadioRepositoryTest.kt create mode 100644 app/src/test/java/cc/skysparkle/fogwave/player/PlaybackPolicyTest.kt diff --git a/app/src/test/java/cc/skysparkle/fogwave/data/RadioRepositoryTest.kt b/app/src/test/java/cc/skysparkle/fogwave/data/RadioRepositoryTest.kt new file mode 100644 index 0000000..ca742a7 --- /dev/null +++ b/app/src/test/java/cc/skysparkle/fogwave/data/RadioRepositoryTest.kt @@ -0,0 +1,68 @@ +package cc.skysparkle.fogwave.data + +import android.content.Context +import androidx.core.content.edit +import androidx.test.core.app.ApplicationProvider +import cc.skysparkle.fogwave.data.repository.RadioRepository +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [36]) +class RadioRepositoryTest { + + private val context: Context = ApplicationProvider.getApplicationContext() + + @Before + fun clearPrefs() { + context.getSharedPreferences("fogwave_prefs", Context.MODE_PRIVATE).edit { clear() } + } + + @Test + fun favoritesToggleAndSurviveARestart() { + val repository = RadioRepository(context) + assertTrue(repository.toggleFavorite("zip")) + assertTrue(repository.toggleFavorite("lrt")) + assertFalse(repository.toggleFavorite("zip")) + + assertEquals(setOf("lrt"), RadioRepository(context).favoritesFlow.value) + } + + @Test + fun unknownStationsAreDroppedFromSavedFavorites() { + context.getSharedPreferences("fogwave_prefs", Context.MODE_PRIVATE) + .edit { putStringSet("favorites", setOf("zip", "removed_station")) } + + assertEquals(setOf("zip"), RadioRepository(context).favoritesFlow.value) + } + + @Test + fun recentsPutTheLatestFirstWithoutDuplicatesAndKeepTen() { + val repository = RadioRepository(context) + val ids = repository.stations.map { it.id } + ids.forEach { repository.pushRecent(it) } + repository.pushRecent(ids[3]) + + val recents = RadioRepository(context).recentsFlow.value + assertEquals(10, recents.size) + assertEquals(ids[3], recents.first()) + assertEquals(recents.size, recents.toSet().size) + } + + @Test + fun mostPlayedStationsComeFirst() { + val repository = RadioRepository(context) + repeat(3) { repository.recordStationPlayed("lr2") } + repository.recordStationPlayed("rc") + + val sorted = RadioRepository(context).stationsSortedByPopularity() + assertEquals(listOf("lr2", "rc"), sorted.take(2).map { it.id }) + assertEquals(repository.stations.size, sorted.size) + } +} diff --git a/app/src/test/java/cc/skysparkle/fogwave/player/PlaybackPolicyTest.kt b/app/src/test/java/cc/skysparkle/fogwave/player/PlaybackPolicyTest.kt new file mode 100644 index 0000000..5166ffa --- /dev/null +++ b/app/src/test/java/cc/skysparkle/fogwave/player/PlaybackPolicyTest.kt @@ -0,0 +1,119 @@ +package cc.skysparkle.fogwave.player + +import android.net.Uri +import android.os.Looper +import androidx.annotation.OptIn +import androidx.media3.common.PlaybackException +import androidx.media3.common.Player +import androidx.media3.common.SimpleBasePlayer +import androidx.media3.common.util.UnstableApi +import androidx.media3.datasource.DataSpec +import androidx.media3.datasource.HttpDataSource +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@OptIn(UnstableApi::class) +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [36]) +class PlaybackPolicyTest { + + private fun httpError(code: Int) = PlaybackException( + "HTTP $code", + HttpDataSource.InvalidResponseCodeException( + code, null, null, emptyMap(), DataSpec(Uri.parse("https://example.com/stream")), ByteArray(0) + ), + PlaybackException.ERROR_CODE_IO_BAD_HTTP_STATUS + ) + + private fun error(code: Int) = PlaybackException("error", null, code) + + @Test + fun clientErrorsGetOnlyAFewAttempts() { + assertEquals(MAX_PERMANENT_ERROR_RETRY_ATTEMPTS, maxRetryAttemptsFor(httpError(404))) + assertEquals(MAX_PERMANENT_ERROR_RETRY_ATTEMPTS, maxRetryAttemptsFor(httpError(403))) + } + + @Test + fun timeoutsRateLimitsAndServerErrorsGetTheFullBackoff() { + assertEquals(MAX_RETRY_ATTEMPTS, maxRetryAttemptsFor(httpError(408))) + assertEquals(MAX_RETRY_ATTEMPTS, maxRetryAttemptsFor(httpError(429))) + assertEquals(MAX_RETRY_ATTEMPTS, maxRetryAttemptsFor(httpError(503))) + } + + @Test + fun unsupportedStreamsArePermanentAndNetworkErrorsAreNot() { + assertEquals( + MAX_PERMANENT_ERROR_RETRY_ATTEMPTS, + maxRetryAttemptsFor(error(PlaybackException.ERROR_CODE_PARSING_CONTAINER_UNSUPPORTED)) + ) + assertEquals( + MAX_PERMANENT_ERROR_RETRY_ATTEMPTS, + maxRetryAttemptsFor(error(PlaybackException.ERROR_CODE_IO_CLEARTEXT_NOT_PERMITTED)) + ) + assertEquals( + MAX_RETRY_ATTEMPTS, + maxRetryAttemptsFor(error(PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED)) + ) + assertEquals( + MAX_RETRY_ATTEMPTS, + maxRetryAttemptsFor(error(PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT)) + ) + } + + /** A player that only reports the given state. */ + private class FakePlayer(private val fixed: State) : SimpleBasePlayer(Looper.getMainLooper()) { + override fun getState(): State = fixed + } + + private fun player( + playbackState: Int, + playWhenReady: Boolean, + hasItem: Boolean = true, + error: PlaybackException? = null + ): Player = FakePlayer( + SimpleBasePlayer.State.Builder() + .setAvailableCommands(Player.Commands.Builder().addAllCommands().build()) + .setPlaylist(if (hasItem) listOf(SimpleBasePlayer.MediaItemData.Builder("station").build()) else emptyList()) + .setPlaybackState(playbackState) + .setPlayWhenReady(playWhenReady, Player.PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST) + .setPlayerError(error) + .build() + ) + + private fun derive(p: Player) = deriveState(p) { "failed" } + + @Test + fun readyMapsToPlayingOrPaused() { + assertEquals(PlayerState.Playing, derive(player(Player.STATE_READY, playWhenReady = true))) + assertEquals(PlayerState.Paused, derive(player(Player.STATE_READY, playWhenReady = false))) + } + + @Test + fun bufferingAndADroppedStreamShowConnectingWhilePlayRequested() { + assertEquals(PlayerState.Connecting, derive(player(Player.STATE_BUFFERING, playWhenReady = true))) + assertEquals(PlayerState.Connecting, derive(player(Player.STATE_ENDED, playWhenReady = true))) + assertEquals(PlayerState.Paused, derive(player(Player.STATE_BUFFERING, playWhenReady = false))) + } + + @Test + fun idleDependsOnWhetherAStationIsLoaded() { + assertEquals(PlayerState.Idle, derive(player(Player.STATE_IDLE, playWhenReady = false, hasItem = false))) + assertEquals(PlayerState.Connecting, derive(player(Player.STATE_IDLE, playWhenReady = true))) + assertEquals(PlayerState.Paused, derive(player(Player.STATE_IDLE, playWhenReady = false))) + } + + @Test + fun anErrorWinsOverThePlaybackState() { + val state = derive( + player( + Player.STATE_IDLE, + playWhenReady = true, + error = error(PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED) + ) + ) + assertEquals(PlayerState.Error("failed"), state) + } +}