|
| 1 | +/* |
| 2 | + * Copyright 2021 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package androidx.navigation.compose |
| 18 | + |
| 19 | +import androidx.compose.runtime.remember |
| 20 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 21 | +import androidx.compose.runtime.saveable.rememberSaveableStateHolder |
| 22 | +import androidx.compose.ui.platform.LocalLifecycleOwner |
| 23 | +import androidx.compose.ui.platform.LocalSavedStateRegistryOwner |
| 24 | +import androidx.compose.ui.test.junit4.StateRestorationTester |
| 25 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 26 | +import androidx.lifecycle.LifecycleOwner |
| 27 | +import androidx.lifecycle.ViewModelStoreOwner |
| 28 | +import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner |
| 29 | +import androidx.navigation.NavBackStackEntry |
| 30 | +import androidx.navigation.testing.TestNavigatorState |
| 31 | +import androidx.savedstate.SavedStateRegistryOwner |
| 32 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 33 | +import androidx.test.filters.LargeTest |
| 34 | +import androidx.testutils.TestNavigator |
| 35 | +import com.google.common.truth.Truth.assertThat |
| 36 | +import com.google.common.truth.Truth.assertWithMessage |
| 37 | +import org.junit.Rule |
| 38 | +import org.junit.Test |
| 39 | +import org.junit.runner.RunWith |
| 40 | + |
| 41 | +@LargeTest |
| 42 | +@RunWith(AndroidJUnit4::class) |
| 43 | +class NavBackStackEntryProviderTest { |
| 44 | + |
| 45 | + @get:Rule |
| 46 | + val composeTestRule = createComposeRule() |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testViewModelStoreOwnerProvided() { |
| 50 | + val backStackEntry = createBackStackEntry() |
| 51 | + var viewModelStoreOwner: ViewModelStoreOwner? = null |
| 52 | + |
| 53 | + composeTestRule.setContent { |
| 54 | + val saveableStateHolder = rememberSaveableStateHolder() |
| 55 | + backStackEntry.LocalOwnersProvider(saveableStateHolder) { |
| 56 | + viewModelStoreOwner = LocalViewModelStoreOwner.current |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + assertWithMessage("ViewModelStoreOwner is provided by $backStackEntry") |
| 61 | + .that(viewModelStoreOwner).isEqualTo(backStackEntry) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun testLifecycleOwnerProvided() { |
| 66 | + val backStackEntry = createBackStackEntry() |
| 67 | + var lifecycleOwner: LifecycleOwner? = null |
| 68 | + |
| 69 | + composeTestRule.setContent { |
| 70 | + val saveableStateHolder = rememberSaveableStateHolder() |
| 71 | + backStackEntry.LocalOwnersProvider(saveableStateHolder) { |
| 72 | + lifecycleOwner = LocalLifecycleOwner.current |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + assertWithMessage("LifecycleOwner is provided by $backStackEntry") |
| 77 | + .that(lifecycleOwner).isEqualTo(backStackEntry) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun testLocalSavedStateRegistryOwnerProvided() { |
| 82 | + val backStackEntry = createBackStackEntry() |
| 83 | + var localSavedStateRegistryOwner: SavedStateRegistryOwner? = null |
| 84 | + |
| 85 | + composeTestRule.setContent { |
| 86 | + val saveableStateHolder = rememberSaveableStateHolder() |
| 87 | + backStackEntry.LocalOwnersProvider(saveableStateHolder) { |
| 88 | + localSavedStateRegistryOwner = LocalSavedStateRegistryOwner.current |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + assertWithMessage("LocalSavedStateRegistryOwner is provided by $backStackEntry") |
| 93 | + .that(localSavedStateRegistryOwner).isEqualTo(backStackEntry) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun testSaveableValueInContentIsSaved() { |
| 98 | + val backStackEntry = createBackStackEntry() |
| 99 | + val restorationTester = StateRestorationTester(composeTestRule) |
| 100 | + var array: IntArray? = null |
| 101 | + |
| 102 | + restorationTester.setContent { |
| 103 | + val saveableStateHolder = rememberSaveableStateHolder() |
| 104 | + backStackEntry.LocalOwnersProvider(saveableStateHolder) { |
| 105 | + array = rememberSaveable { |
| 106 | + intArrayOf(0) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + assertThat(array).isEqualTo(intArrayOf(0)) |
| 112 | + |
| 113 | + composeTestRule.runOnUiThread { |
| 114 | + array!![0] = 1 |
| 115 | + // we null it to ensure recomposition happened |
| 116 | + array = null |
| 117 | + } |
| 118 | + |
| 119 | + restorationTester.emulateSavedInstanceStateRestore() |
| 120 | + |
| 121 | + assertThat(array).isEqualTo(intArrayOf(1)) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun testNonSaveableValueInContentIsNotSaved() { |
| 126 | + val backStackEntry = createBackStackEntry() |
| 127 | + val restorationTester = StateRestorationTester(composeTestRule) |
| 128 | + var nonSaveable: IntArray? = null |
| 129 | + val initialValue = intArrayOf(10) |
| 130 | + |
| 131 | + restorationTester.setContent { |
| 132 | + val saveableStateHolder = rememberSaveableStateHolder() |
| 133 | + backStackEntry.LocalOwnersProvider(saveableStateHolder) { |
| 134 | + nonSaveable = remember { initialValue } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + assertThat(nonSaveable).isEqualTo(initialValue) |
| 139 | + |
| 140 | + composeTestRule.runOnUiThread { |
| 141 | + nonSaveable!![0] = 1 |
| 142 | + // we null it to ensure recomposition happened |
| 143 | + nonSaveable = null |
| 144 | + } |
| 145 | + |
| 146 | + restorationTester.emulateSavedInstanceStateRestore() |
| 147 | + |
| 148 | + assertThat(nonSaveable).isEqualTo(initialValue) |
| 149 | + } |
| 150 | + |
| 151 | + private fun createBackStackEntry(): NavBackStackEntry { |
| 152 | + val testNavigator = TestNavigator() |
| 153 | + val testNavigatorState = TestNavigatorState() |
| 154 | + testNavigator.onAttach(testNavigatorState) |
| 155 | + val backStackEntry = testNavigatorState.createBackStackEntry( |
| 156 | + testNavigator.createDestination(), |
| 157 | + null |
| 158 | + ) |
| 159 | + // We navigate to move the NavBackStackEntry to the correct state |
| 160 | + testNavigator.navigate(listOf(backStackEntry), null, null) |
| 161 | + return backStackEntry |
| 162 | + } |
| 163 | +} |
0 commit comments