Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / launcher / src / test / java / com / vayunmathur / launcher / ui / LauncherTvUiTest.kt
LauncherTvUiTest.kt210 lines · 6.8 KiBRaw
| 1 | package com.vayunmathur.launcher.ui |
| 2 | |
| 3 | import androidx.compose.ui.test.assertIsDisplayed |
| 4 | import androidx.compose.ui.test.junit4.createComposeRule |
| 5 | import androidx.compose.ui.test.onAllNodesWithText |
| 6 | import androidx.compose.ui.test.onNodeWithText |
| 7 | import androidx.compose.ui.test.performClick |
| 8 | import androidx.compose.ui.unit.dp |
| 9 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 10 | import com.vayunmathur.launcher.platform.TvHdmiInput |
| 11 | import com.vayunmathur.launcher.platform.TvHomeState |
| 12 | import com.vayunmathur.launcher.platform.TvTile |
| 13 | import com.vayunmathur.launcher.ui.components.TvBannerTile |
| 14 | import org.junit.Rule |
| 15 | import org.junit.Test |
| 16 | import org.junit.runner.RunWith |
| 17 | import kotlin.test.assertEquals |
| 18 | |
| 19 | /** |
| 20 | * Compose tests (Robolectric + createComposeRule) for the TV home screen: |
| 21 | * [TvBannerTile] states and press handling, [TvAppGrid] rows, [TvBottomRow] |
| 22 | * inputs/store tiles, and [TvHomeScreen] end to end. |
| 23 | */ |
| 24 | @RunWith(AndroidJUnit4::class) |
| 25 | class LauncherTvUiTest { |
| 26 | |
| 27 | @get:Rule val rule = createComposeRule() |
| 28 | |
| 29 | private fun tile(pkg: String, label: String) = TvTile( |
| 30 | packageName = pkg, |
| 31 | label = label, |
| 32 | banner = null, |
| 33 | appIcon = null, |
| 34 | userInstalled = true, |
| 35 | ) |
| 36 | |
| 37 | private val tiles = listOf( |
| 38 | tile("com.example.a", "Alpha"), |
| 39 | tile("com.example.b", "Beta"), |
| 40 | tile("com.example.c", "Gamma"), |
| 41 | ) |
| 42 | |
| 43 | @Test fun bannerTileRendersLabelAndPresses() { |
| 44 | var pressed = false |
| 45 | rule.setContent { |
| 46 | TvBannerTile( |
| 47 | tile = tiles[0], |
| 48 | tileWidth = 200.dp, |
| 49 | artHeight = 100.dp, |
| 50 | moving = false, |
| 51 | dimmed = false, |
| 52 | focusRequester = null, |
| 53 | onPress = { pressed = true }, |
| 54 | onEdit = {}, |
| 55 | onMove = {}, |
| 56 | ) |
| 57 | } |
| 58 | // Label shows on the tile and in the caption below it. |
| 59 | assertEquals(2, rule.onAllNodesWithText("Alpha").fetchSemanticsNodes().size) |
| 60 | rule.onAllNodesWithText("Alpha")[0].performClick() |
| 61 | assertEquals(true, pressed) |
| 62 | } |
| 63 | |
| 64 | @Test fun tileWithBannerArtRenders() { |
| 65 | rule.setContent { |
| 66 | TvBannerTile( |
| 67 | tile = tiles[0].copy(banner = android.graphics.drawable.ColorDrawable(0xFF112233.toInt())), |
| 68 | tileWidth = 200.dp, |
| 69 | artHeight = 100.dp, |
| 70 | moving = false, |
| 71 | dimmed = false, |
| 72 | focusRequester = null, |
| 73 | onPress = {}, |
| 74 | onEdit = {}, |
| 75 | onMove = {}, |
| 76 | ) |
| 77 | } |
| 78 | rule.waitForIdle() |
| 79 | } |
| 80 | |
| 81 | @Test fun focusedTileShowsRing() { |
| 82 | val requester = androidx.compose.ui.focus.FocusRequester() |
| 83 | rule.setContent { |
| 84 | TvBannerTile( |
| 85 | tile = tiles[0], |
| 86 | tileWidth = 200.dp, |
| 87 | artHeight = 100.dp, |
| 88 | moving = false, |
| 89 | dimmed = false, |
| 90 | focusRequester = requester, |
| 91 | onPress = {}, |
| 92 | onEdit = {}, |
| 93 | onMove = {}, |
| 94 | ) |
| 95 | } |
| 96 | rule.runOnIdle { requester.requestFocus() } |
| 97 | rule.waitForIdle() |
| 98 | // Focused: caption still shows under the ring. |
| 99 | assertEquals(2, rule.onAllNodesWithText("Alpha").fetchSemanticsNodes().size) |
| 100 | } |
| 101 | |
| 102 | @Test fun dimmedTileRenders() { |
| 103 | rule.setContent { |
| 104 | TvBannerTile( |
| 105 | tile = tiles[1], |
| 106 | tileWidth = 200.dp, |
| 107 | artHeight = 100.dp, |
| 108 | moving = false, |
| 109 | dimmed = true, |
| 110 | focusRequester = null, |
| 111 | onPress = {}, |
| 112 | onEdit = {}, |
| 113 | onMove = {}, |
| 114 | ) |
| 115 | } |
| 116 | assertEquals(2, rule.onAllNodesWithText("Beta").fetchSemanticsNodes().size) |
| 117 | } |
| 118 | |
| 119 | @Test fun movingTileShowsMovingBadge() { |
| 120 | rule.setContent { |
| 121 | TvBannerTile( |
| 122 | tile = tiles[0], |
| 123 | tileWidth = 200.dp, |
| 124 | artHeight = 100.dp, |
| 125 | moving = true, |
| 126 | dimmed = false, |
| 127 | focusRequester = null, |
| 128 | onPress = {}, |
| 129 | onEdit = {}, |
| 130 | onMove = {}, |
| 131 | ) |
| 132 | } |
| 133 | rule.onNodeWithText("MOVING", substring = true).assertIsDisplayed() |
| 134 | } |
| 135 | |
| 136 | @Test fun appGridRendersRowTiles() { |
| 137 | rule.setContent { |
| 138 | TvAppGrid( |
| 139 | state = TvHomeState(rowApps = tiles), |
| 140 | onPressTile = {}, |
| 141 | onOpenEditMenu = {}, |
| 142 | onMoveKey = { _, _ -> }, |
| 143 | ) |
| 144 | } |
| 145 | assertEquals(2, rule.onAllNodesWithText("Alpha").fetchSemanticsNodes().size) |
| 146 | assertEquals(2, rule.onAllNodesWithText("Beta").fetchSemanticsNodes().size) |
| 147 | assertEquals(2, rule.onAllNodesWithText("Gamma").fetchSemanticsNodes().size) |
| 148 | } |
| 149 | |
| 150 | @Test fun appGridPressReachesCallback() { |
| 151 | var pressed: String? = null |
| 152 | rule.setContent { |
| 153 | TvAppGrid( |
| 154 | state = TvHomeState(rowApps = tiles), |
| 155 | onPressTile = { pressed = it }, |
| 156 | onOpenEditMenu = {}, |
| 157 | onMoveKey = { _, _ -> }, |
| 158 | ) |
| 159 | } |
| 160 | rule.onAllNodesWithText("Beta")[0].performClick() |
| 161 | assertEquals("com.example.b", pressed) |
| 162 | } |
| 163 | |
| 164 | @Test fun bottomRowRendersInputsAndStore() { |
| 165 | rule.setContent { |
| 166 | TvBottomRow( |
| 167 | state = TvHomeState( |
| 168 | showAppStore = true, |
| 169 | hdmiInputs = listOf( |
| 170 | TvHdmiInput("hdmi1", "HDMI 1"), |
| 171 | ), |
| 172 | ), |
| 173 | onOpenAppStore = {}, |
| 174 | onOpenInput = {}, |
| 175 | onOpenSettings = {}, |
| 176 | onToggleReorder = {}, |
| 177 | ) |
| 178 | } |
| 179 | rule.onNodeWithText("HDMI 1").assertIsDisplayed() |
| 180 | } |
| 181 | |
| 182 | @Test fun homeScreenRendersClockAndTiles() { |
| 183 | var pressed: String? = null |
| 184 | rule.setContent { |
| 185 | TvHomeScreen( |
| 186 | state = TvHomeState(rowApps = tiles), |
| 187 | onPressTile = { pressed = it }, |
| 188 | onOpenAppStore = {}, |
| 189 | onOpenInput = {}, |
| 190 | onOpenSettings = {}, |
| 191 | onOpenNetworkSettings = {}, |
| 192 | onOpenEditMenu = {}, |
| 193 | onCloseEditMenu = {}, |
| 194 | onStartMove = {}, |
| 195 | onCancelMove = {}, |
| 196 | onToggleReorder = {}, |
| 197 | onDisarmReorder = {}, |
| 198 | onMoveKey = { _, _ -> }, |
| 199 | onRemoveTile = {}, |
| 200 | onOpenAddMenu = {}, |
| 201 | onPickAddCandidate = {}, |
| 202 | onUninstallOrInfo = {}, |
| 203 | onRequestHomeRole = {}, |
| 204 | ) |
| 205 | } |
| 206 | assertEquals(2, rule.onAllNodesWithText("Alpha").fetchSemanticsNodes().size) |
| 207 | rule.onAllNodesWithText("Alpha")[0].performClick() |
| 208 | assertEquals("com.example.a", pressed) |
| 209 | } |
| 210 | } |