16
16
17
17
package androidx.navigation.compose
18
18
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
25
19
import androidx.compose.runtime.saveable.rememberSaveable
26
20
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
27
- import androidx.compose.runtime.setValue
28
21
import androidx.compose.ui.platform.LocalLifecycleOwner
29
22
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner
30
23
import androidx.compose.ui.test.junit4.StateRestorationTester
31
24
import androidx.compose.ui.test.junit4.createComposeRule
32
- import androidx.lifecycle.Lifecycle
33
25
import androidx.lifecycle.LifecycleOwner
34
26
import androidx.lifecycle.ViewModelStoreOwner
35
27
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
36
- import androidx.navigation.NavDestination
37
- import androidx.navigation.NavigatorState
38
28
import androidx.navigation.testing.TestNavigatorState
39
29
import androidx.savedstate.SavedStateRegistryOwner
40
30
import androidx.test.ext.junit.runners.AndroidJUnit4
41
- import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
42
31
import androidx.testutils.TestNavigator
43
32
import com.google.common.truth.Truth.assertThat
44
33
import com.google.common.truth.Truth.assertWithMessage
45
- import kotlinx.coroutines.runBlocking
46
34
import org.junit.Rule
47
35
import org.junit.Test
48
36
import org.junit.runner.RunWith
49
- import kotlin.random.Random
50
37
51
38
@RunWith(AndroidJUnit4 ::class )
52
39
class NavBackStackEntryProviderTest {
@@ -58,10 +45,13 @@ class NavBackStackEntryProviderTest {
58
45
fun testViewModelStoreOwnerProvided () {
59
46
val testNavigator = TestNavigator ()
60
47
val testNavigatorState = TestNavigatorState ()
61
- val backStackEntry = testNavigatorState.createActiveBackStackEntry(
48
+ testNavigator.onAttach(testNavigatorState)
49
+ val backStackEntry = testNavigatorState.createBackStackEntry(
62
50
testNavigator.createDestination(),
63
51
null
64
52
)
53
+ testNavigator.navigate(listOf (backStackEntry), null , null )
54
+
65
55
var viewModelStoreOwner: ViewModelStoreOwner ? = null
66
56
67
57
composeTestRule.setContent {
@@ -78,9 +68,14 @@ class NavBackStackEntryProviderTest {
78
68
@Test
79
69
fun testLifecycleOwnerProvided () {
80
70
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
+
84
79
var lifecycleOwner: LifecycleOwner ? = null
85
80
86
81
composeTestRule.setContent {
@@ -97,9 +92,14 @@ class NavBackStackEntryProviderTest {
97
92
@Test
98
93
fun testLocalSavedStateRegistryOwnerProvided () {
99
94
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
+
103
103
var localSavedStateRegistryOwner: SavedStateRegistryOwner ? = null
104
104
105
105
composeTestRule.setContent {
@@ -115,14 +115,18 @@ class NavBackStackEntryProviderTest {
115
115
116
116
@Test
117
117
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
+
118
127
val restorationTester = StateRestorationTester (composeTestRule)
119
128
var array: IntArray? = null
120
129
121
- val testNavigator = TestNavigator ()
122
- val navigatorState = TestNavigatorState ()
123
- val backStackEntry =
124
- navigatorState.createActiveBackStackEntry(testNavigator.createDestination())
125
-
126
130
restorationTester.setContent {
127
131
val saveableStateHolder = rememberSaveableStateHolder()
128
132
backStackEntry.provideToCompositionLocals(saveableStateHolder) {
@@ -144,16 +148,4 @@ class NavBackStackEntryProviderTest {
144
148
145
149
assertThat(array).isEqualTo(intArrayOf(1 ))
146
150
}
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
-
159
151
}
0 commit comments