diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1ec51e5..3c3c2d7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,7 +62,7 @@ jobs: :sample-compose:main-parallel:routing:assembleDebug \ :sample-compose:categories:ui:assembleDebug \ :sample-compose:login:routing:assembleDebug \ - :way-compose:assembleDebug \ + :way-compose:assembleAndroidMain \ :sample-compose:main-parallel:ui:assembleDebug \ :sample-compose:categories:routing:assembleDebug \ :sample-compose:permissions:routing:assembleDebug diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 507d337..9ba4fbe 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,7 +11,8 @@ permissions: jobs: publish-libraries: - runs-on: ubuntu-latest + # iOS targets only compile on macOS; on Linux they are skipped and the release would ship without them + runs-on: macos-latest environment: release steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index 105b815..a5b2c28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Changelog +## 0.12.0 - 2026-11-08 + +### Added + +* `way` is published for Android and iOS (`iosArm64`, `iosSimulatorArm64`, `iosX64`) next to JVM. The coordinates + stay `ru.kode:way`: Gradle metadata gives each consumer the artifact of its platform, an Android module gets + `way-android` instead of the JVM jar. +* `way-compose` is a Kotlin Multiplatform library for Android and iOS. On Android it depends on androidx Compose + 1.10.6 as before; on iOS it uses Compose Multiplatform 1.10.3. + +### Changed + +* `way-compose` is built with the Android Kotlin Multiplatform library plugin instead of `com.android.library`. + The coordinates stay `ru.kode:way-compose`. +* The javadoc jars are empty, which makes the upload to Maven Central smaller. + ## 0.11.0 - 2026-10-06 ### Added diff --git a/README.md b/README.md index ce0e0d6..f0c1ea8 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ The Compose integration (`:way-compose`) renders active nodes with `NodeHost`. ## Modules -- `:way` - Runtime library structured as a Kotlin Multiplatform project (common + JVM). **Current release targets JVM/Android only. iOS and other native targets are planned for a future release.** -- `:way-compose` - Android Compose integration for `NavigationService`. +- `:way` - Kotlin Multiplatform runtime library: JVM, Android, iOS (`iosArm64`, `iosSimulatorArm64`, `iosX64`). +- `:way-compose` - Compose integration for `NavigationService`: Android (androidx Compose) and iOS (Compose Multiplatform). - `:way-gradle-plugin` - Gradle plugin (`id("ru.kode.way")`) that generates code from `.dot` schemas. -- `:sample` - JVM sample (KMP multi-target support is not yet released). +- `:sample` - JVM sample. - `:sample-compose:*` - Android sample split into feature modules. - `:sample-compose:main-parallel:*` - tab-bar example using `ParallelFlowNode` (see [Compose Integration](#compose-integration)). - `:sample-compose:app:routing` - `google`/`huawei` product flavors with a per-flavor `.dot` extension (see [Flavor-Specific Routing](#flavor-specific-routing)). diff --git a/build.gradle.kts b/build.gradle.kts index dd86955..e1ecf19 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,6 +4,9 @@ plugins { alias(libs.plugins.kotlinMultiplatform) apply false alias(libs.plugins.kotlinCompose) apply false alias(libs.plugins.androidApplication) apply false + alias(libs.plugins.androidKmpLibrary) apply false + alias(libs.plugins.ksp) apply false + alias(libs.plugins.kotest) apply false alias(libs.plugins.kotlinJvm) apply false alias(libs.plugins.vanniktech.maven.publish) apply false } diff --git a/gradle.properties b/gradle.properties index 776ffb9..38b3d8b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ kotlin.native.ignoreDisabledTargets=true android.useAndroidX=true -versionName=0.11.0 +versionName=0.12.0 pomGroupId=ru.kode pomDescription=Navigation library based on statechart-like node graphs pomUrl=https://github.com/appKODE/way diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index cd5306d..bdeb13b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,6 +7,8 @@ kotlin = "2.3.20" spotless = "8.4.0" turbine = "1.2.1" compose = "1.10.6" +# Compose Multiplatform for iOS; its Android artifacts map to androidx compose 1.10.5, so `compose` above wins +composeMultiplatform = "1.10.3" androidExtensions = "1.13.0" agp = "9.0.1" kotlinPoet = "2.2.0" @@ -34,6 +36,9 @@ composeRuntime = { module = "androidx.compose.runtime:runtime", version.ref = "c composeMaterial = { module = "androidx.compose.material:material", version.ref = "compose" } composeAnimation = { module = "androidx.compose.animation:animation", version.ref = "compose" } composeFoundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" } +cmpUi = { module = "org.jetbrains.compose.ui:ui", version.ref = "composeMultiplatform" } +cmpAnimation = { module = "org.jetbrains.compose.animation:animation", version.ref = "composeMultiplatform" } +cmpFoundation = { module = "org.jetbrains.compose.foundation:foundation", version.ref = "composeMultiplatform" } composeUiTestJunit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose" } composeUiTestManifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "compose" } robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" } @@ -60,8 +65,10 @@ kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } kotlinCompose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } spotless = { id = "com.diffplug.spotless", version.ref = "spotless" } androidLibrary = { id = "com.android.library", version.ref = "agp" } +androidKmpLibrary = { id = "com.android.kotlin.multiplatform.library", version.ref = "agp" } androidApplication = { id = "com.android.application", version.ref = "agp" } way = { id = "ru.kode.way" } gradlePublish = { id = "com.gradle.plugin-publish", version.ref = "gradlePublishPlugin" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } +kotest = { id = "io.kotest", version.ref = "koTest" } vanniktech-maven-publish = { id = "com.vanniktech.maven.publish", version.ref = "vanniktechMavenPublish" } diff --git a/way-compose/build.gradle.kts b/way-compose/build.gradle.kts index 453b818..39d77b1 100644 --- a/way-compose/build.gradle.kts +++ b/way-compose/build.gradle.kts @@ -1,9 +1,11 @@ -import com.vanniktech.maven.publish.AndroidSingleVariantLibrary import com.vanniktech.maven.publish.JavadocJar +import com.vanniktech.maven.publish.KotlinMultiplatform +import org.jetbrains.kotlin.gradle.dsl.JvmTarget @Suppress("DSL_SCOPE_VIOLATION") plugins { - id(libs.plugins.androidLibrary.get().pluginId) + id(libs.plugins.kotlinMultiplatform.get().pluginId) + id(libs.plugins.androidKmpLibrary.get().pluginId) alias(libs.plugins.kotlinCompose) alias(libs.plugins.vanniktech.maven.publish) } @@ -11,37 +13,48 @@ plugins { group = providers.gradleProperty("pomGroupId").get() version = providers.gradleProperty("versionName").get() -android { - namespace = "ru.kode.way.compose" - - compileSdk = libs.versions.compileSdk.get().toInt() - - defaultConfig { +kotlin { + androidLibrary { + namespace = "ru.kode.way.compose" + compileSdk = libs.versions.compileSdk.get().toInt() minSdk = 21 + // JVM 11 bytecode, but no jvmToolchain(11): Robolectric host tests need the running JDK 17+ + compilerOptions { jvmTarget.set(JvmTarget.JVM_11) } - aarMetadata { - minCompileSdk = 21 + withHostTestBuilder {}.configure { + isIncludeAndroidResources = true } } - compileOptions { - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 - } - - buildFeatures { - compose = true - buildConfig = true - } - - testOptions { - unitTests.isIncludeAndroidResources = true + iosArm64() + iosSimulatorArm64() + iosX64() + + sourceSets { + commonMain.dependencies { + // Compose Multiplatform; on Android these resolve to the androidx artifacts below + api(libs.cmpUi) + api(libs.cmpAnimation) + api(libs.cmpFoundation) + implementation(project(":way")) + } + androidMain.dependencies { + api(libs.composeUi) + api(libs.composeAnimation) + api(libs.composeFoundation) + } + getByName("androidHostTest").dependencies { + implementation(libs.composeUiTestJunit4) + implementation(libs.composeUiTestManifest) + implementation(libs.robolectric) + implementation(libs.junit) + } } } mavenPublishing { coordinates(artifactId = "way-compose") - configure(AndroidSingleVariantLibrary(javadocJar = JavadocJar.Empty())) + configure(KotlinMultiplatform(javadocJar = JavadocJar.Empty())) publishToMavenCentral() signAllPublications() @@ -85,15 +98,3 @@ mavenPublishing { } } } - -dependencies { - api(libs.composeUi) - api(libs.composeAnimation) - api(libs.composeFoundation) - implementation(project(":way")) - - testImplementation(libs.composeUiTestJunit4) - testImplementation(libs.composeUiTestManifest) - testImplementation(libs.robolectric) - testImplementation(libs.junit) -} diff --git a/way-compose/src/test/kotlin/ru/kode/way/compose/EventSinkTest.kt b/way-compose/src/androidHostTest/kotlin/ru/kode/way/compose/EventSinkTest.kt similarity index 100% rename from way-compose/src/test/kotlin/ru/kode/way/compose/EventSinkTest.kt rename to way-compose/src/androidHostTest/kotlin/ru/kode/way/compose/EventSinkTest.kt diff --git a/way-compose/src/test/kotlin/ru/kode/way/compose/FocusedRegionHostTest.kt b/way-compose/src/androidHostTest/kotlin/ru/kode/way/compose/FocusedRegionHostTest.kt similarity index 100% rename from way-compose/src/test/kotlin/ru/kode/way/compose/FocusedRegionHostTest.kt rename to way-compose/src/androidHostTest/kotlin/ru/kode/way/compose/FocusedRegionHostTest.kt diff --git a/way-compose/src/test/kotlin/ru/kode/way/compose/NodeRetentionTest.kt b/way-compose/src/androidHostTest/kotlin/ru/kode/way/compose/NodeRetentionTest.kt similarity index 100% rename from way-compose/src/test/kotlin/ru/kode/way/compose/NodeRetentionTest.kt rename to way-compose/src/androidHostTest/kotlin/ru/kode/way/compose/NodeRetentionTest.kt diff --git a/way-compose/src/test/kotlin/ru/kode/way/compose/TestNavigation.kt b/way-compose/src/androidHostTest/kotlin/ru/kode/way/compose/TestNavigation.kt similarity index 100% rename from way-compose/src/test/kotlin/ru/kode/way/compose/TestNavigation.kt rename to way-compose/src/androidHostTest/kotlin/ru/kode/way/compose/TestNavigation.kt diff --git a/way-compose/src/test/resources/robolectric.properties b/way-compose/src/androidHostTest/resources/robolectric.properties similarity index 100% rename from way-compose/src/test/resources/robolectric.properties rename to way-compose/src/androidHostTest/resources/robolectric.properties diff --git a/way-compose/src/androidMain/kotlin/ru/kode/way/compose/Platform.android.kt b/way-compose/src/androidMain/kotlin/ru/kode/way/compose/Platform.android.kt new file mode 100644 index 0000000..5ea6317 --- /dev/null +++ b/way-compose/src/androidMain/kotlin/ru/kode/way/compose/Platform.android.kt @@ -0,0 +1,18 @@ +package ru.kode.way.compose + +import android.content.pm.ApplicationInfo +import android.util.Log +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +internal actual fun logWarn(tag: String, message: String) { + Log.w(tag, message) +} + +internal actual fun logDebug(tag: String, message: String) { + Log.d(tag, message) +} + +@Composable +internal actual fun isDebuggable(): Boolean = + LocalContext.current.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0 diff --git a/way-compose/src/main/kotlin/ru/kode/way/compose/ComposableNode.kt b/way-compose/src/commonMain/kotlin/ru/kode/way/compose/ComposableNode.kt similarity index 100% rename from way-compose/src/main/kotlin/ru/kode/way/compose/ComposableNode.kt rename to way-compose/src/commonMain/kotlin/ru/kode/way/compose/ComposableNode.kt diff --git a/way-compose/src/main/kotlin/ru/kode/way/compose/LocalNavigationService.kt b/way-compose/src/commonMain/kotlin/ru/kode/way/compose/LocalNavigationService.kt similarity index 100% rename from way-compose/src/main/kotlin/ru/kode/way/compose/LocalNavigationService.kt rename to way-compose/src/commonMain/kotlin/ru/kode/way/compose/LocalNavigationService.kt diff --git a/way-compose/src/main/kotlin/ru/kode/way/compose/NodeHost.kt b/way-compose/src/commonMain/kotlin/ru/kode/way/compose/NodeHost.kt similarity index 98% rename from way-compose/src/main/kotlin/ru/kode/way/compose/NodeHost.kt rename to way-compose/src/commonMain/kotlin/ru/kode/way/compose/NodeHost.kt index 3704c64..445ecc2 100644 --- a/way-compose/src/main/kotlin/ru/kode/way/compose/NodeHost.kt +++ b/way-compose/src/commonMain/kotlin/ru/kode/way/compose/NodeHost.kt @@ -1,7 +1,5 @@ package ru.kode.way.compose -import android.content.pm.ApplicationInfo -import android.util.Log import androidx.compose.animation.AnimatedContent import androidx.compose.animation.AnimatedContentTransitionScope import androidx.compose.animation.AnimatedContentTransitionScope.SlideDirection @@ -29,7 +27,6 @@ import androidx.compose.runtime.saveable.SaveableStateHolder import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveableStateHolder import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext import ru.kode.way.FlowTransition import ru.kode.way.NavigationService import ru.kode.way.NavigationState @@ -90,7 +87,7 @@ private fun FirstRegionFallback( root: NodeWithPath?, ) { if (root != null) { - Log.w( + logWarn( LOG_TAG, "parallel-flow root \"${root.path}\" is not a ComposableNode; only the first region will render — " + "implement ComposableNode on it and call NodeHost(regionId) per sub-region in its Content()", @@ -193,13 +190,13 @@ private fun NodeAnimatedContent( } else { if (path != null) { if (node is ParallelFlowNode<*>) { - Log.w( + logWarn( LOG_TAG, "parallel-flow node at \"$path\" is not a ComposableNode; rendering an empty content — " + "implement ComposableNode on it and call NodeHost(regionId) per sub-region in its Content()", ) } else { - Log.d(LOG_TAG, "didn't find a ComposableNode for \"$path\", rendering an empty content") + logDebug(LOG_TAG, "didn't find a ComposableNode for \"$path\", rendering an empty content") } } Box {} @@ -277,7 +274,7 @@ fun NodeHost(nodeBuilder: NodeBuilder, onFinishRequest: (R) -> FlowTra // nodeBuilder only, so a new lambda passed on recomposition (the common case for an inline // lambda) would otherwise be ignored and the stale callback kept forever. val currentOnFinishRequest by rememberUpdatedState(onFinishRequest) - val isDebuggable = LocalContext.current.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0 + val isDebuggable = isDebuggable() val service = remember(nodeBuilder) { NavigationService(nodeBuilder) { result: R -> currentOnFinishRequest(result) } // debug builds throw on a dropped event so the bug surfaces, release builds drop it silently diff --git a/way-compose/src/commonMain/kotlin/ru/kode/way/compose/Platform.kt b/way-compose/src/commonMain/kotlin/ru/kode/way/compose/Platform.kt new file mode 100644 index 0000000..6b65080 --- /dev/null +++ b/way-compose/src/commonMain/kotlin/ru/kode/way/compose/Platform.kt @@ -0,0 +1,10 @@ +package ru.kode.way.compose + +import androidx.compose.runtime.Composable + +internal expect fun logWarn(tag: String, message: String) + +internal expect fun logDebug(tag: String, message: String) + +@Composable +internal expect fun isDebuggable(): Boolean diff --git a/way-compose/src/main/kotlin/ru/kode/way/compose/TransitionSpecs.kt b/way-compose/src/commonMain/kotlin/ru/kode/way/compose/TransitionSpecs.kt similarity index 100% rename from way-compose/src/main/kotlin/ru/kode/way/compose/TransitionSpecs.kt rename to way-compose/src/commonMain/kotlin/ru/kode/way/compose/TransitionSpecs.kt diff --git a/way-compose/src/iosMain/kotlin/ru/kode/way/compose/Platform.ios.kt b/way-compose/src/iosMain/kotlin/ru/kode/way/compose/Platform.ios.kt new file mode 100644 index 0000000..910a928 --- /dev/null +++ b/way-compose/src/iosMain/kotlin/ru/kode/way/compose/Platform.ios.kt @@ -0,0 +1,16 @@ +package ru.kode.way.compose + +import androidx.compose.runtime.Composable +import kotlin.experimental.ExperimentalNativeApi + +internal actual fun logWarn(tag: String, message: String) { + println("W/$tag: $message") +} + +internal actual fun logDebug(tag: String, message: String) { + println("D/$tag: $message") +} + +@OptIn(ExperimentalNativeApi::class) +@Composable +internal actual fun isDebuggable(): Boolean = Platform.isDebugBinary diff --git a/way-compose/src/main/AndroidManifest.xml b/way-compose/src/main/AndroidManifest.xml deleted file mode 100644 index 8bdb7e1..0000000 --- a/way-compose/src/main/AndroidManifest.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/way-gradle-plugin/build.gradle.kts b/way-gradle-plugin/build.gradle.kts index 74704c4..b3d2e8e 100644 --- a/way-gradle-plugin/build.gradle.kts +++ b/way-gradle-plugin/build.gradle.kts @@ -39,7 +39,7 @@ gradlePlugin { implementationClass = "ru.kode.way.gradle.WayPlugin" displayName = "Code generation gradle plugin for Way navigation library" description = "Code generation gradle plugin for Way navigation library" - tags = listOf("navigation", "codegen", "android", "kmm", "compose", "compose-desktop") + tags = listOf("navigation", "codegen", "android", "ios", "kmp", "compose", "compose-multiplatform") } } } diff --git a/way/build.gradle.kts b/way/build.gradle.kts index abba7b3..fd0ba5e 100644 --- a/way/build.gradle.kts +++ b/way/build.gradle.kts @@ -1,6 +1,10 @@ @Suppress("DSL_SCOPE_VIOLATION") plugins { id(libs.plugins.kotlinMultiplatform.get().pluginId) + id(libs.plugins.androidKmpLibrary.get().pluginId) + // KSP before Kotest: the Kotest plugin needs it to run specs on native + id(libs.plugins.ksp.get().pluginId) + id(libs.plugins.kotest.get().pluginId) alias(libs.plugins.way) alias(libs.plugins.vanniktech.maven.publish) } @@ -17,6 +21,16 @@ kotlin { } } + androidLibrary { + namespace = "ru.kode.way" + compileSdk = libs.versions.compileSdk.get().toInt() + minSdk = 21 + } + + iosArm64() + iosSimulatorArm64() + iosX64() + sourceSets { val commonMain by getting { dependencies { @@ -29,8 +43,6 @@ kotlin { implementation(libs.turbine) } } - val jvmMain by getting { - } val jvmTest by getting { dependencies { implementation(libs.bundles.koTestJvm) diff --git a/way/src/commonTest/kotlin/ru/kode/way/Charset.kt b/way/src/commonTest/kotlin/ru/kode/way/Charset.kt new file mode 100644 index 0000000..44f5ee7 --- /dev/null +++ b/way/src/commonTest/kotlin/ru/kode/way/Charset.kt @@ -0,0 +1,4 @@ +package ru.kode.way + +// Stand-in for java.nio.charset.Charset so the nav-service12 schema compiles on every target +enum class Charset { UTF_8, UTF_32 } diff --git a/way/src/commonTest/kotlin/ru/kode/way/DisposeLifecycleTest.kt b/way/src/commonTest/kotlin/ru/kode/way/DisposeLifecycleTest.kt index 5afa993..a350762 100644 --- a/way/src/commonTest/kotlin/ru/kode/way/DisposeLifecycleTest.kt +++ b/way/src/commonTest/kotlin/ru/kode/way/DisposeLifecycleTest.kt @@ -330,91 +330,6 @@ class DisposeLifecycleTest : (thrown is IllegalStateException) shouldBe true } - should("addTransitionListener after dispose() does not register the listener (no leak)") { - // The leak fix asserts that addTransitionListener returns early when isDisposed, - // so the listener instance is not retained in the internal `listeners` ArrayList. - // Without the fix the listener would stay in the list forever and never be invoked - // (send is a no-op after dispose), leaking the closure and everything it captures. - // - // Assert via reflection on the private `listeners` field so the test fails when the - // listener IS appended post-dispose, even though send post-dispose hides the bug - // through any observable callback channel. - val sut = NavigationService( - TestNodeBuilder( - NavService01Schema(), - mapOf( - "app" to TestFlowNode(initialTarget = Target.app01.intro), - "app.intro" to TestScreenNode(), - ), - ), - onFinishRequest = { _: Int -> Stay }, - ) - sut.start() - sut.dispose() - - val leakyListener: (NavigationState) -> Unit = { _ -> } - sut.addTransitionListener(leakyListener) - - val listenersField = NavigationService::class.java.getDeclaredField("listeners").apply { isAccessible = true } - - @Suppress("UNCHECKED_CAST") - val internalListeners = listenersField.get(sut) as List<(NavigationState) -> Unit> - internalListeners shouldBe emptyList() - } - - should("addServiceExtensionPoint after dispose() does not register the extension point (no leak)") { - val sut = NavigationService( - TestNodeBuilder( - NavService01Schema(), - mapOf( - "app" to TestFlowNode(initialTarget = Target.app01.intro), - "app.intro" to TestScreenNode(), - ), - ), - onFinishRequest = { _: Int -> Stay }, - ) - sut.start() - sut.dispose() - - val leakyPoint = TestServiceExtensionPoint( - preTransition = { _, _, _ -> }, - postTransition = { _, _, _ -> }, - ) - sut.addServiceExtensionPoint(leakyPoint) - - val field = NavigationService::class.java.getDeclaredField("serviceExtensionPoints") - .apply { isAccessible = true } - - @Suppress("UNCHECKED_CAST") - val internal = field.get(sut) as List> - internal shouldBe emptyList() - } - - should("addNodeExtensionPoint after dispose() does not register the extension point (no leak)") { - val sut = NavigationService( - TestNodeBuilder( - NavService01Schema(), - mapOf( - "app" to TestFlowNode(initialTarget = Target.app01.intro), - "app.intro" to TestScreenNode(), - ), - ), - onFinishRequest = { _: Int -> Stay }, - ) - sut.start() - sut.dispose() - - val leakyPoint = TestNodeExtensionPoint() - sut.addNodeExtensionPoint(leakyPoint) - - // State's _nodeExtensionPoints was cleared by dispose(); a guarded add must NOT - // re-populate it from a disposed state. - val stateField = NavigationService::class.java.getDeclaredField("state") - .apply { isAccessible = true } - val navState = stateField.get(sut) as NavigationState - navState._nodeExtensionPoints shouldBe emptyList() - } - should("removeTransitionListener after dispose() is a safe no-op") { val sut = NavigationService( TestNodeBuilder( diff --git a/way/src/commonTest/kotlin/ru/kode/way/NavigationServiceTest.kt b/way/src/commonTest/kotlin/ru/kode/way/NavigationServiceTest.kt index 0772018..9b919b3 100644 --- a/way/src/commonTest/kotlin/ru/kode/way/NavigationServiceTest.kt +++ b/way/src/commonTest/kotlin/ru/kode/way/NavigationServiceTest.kt @@ -35,7 +35,6 @@ import ru.kode.way.par01.top.par01Top import ru.kode.way.par03.alpha.par03Alpha import ru.kode.way.par03.beta.par03Beta import ru.kode.way.par03.par03App -import java.nio.charset.Charset import ru.kode.way.nav01.app as app01 import ru.kode.way.nav02.AppChildFinishRequest as Nav02AppChildFinishRequest import ru.kode.way.nav02.app as app02 @@ -798,10 +797,10 @@ class NavigationServiceTest : val nodeBuilder = AppNodeBuilder( object : AppNodeBuilder.Factory { override fun createRootNode(timeout: Int) = TestFlowNode( - initialTarget = Target.app12.page1(Charsets.UTF_32), + initialTarget = Target.app12.page1(Charset.UTF_32), payload = timeout, transitions = listOf( - tr("A", Target.app12.login(charset = Charsets.UTF_32, defaultUserName = "Dima")), + tr("A", Target.app12.login(charset = Charset.UTF_32, defaultUserName = "Dima")), tr(Stay), ), ) @@ -838,7 +837,7 @@ class NavigationServiceTest : // root flow node should receive an argument (aliveNodes["app"] as TestFlowNode?)?.payload shouldBe 42 // initial node of root flow should receive an argument - (aliveNodes["app.page1"] as TestScreenNode?)?.payload shouldBe Charsets.UTF_32 + (aliveNodes["app.page1"] as TestScreenNode?)?.payload shouldBe Charset.UTF_32 } sut.send(TestEvent("A")) @@ -863,10 +862,10 @@ class NavigationServiceTest : val nodeBuilder = AppNodeBuilder( object : AppNodeBuilder.Factory { override fun createRootNode(timeout: Int) = TestFlowNode( - initialTarget = Target.app12.page1(Charsets.UTF_32), + initialTarget = Target.app12.page1(Charset.UTF_32), payload = timeout, transitions = listOf( - tr("L", Target.app12.login(charset = Charsets.UTF_8, defaultUserName = "Dima")), + tr("L", Target.app12.login(charset = Charset.UTF_8, defaultUserName = "Dima")), tr("P2", Target.app12.page2), tr(Stay), ), @@ -900,7 +899,7 @@ class NavigationServiceTest : sut.send(TestEvent("L")) awaitItem().apply { active shouldBe "app.page1.login.credentials" - (aliveNodes["app.page1"] as TestScreenNode?)?.payload shouldBe Charsets.UTF_32 + (aliveNodes["app.page1"] as TestScreenNode?)?.payload shouldBe Charset.UTF_32 } // page1 is pruned together with its stored payload @@ -911,7 +910,7 @@ class NavigationServiceTest : sut.send(TestEvent("L")) awaitItem().apply { active shouldBe "app.page1.login.credentials" - (aliveNodes["app.page1"] as TestScreenNode?)?.payload shouldBe Charsets.UTF_8 + (aliveNodes["app.page1"] as TestScreenNode?)?.payload shouldBe Charset.UTF_8 } } } @@ -921,14 +920,14 @@ class NavigationServiceTest : val nodeBuilder = AppNodeBuilder( object : AppNodeBuilder.Factory { override fun createRootNode(timeout: Int) = TestFlowNode( - initialTarget = Target.app12.page1(Charsets.UTF_32), + initialTarget = Target.app12.page1(Charset.UTF_32), payload = timeout, transitions = listOf( tr("P2", Target.app12.page2), tr( "ABS", NavigateTo( - AbsoluteTarget(rootSegment, Target.app12.login(charset = Charsets.UTF_8, defaultUserName = "Dima")), + AbsoluteTarget(rootSegment, Target.app12.login(charset = Charset.UTF_8, defaultUserName = "Dima")), ), ), tr(Stay), @@ -965,7 +964,7 @@ class NavigationServiceTest : sut.send(TestEvent("ABS")) awaitItem().apply { active shouldBe "app.page1.login.credentials.otp" - (aliveNodes["app.page1"] as TestScreenNode?)?.payload shouldBe Charsets.UTF_8 + (aliveNodes["app.page1"] as TestScreenNode?)?.payload shouldBe Charset.UTF_8 (aliveNodes["app.page1.login.credentials"] as TestScreenNode?)?.payload shouldBe "800" } } @@ -1079,10 +1078,10 @@ class NavigationServiceTest : val nodeBuilder = AppNodeBuilder( object : AppNodeBuilder.Factory { override fun createRootNode(timeout: Int) = TestFlowNode( - initialTarget = Target.app12.page1(Charsets.UTF_32), + initialTarget = Target.app12.page1(Charset.UTF_32), payload = timeout, transitions = listOf( - tr("A", Target.app12.login(charset = Charsets.UTF_32, defaultUserName = "Dima")), + tr("A", Target.app12.login(charset = Charset.UTF_32, defaultUserName = "Dima")), tr(Stay), ), ) @@ -1427,7 +1426,7 @@ class NavigationServiceTest : } should("correctly transition when given an absolute target") { - val loginFlowTarget = Target.app12.login(charset = Charsets.UTF_32, defaultUserName = "Gregoriy") + val loginFlowTarget = Target.app12.login(charset = Charset.UTF_32, defaultUserName = "Gregoriy") val loginCredentialsTarget = Target.login12.credentials(defaultPhone = "800") val loginOtpTarget = Target.login12.otp(defaultPhone = "+7981123456", useAnimation = true) val rootSegment = NavService12Schema(NavService12LoginSchema()).rootSegment @@ -1448,7 +1447,7 @@ class NavigationServiceTest : val nodeBuilder = AppNodeBuilder( object : AppNodeBuilder.Factory { override fun createRootNode(timeout: Int) = TestFlowNode( - initialTarget = Target.app12.page1(Charsets.UTF_32), + initialTarget = Target.app12.page1(Charset.UTF_32), payload = timeout, transitions = listOf( tr("A", absoluteTarget), diff --git a/way/src/commonTest/kotlin/ru/kode/way/NodeEventSinkTest.kt b/way/src/commonTest/kotlin/ru/kode/way/NodeEventSinkTest.kt index 05703b4..3a6a786 100644 --- a/way/src/commonTest/kotlin/ru/kode/way/NodeEventSinkTest.kt +++ b/way/src/commonTest/kotlin/ru/kode/way/NodeEventSinkTest.kt @@ -30,7 +30,6 @@ import ru.kode.way.parlazyint.inner.leftTab import ru.kode.way.parlazyint.inner.rightTab import ru.kode.way.parlazyint.outerApp import ru.kode.way.partoproot.ParallelTestTopRootSchema -import java.nio.charset.Charset import ru.kode.way.nav05.app as app05 import ru.kode.way.nav12.AppNodeBuilder as Nav12AppNodeBuilder import ru.kode.way.nav12.LoginNodeBuilder as Nav12LoginNodeBuilder @@ -391,7 +390,7 @@ class NodeEventSinkTest : Nav12AppNodeBuilder( object : Nav12AppNodeBuilder.Factory { override fun createRootNode(timeout: Int) = TestFlowNode( - initialTarget = Target.app12.page1(Charsets.UTF_8), + initialTarget = Target.app12.page1(Charset.UTF_8), transitions = listOf( tr("L", Target.app12.login(defaultUserName = "alice")), tr("P2", Target.app12.page2), diff --git a/way/src/commonTest/kotlin/ru/kode/way/SchemaCompositionTest.kt b/way/src/commonTest/kotlin/ru/kode/way/SchemaCompositionTest.kt index b5e3ace..4666c1b 100644 --- a/way/src/commonTest/kotlin/ru/kode/way/SchemaCompositionTest.kt +++ b/way/src/commonTest/kotlin/ru/kode/way/SchemaCompositionTest.kt @@ -18,7 +18,6 @@ import ru.kode.way.nav12.AppNodeBuilder import ru.kode.way.nav12.LoginNodeBuilder import ru.kode.way.nav12.NavService12LoginSchema import ru.kode.way.nav12.NavService12Schema -import java.nio.charset.Charset import ru.kode.way.nav12.AppChildFinishRequest as Nav12AppChildFinishRequest import ru.kode.way.nav12.app as app12 import ru.kode.way.nav12.login as login12 @@ -96,7 +95,7 @@ class SchemaCompositionTest : ShouldSpec() { val nodeBuilder = AppNodeBuilder( object : AppNodeBuilder.Factory { override fun createRootNode(timeout: Int) = TestFlowNode( - initialTarget = Target.app12.page1(Charsets.UTF_32), + initialTarget = Target.app12.page1(Charset.UTF_32), payload = timeout, transitions = listOf( tr("C", loginWithoutPayload), @@ -204,11 +203,11 @@ class SchemaCompositionTest : ShouldSpec() { val nodeBuilder = AppNodeBuilder( object : AppNodeBuilder.Factory { override fun createRootNode(timeout: Int) = TestFlowNode( - initialTarget = Target.app12.page1(Charsets.UTF_32), + initialTarget = Target.app12.page1(Charset.UTF_32), transitions = listOf( tr("L", Target.app12.login(defaultUserName = "alice")), tr("NL", FlowTarget(Target.app12.login(defaultUserName = "unused").path)), - tr("P1", Target.app12.page1(Charsets.UTF_8)), + tr("P1", Target.app12.page1(Charset.UTF_8)), tr("P2", Target.app12.page2), tr("H", HistoryTarget(loginPath, deep = true)), tr("S", HistoryTarget(loginPath, deep = false)), @@ -273,7 +272,7 @@ class SchemaCompositionTest : ShouldSpec() { f.dropped.shouldBeEmpty() f.states.last().active shouldBe "app.page1.login.credentials.otp" - f.payloadOf("app.page1") shouldBe Charsets.UTF_32 + f.payloadOf("app.page1") shouldBe Charset.UTF_32 (f.states.last().aliveNodes["app.page1.login"] as TestFlowNode).payload shouldBe "alice" f.payloadOf("app.page1.login.credentials") shouldBe "+700" f.payloadOf("app.page1.login.credentials.otp") shouldBe true @@ -297,12 +296,12 @@ class SchemaCompositionTest : ShouldSpec() { f.sut.start(42) f.openOtpAndLeave() f.sut.send(TestEvent("P1")) - f.payloadOf("app.page1") shouldBe Charsets.UTF_8 + f.payloadOf("app.page1") shouldBe Charset.UTF_8 f.sut.send(TestEvent("H")) f.states.last().active shouldBe "app.page1.login.credentials.otp" - f.payloadOf("app.page1") shouldBe Charsets.UTF_8 + f.payloadOf("app.page1") shouldBe Charset.UTF_8 f.payloadOf("app.page1.login.credentials") shouldBe "+700" } diff --git a/way/src/commonTest/way/nav-service12-app.dot b/way/src/commonTest/way/nav-service12-app.dot index 676564c..e5f9f41 100644 --- a/way/src/commonTest/way/nav-service12-app.dot +++ b/way/src/commonTest/way/nav-service12-app.dot @@ -14,7 +14,7 @@ digraph NavService12 { resultType = "kotlin.Int", ] - page1 [parameterName = "charset", parameterType = "java.nio.charset.Charset"] + page1 [parameterName = "charset", parameterType = "ru.kode.way.Charset"] app -> page1 -> login app -> page2 diff --git a/way/src/jvmTest/kotlin/ru/kode/way/DisposeLifecycleReflectionTest.kt b/way/src/jvmTest/kotlin/ru/kode/way/DisposeLifecycleReflectionTest.kt new file mode 100644 index 0000000..97b8a0a --- /dev/null +++ b/way/src/jvmTest/kotlin/ru/kode/way/DisposeLifecycleReflectionTest.kt @@ -0,0 +1,95 @@ +package ru.kode.way + +import io.kotest.core.spec.style.ShouldSpec +import io.kotest.matchers.shouldBe +import ru.kode.way.nav01.NavService01Schema +import ru.kode.way.nav01.app as app01 + +// Reads NavigationService internals via JVM reflection, so it can't live in commonTest +class DisposeLifecycleReflectionTest : + ShouldSpec({ + should("addTransitionListener after dispose() does not register the listener (no leak)") { + // The leak fix asserts that addTransitionListener returns early when isDisposed, + // so the listener instance is not retained in the internal `listeners` ArrayList. + // Without the fix the listener would stay in the list forever and never be invoked + // (send is a no-op after dispose), leaking the closure and everything it captures. + // + // Assert via reflection on the private `listeners` field so the test fails when the + // listener IS appended post-dispose, even though send post-dispose hides the bug + // through any observable callback channel. + val sut = NavigationService( + TestNodeBuilder( + NavService01Schema(), + mapOf( + "app" to TestFlowNode(initialTarget = Target.app01.intro), + "app.intro" to TestScreenNode(), + ), + ), + onFinishRequest = { _: Int -> Stay }, + ) + sut.start() + sut.dispose() + + val leakyListener: (NavigationState) -> Unit = { _ -> } + sut.addTransitionListener(leakyListener) + + val listenersField = NavigationService::class.java.getDeclaredField("listeners").apply { isAccessible = true } + + @Suppress("UNCHECKED_CAST") + val internalListeners = listenersField.get(sut) as List<(NavigationState) -> Unit> + internalListeners shouldBe emptyList() + } + + should("addServiceExtensionPoint after dispose() does not register the extension point (no leak)") { + val sut = NavigationService( + TestNodeBuilder( + NavService01Schema(), + mapOf( + "app" to TestFlowNode(initialTarget = Target.app01.intro), + "app.intro" to TestScreenNode(), + ), + ), + onFinishRequest = { _: Int -> Stay }, + ) + sut.start() + sut.dispose() + + val leakyPoint = TestServiceExtensionPoint( + preTransition = { _, _, _ -> }, + postTransition = { _, _, _ -> }, + ) + sut.addServiceExtensionPoint(leakyPoint) + + val field = NavigationService::class.java.getDeclaredField("serviceExtensionPoints") + .apply { isAccessible = true } + + @Suppress("UNCHECKED_CAST") + val internal = field.get(sut) as List> + internal shouldBe emptyList() + } + + should("addNodeExtensionPoint after dispose() does not register the extension point (no leak)") { + val sut = NavigationService( + TestNodeBuilder( + NavService01Schema(), + mapOf( + "app" to TestFlowNode(initialTarget = Target.app01.intro), + "app.intro" to TestScreenNode(), + ), + ), + onFinishRequest = { _: Int -> Stay }, + ) + sut.start() + sut.dispose() + + val leakyPoint = TestNodeExtensionPoint() + sut.addNodeExtensionPoint(leakyPoint) + + // State's _nodeExtensionPoints was cleared by dispose(); a guarded add must NOT + // re-populate it from a disposed state. + val stateField = NavigationService::class.java.getDeclaredField("state") + .apply { isAccessible = true } + val navState = stateField.get(sut) as NavigationState + navState._nodeExtensionPoints shouldBe emptyList() + } + })