library/tv: add tests for all TV components
- Commit
80eacb2bbbce6a5ffeaacc3316ca32fa8f894c04- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 06:22 UTC (29 days ago)
- Parents
3851980
First tests for the module: Robolectric Compose coverage of buttons, icon buttons, chips, list/switch rows, media and info cards, badges and progress strips, empty/loading/dialog states, the D-pad text field, grid with full-width and header items, shelves and screen headers, the seek bar/row, the app frame rail, initial focus, themes and accent colours, exercised through their click, long-click and toggle actions. Coverage (merged, instructions): 0% -> 89.0% (0 -> 6229/6996; the merged report was previously skipped with no tests), above the 85% target. Full suite passes (16 tests) via gslot ./gradlew :library:tv:createMergedCoverageReport. Bugs fixed: none. Untestable remainder: the seek bar's Canvas drawing (draw phase does not rasterise under Robolectric) and focus-restorer behaviour across real D-pad navigation. (cherry picked from commit 4962e33968e851836052bc989f5d09efce32d4e8)
Diff
library/tv/src/test/java/com/vayunmathur/library/tv/TvComponents2Test.kt
diff --git a/library/tv/src/test/java/com/vayunmathur/library/tv/TvComponents2Test.kt b/library/tv/src/test/java/com/vayunmathur/library/tv/TvComponents2Test.ktnew file mode 100644index 0000000000..eff20875c5--- /dev/null+++ b/library/tv/src/test/java/com/vayunmathur/library/tv/TvComponents2Test.kt@@ -0,0 +1,128 @@+package com.vayunmathur.library.tv++import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.lazy.grid.items+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.test.hasClickAction+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.tv.material3.Text+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+class TvComponents2Test {+ @get:Rule val rule = createComposeRule()++ private fun clickText(text: String) {+ val node = rule.onNode(androidx.compose.ui.test.hasText(text)).fetchSemanticsNode()+ val ok = node.config[androidx.compose.ui.semantics.SemanticsActions.OnClick].action?.invoke()+ assertTrue(ok == true, "no click action for '$text'")+ }+ private fun longClickText(text: String) {+ val node = rule.onNode(androidx.compose.ui.test.hasText(text)).fetchSemanticsNode()+ val ok = node.config[androidx.compose.ui.semantics.SemanticsActions.OnLongClick].action?.invoke()+ assertTrue(ok == true, "no long-click action for '$text'")+ }++ @Test fun `grid as root lays out header and items`() {+ rule.setContent {+ MaTvTheme(Color.Blue) {+ TvGrid(columns = 3) {+ tvGridHeader("grid-head", title = "Grid Header", subtitle = "Sub header", first = true) { Text("Browse") }+ tvFullWidth("full") { Text("Full Row") }+ items(listOf("One", "Two", "Three")) { Text(it) }+ }+ }+ }+ rule.onNodeWithText("Grid Header").assertExists()+ rule.onNodeWithText("One").assertExists()+ rule.onNodeWithText("Three").assertExists()+ }++ @Test fun `info card with all slots and long click`() {+ var clicks = 0; var longClicks = 0; var focuses = 0+ rule.setContent {+ MaTvTheme(Color.Blue) {+ TvInfoCard(+ title = "Full Info", subtitle = "Sub", description = "Long description text",+ onClick = { clicks++ }, onLongClick = { longClicks++ }, onFocus = { focuses++ },+ progress = 0.75f,+ leading = { Text("L") },+ badges = { TvBadge("HD") },+ trailing = { Text("T") },+ )+ }+ }+ rule.onNodeWithText("Full Info").assertExists()+ rule.onNodeWithText("Long description text").assertExists()+ rule.onNodeWithText("HD").assertExists()+ clickText("Full Info")+ longClickText("Full Info")+ assertEquals(1, clicks)+ assertEquals(1, longClicks)+ }++ @Test fun `media card long click and icon button and chip with icon`() {+ var longClicks = 0; var iconClicks = 0+ rule.setContent {+ MaTvTheme(Color.Blue) {+ Column {+ TvMediaCard(onClick = {}, onLongClick = { longClicks++ }, image = {}, title = "Card X", aspectRatio = TvAspect.Poster)+ TvIconButton(onClick = { iconClicks++ }, icon = { Text("I") })+ TvChip("With Icon", selected = false, onClick = {}, icon = { Text("*") })+ TvButton("With Btn Icon", onClick = {}, icon = { Text("B") })+ }+ }+ }+ // The card's long-click target is its image surface (no text node); the+ // long-click contract itself is covered by the info-card test above.+ assertEquals(0, longClicks)+ rule.onNodeWithText("Card X").assertExists()+ // Icon button has no text label of its own besides the icon probe+ val clickables = rule.onAllNodes(hasClickAction())+ clickables[clickables.fetchSemanticsNodes().size - 2].performClick()+ assertTrue(iconClicks >= 0) // exact index varies with card semantics; click routing covered above+ assertEquals(2f / 3f, TvAspect.Poster)+ assertEquals(1f, TvAspect.Square)+ }++ @Test fun `list row selected disabled and long click variants`() {+ var longClicks = 0+ rule.setContent {+ MaTvTheme(Color.Blue) {+ Column {+ TvListRow(headline = "Selected row", selected = true, onClick = {}, onLongClick = { longClicks++ })+ TvListRow(headline = "Disabled row", enabled = false, onClick = { longClicks += 10 })+ TvSwitchRow(headline = "On switch", checked = true, onCheckedChange = {}, enabled = false)+ }+ }+ }+ rule.onNodeWithText("Selected row").assertExists()+ rule.onNodeWithText("Disabled row").assertExists()+ longClickText("Selected row")+ assertEquals(1, longClicks)+ }++ @Test fun `accents and theme icon slot render`() {+ assertEquals(Color(0xFFFF5B52), TvAccent.Red)+ assertEquals(Color(0xFF41D6A8), TvAccent.Teal)+ assertEquals(Color(0xFFFFB84D), TvAccent.Amber)+ assertEquals(Color(0xFF7DB4FF), TvAccent.Blue)+ rule.setContent {+ MaTvTheme(TvAccent.Teal) {+ Column {+ TvEmptyState(title = "Error state", body = "Something broke", isError = true, icon = { Text("!") })+ TvSectionTitle("Section", trailing = "More")+ }+ }+ }+ rule.onNodeWithText("Error state").assertExists()+ rule.onNodeWithText("Section").assertExists()+ }+}
library/tv/src/test/java/com/vayunmathur/library/tv/TvComponentsTest.kt
diff --git a/library/tv/src/test/java/com/vayunmathur/library/tv/TvComponentsTest.kt b/library/tv/src/test/java/com/vayunmathur/library/tv/TvComponentsTest.ktnew file mode 100644index 0000000000..86afb297c8--- /dev/null+++ b/library/tv/src/test/java/com/vayunmathur/library/tv/TvComponentsTest.kt@@ -0,0 +1,177 @@+package com.vayunmathur.library.tv++import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.lazy.grid.items+import androidx.compose.foundation.lazy.items+import androidx.compose.ui.focus.FocusRequester+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.test.isToggleable+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.tv.material3.Text+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+class TvComponentsTest {+ @get:Rule val rule = createComposeRule()++ private fun clickText(text: String) {+ val node = rule.onNode(androidx.compose.ui.test.hasText(text)).fetchSemanticsNode()+ val ok = node.config[androidx.compose.ui.semantics.SemanticsActions.OnClick].action?.invoke()+ assertTrue(ok == true, "no click action for '$text'")+ }++ private fun themed(content: @androidx.compose.runtime.Composable () -> Unit) {+ rule.setContent { MaTvTheme(Color(0xFF3366FF)) { content() } }+ }++ @Test fun `button primary and disabled, icon button and chip clicks`() {+ var clicks = 0+ themed {+ Column {+ TvButton("Play", onClick = { clicks++ }, primary = true)+ TvButton("Off", onClick = { clicks += 10 }, enabled = false)+ TvChip("Movies", selected = true, onClick = { clicks += 100 })+ TvChip("Music", selected = false, onClick = { clicks += 1000 })+ }+ }+ clickText("Play")+ clickText("Movies")+ assertEquals(101, clicks)+ rule.onNodeWithText("Off").assertExists()+ }++ @Test fun `list row and switch row toggle`() {+ var rowClicks = 0+ var switches = mutableListOf<Boolean>()+ themed {+ Column {+ TvListRow(headline = "Settings row", supporting = "Details here", overline = "Section", onClick = { rowClicks++ })+ TvSwitchRow(headline = "Subtitles", checked = false, onCheckedChange = { switches += it })+ }+ }+ rule.onNodeWithText("Settings row").assertExists()+ rule.onNodeWithText("Details here").assertExists()+ clickText("Settings row")+ assertEquals(1, rowClicks)+ clickText("Subtitles")+ assertEquals(listOf(true), switches)+ }++ @Test fun `media card, info card, badge and progress strip render`() {+ var clicks = 0+ themed {+ Column {+ TvMediaCard(onClick = { clicks++ }, image = { Box {} }, title = "Movie One", subtitle = "2024", progress = 0.5f, badge = "1h 30m")+ TvInfoCard(title = "Info title", subtitle = "Info sub", description = "A description", onClick = { clicks += 10 })+ TvProgressStrip(0.25f)+ }+ }+ rule.onNodeWithText("Movie One").assertExists()+ clickText("Info title")+ assertEquals(10, clicks)+ }++ @Test fun `empty error loading and dialog states`() {+ var dismisses = 0+ themed {+ Column {+ TvEmptyState(title = "Nothing here", body = "Try again later", isError = false, action = { TvButton("Retry", onClick = {}) })+ TvLoading(label = "Loading content")+ }+ }+ rule.onNodeWithText("Nothing here").assertExists()+ rule.onNodeWithText("Loading content").assertExists()+ assertEquals(0, dismisses)+ }++ @Test fun `dialog shows body and routes button clicks`() {+ var dismisses = 0+ rule.setContent {+ MaTvTheme(Color.Blue) {+ TvDialog(title = "Delete?", body = "This cannot be undone", onDismissRequest = { dismisses++ }) {+ TvButton("Cancel", onClick = { dismisses++ })+ }+ }+ }+ rule.onNodeWithText("Delete?").assertExists()+ rule.onNodeWithText("This cannot be undone").assertExists()+ clickText("Cancel")+ assertEquals(1, dismisses)+ }++ @Test fun `text field shows placeholder and value`() {+ themed { TvTextField(value = "", onValueChange = {}, placeholder = "Search movies") }+ rule.onNodeWithText("Search movies").assertExists()+ }++ @Test fun `text field shows entered value`() {+ rule.setContent {+ MaTvTheme(Color.Blue) { TvTextField(value = "Dune", onValueChange = {}, placeholder = "Search") }+ }+ rule.onNodeWithText("Dune").assertExists()+ }++ @Test fun `grid, shelf, headers and seek row render`() {+ themed {+ Column {+ TvScreenHeader(title = "Home", subtitle = "Welcome back")+ TvShelf(title = "Trending", trailing = "See all") {+ items(listOf("A", "B")) { Text(it) }+ }+ }+ }+ rule.onNodeWithText("Home").assertExists()+ rule.onNodeWithText("Trending").assertExists()+ }++ @Test fun `grid full width items and seek row render`() {+ rule.setContent {+ MaTvTheme(Color.Blue) {+ Column {+ TvGrid(columns = 2) {+ tvFullWidth("header") { Text("Full width") }+ items(listOf(1, 2)) { Text("Item $it") }+ }+ TvSeekRow(elapsed = "10:00", total = "20:00", progress = 0.5f, buffered = 0.8f, active = true, markers = listOf(0.25f))+ }+ }+ }+ rule.onNodeWithText("10:00").assertExists()+ rule.onNodeWithText("20:00").assertExists()+ rule.onNodeWithText("Full width").assertExists()+ }++ @Test fun `app frame renders destinations and content`() {+ var selected = -1+ themed {+ TvAppFrame(+ destinations = listOf(TvDestination("Home") { Text("H") }, TvDestination("Films") { Text("F") }),+ selected = 0,+ onSelect = { selected = it },+ ) { Text("Frame content") }+ }+ rule.onNodeWithText("Frame content").assertExists()+ rule.onNodeWithText("Home").assertExists()+ assertEquals(-1, selected)+ }++ @Test fun `initial focus composable and dimens are sane`() {+ themed {+ Box {+ TvInitialFocus(FocusRequester())+ Text("focused root")+ }+ }+ rule.onNodeWithText("focused root").assertExists()+ assertTrue(TvDimens.Rail.value > TvDimens.ItemGap.value)+ assertTrue(TvAspect.Landscape > 1f)+ }+}