Skip to content

Commit f354e66

Browse files
committed
fix: ignore cancelled player scrubs
1 parent 52f3753 commit f354e66

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package dev.typetype.android.feature.player.components
2+
3+
import androidx.activity.ComponentActivity
4+
import androidx.compose.foundation.layout.size
5+
import androidx.compose.material3.MaterialTheme
6+
import androidx.compose.ui.Modifier
7+
import androidx.compose.ui.test.cancel
8+
import androidx.compose.ui.test.centerLeft
9+
import androidx.compose.ui.test.centerRight
10+
import androidx.compose.ui.test.down
11+
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
12+
import androidx.compose.ui.test.moveTo
13+
import androidx.compose.ui.test.onNodeWithTag
14+
import androidx.compose.ui.test.performTouchInput
15+
import androidx.compose.ui.platform.testTag
16+
import androidx.compose.ui.unit.dp
17+
import androidx.test.ext.junit.runners.AndroidJUnit4
18+
import org.junit.Assert.assertEquals
19+
import org.junit.Assert.assertTrue
20+
import org.junit.Rule
21+
import org.junit.Test
22+
import org.junit.runner.RunWith
23+
24+
@RunWith(AndroidJUnit4::class)
25+
class PlayerTimeBarGestureTest {
26+
@get:Rule
27+
val composeRule = createAndroidComposeRule<ComponentActivity>()
28+
29+
@Test
30+
fun cancelledDragDoesNotCommitASeek() {
31+
val scrubbed = mutableListOf<Long>()
32+
val committed = mutableListOf<Long>()
33+
var cancellations = 0
34+
showTimeline(scrubbed::add, committed::add) { cancellations++ }
35+
36+
composeRule.onNodeWithTag(TIMELINE_TAG).performTouchInput {
37+
down(centerLeft)
38+
moveTo(centerRight, delayMillis = 200L)
39+
cancel()
40+
}
41+
42+
composeRule.runOnIdle {
43+
assertTrue(scrubbed.isNotEmpty())
44+
assertTrue(committed.isEmpty())
45+
assertEquals(1, cancellations)
46+
}
47+
}
48+
49+
@Test
50+
fun completedDragCommitsTheLatestPosition() {
51+
val scrubbed = mutableListOf<Long>()
52+
val committed = mutableListOf<Long>()
53+
var cancellations = 0
54+
showTimeline(scrubbed::add, committed::add) { cancellations++ }
55+
56+
composeRule.onNodeWithTag(TIMELINE_TAG).performTouchInput {
57+
down(centerLeft)
58+
moveTo(centerRight, delayMillis = 200L)
59+
up()
60+
}
61+
62+
composeRule.runOnIdle {
63+
assertTrue(scrubbed.isNotEmpty())
64+
assertEquals(scrubbed.last(), committed.single())
65+
assertEquals(0, cancellations)
66+
}
67+
}
68+
69+
private fun showTimeline(
70+
onScrub: (Long) -> Unit,
71+
onScrubFinished: (Long) -> Unit,
72+
onScrubCancelled: () -> Unit,
73+
) {
74+
composeRule.setContent {
75+
MaterialTheme {
76+
TimelineTrack(
77+
positionMs = 10_000L,
78+
durationMs = 60_000L,
79+
segments = emptyList(),
80+
compact = false,
81+
onScrub = onScrub,
82+
onScrubFinished = onScrubFinished,
83+
onScrubCancelled = onScrubCancelled,
84+
modifier = Modifier
85+
.size(width = 240.dp, height = 36.dp)
86+
.testTag(TIMELINE_TAG),
87+
)
88+
}
89+
}
90+
}
91+
92+
private companion object {
93+
const val TIMELINE_TAG = "player_time_bar_timeline"
94+
}
95+
}

app/src/main/java/dev/typetype/android/feature/player/components/PlayerTimeBar.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ fun PlayerTimeBar(
8787
player.seekTo(targetMs)
8888
scrubPositionMs = null
8989
},
90+
onScrubCancelled = { scrubPositionMs = null },
9091
modifier = Modifier
9192
.weight(1f)
9293
.padding(horizontal = if (compact) 2.dp else 4.dp)
@@ -106,13 +107,14 @@ fun PlayerTimeBar(
106107
}
107108

108109
@Composable
109-
private fun TimelineTrack(
110+
internal fun TimelineTrack(
110111
positionMs: Long,
111112
durationMs: Long,
112113
segments: List<SponsorBlockSegment>,
113114
compact: Boolean,
114115
onScrub: (Long) -> Unit,
115116
onScrubFinished: (Long) -> Unit,
117+
onScrubCancelled: () -> Unit,
116118
modifier: Modifier = Modifier,
117119
) {
118120
val activeColor = MaterialTheme.colorScheme.primary
@@ -138,7 +140,7 @@ private fun TimelineTrack(
138140
onScrub(lastTargetMs)
139141
},
140142
onDragEnd = { onScrubFinished(lastTargetMs) },
141-
onDragCancel = { onScrubFinished(lastTargetMs) },
143+
onDragCancel = onScrubCancelled,
142144
)
143145
},
144146
) {

0 commit comments

Comments
 (0)