-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce new NavBackStackEntry#provideToCompositionLocals extension …
…that provides the NavBackStackEntry to the composition locals needed for all Compose-related navigators. Test: NavBackStackEntryProviderTest Fixes: b/187229439 Change-Id: If6af74ec67fbedef2cf5d623e9fc44ab6bf76e39
- Loading branch information
Showing
6 changed files
with
255 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
...compose/src/androidTest/java/androidx/navigation/compose/NavBackStackEntryProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2021 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.navigation.compose | ||
|
||
import android.os.Bundle | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.SaveableStateHolder | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.saveable.rememberSaveableStateHolder | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner | ||
import androidx.compose.ui.test.junit4.StateRestorationTester | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner | ||
import androidx.navigation.NavDestination | ||
import androidx.navigation.NavigatorState | ||
import androidx.navigation.testing.TestNavigatorState | ||
import androidx.savedstate.SavedStateRegistryOwner | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread | ||
import androidx.testutils.TestNavigator | ||
import com.google.common.truth.Truth.assertThat | ||
import com.google.common.truth.Truth.assertWithMessage | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import kotlin.random.Random | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class NavBackStackEntryProviderTest { | ||
|
||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun testViewModelStoreOwnerProvided() { | ||
val testNavigator = TestNavigator() | ||
val testNavigatorState = TestNavigatorState() | ||
val backStackEntry = testNavigatorState.createActiveBackStackEntry( | ||
testNavigator.createDestination(), | ||
null | ||
) | ||
var viewModelStoreOwner: ViewModelStoreOwner? = null | ||
|
||
composeTestRule.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.provideToCompositionLocals(saveableStateHolder) { | ||
viewModelStoreOwner = LocalViewModelStoreOwner.current | ||
} | ||
} | ||
|
||
assertWithMessage("ViewModelStoreOwner is provided by $backStackEntry") | ||
.that(viewModelStoreOwner).isEqualTo(backStackEntry) | ||
} | ||
|
||
@Test | ||
fun testLifecycleOwnerProvided() { | ||
val testNavigator = TestNavigator() | ||
val navigatorState = TestNavigatorState() | ||
val backStackEntry = | ||
navigatorState.createActiveBackStackEntry(testNavigator.createDestination()) | ||
var lifecycleOwner: LifecycleOwner? = null | ||
|
||
composeTestRule.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.provideToCompositionLocals(saveableStateHolder) { | ||
lifecycleOwner = LocalLifecycleOwner.current | ||
} | ||
} | ||
|
||
assertWithMessage("LifecycleOwner is provided by $backStackEntry") | ||
.that(lifecycleOwner).isEqualTo(backStackEntry) | ||
} | ||
|
||
@Test | ||
fun testLocalSavedStateRegistryOwnerProvided() { | ||
val testNavigator = TestNavigator() | ||
val navigatorState = TestNavigatorState() | ||
val backStackEntry = | ||
navigatorState.createActiveBackStackEntry(testNavigator.createDestination()) | ||
var localSavedStateRegistryOwner: SavedStateRegistryOwner? = null | ||
|
||
composeTestRule.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.provideToCompositionLocals(saveableStateHolder) { | ||
localSavedStateRegistryOwner = LocalSavedStateRegistryOwner.current | ||
} | ||
} | ||
|
||
assertWithMessage("LocalSavedStateRegistryOwner is provided by $backStackEntry") | ||
.that(localSavedStateRegistryOwner).isEqualTo(backStackEntry) | ||
} | ||
|
||
@Test | ||
fun testSaveableValueInContentIsSaved() { | ||
val restorationTester = StateRestorationTester(composeTestRule) | ||
var array: IntArray? = null | ||
|
||
val testNavigator = TestNavigator() | ||
val navigatorState = TestNavigatorState() | ||
val backStackEntry = | ||
navigatorState.createActiveBackStackEntry(testNavigator.createDestination()) | ||
|
||
restorationTester.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.provideToCompositionLocals(saveableStateHolder) { | ||
array = rememberSaveable { | ||
intArrayOf(0) | ||
} | ||
} | ||
} | ||
|
||
assertThat(array).isEqualTo(intArrayOf(0)) | ||
|
||
composeTestRule.runOnUiThread { | ||
array!![0] = 1 | ||
// we null it to ensure recomposition happened | ||
array = null | ||
} | ||
|
||
restorationTester.emulateSavedInstanceStateRestore() | ||
|
||
assertThat(array).isEqualTo(intArrayOf(1)) | ||
} | ||
|
||
// By default, NavBackStackEntrys are in the INITIALIZED state and then get moved to the next | ||
// appropriate state by the NavController. In case we aren't testing with a NavController, | ||
// this sets the entry's lifecycle state to the passed state so that the entry is active. | ||
private fun NavigatorState.createActiveBackStackEntry( | ||
destination: NavDestination, | ||
arguments: Bundle? = null, | ||
lifecycleState: Lifecycle.State = Lifecycle.State.RESUMED | ||
) = createBackStackEntry(destination, arguments).apply { | ||
runOnUiThread { maxLifecycle = lifecycleState } | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...navigation-compose/src/main/java/androidx/navigation/compose/NavBackStackEntryProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2021 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.navigation.compose | ||
|
||
import android.annotation.SuppressLint | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.saveable.SaveableStateHolder | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.NavBackStackEntry | ||
import java.util.UUID | ||
|
||
|
||
/** | ||
* Provides [this] [NavBackStackEntry] as [LocalViewModelStoreOwner], [LocalLifecycleOwner] and | ||
* [LocalSavedStateRegistryOwner] to the [content] and saves the [content]'s saveable states with | ||
* the given [saveableStateHolder]. | ||
* | ||
* @param saveableStateHolder The [SaveableStateHolder] that holds the saved states | ||
* @param content The content [Composable] | ||
*/ | ||
@SuppressLint("ComposableNaming") | ||
@Composable | ||
public fun NavBackStackEntry.provideToCompositionLocals( | ||
saveableStateHolder: SaveableStateHolder, | ||
content: @Composable () -> Unit | ||
) { | ||
CompositionLocalProvider( | ||
LocalViewModelStoreOwner provides this, | ||
LocalLifecycleOwner provides this, | ||
LocalSavedStateRegistryOwner provides this | ||
) { | ||
saveableStateHolder.SaveableStateProvider { | ||
content() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun SaveableStateHolder.SaveableStateProvider(content: @Composable () -> Unit) { | ||
val viewModel = viewModel<BackStackEntryIdViewModel>() | ||
viewModel.saveableStateHolder = this | ||
SaveableStateProvider(viewModel.id, content) | ||
} | ||
|
||
internal class BackStackEntryIdViewModel(handle: SavedStateHandle) : ViewModel() { | ||
|
||
private val IdKey = "SaveableStateHolder_BackStackEntryKey" | ||
|
||
// we create our own id for each back stack entry to support multiple entries of the same | ||
// destination. this id will be restored by SavedStateHandle | ||
val id: UUID = handle.get<UUID>(IdKey) ?: UUID.randomUUID().also { handle.set(IdKey, it) } | ||
|
||
var saveableStateHolder: SaveableStateHolder? = null | ||
|
||
// onCleared will be called on the entries removed from the back stack. here we notify | ||
// RestorableStateHolder that we shouldn't save the state for this id, so when we open this | ||
// destination again the state will not be restored. | ||
override fun onCleared() { | ||
super.onCleared() | ||
saveableStateHolder?.removeState(id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters