|
| 1 | +package video.typetype.tv.ui |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Box |
| 4 | +import androidx.compose.foundation.layout.height |
| 5 | +import androidx.compose.material.icons.Icons |
| 6 | +import androidx.compose.material.icons.rounded.PlaylistPlay |
| 7 | +import androidx.compose.runtime.getValue |
| 8 | +import androidx.compose.runtime.mutableStateOf |
| 9 | +import androidx.compose.runtime.remember |
| 10 | +import androidx.compose.runtime.setValue |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.draw.clipToBounds |
| 13 | +import androidx.compose.ui.focus.FocusRequester |
| 14 | +import androidx.compose.ui.input.key.Key |
| 15 | +import androidx.compose.ui.semantics.Role |
| 16 | +import androidx.compose.ui.semantics.SemanticsProperties |
| 17 | +import androidx.compose.ui.test.SemanticsMatcher |
| 18 | +import androidx.compose.ui.test.SemanticsNodeInteraction |
| 19 | +import androidx.compose.ui.test.assertIsFocused |
| 20 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 21 | +import androidx.compose.ui.test.performKeyInput |
| 22 | +import androidx.compose.ui.test.pressKey |
| 23 | +import androidx.compose.ui.test.requestFocus |
| 24 | +import androidx.compose.ui.unit.dp |
| 25 | +import org.junit.Assert.assertTrue |
| 26 | +import org.junit.Rule |
| 27 | +import org.junit.Test |
| 28 | +import video.typetype.tv.ui.components.RailEntry |
| 29 | +import video.typetype.tv.ui.components.SectionRail |
| 30 | + |
| 31 | +// The library rail lists the user's playlists after its sections: with many of them, Down must scroll the rail to |
| 32 | +// each entry, down to the last one, and Up back to the first. |
| 33 | +class SectionRailScrollTest { |
| 34 | + @get:Rule |
| 35 | + val rule = createComposeRule() |
| 36 | + |
| 37 | + private val entries = (1..ENTRIES).map { RailEntry("p$it", "Playlist $it", Icons.Rounded.PlaylistPlay) } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun downReachesAndShowsEveryEntry() { |
| 41 | + rule.setContent { |
| 42 | + var selected by remember { mutableStateOf(entries.first().id) } |
| 43 | + val requester = remember { FocusRequester() } |
| 44 | + Box(Modifier.height(WINDOW).clipToBounds()) { |
| 45 | + SectionRail("Library", entries, selected, onSelect = { selected = it }, selectedRequester = requester) |
| 46 | + } |
| 47 | + } |
| 48 | + val tabs = rule.onAllNodes(SemanticsMatcher.expectValue(SemanticsProperties.Role, Role.Tab)) |
| 49 | + tabs[0].requestFocus() |
| 50 | + rule.waitForIdle() |
| 51 | + repeat(ENTRIES - 1) { tabs[it].press(Key.DirectionDown) } |
| 52 | + assertWhole(tabs[ENTRIES - 1]) |
| 53 | + repeat(ENTRIES - 1) { tabs[ENTRIES - 1 - it].press(Key.DirectionUp) } |
| 54 | + assertWhole(tabs[0]) |
| 55 | + } |
| 56 | + |
| 57 | + private fun SemanticsNodeInteraction.press(key: Key) { |
| 58 | + performKeyInput { pressKey(key) } |
| 59 | + rule.waitForIdle() |
| 60 | + } |
| 61 | + |
| 62 | + private fun assertWhole(tab: SemanticsNodeInteraction) { |
| 63 | + tab.assertIsFocused() |
| 64 | + val node = tab.fetchSemanticsNode() |
| 65 | + val window = with(rule.density) { WINDOW.toPx() } |
| 66 | + val whole = with(rule.density) { ENTRY_MIN_HEIGHT.toPx() } |
| 67 | + assertTrue("the focused entry is cut by the screen", node.positionInRoot.y >= 0f && node.positionInRoot.y + node.size.height <= window) |
| 68 | + assertTrue("the focused entry is squeezed", node.size.height >= whole) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +private const val ENTRIES = 12 |
| 73 | +private val WINDOW = 400.dp |
| 74 | +private val ENTRY_MIN_HEIGHT = 40.dp |
0 commit comments