This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalCoroutinesApi::class) | |
class NeverEndingCoroutineTest { | |
@Test | |
fun `should emit error when playSound throws`() = runTest { | |
val exception = Exception("Oopsie") | |
val soundPlayer = mockk<SoundPlayer>() | |
coEvery { soundPlayer.playSound() } throws exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `should increase the playback counter after the sound has finished playing`() = runTest { | |
val soundPlayer = mockk<SoundPlayer>() | |
coEvery { soundPlayer.playSound() } coAnswers { delay(1000) } | |
val sut = MediaPlayer(this, soundPlayer) | |
sut.play() | |
advanceTimeBy(1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `should not increase the playback counter until the sound has finished playing`() = runTest { | |
val soundPlayer = mockk<SoundPlayer>() | |
coEvery { soundPlayer.playSound() } coAnswers { delay(1000) } | |
val sut = MediaPlayer(this, soundPlayer) | |
sut.play() | |
advanceTimeBy(500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SoundPlayer { | |
suspend fun playSound() | |
} | |
class MediaPlayer( | |
private val scope: CoroutineScope, | |
private val soundPlayer: SoundPlayer | |
) { | |
var playbackCounter = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `should change state to Playing after play() called`() = runTest(UnconfinedTestDispatcher()) { | |
val soundPlayer = mockk<SoundPlayer>() | |
coJustRun { soundPlayer.playSound() } | |
val sut = MediaPlayer(this, soundPlayer) | |
sut.playerState.test { | |
awaitItem() shouldBe PlayerState.Stopped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SoundPlayer { | |
suspend fun playSound() | |
} | |
sealed class PlayerState { | |
object Stopped : PlayerState() | |
object Playing : PlayerState() | |
} | |
class MediaPlayer( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MediaPlayer(...) { | |
... | |
fun dispose() { | |
scope.cancel() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IssueReporter { | |
fun reportIssue(e: Exception) | |
} | |
interface SoundPlayer { | |
suspend fun playSound() | |
} | |
class MediaPlayer( | |
private val scope: CoroutineScope, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SoundPlayer { | |
suspend fun playSound() | |
} | |
class MediaPlayer( | |
private val scope: CoroutineScope, | |
private val soundPlayer: SoundPlayer | |
) { | |
val playerErrors = MutableSharedFlow<Exception>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListAdapter(val itemClickListener : () -> Unit) { ... |
NewerOlder