Skip to content

Commit

Permalink
add BackDispatcherTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Jun 26, 2023
1 parent f4524b5 commit b9cf412
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Versions {

object Kotlin {
const val lang = "1.8.20"
const val coroutines = "1.6.4"
const val coroutines = "1.7.1"
}

object Java {
Expand Down
1 change: 1 addition & 0 deletions precompose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ kotlin {
api(compose.animation)
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.Kotlin.coroutines}")
// @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
// implementation(compose.uiTestJUnit4)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface BackDispatcherOwner {
}

class BackDispatcher {
private val handlers = arrayListOf<BackHandler>()
// internal for testing
internal val handlers = arrayListOf<BackHandler>()

fun onBackPress() {
handlers.lastOrNull {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package moe.tlaster.precompose.navigation

import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import moe.tlaster.precompose.ui.BackDispatcher
import moe.tlaster.precompose.ui.BackHandler
import kotlin.test.Test
import kotlin.test.assertEquals

class BackDispatcherTest {

@Test
fun onBackPress_should_call_handleBackPress_on_the_last_enabled_handler() {
val dispatcher = BackDispatcher()
val handler1 = object : BackHandler {
override val isEnabled = true
override fun handleBackPress() {}
}
val handler2 = object : BackHandler {
override val isEnabled = true
override fun handleBackPress() {}
}
dispatcher.register(handler1)
dispatcher.register(handler2)

dispatcher.onBackPress()

assertEquals(handler2, dispatcher.handlers.last())
}

@Test
fun canHandleBackPress_should_return_true_if_any_handler_is_enabled() = runTest {
val dispatcher = BackDispatcher()
val handler1 = object : BackHandler {
override val isEnabled = true
override fun handleBackPress() {}
}
val handler2 = object : BackHandler {
override val isEnabled = true
override fun handleBackPress() {}
}
dispatcher.register(handler1)
dispatcher.register(handler2)

assertEquals(true, dispatcher.canHandleBackPress.first())
}
}

0 comments on commit b9cf412

Please sign in to comment.