Skip to content

Commit 3c1bb7b

Browse files
committed
Use TestNavigatorState to set up NavBackStackEntry instead of the ugly createActiveBackStackEntry hack
Change-Id: I8b7101292e107dded8426a7e38f100451720ac95
1 parent b961836 commit 3c1bb7b

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

navigation/navigation-compose/src/androidTest/java/androidx/navigation/compose/NavBackStackEntryProviderTest.kt

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,24 @@
1616

1717
package androidx.navigation.compose
1818

19-
import android.os.Bundle
20-
import androidx.compose.runtime.DisposableEffect
21-
import androidx.compose.runtime.getValue
22-
import androidx.compose.runtime.mutableStateOf
23-
import androidx.compose.runtime.remember
24-
import androidx.compose.runtime.saveable.SaveableStateHolder
2519
import androidx.compose.runtime.saveable.rememberSaveable
2620
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
27-
import androidx.compose.runtime.setValue
2821
import androidx.compose.ui.platform.LocalLifecycleOwner
2922
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner
3023
import androidx.compose.ui.test.junit4.StateRestorationTester
3124
import androidx.compose.ui.test.junit4.createComposeRule
32-
import androidx.lifecycle.Lifecycle
3325
import androidx.lifecycle.LifecycleOwner
3426
import androidx.lifecycle.ViewModelStoreOwner
3527
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
36-
import androidx.navigation.NavDestination
37-
import androidx.navigation.NavigatorState
3828
import androidx.navigation.testing.TestNavigatorState
3929
import androidx.savedstate.SavedStateRegistryOwner
4030
import androidx.test.ext.junit.runners.AndroidJUnit4
41-
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
4231
import androidx.testutils.TestNavigator
4332
import com.google.common.truth.Truth.assertThat
4433
import com.google.common.truth.Truth.assertWithMessage
45-
import kotlinx.coroutines.runBlocking
4634
import org.junit.Rule
4735
import org.junit.Test
4836
import org.junit.runner.RunWith
49-
import kotlin.random.Random
5037

5138
@RunWith(AndroidJUnit4::class)
5239
class NavBackStackEntryProviderTest {
@@ -58,10 +45,13 @@ class NavBackStackEntryProviderTest {
5845
fun testViewModelStoreOwnerProvided() {
5946
val testNavigator = TestNavigator()
6047
val testNavigatorState = TestNavigatorState()
61-
val backStackEntry = testNavigatorState.createActiveBackStackEntry(
48+
testNavigator.onAttach(testNavigatorState)
49+
val backStackEntry = testNavigatorState.createBackStackEntry(
6250
testNavigator.createDestination(),
6351
null
6452
)
53+
testNavigator.navigate(listOf(backStackEntry), null, null)
54+
6555
var viewModelStoreOwner: ViewModelStoreOwner? = null
6656

6757
composeTestRule.setContent {
@@ -78,9 +68,14 @@ class NavBackStackEntryProviderTest {
7868
@Test
7969
fun testLifecycleOwnerProvided() {
8070
val testNavigator = TestNavigator()
81-
val navigatorState = TestNavigatorState()
82-
val backStackEntry =
83-
navigatorState.createActiveBackStackEntry(testNavigator.createDestination())
71+
val testNavigatorState = TestNavigatorState()
72+
testNavigator.onAttach(testNavigatorState)
73+
val backStackEntry = testNavigatorState.createBackStackEntry(
74+
testNavigator.createDestination(),
75+
null
76+
)
77+
testNavigator.navigate(listOf(backStackEntry), null, null)
78+
8479
var lifecycleOwner: LifecycleOwner? = null
8580

8681
composeTestRule.setContent {
@@ -97,9 +92,14 @@ class NavBackStackEntryProviderTest {
9792
@Test
9893
fun testLocalSavedStateRegistryOwnerProvided() {
9994
val testNavigator = TestNavigator()
100-
val navigatorState = TestNavigatorState()
101-
val backStackEntry =
102-
navigatorState.createActiveBackStackEntry(testNavigator.createDestination())
95+
val testNavigatorState = TestNavigatorState()
96+
testNavigator.onAttach(testNavigatorState)
97+
val backStackEntry = testNavigatorState.createBackStackEntry(
98+
testNavigator.createDestination(),
99+
null
100+
)
101+
testNavigator.navigate(listOf(backStackEntry), null, null)
102+
103103
var localSavedStateRegistryOwner: SavedStateRegistryOwner? = null
104104

105105
composeTestRule.setContent {
@@ -115,14 +115,18 @@ class NavBackStackEntryProviderTest {
115115

116116
@Test
117117
fun testSaveableValueInContentIsSaved() {
118+
val testNavigator = TestNavigator()
119+
val testNavigatorState = TestNavigatorState()
120+
testNavigator.onAttach(testNavigatorState)
121+
val backStackEntry = testNavigatorState.createBackStackEntry(
122+
testNavigator.createDestination(),
123+
null
124+
)
125+
testNavigator.navigate(listOf(backStackEntry), null, null)
126+
118127
val restorationTester = StateRestorationTester(composeTestRule)
119128
var array: IntArray? = null
120129

121-
val testNavigator = TestNavigator()
122-
val navigatorState = TestNavigatorState()
123-
val backStackEntry =
124-
navigatorState.createActiveBackStackEntry(testNavigator.createDestination())
125-
126130
restorationTester.setContent {
127131
val saveableStateHolder = rememberSaveableStateHolder()
128132
backStackEntry.provideToCompositionLocals(saveableStateHolder) {
@@ -144,16 +148,4 @@ class NavBackStackEntryProviderTest {
144148

145149
assertThat(array).isEqualTo(intArrayOf(1))
146150
}
147-
148-
// By default, NavBackStackEntrys are in the INITIALIZED state and then get moved to the next
149-
// appropriate state by the NavController. In case we aren't testing with a NavController,
150-
// this sets the entry's lifecycle state to the passed state so that the entry is active.
151-
private fun NavigatorState.createActiveBackStackEntry(
152-
destination: NavDestination,
153-
arguments: Bundle? = null,
154-
lifecycleState: Lifecycle.State = Lifecycle.State.RESUMED
155-
) = createBackStackEntry(destination, arguments).apply {
156-
runOnUiThread { maxLifecycle = lifecycleState }
157-
}
158-
159151
}

navigation/navigation-compose/src/main/java/androidx/navigation/compose/NavBackStackEntryProvider.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import androidx.lifecycle.viewmodel.compose.viewModel
3030
import androidx.navigation.NavBackStackEntry
3131
import java.util.UUID
3232

33-
3433
/**
3534
* Provides [this] [NavBackStackEntry] as [LocalViewModelStoreOwner], [LocalLifecycleOwner] and
3635
* [LocalSavedStateRegistryOwner] to the [content] and saves the [content]'s saveable states with

0 commit comments

Comments
 (0)