Modern Apps git

← Modern-Apps.git

music: navigation host tests over a populated library

Commit
120c5680f1f653622109cb595f350060766ac67c
Author
Vayun Mathur <[email protected]>
When
2026-11-17 02:13 UTC (19 days ago)
Parents
745ccd9
Coverage top-up for :music (CI 428a82e90: 75.5% INSTRUCTION).
Local merged report before: 75.5% (22732/30111); after: 77.4%
(23319/30111).

NavigationHostTest renders the real Navigation host - whose
entry wiring, tab resets and refresh kickoff had never run -
over the populated fake library (TestMusicRepository +
FakeMediaProvider, the populated-suites setup): the songs
entry lands on the refreshed library, every tab label switches
to its own entry in the same host, and returning home shows
the library again.

Documented ceiling (why not 80%): the remaining missed code
sits behind four host/device boundaries, each already noted in
the module's existing suites: CastPlayback's play/artwork/
resource-serving bodies (~1100 instructions) only run inside a
CastClient content session with a television answering;
the Android Auto car screens and MusicCarSession need a car
host; PlaybackService's MediaSession callbacks and
PlaybackManager's controller loop need the media session
service bound; and tag/artwork resolution
(MusicRepository backfill, Util boundedArtwork,
AudioPreviewActivity titles) needs a real
MediaMetadataRetriever/MediaStore, which Robolectric shadows
cannot serve. The library, queue, playlist and playback-state
logic around those boundaries is covered.

Verified: ./gradlew :music:testDevUnitTest
:music:createMergedCoverageReport green, 77.4%.

Diff

music/src/test/java/com/vayunmathur/music/ui/NavigationHostTest.kt
diff --git a/music/src/test/java/com/vayunmathur/music/ui/NavigationHostTest.kt b/music/src/test/java/com/vayunmathur/music/ui/NavigationHostTest.ktnew file mode 100644index 0000000000..9008760425--- /dev/null+++ b/music/src/test/java/com/vayunmathur/music/ui/NavigationHostTest.kt@@ -0,0 +1,105 @@+package com.vayunmathur.music.ui++import android.app.Application+import android.content.ComponentName+import android.os.Binder+import android.os.Looper+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onAllNodesWithText+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.music.FakeAndroidKeyStore+import com.vayunmathur.music.FakeMediaProvider+import com.vayunmathur.music.Navigation+import com.vayunmathur.music.R+import com.vayunmathur.music.data.MusicRepository+import com.vayunmathur.music.data.TestMusicRepository+import com.vayunmathur.music.platform.MusicViewModel+import com.vayunmathur.music.platform.PlaybackManager+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.runBlocking+import kotlinx.coroutines.test.resetMain+import kotlinx.coroutines.test.setMain+import org.junit.After+import org.junit.Before+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows+import org.robolectric.annotation.Config+import org.robolectric.shadows.ShadowContentResolver+import kotlin.test.Test+import kotlin.test.assertTrue++/**+ * The real [Navigation] host over a populated library. Every tab content+ * and detail screen already had direct render tests, but the host itself+ * - its entry wiring, tab resets and the refresh kickoff - never ran.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class NavigationHostTest {++    @get:Rule val composeRule = createComposeRule()++    private val app: Application get() = ApplicationProvider.getApplicationContext()+    private lateinit var repo: MusicRepository++    @OptIn(ExperimentalCoroutinesApi::class)+    @Before fun setUp() {+        Dispatchers.setMain(Dispatchers.Unconfined)+        FakeMediaProvider.enabled = true+        FakeAndroidKeyStore.install()+        ShadowContentResolver.registerProviderInternal("media", FakeMediaProvider())+        Shadows.shadowOf(app).setComponentNameAndServiceForBindService(+            ComponentName(app, com.vayunmathur.music.service.PlaybackService::class.java), Binder(),+        )+        runCatching {+            androidx.work.WorkManager.initialize(app, androidx.work.Configuration.Builder().build())+        }+        val (r, _) = TestMusicRepository.install(app)+        repo = r+        runBlocking { repo.refresh() }+    }++    @OptIn(ExperimentalCoroutinesApi::class)+    @After fun tearDown() {+        Dispatchers.resetMain()+        FakeMediaProvider.enabled = false+    }++    private fun vm() = MusicViewModel(app, repo, PlaybackManager.getInstance(app))++    private fun awaitText(text: String) {+        val deadline = System.currentTimeMillis() + 15_000+        while (System.currentTimeMillis() < deadline) {+            Shadows.shadowOf(Looper.getMainLooper()).idle()+            composeRule.waitForIdle()+            if (composeRule.onAllNodesWithText(text, substring = true).fetchSemanticsNodes().isNotEmpty()) return+            Thread.sleep(50)+        }+        composeRule.onNodeWithText(text, substring = true).assertExists()+    }++    @Test fun navigationHostRendersTabsAndSwitchesEntries() {+        composeRule.setContent { Navigation(vm()) }+        // Home (songs) entry, refreshed by the host's own kickoff.+        awaitText("Alpha Song")+        // Each tab label switches to that tab's entry in the same host.+        composeRule.onNodeWithText(app.getString(R.string.label_albums)).performClick()+        composeRule.waitForIdle()+        composeRule.onNodeWithText(app.getString(R.string.nav_artists)).performClick()+        composeRule.waitForIdle()+        composeRule.onNodeWithText(app.getString(R.string.nav_playlists)).performClick()+        composeRule.waitForIdle()+        composeRule.onNodeWithText(app.getString(com.vayunmathur.library.ui.R.string.home)).performClick()+        awaitText("Alpha Song")+        assertTrue(+            composeRule.onAllNodesWithText("Alpha Song").fetchSemanticsNodes().isNotEmpty(),+            "returning home must show the library again",+        )+    }++}