From b9cf4129fcd061cfda56fa4aa644c05f952f9220 Mon Sep 17 00:00:00 2001 From: Tlaster Date: Mon, 26 Jun 2023 13:25:40 +0800 Subject: [PATCH] add BackDispatcherTest --- buildSrc/src/main/kotlin/Versions.kt | 2 +- precompose/build.gradle.kts | 1 + .../tlaster/precompose/ui/BackPressAdapter.kt | 3 +- .../navigation/BackDispatcherTest.kt | 47 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 precompose/src/commonTest/kotlin/moe/tlaster/precompose/navigation/BackDispatcherTest.kt diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index c6d7f311..837bcc6d 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -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 { diff --git a/precompose/build.gradle.kts b/precompose/build.gradle.kts index 65729c5d..4fe81294 100644 --- a/precompose/build.gradle.kts +++ b/precompose/build.gradle.kts @@ -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) } diff --git a/precompose/src/commonMain/kotlin/moe/tlaster/precompose/ui/BackPressAdapter.kt b/precompose/src/commonMain/kotlin/moe/tlaster/precompose/ui/BackPressAdapter.kt index d3173f27..fcf9a750 100644 --- a/precompose/src/commonMain/kotlin/moe/tlaster/precompose/ui/BackPressAdapter.kt +++ b/precompose/src/commonMain/kotlin/moe/tlaster/precompose/ui/BackPressAdapter.kt @@ -12,7 +12,8 @@ interface BackDispatcherOwner { } class BackDispatcher { - private val handlers = arrayListOf() + // internal for testing + internal val handlers = arrayListOf() fun onBackPress() { handlers.lastOrNull { diff --git a/precompose/src/commonTest/kotlin/moe/tlaster/precompose/navigation/BackDispatcherTest.kt b/precompose/src/commonTest/kotlin/moe/tlaster/precompose/navigation/BackDispatcherTest.kt new file mode 100644 index 00000000..bbadb426 --- /dev/null +++ b/precompose/src/commonTest/kotlin/moe/tlaster/precompose/navigation/BackDispatcherTest.kt @@ -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()) + } +}