Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LifecycleOwner in tests #1294

Open
wants to merge 1 commit into
base: jb-main
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions compose/ui/ui-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,17 @@ if (AndroidXComposePlugin.isMultiplatformEnabled(project)) {
implementation(libs.kotlinTest)
}

skikoMain {
dependsOn(commonMain)
skikoMain.dependencies {
implementation(project(":lifecycle:lifecycle-common"))
implementation(project(":lifecycle:lifecycle-runtime"))
Comment on lines +151 to +152
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be in common?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't used in Android or commonMain, so we should keep it here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not required in common, as I introduced changes in skikoMain/skikoTest only, so I decided to include dependencies only for these source roots.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see some common API at some point, but it requires changes in AOSP first

}

skikoTest.dependencies {
implementation(libs.kotlinTest)
implementation(project(":lifecycle:lifecycle-runtime-compose"))
}

skikoMain.dependsOn(commonMain)
desktopMain.dependsOn(skikoMain)
jsNativeMain.dependsOn(skikoMain)
jsMain.dependsOn(jsNativeMain)
Expand All @@ -168,6 +175,15 @@ if (AndroidXComposePlugin.isMultiplatformEnabled(project)) {
androidCommonTest.dependsOn(commonTest)
androidTest.dependsOn(androidCommonTest)
androidAndroidTest.dependsOn(androidCommonTest)

desktopTest {
dependsOn(skikoTest)
dependencies {
implementation(libs.skikoCurrentOs)
}
}
nativeTest.dependsOn(skikoTest)
wasmJsTest.dependsOn(skikoTest)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package androidx.compose.ui.test

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.InternalComposeUiApi
Expand All @@ -26,6 +27,7 @@ import androidx.compose.ui.graphics.asComposeCanvas
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.node.RootForTest
import androidx.compose.ui.platform.InfiniteAnimationPolicy
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.PlatformContext
import androidx.compose.ui.platform.WindowInfo
import androidx.compose.ui.scene.ComposeScene
Expand All @@ -39,6 +41,9 @@ import androidx.compose.ui.text.input.PlatformTextInputService
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntSize
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.cancellation.CancellationException
Expand Down Expand Up @@ -318,11 +323,12 @@ class SkikoComposeUiTest @InternalTestApi constructor(
}

override fun setContent(composable: @Composable () -> Unit) {
val content = ProvideTestCompositionLocals(composable)
if (isOnUiThread()) {
scene.setContent(content = composable)
scene.setContent(content = content)
} else {
runOnUiThread {
scene.setContent(content = composable)
scene.setContent(content = content)
}

// Only wait for idleness if not on the UI thread. If we are on the UI thread, the
Expand Down Expand Up @@ -429,6 +435,19 @@ class SkikoComposeUiTest @InternalTestApi constructor(
private inner class TestComposeSceneContext : ComposeSceneContext {
override val platformContext = TestContext()
}

private val lifecycleOwner = TestLifecycleOwner()

override fun sendLifecycleEvent(event: Lifecycle.Event) {
lifecycleOwner.lifecycle.handleLifecycleEvent(event)
}

private fun ProvideTestCompositionLocals(composable: @Composable () -> Unit): @Composable () -> Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private fun ProvideTestCompositionLocals(composable: @Composable () -> Unit): @Composable () -> Unit = {
@Composable
private fun ProvideTestCompositionLocals(content: @Composable () -> Unit) {

CompositionLocalProvider(
LocalLifecycleOwner provides lifecycleOwner,
content = composable
)
}
}

@ExperimentalTestApi
Expand All @@ -443,4 +462,9 @@ actual sealed interface ComposeUiTest : SemanticsNodeInteractionsProvider {
actual fun registerIdlingResource(idlingResource: IdlingResource)
actual fun unregisterIdlingResource(idlingResource: IdlingResource)
actual fun setContent(composable: @Composable () -> Unit)
fun sendLifecycleEvent(event: Lifecycle.Event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KDoc is required for all public APIs

}

private class TestLifecycleOwner : LifecycleOwner {
override val lifecycle = LifecycleRegistry(this)
}
53 changes: 53 additions & 0 deletions compose/ui/ui-test/src/skikoTest/kotlin/LifecycleInTestsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui.test

import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@OptIn(ExperimentalTestApi::class)
class LifecycleInTestsTest {
@Test
fun lifecycleInComposeTest() = runComposeUiTest {
var onCreatedEffectCalled = false
var onResumeEffectCalled = false

setContent {
LifecycleEventEffect(Lifecycle.Event.ON_CREATE) {
onCreatedEffectCalled = true
}
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
onResumeEffectCalled = true
}
}

assertFalse(onCreatedEffectCalled)
assertFalse(onResumeEffectCalled)

sendLifecycleEvent(Lifecycle.Event.ON_START)
assertTrue(onCreatedEffectCalled)
assertFalse(onResumeEffectCalled)

sendLifecycleEvent(Lifecycle.Event.ON_RESUME)
assertTrue(onCreatedEffectCalled)
assertTrue(onResumeEffectCalled)
}
}
Loading