Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to pillarbox-player #430

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ import kotlin.time.Duration.Companion.milliseconds
*
* @param timeProvider A callback invoked whenever the current time is needed.
*/
class TotalPlaytimeCounter(
private val timeProvider: () -> Long = { System.currentTimeMillis() },
class TotalPlaytimeCounter internal constructor(
private val timeProvider: () -> Long,
) {
private var totalPlayTime: Duration = Duration.ZERO
private var lastPlayTime = 0L

constructor() : this(
timeProvider = { System.currentTimeMillis() },
)

/**
* Play
* Calling twice play after sometime will compute totalPlaytime
Expand Down
1 change: 1 addition & 0 deletions pillarbox-player/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ dependencies {
testImplementation(project(":pillarbox-player-testutils"))

testImplementation(libs.junit)
testImplementation(libs.kotlin.test)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.mockk)
testImplementation(libs.mockk.dsl)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.extension

import androidx.media3.common.Player
import androidx.media3.common.Player.Commands
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class PlayerCommandsTest {
@Test
fun canSeekToNext() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_SEEK_TO_NEXT, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canSeekToNext())
assertFalse(player.availableCommands.canSeekToNext())
assertTrue(player.availableCommands.canSeekToNext())
}

@Test
fun canSeekToPrevious() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_SEEK_TO_PREVIOUS, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canSeekToPrevious())
assertFalse(player.availableCommands.canSeekToPrevious())
assertTrue(player.availableCommands.canSeekToPrevious())
}

@Test
fun canSeekForward() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_SEEK_FORWARD, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canSeekForward())
assertFalse(player.availableCommands.canSeekForward())
assertTrue(player.availableCommands.canSeekForward())
}

@Test
fun canSeekBack() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_SEEK_BACK, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canSeekBack())
assertFalse(player.availableCommands.canSeekBack())
assertTrue(player.availableCommands.canSeekBack())
}

@Test
fun canSeek() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canSeek())
assertFalse(player.availableCommands.canSeek())
assertTrue(player.availableCommands.canSeek())
}

@Test
fun canPlayPause() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_PLAY_PAUSE, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canPlayPause())
assertFalse(player.availableCommands.canPlayPause())
assertTrue(player.availableCommands.canPlayPause())
}

@Test
fun canGetTracks() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TRACKS, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canGetTracks())
assertFalse(player.availableCommands.canGetTracks())
assertTrue(player.availableCommands.canGetTracks())
}

@Test
fun canSetTrackSelectionParameters() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_SET_TRACK_SELECTION_PARAMETERS, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canSetTrackSelectionParameters())
assertFalse(player.availableCommands.canSetTrackSelectionParameters())
assertTrue(player.availableCommands.canSetTrackSelectionParameters())
}

@Test
fun canSpeedAndPitch() {
val player = mockk<Player> {
every { availableCommands } returnsMany listOf(
Commands.Builder().build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_GET_TEXT).build(),
Commands.Builder().addAll(Player.COMMAND_STOP, Player.COMMAND_SET_SPEED_AND_PITCH, Player.COMMAND_GET_TEXT).build()
)
}

assertFalse(player.availableCommands.canSpeedAndPitch())
assertFalse(player.availableCommands.canSpeedAndPitch())
assertTrue(player.availableCommands.canSpeedAndPitch())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.extension

import android.util.Rational
import androidx.media3.common.VideoSize
import org.junit.Assert.assertEquals
import org.junit.Test

class VideoSizeTest {
@Test
fun toRational_unknown_video_size() {
val input = VideoSize.UNKNOWN
assertEquals(RATIONAL_ONE, input.toRational())
}

@Test
fun toRational_with_a_16_9_aspect_ratio() {
val input = VideoSize(1920, 1080)
assertEquals(Rational(1920, 1080), input.toRational())
}

@Test
fun toRational_with_a_square_aspect_ratio() {
val input = VideoSize(500, 500)
assertEquals(Rational(500, 500), input.toRational())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ val Format.videoSize: VideoSize
* Selection flags as string
*/
fun Format.selectionString(): String {
val selectionFlags: MutableList<String> = ArrayList()
val selectionFlags = mutableListOf<String>()
if (hasSelection(C.SELECTION_FLAG_AUTOSELECT)) {
selectionFlags.add("auto")
}
Expand All @@ -69,7 +69,7 @@ fun Format.selectionString(): String {
*/
@Suppress("CyclomaticComplexMethod")
fun Format.roleString(): String {
val roleFlags: MutableList<String> = ArrayList()
val roleFlags = mutableListOf<String>()
if (hasRole(C.ROLE_FLAG_MAIN)) {
roleFlags.add("main")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,16 @@ package ch.srgssr.pillarbox.player.extension
import androidx.media3.common.MediaItem
import androidx.media3.common.Player

/**
* Resume playback
*/
fun Player.startPlayback() {
when (playbackState) {
Player.STATE_IDLE -> {
prepare()
}

Player.STATE_ENDED -> {
seekToDefaultPosition()
}

else -> {
// Nothing
}
}
play()
}

/**
* Get a snapshot of the current media items
*/
fun Player.getCurrentMediaItems(): List<MediaItem> {
if (mediaItemCount == 0) {
val count = mediaItemCount
if (count == 0) {
return emptyList()
}
val count = mediaItemCount
return ArrayList<MediaItem>(count).apply {
for (i in 0 until count) {
return buildList(count) {
repeat(count) { i ->
add(getMediaItemAt(i))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
@file:Suppress("unused")

package ch.srgssr.pillarbox.player.extension

import androidx.media3.common.Player
Expand Down

This file was deleted.

Loading
Loading