|
| 1 | +package com.arflix.tv.ui.screens.tv.live |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Box |
| 4 | +import androidx.compose.foundation.layout.height |
| 5 | +import androidx.compose.foundation.layout.width |
| 6 | +import androidx.compose.foundation.lazy.rememberLazyListState |
| 7 | +import androidx.compose.runtime.mutableStateOf |
| 8 | +import androidx.compose.runtime.LaunchedEffect |
| 9 | +import androidx.activity.OnBackPressedDispatcher |
| 10 | +import androidx.activity.compose.BackHandler |
| 11 | +import androidx.activity.compose.LocalOnBackPressedDispatcherOwner |
| 12 | +import androidx.compose.ui.Modifier |
| 13 | +import androidx.compose.ui.input.key.Key |
| 14 | +import androidx.compose.ui.test.* |
| 15 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 16 | +import androidx.compose.ui.unit.dp |
| 17 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 18 | +import com.arflix.tv.data.model.IptvChannel |
| 19 | +import org.junit.Assert.* |
| 20 | +import org.junit.Rule |
| 21 | +import org.junit.Test |
| 22 | +import org.junit.runner.RunWith |
| 23 | +import kotlinx.coroutines.delay |
| 24 | + |
| 25 | +@RunWith(AndroidJUnit4::class) |
| 26 | +@OptIn(ExperimentalTestApi::class) |
| 27 | +class GuideNavigationDeviceTest { |
| 28 | + @get:Rule val compose = createComposeRule() |
| 29 | + private val rows by lazy { (0 until 55_000).map { index -> |
| 30 | + IptvChannel(id = "test:$index", name = "Channel $index", group = "News", |
| 31 | + streamUrl = "https://example.test/live").enrichForFastStartup(index + 1) |
| 32 | + } } |
| 33 | + |
| 34 | + @Test fun programmeNavigationRevealsOffscreenRowsWithoutLosingFocus() { |
| 35 | + val mode = mutableStateOf(EpgGridFocusMode.ChannelList) |
| 36 | + var focused = "" |
| 37 | + compose.setContent { |
| 38 | + Box(Modifier.width(900.dp).height(400.dp)) { |
| 39 | + EpgGrid(channels = rows.take(144), clockTickMillis = 1_783_000_000_000L, |
| 40 | + nowNext = emptyMap(), selectedChannelId = "test:0", focusSelectedChannelSignal = 1, |
| 41 | + scrollResetKey = "all", onChannelSelect = {}, favorites = emptySet(), |
| 42 | + focusMode = mode.value, onEnterEpg = { mode.value = EpgGridFocusMode.Epg }, |
| 43 | + onChannelFocused = { focused = it.id }) |
| 44 | + } |
| 45 | + } |
| 46 | + compose.onRoot().performKeyInput { pressKey(Key.DirectionRight) } |
| 47 | + repeat(30) { compose.onRoot().performKeyInput { pressKey(Key.DirectionDown) } } |
| 48 | + compose.runOnIdle { assertEquals("test:30", focused) } |
| 49 | + repeat(20) { compose.onRoot().performKeyInput { pressKey(Key.DirectionUp) } } |
| 50 | + compose.runOnIdle { assertEquals("test:10", focused) } |
| 51 | + } |
| 52 | + |
| 53 | + @Test fun backClosesMenuInsteadOfMovingTheGuideBehindIt() { |
| 54 | + val menuOpen = mutableStateOf(true) |
| 55 | + var guideBacks = 0 |
| 56 | + lateinit var dispatcher: OnBackPressedDispatcher |
| 57 | + compose.setContent { |
| 58 | + dispatcher = LocalOnBackPressedDispatcherOwner.current!!.onBackPressedDispatcher |
| 59 | + BackHandler(enabled = menuOpen.value) { menuOpen.value = false } |
| 60 | + EpgGrid(channels = rows.take(8), clockTickMillis = 1_783_000_000_000L, |
| 61 | + nowNext = emptyMap(), selectedChannelId = "test:0", focusSelectedChannelSignal = 1, |
| 62 | + scrollResetKey = "all", onChannelSelect = {}, favorites = emptySet(), |
| 63 | + gridFocused = true, backHandlingEnabled = !menuOpen.value, |
| 64 | + onMoveLeftFromChannels = { guideBacks++ }) |
| 65 | + } |
| 66 | + compose.runOnIdle { dispatcher.onBackPressed() } |
| 67 | + compose.runOnIdle { assertFalse(menuOpen.value); assertEquals(0, guideBacks) } |
| 68 | + compose.runOnIdle { dispatcher.onBackPressed() } |
| 69 | + compose.runOnIdle { assertEquals(1, guideBacks) } |
| 70 | + } |
| 71 | + |
| 72 | + @Test fun remoteScrollCrossesPageBoundariesAndCanReverseWithoutReset() { |
| 73 | + val channels = mutableStateOf(rows.take(144)) |
| 74 | + val requested = mutableStateOf(144) |
| 75 | + var focused = "" |
| 76 | + compose.setContent { |
| 77 | + LaunchedEffect(requested.value) { |
| 78 | + delay(100L) |
| 79 | + channels.value = rows.take(requested.value) |
| 80 | + } |
| 81 | + Box(Modifier.width(900.dp).height(400.dp)) { |
| 82 | + EpgGrid(channels = channels.value, totalChannelCount = rows.size, |
| 83 | + clockTickMillis = 1_783_000_000_000L, nowNext = emptyMap(), |
| 84 | + selectedChannelId = "test:0", focusSelectedChannelSignal = 1, |
| 85 | + scrollResetKey = "all", onChannelSelect = {}, favorites = emptySet(), |
| 86 | + onChannelFocused = { focused = it.id }, |
| 87 | + onRequestNextChannels = { |
| 88 | + requested.value = nextGuidePageLimit(channels.value.size, requested.value, rows.size) |
| 89 | + }) |
| 90 | + } |
| 91 | + } |
| 92 | + compose.waitForIdle() |
| 93 | + repeat(170) { |
| 94 | + compose.onRoot().performKeyInput { pressKey(Key.DirectionDown) } |
| 95 | + } |
| 96 | + compose.runOnIdle { assertEquals("test:170", focused) } |
| 97 | + repeat(20) { compose.onRoot().performKeyInput { pressKey(Key.DirectionUp) } } |
| 98 | + compose.runOnIdle { assertEquals("test:150", focused) } |
| 99 | + compose.onNodeWithTag("iptv-channel:test:150").assertIsDisplayed() |
| 100 | + } |
| 101 | + |
| 102 | + @Test fun touchScrollSurvivesAppendAndMetadataRefresh() { |
| 103 | + val channels = mutableStateOf(rows.take(144)) |
| 104 | + var firstVisible = 0 |
| 105 | + compose.setContent { |
| 106 | + Box(Modifier.width(380.dp).height(620.dp)) { |
| 107 | + EpgGrid(channels = channels.value, totalChannelCount = rows.size, |
| 108 | + clockTickMillis = 1_783_000_000_000L, nowNext = emptyMap(), compact = true, |
| 109 | + selectedChannelId = null, focusSelectedChannelSignal = 0, |
| 110 | + scrollResetKey = "all", onChannelSelect = {}, favorites = emptySet(), |
| 111 | + onVisibleChannelRange = { first, _ -> firstVisible = first }) |
| 112 | + } |
| 113 | + } |
| 114 | + compose.onNodeWithTag("iptv-guide").performScrollToIndex(90) |
| 115 | + compose.waitForIdle() |
| 116 | + var before = 0 |
| 117 | + compose.runOnIdle { before = firstVisible; channels.value = rows.take(336) } |
| 118 | + compose.waitForIdle() |
| 119 | + compose.runOnIdle { assertEquals(before, firstVisible) } |
| 120 | + compose.onNodeWithTag("iptv-guide").performTouchInput { swipeUp() } |
| 121 | + compose.waitForIdle() |
| 122 | + compose.runOnIdle { assertTrue(firstVisible > before) } |
| 123 | + } |
| 124 | + |
| 125 | + @Test fun favoriteReorderKeepsStableFocusedChannel() { |
| 126 | + val channels = mutableStateOf(rows.take(8)) |
| 127 | + compose.setContent { |
| 128 | + EpgGrid(channels = channels.value, clockTickMillis = 1_783_000_000_000L, |
| 129 | + nowNext = emptyMap(), selectedChannelId = "test:3", focusSelectedChannelSignal = 1, |
| 130 | + scrollResetKey = "fav", onChannelSelect = {}, favorites = setOf("test:3")) |
| 131 | + } |
| 132 | + compose.waitForIdle() |
| 133 | + compose.runOnIdle { |
| 134 | + channels.value = channels.value.toMutableList().apply { add(2, removeAt(3)) } |
| 135 | + } |
| 136 | + compose.waitForIdle() |
| 137 | + compose.onNodeWithTag("iptv-channel:test:3").assertIsFocused() |
| 138 | + } |
| 139 | + |
| 140 | + @Test fun removingFocusedFavoriteFocusesTheNextChannel() { |
| 141 | + val channels = mutableStateOf(rows.take(8)) |
| 142 | + compose.setContent { |
| 143 | + EpgGrid(channels = channels.value, clockTickMillis = 1_783_000_000_000L, |
| 144 | + nowNext = emptyMap(), selectedChannelId = "test:3", focusSelectedChannelSignal = 1, |
| 145 | + scrollResetKey = "fav", onChannelSelect = {}, favorites = setOf("test:3"), |
| 146 | + gridFocused = true) |
| 147 | + } |
| 148 | + compose.waitForIdle() |
| 149 | + compose.runOnIdle { channels.value = channels.value.filterNot { it.id == "test:3" } } |
| 150 | + compose.waitForIdle() |
| 151 | + compose.onNodeWithTag("iptv-channel:test:4").assertIsFocused() |
| 152 | + compose.onRoot().performKeyInput { pressKey(Key.DirectionDown) } |
| 153 | + compose.onNodeWithTag("iptv-channel:test:5").assertIsFocused() |
| 154 | + } |
| 155 | + |
| 156 | + @Test fun touchCategoryMenuHidesGroupWithoutLeavingHiddenShortcut() { |
| 157 | + val hidden = mutableStateOf(emptySet<String>()) |
| 158 | + compose.setContent { |
| 159 | + val state = buildPagedStartupChannelState( |
| 160 | + channels = rows.take(1).map { it.source }, totalChannelCount = 55_000, |
| 161 | + playlistGroupCounts = listOf(Triple("test", "News", 54_990), Triple("test", "Movies", 10)), |
| 162 | + favorites = emptySet(), recents = emptySet(), hiddenGroups = hidden.value) |
| 163 | + CategorySidebar(tree = state.tree, selectedId = "all", expanded = true, |
| 164 | + listState = rememberLazyListState(), onSelect = {}, onOpenSearch = {}, |
| 165 | + isTouchDevice = true, |
| 166 | + onHideCategory = { playlist, group -> hidden.value = setOf("$playlist|$group") }) |
| 167 | + } |
| 168 | + compose.onNodeWithText("News").performTouchInput { longClick() } |
| 169 | + compose.onNodeWithText("Hide category").performClick() |
| 170 | + compose.waitForIdle() |
| 171 | + compose.runOnIdle { assertEquals(setOf("test|News"), hidden.value) } |
| 172 | + compose.onNodeWithText("News").assertDoesNotExist() |
| 173 | + compose.onNodeWithText("Movies").assertIsDisplayed() |
| 174 | + } |
| 175 | +} |
0 commit comments