|
| 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.material |
| 18 | + |
| 19 | +import android.net.Uri |
| 20 | +import androidx.compose.material.ExperimentalMaterialApi |
| 21 | +import androidx.compose.material.ModalBottomSheetState |
| 22 | +import androidx.compose.material.ModalBottomSheetValue |
| 23 | +import androidx.compose.ui.platform.LocalContext |
| 24 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 25 | +import androidx.core.net.toUri |
| 26 | +import androidx.navigation.NavDeepLinkRequest |
| 27 | +import androidx.navigation.compose.NavHost |
| 28 | +import androidx.navigation.compose.navArgument |
| 29 | +import androidx.navigation.contains |
| 30 | +import androidx.navigation.navDeepLink |
| 31 | +import androidx.navigation.navigation |
| 32 | +import androidx.navigation.testing.TestNavHostController |
| 33 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 34 | +import androidx.test.filters.LargeTest |
| 35 | +import com.google.common.truth.Truth.assertThat |
| 36 | +import com.google.common.truth.Truth.assertWithMessage |
| 37 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 38 | +import org.junit.Rule |
| 39 | +import org.junit.Test |
| 40 | +import org.junit.runner.RunWith |
| 41 | + |
| 42 | +@OptIn(ExperimentalCoroutinesApi::class, ExperimentalMaterialApi::class) |
| 43 | +@LargeTest |
| 44 | +@RunWith(AndroidJUnit4::class) |
| 45 | +internal class NavGraphBuilderTest { |
| 46 | + @get:Rule |
| 47 | + val composeTestRule = createComposeRule() |
| 48 | + |
| 49 | + @Test |
| 50 | + fun testCurrentBackStackEntryNavigate() { |
| 51 | + lateinit var navController: TestNavHostController |
| 52 | + val key = "key" |
| 53 | + val arg = "myarg" |
| 54 | + composeTestRule.setContent { |
| 55 | + navController = TestNavHostController(LocalContext.current) |
| 56 | + navController.navigatorProvider.addNavigator(createBottomSheetNavigator()) |
| 57 | + |
| 58 | + NavHost(navController, startDestination = firstRoute) { |
| 59 | + bottomSheet(firstRoute) { } |
| 60 | + bottomSheet("$secondRoute/{$key}") { } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + composeTestRule.runOnUiThread { |
| 65 | + navController.navigate("$secondRoute/$arg") |
| 66 | + assertThat(navController.currentBackStackEntry!!.arguments!!.getString(key)) |
| 67 | + .isEqualTo(arg) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun testDefaultArguments() { |
| 73 | + lateinit var navController: TestNavHostController |
| 74 | + val key = "key" |
| 75 | + val defaultArg = "default" |
| 76 | + composeTestRule.setContent { |
| 77 | + navController = TestNavHostController(LocalContext.current) |
| 78 | + navController.navigatorProvider.addNavigator(createBottomSheetNavigator()) |
| 79 | + |
| 80 | + NavHost(navController, startDestination = firstRoute) { |
| 81 | + bottomSheet(firstRoute) { } |
| 82 | + bottomSheet( |
| 83 | + secondRoute, |
| 84 | + arguments = listOf(navArgument(key) { defaultValue = defaultArg }) |
| 85 | + ) { } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + composeTestRule.runOnUiThread { |
| 90 | + navController.navigate(secondRoute) |
| 91 | + assertThat(navController.currentBackStackEntry!!.arguments!!.getString(key)) |
| 92 | + .isEqualTo(defaultArg) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun testDeepLink() { |
| 98 | + lateinit var navController: TestNavHostController |
| 99 | + val uriString = "https://www.example.com" |
| 100 | + val deeplink = NavDeepLinkRequest.Builder.fromUri(Uri.parse(uriString)).build() |
| 101 | + composeTestRule.setContent { |
| 102 | + navController = TestNavHostController(LocalContext.current) |
| 103 | + navController.navigatorProvider.addNavigator(createBottomSheetNavigator()) |
| 104 | + |
| 105 | + NavHost(navController, startDestination = firstRoute) { |
| 106 | + bottomSheet(firstRoute) { } |
| 107 | + bottomSheet( |
| 108 | + secondRoute, |
| 109 | + deepLinks = listOf(navDeepLink { uriPattern = uriString }) |
| 110 | + ) { } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + composeTestRule.runOnUiThread { |
| 115 | + navController.navigate(uriString.toUri()) |
| 116 | + assertThat(navController.currentBackStackEntry!!.destination.hasDeepLink(deeplink)) |
| 117 | + .isTrue() |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun testNavigationNestedStart() { |
| 123 | + lateinit var navController: TestNavHostController |
| 124 | + composeTestRule.setContent { |
| 125 | + navController = TestNavHostController(LocalContext.current) |
| 126 | + navController.navigatorProvider.addNavigator(createBottomSheetNavigator()) |
| 127 | + |
| 128 | + NavHost(navController, startDestination = firstRoute) { |
| 129 | + navigation(startDestination = secondRoute, route = firstRoute) { |
| 130 | + bottomSheet(secondRoute) { } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + composeTestRule.runOnUiThread { |
| 136 | + assertWithMessage("Destination should be added to the graph") |
| 137 | + .that(firstRoute in navController.graph) |
| 138 | + .isTrue() |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun testNavigationNestedInGraph() { |
| 144 | + lateinit var navController: TestNavHostController |
| 145 | + composeTestRule.setContent { |
| 146 | + navController = TestNavHostController(LocalContext.current) |
| 147 | + navController.navigatorProvider.addNavigator(createBottomSheetNavigator()) |
| 148 | + |
| 149 | + NavHost(navController, startDestination = firstRoute) { |
| 150 | + bottomSheet(firstRoute) { } |
| 151 | + navigation(startDestination = thirdRoute, route = secondRoute) { |
| 152 | + bottomSheet(thirdRoute) { } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + composeTestRule.runOnUiThread { |
| 158 | + navController.navigate(secondRoute) |
| 159 | + assertWithMessage("Destination should be added to the graph") |
| 160 | + .that(secondRoute in navController.graph) |
| 161 | + .isTrue() |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private fun createBottomSheetNavigator() = |
| 166 | + BottomSheetNavigator(ModalBottomSheetState(ModalBottomSheetValue.Hidden)) |
| 167 | +} |
| 168 | + |
| 169 | +private const val firstRoute = "first" |
| 170 | +private const val secondRoute = "second" |
| 171 | +private const val thirdRoute = "third" |
0 commit comments