Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Closes #4161: Make MediaState extension methods visible for consuming apps. #4351

Merged
merged 1 commit into from
Sep 10, 2019
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 @@ -5,6 +5,7 @@
package mozilla.components.feature.media.ext

import android.support.v4.media.session.PlaybackStateCompat
import mozilla.components.browser.session.Session
import mozilla.components.concept.engine.media.Media
import mozilla.components.feature.media.state.MediaState

Expand All @@ -24,6 +25,17 @@ internal fun MediaState.getMedia(): List<Media> {
}
}

/**
* Get the [Session] that caused this [MediaState] (if any).
*/
fun MediaState.getSession(): Session? {
return when (this) {
is MediaState.Playing -> session
is MediaState.Paused -> session
else -> null
}
}

/**
* Turns the [MediaState] into a [PlaybackStateCompat] to be used with a `MediaSession`.
*/
Expand Down Expand Up @@ -53,16 +65,16 @@ internal fun MediaState.toPlaybackState() =
/**
* If this state is [MediaState.Playing] then pause all playing [Media].
*/
internal fun MediaState.pauseIfPlaying() {
fun MediaState.pauseIfPlaying() {
if (this is MediaState.Playing) {
media.pause()
}
}

/**
* If this state is [MediaState.Paused] then resume playing all paused media.
* If this state is [MediaState.Paused] then resume playing all paused [Media].
*/
internal fun MediaState.playIfPaused() {
fun MediaState.playIfPaused() {
if (this is MediaState.Paused) {
media.play()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.feature.media.ext

import mozilla.components.browser.session.Session
import mozilla.components.concept.engine.media.Media
import mozilla.components.feature.media.MockMedia
import mozilla.components.feature.media.state.MediaState
import mozilla.components.support.test.mock
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
import org.mockito.Mockito.never
import org.mockito.Mockito.verify

class MediaStateKtTest {
@Test
fun `getSession() extension method`() {
val noneState = MediaState.None
assertNull(noneState.getSession())

val playingState = MediaState.Playing(
Session(id = "test1", initialUrl = "https://www.mozilla.org"),
emptyList())
assertNotNull(playingState.getSession())
assertEquals("test1", playingState.getSession()!!.id)

val pausedState = MediaState.Paused(
Session(id = "test2", initialUrl = "https://www.mozilla.org"),
emptyList())
assertNotNull(pausedState.getSession())
assertEquals("test2", pausedState.getSession()!!.id)
}

@Test
fun `pauseIfPlaying() extension method`() {
val noneState = MediaState.None
noneState.pauseIfPlaying() // Does nothing and has no media -> nothing to verify/assert.

val playingMedia: Media = MockMedia(Media.PlaybackState.PLAYING)
val playingState = MediaState.Playing(session = mock(), media = listOf(playingMedia))
playingState.pauseIfPlaying()
verify(playingMedia.controller).pause()

val pausedMedia: Media = MockMedia(Media.PlaybackState.PAUSE)
val pausedState = MediaState.Paused(session = mock(), media = listOf(pausedMedia))
pausedState.pauseIfPlaying()
verify(pausedMedia.controller, never()).pause()
}

@Test
fun `playIfPaused() extension method`() {
val noneState = MediaState.None
noneState.playIfPaused() // Does nothing and has no media -> nothing to verify/assert.

val playingMedia: Media = MockMedia(Media.PlaybackState.PLAYING)
val playingState = MediaState.Playing(session = mock(), media = listOf(playingMedia))
playingState.playIfPaused()
verify(playingMedia.controller, never()).play()

val pausedMedia: Media = MockMedia(Media.PlaybackState.PAUSE)
val pausedState = MediaState.Paused(session = mock(), media = listOf(pausedMedia))
pausedState.playIfPaused()
verify(pausedMedia.controller).play()
}
}