diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/media/WaveformPlaybackView.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/media/WaveformPlaybackView.kt index f91cf96a95..fe9507cb45 100644 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/media/WaveformPlaybackView.kt +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/media/WaveformPlaybackView.kt @@ -200,11 +200,15 @@ internal fun WaveformPlaybackViewPreview() = ElementPreview { } } -private fun ImmutableList.normalisedData(maxSamplesCount: Int): ImmutableList { +internal fun ImmutableList.normalisedData(maxSamplesCount: Int): ImmutableList { if (maxSamplesCount <= 0) { return persistentListOf() } + if (isEmpty()) { + return List(maxSamplesCount) { 0f }.toImmutableList() + } + // Filter the data to keep only the expected number of samples val result = if (this.size > maxSamplesCount) { (0..().normalisedData(30) + assertThat(result).hasSize(30) + assertThat(result.toSet()).containsExactly(0f) + } + + @Test + fun `an empty waveform stays empty when there is no room to display it`() { + assertThat(persistentListOf().normalisedData(0)).isEmpty() + assertThat(persistentListOf().normalisedData(-1)).isEmpty() + } + + @Test + fun `a waveform shorter than the display count is kept as it is`() { + val waveform = persistentListOf(0.1f, 0.2f, 0.3f) + assertThat(waveform.normalisedData(30)).containsExactly(0.1f, 0.2f, 0.3f).inOrder() + } + + @Test + fun `a waveform longer than the display count is downsampled`() { + val waveform = persistentListOf(0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f) + assertThat(waveform.normalisedData(3)).hasSize(3) + } +}