|
| 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 | +} |
0 commit comments