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

[Navigation] Introduce NavBackStackEntry#provideToCompositionLocals #175

Prev Previous commit
Next Next commit
Extract back stack entry creation into test-private method
Change-Id: I8b8c3c03b190e083928d013d82495bc98a9c5a6f
  • Loading branch information
jossiwolf committed May 6, 2021
commit 6dc8b2c6b498babd637ac7666b31fc5c8af2e5d5
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package androidx.navigation.compose

import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
import androidx.compose.ui.platform.LocalLifecycleOwner
Expand All @@ -25,16 +26,19 @@ import androidx.compose.ui.test.junit4.createComposeRule
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.navigation.NavBackStackEntry
import androidx.navigation.testing.TestNavigatorState
import androidx.savedstate.SavedStateRegistryOwner
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.testutils.TestNavigator
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@LargeTest
@RunWith(AndroidJUnit4::class)
class NavBackStackEntryProviderTest {

Expand All @@ -43,15 +47,7 @@ class NavBackStackEntryProviderTest {

@Test
fun testViewModelStoreOwnerProvided() {
val testNavigator = TestNavigator()
val testNavigatorState = TestNavigatorState()
testNavigator.onAttach(testNavigatorState)
val backStackEntry = testNavigatorState.createBackStackEntry(
testNavigator.createDestination(),
null
)
testNavigator.navigate(listOf(backStackEntry), null, null)

val backStackEntry = createBackStackEntry()
var viewModelStoreOwner: ViewModelStoreOwner? = null

composeTestRule.setContent {
Expand All @@ -67,15 +63,7 @@ class NavBackStackEntryProviderTest {

@Test
fun testLifecycleOwnerProvided() {
val testNavigator = TestNavigator()
val testNavigatorState = TestNavigatorState()
testNavigator.onAttach(testNavigatorState)
val backStackEntry = testNavigatorState.createBackStackEntry(
testNavigator.createDestination(),
null
)
testNavigator.navigate(listOf(backStackEntry), null, null)

val backStackEntry = createBackStackEntry()
var lifecycleOwner: LifecycleOwner? = null

composeTestRule.setContent {
Expand All @@ -91,15 +79,7 @@ class NavBackStackEntryProviderTest {

@Test
fun testLocalSavedStateRegistryOwnerProvided() {
val testNavigator = TestNavigator()
val testNavigatorState = TestNavigatorState()
testNavigator.onAttach(testNavigatorState)
val backStackEntry = testNavigatorState.createBackStackEntry(
testNavigator.createDestination(),
null
)
testNavigator.navigate(listOf(backStackEntry), null, null)

val backStackEntry = createBackStackEntry()
var localSavedStateRegistryOwner: SavedStateRegistryOwner? = null

composeTestRule.setContent {
Expand All @@ -115,15 +95,7 @@ class NavBackStackEntryProviderTest {

@Test
fun testSaveableValueInContentIsSaved() {
val testNavigator = TestNavigator()
val testNavigatorState = TestNavigatorState()
testNavigator.onAttach(testNavigatorState)
val backStackEntry = testNavigatorState.createBackStackEntry(
testNavigator.createDestination(),
null
)
testNavigator.navigate(listOf(backStackEntry), null, null)

val backStackEntry = createBackStackEntry()
val restorationTester = StateRestorationTester(composeTestRule)
var array: IntArray? = null

Expand All @@ -148,4 +120,17 @@ class NavBackStackEntryProviderTest {

assertThat(array).isEqualTo(intArrayOf(1))
}

private fun createBackStackEntry(): NavBackStackEntry {
val testNavigator = TestNavigator()
val testNavigatorState = TestNavigatorState()
testNavigator.onAttach(testNavigatorState)
val backStackEntry = testNavigatorState.createBackStackEntry(
testNavigator.createDestination(),
null
)
// We navigate to move the NavBackStackEntry to the correct state
testNavigator.navigate(listOf(backStackEntry), null, null)
return backStackEntry
}
}