Skip to content

Commit

Permalink
Fix TrackPositionUiModelMapper for SeekProjection at 0 position (#1757)
Browse files Browse the repository at this point in the history
* Fix TrackPositionUiModelMapper for SeekProjection to work for seek to 0 duration

* Add tests
  • Loading branch information
laiyichin authored Oct 17, 2023
1 parent 44dd7a0 commit edd893b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public object TrackPositionUiModelMapper {
if (currentPositionMs == null || durationMs == null || durationMs <= 0) {
return TrackPositionUiModel.Hidden
}
event.playbackState.seekProjection?.takeIf { it > Duration.ZERO }?.let { seek ->
event.playbackState.seekProjection?.takeIf { it >= Duration.ZERO }?.let { seek ->
val percent = seek.inWholeMilliseconds.toFloat() / durationMs.toFloat()
return TrackPositionUiModel.SeekProjection(percent, duration, seek)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.android.horologist.media.model.PlayerState
import com.google.android.horologist.media.ui.state.model.TrackPositionUiModel
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit
import kotlin.time.toDuration
Expand All @@ -38,6 +39,7 @@ class TrackPositionUiModelMapperTest {
playerState = PlayerState.Playing,
isLive = false,
currentPosition = current,
seekProjection = null,
duration = duration,
playbackSpeed = 1f,
),
Expand Down Expand Up @@ -65,6 +67,7 @@ class TrackPositionUiModelMapperTest {
playerState = PlayerState.Stopped,
isLive = false,
currentPosition = current,
seekProjection = null,
duration = duration,
playbackSpeed = 1f,
),
Expand All @@ -91,6 +94,7 @@ class TrackPositionUiModelMapperTest {
playerState = PlayerState.Loading,
isLive = false,
currentPosition = current,
seekProjection = null,
duration = duration,
playbackSpeed = 1f,
),
Expand All @@ -106,6 +110,59 @@ class TrackPositionUiModelMapperTest {
assertThat(result.isLoading).isTrue()
}

@Test
fun givenSeekProjectionNotNullButZero_thenMapsCorrectly() {
// given
val current = 1.seconds
val duration = 2.seconds
val playbackStateEvent = PlaybackStateEvent(
PlaybackState(
playerState = PlayerState.Playing,
isLive = false,
currentPosition = current,
seekProjection = Duration.ZERO,
duration = duration,
playbackSpeed = 1f,
),
cause = PlaybackStateEvent.Cause.PositionDiscontinuity,
)

// when
val result = TrackPositionUiModelMapper.map(playbackStateEvent)

// then
assertThat(result).isInstanceOf(TrackPositionUiModel.SeekProjection::class.java)
result as TrackPositionUiModel.SeekProjection
assertThat(result.percent).isEqualTo(0.0f)
}

@Test
fun givenSeekProjectionNotNullAndNotZero_thenMapsCorrectly() {
// given
val current = 0.seconds
val duration = 2.seconds
val seekDuration = 1.seconds
val playbackStateEvent = PlaybackStateEvent(
PlaybackState(
playerState = PlayerState.Playing,
isLive = false,
currentPosition = current,
seekProjection = seekDuration,
duration = duration,
playbackSpeed = 1f,
),
cause = PlaybackStateEvent.Cause.PositionDiscontinuity,
)

// when
val result = TrackPositionUiModelMapper.map(playbackStateEvent)

// then
assertThat(result).isInstanceOf(TrackPositionUiModel.SeekProjection::class.java)
result as TrackPositionUiModel.SeekProjection
assertThat(result.percent).isEqualTo(0.5f)
}

@Test
fun givenUnknownMediaPosition_thenMapsCorrectly() {
// given
Expand Down

0 comments on commit edd893b

Please sign in to comment.