|
| 1 | +package com.squareup.workflow1.ui.compose |
| 2 | + |
| 3 | +import androidx.compose.runtime.CompositionLocalProvider |
| 4 | +import androidx.compose.runtime.LaunchedEffect |
| 5 | +import androidx.compose.runtime.getValue |
| 6 | +import androidx.compose.runtime.mutableStateOf |
| 7 | +import androidx.compose.runtime.remember |
| 8 | +import androidx.compose.runtime.setValue |
| 9 | +import androidx.compose.ui.platform.LocalLifecycleOwner |
| 10 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 11 | +import androidx.lifecycle.Lifecycle |
| 12 | +import androidx.lifecycle.Lifecycle.State.CREATED |
| 13 | +import androidx.lifecycle.Lifecycle.State.RESUMED |
| 14 | +import androidx.lifecycle.LifecycleOwner |
| 15 | +import androidx.lifecycle.LifecycleRegistry |
| 16 | +import com.google.common.truth.Truth.assertThat |
| 17 | +import org.junit.Rule |
| 18 | +import org.junit.Test |
| 19 | + |
| 20 | +class ComposeLifecycleOwnerTest { |
| 21 | + |
| 22 | + @get:Rule |
| 23 | + val composeTestRule = createComposeRule() |
| 24 | + |
| 25 | + private var mParentLifecycle: LifecycleRegistry? = null |
| 26 | + |
| 27 | + @Test |
| 28 | + fun childLifecycleOwner_initialStateIsResumedWhenParentIsResumed() { |
| 29 | + val parentLifecycle = ensureParentLifecycle() |
| 30 | + |
| 31 | + lateinit var childLifecycleOwner: LifecycleOwner |
| 32 | + composeTestRule.setContent { |
| 33 | + parentLifecycle.currentState = RESUMED |
| 34 | + childLifecycleOwner = rememberChildLifecycleOwner(parentLifecycle) |
| 35 | + CompositionLocalProvider(LocalLifecycleOwner provides childLifecycleOwner) { |
| 36 | + // let's assert right away as things are composing, because we want to ensure that |
| 37 | + // the lifecycle is in the correct state as soon as possible & not just after composition |
| 38 | + // has finished |
| 39 | + assertThat(childLifecycleOwner.lifecycle.currentState).isEqualTo(Lifecycle.State.RESUMED) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Allow the composition to complete |
| 44 | + composeTestRule.waitForIdle() |
| 45 | + |
| 46 | + // Outside the composition, assert the lifecycle state again |
| 47 | + assertThat(childLifecycleOwner.lifecycle.currentState) |
| 48 | + .isEqualTo(Lifecycle.State.RESUMED) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun childLifecycleOwner_initialStateIsResumedAfterParentResumed() { |
| 53 | + val parentLifecycle = ensureParentLifecycle() |
| 54 | + |
| 55 | + lateinit var childLifecycleOwner: LifecycleOwner |
| 56 | + composeTestRule.setContent { |
| 57 | + childLifecycleOwner = rememberChildLifecycleOwner(parentLifecycle) |
| 58 | + parentLifecycle.currentState = CREATED |
| 59 | + CompositionLocalProvider(LocalLifecycleOwner provides childLifecycleOwner) { |
| 60 | + // let's assert right away as things are composing, because we want to ensure that |
| 61 | + // the lifecycle is in the correct state as soon as possible & not just after composition |
| 62 | + // has finished |
| 63 | + assertThat(childLifecycleOwner.lifecycle.currentState).isEqualTo(Lifecycle.State.CREATED) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Allow the composition to complete |
| 68 | + composeTestRule.waitForIdle() |
| 69 | + |
| 70 | + // Outside the composition, assert the lifecycle state again |
| 71 | + assertThat(childLifecycleOwner.lifecycle.currentState) |
| 72 | + .isEqualTo(Lifecycle.State.CREATED) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun childLifecycleOwner_initialStateRemainsSameAfterParentLifecycleChange() { |
| 77 | + lateinit var updatedChildLifecycleOwner: LifecycleOwner |
| 78 | + lateinit var tempChildLifecycleOwner: LifecycleOwner |
| 79 | + |
| 80 | + val customParentLifecycleOwner: LifecycleOwner = object : LifecycleOwner { |
| 81 | + private val registry = LifecycleRegistry(this) |
| 82 | + override val lifecycle: Lifecycle |
| 83 | + get() = registry |
| 84 | + } |
| 85 | + |
| 86 | + composeTestRule.setContent { |
| 87 | + var seenRecomposition by remember { mutableStateOf(false) } |
| 88 | + // after initial composition, change the parent lifecycle owner |
| 89 | + LaunchedEffect(Unit) { seenRecomposition = true } |
| 90 | + CompositionLocalProvider( |
| 91 | + if (seenRecomposition) { |
| 92 | + LocalLifecycleOwner provides customParentLifecycleOwner |
| 93 | + } else { |
| 94 | + LocalLifecycleOwner provides LocalLifecycleOwner.current |
| 95 | + } |
| 96 | + ) { |
| 97 | + |
| 98 | + updatedChildLifecycleOwner = rememberChildLifecycleOwner() |
| 99 | + // let's save the original reference to lifecycle owner on first pass |
| 100 | + if (!seenRecomposition) { |
| 101 | + tempChildLifecycleOwner = updatedChildLifecycleOwner |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Allow the composition to complete |
| 107 | + composeTestRule.waitForIdle() |
| 108 | + // assert that the [ComposeLifecycleOwner] is the same instance when the parent lifecycle owner |
| 109 | + // is changed. |
| 110 | + assertThat(updatedChildLifecycleOwner).isEqualTo(tempChildLifecycleOwner) |
| 111 | + } |
| 112 | + |
| 113 | + private fun ensureParentLifecycle(): LifecycleRegistry { |
| 114 | + if (mParentLifecycle == null) { |
| 115 | + val owner = object : LifecycleOwner { |
| 116 | + override val lifecycle = LifecycleRegistry.createUnsafe(this) |
| 117 | + } |
| 118 | + mParentLifecycle = owner.lifecycle |
| 119 | + } |
| 120 | + return mParentLifecycle!! |
| 121 | + } |
| 122 | +} |
0 commit comments