Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 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.tv.material3 |
| 18 | |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 19 | import androidx.compose.foundation.background |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 20 | import androidx.compose.foundation.border |
| 21 | import androidx.compose.foundation.focusable |
| 22 | import androidx.compose.foundation.layout.Box |
| 23 | import androidx.compose.foundation.layout.Row |
Aditya Arora | 0aa705f | 2023-04-03 20:34:37 +0530 | [diff] [blame] | 24 | import androidx.compose.foundation.layout.fillMaxSize |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 25 | import androidx.compose.foundation.layout.fillMaxWidth |
| 26 | import androidx.compose.foundation.layout.size |
| 27 | import androidx.compose.foundation.layout.width |
| 28 | import androidx.compose.foundation.text.BasicText |
| 29 | import androidx.compose.runtime.CompositionLocalProvider |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 30 | import androidx.compose.runtime.getValue |
| 31 | import androidx.compose.runtime.mutableStateOf |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 32 | import androidx.compose.runtime.remember |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 33 | import androidx.compose.runtime.setValue |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 34 | import androidx.compose.ui.ExperimentalComposeUiApi |
| 35 | import androidx.compose.ui.Modifier |
| 36 | import androidx.compose.ui.focus.FocusRequester |
| 37 | import androidx.compose.ui.focus.focusRequester |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 38 | import androidx.compose.ui.focus.onFocusChanged |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 39 | import androidx.compose.ui.geometry.Rect |
| 40 | import androidx.compose.ui.graphics.Color |
| 41 | import androidx.compose.ui.input.key.Key |
| 42 | import androidx.compose.ui.platform.LocalLayoutDirection |
| 43 | import androidx.compose.ui.platform.testTag |
| 44 | import androidx.compose.ui.semantics.SemanticsNode |
| 45 | import androidx.compose.ui.test.ExperimentalTestApi |
| 46 | import androidx.compose.ui.test.SemanticsNodeInteraction |
| 47 | import androidx.compose.ui.test.SemanticsNodeInteractionCollection |
| 48 | import androidx.compose.ui.test.assertIsDisplayed |
| 49 | import androidx.compose.ui.test.assertIsEqualTo |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 50 | import androidx.compose.ui.test.assertIsFocused |
Aditya Arora | 0aa705f | 2023-04-03 20:34:37 +0530 | [diff] [blame] | 51 | import androidx.compose.ui.test.assertIsNotFocused |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 52 | import androidx.compose.ui.test.assertWidthIsEqualTo |
| 53 | import androidx.compose.ui.test.getUnclippedBoundsInRoot |
| 54 | import androidx.compose.ui.test.junit4.createComposeRule |
| 55 | import androidx.compose.ui.test.onAllNodesWithText |
| 56 | import androidx.compose.ui.test.onNodeWithTag |
| 57 | import androidx.compose.ui.test.onRoot |
| 58 | import androidx.compose.ui.test.performKeyInput |
| 59 | import androidx.compose.ui.test.pressKey |
| 60 | import androidx.compose.ui.unit.Dp |
| 61 | import androidx.compose.ui.unit.DpRect |
| 62 | import androidx.compose.ui.unit.LayoutDirection |
| 63 | import androidx.compose.ui.unit.dp |
| 64 | import androidx.compose.ui.unit.toSize |
| 65 | import androidx.test.platform.app.InstrumentationRegistry |
| 66 | import org.junit.Rule |
| 67 | import org.junit.Test |
| 68 | |
| 69 | @OptIn(ExperimentalTvMaterial3Api::class) |
| 70 | class ModalNavigationDrawerTest { |
| 71 | @get:Rule |
| 72 | val rule = createComposeRule() |
| 73 | |
| 74 | @Test |
| 75 | fun modalNavigationDrawer_initialStateClosed_closedStateComposableDisplayed() { |
| 76 | rule.setContent { |
| 77 | ModalNavigationDrawer( |
| 78 | drawerState = remember { DrawerState(DrawerValue.Closed) }, |
| 79 | drawerContent = { |
| 80 | BasicText(text = if (it == DrawerValue.Open) "Opened" else "Closed") |
| 81 | } |
| 82 | ) { Box(Modifier.size(200.dp)) } |
| 83 | } |
| 84 | |
| 85 | rule.onAllNodesWithText("Closed").assertAnyAreDisplayed() |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | fun modalNavigationDrawer_initialStateOpen_openStateComposableDisplayed() { |
| 90 | rule.setContent { |
| 91 | ModalNavigationDrawer( |
| 92 | drawerState = remember { DrawerState(DrawerValue.Open) }, |
| 93 | drawerContent = { |
| 94 | BasicText(text = if (it == DrawerValue.Open) "Opened" else "Closed") |
| 95 | }) { BasicText("other content") } |
| 96 | } |
| 97 | |
| 98 | rule.onAllNodesWithText("Opened").assertAnyAreDisplayed() |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | fun modalNavigationDrawer_focusInsideDrawer_openedStateComposableDisplayed() { |
| 103 | InstrumentationRegistry.getInstrumentation().setInTouchMode(false) |
| 104 | val drawerFocusRequester = FocusRequester() |
| 105 | rule.setContent { |
| 106 | val navigationDrawerValue = remember { DrawerState(DrawerValue.Closed) } |
| 107 | ModalNavigationDrawer( |
| 108 | modifier = Modifier.focusRequester(drawerFocusRequester), |
| 109 | drawerState = navigationDrawerValue, |
| 110 | drawerContent = { |
| 111 | BasicText( |
vinekumar | d654f4d | 2023-04-12 18:52:45 +0530 | [diff] [blame^] | 112 | modifier = Modifier.focusable(), |
| 113 | text = if (it == DrawerValue.Open) "Opened" else "Closed" |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 114 | ) |
| 115 | }) { BasicText("other content") } |
| 116 | } |
| 117 | |
| 118 | rule.onAllNodesWithText("Closed").assertAnyAreDisplayed() |
| 119 | |
| 120 | rule.runOnIdle { |
| 121 | drawerFocusRequester.requestFocus() |
| 122 | } |
| 123 | |
| 124 | rule.onAllNodesWithText("Opened").assertAnyAreDisplayed() |
| 125 | } |
| 126 | |
| 127 | @OptIn(ExperimentalTestApi::class, ExperimentalComposeUiApi::class) |
| 128 | @Test |
| 129 | fun modalNavigationDrawer_focusMovesOutOfDrawer_closedStateComposableDisplayed() { |
| 130 | InstrumentationRegistry.getInstrumentation().setInTouchMode(false) |
| 131 | val drawerFocusRequester = FocusRequester() |
| 132 | rule.setContent { |
| 133 | val navigationDrawerValue = remember { DrawerState(DrawerValue.Closed) } |
| 134 | Row { |
| 135 | ModalNavigationDrawer( |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 136 | modifier = Modifier |
| 137 | .focusRequester(drawerFocusRequester) |
| 138 | .focusable(false), |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 139 | drawerState = navigationDrawerValue, |
| 140 | drawerContent = { |
vinekumar | d654f4d | 2023-04-12 18:52:45 +0530 | [diff] [blame^] | 141 | BasicText( |
| 142 | modifier = Modifier.focusable(), |
| 143 | text = if (it == DrawerValue.Open) "Opened" else "Closed" |
| 144 | ) |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 145 | }) { |
| 146 | Box(modifier = Modifier.focusable()) { |
| 147 | BasicText("Button") |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | rule.runOnIdle { |
| 153 | drawerFocusRequester.requestFocus() |
| 154 | } |
| 155 | rule.onAllNodesWithText("Opened").assertAnyAreDisplayed() |
| 156 | rule.onRoot().performKeyInput { pressKey(Key.DirectionRight) } |
| 157 | rule.onAllNodesWithText("Closed").assertAnyAreDisplayed() |
| 158 | } |
| 159 | |
vinekumar | d654f4d | 2023-04-12 18:52:45 +0530 | [diff] [blame^] | 160 | @OptIn(ExperimentalTestApi::class) |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 161 | @Test |
| 162 | fun modalNavigationDrawer_focusMovesIntoDrawer_openStateComposableDisplayed() { |
| 163 | InstrumentationRegistry.getInstrumentation().setInTouchMode(false) |
| 164 | val buttonFocusRequester = FocusRequester() |
| 165 | rule.setContent { |
| 166 | val navigationDrawerValue = remember { DrawerState(DrawerValue.Closed) } |
| 167 | Row { |
| 168 | ModalNavigationDrawer( |
| 169 | drawerState = navigationDrawerValue, |
| 170 | drawerContent = { |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 171 | var isFocused by remember { mutableStateOf(false) } |
| 172 | BasicText( |
| 173 | text = if (it == DrawerValue.Open) "Opened" else "Closed", |
| 174 | modifier = Modifier |
| 175 | .onFocusChanged { focusState -> |
| 176 | isFocused = focusState.isFocused |
| 177 | } |
| 178 | .background(if (isFocused) Color.Green else Color.Yellow) |
| 179 | .focusable() |
| 180 | .testTag("drawerItem") |
| 181 | ) |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 182 | }) { |
| 183 | Box( |
| 184 | modifier = Modifier |
| 185 | .focusRequester(buttonFocusRequester) |
| 186 | .focusable() |
| 187 | ) { |
| 188 | BasicText("Button") |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | rule.runOnIdle { |
| 194 | buttonFocusRequester.requestFocus() |
| 195 | } |
| 196 | rule.onAllNodesWithText("Closed").assertAnyAreDisplayed() |
| 197 | rule.onRoot().performKeyInput { pressKey(Key.DirectionLeft) } |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 198 | rule.waitForIdle() |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 199 | rule.onAllNodesWithText("Opened").assertAnyAreDisplayed() |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 200 | rule.onNodeWithTag("drawerItem").assertIsFocused() |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Test |
| 204 | fun modalNavigationDrawer_closedState_widthOfDrawerIsWidthOfContent() { |
| 205 | val contentWidthBoxTag = "contentWidthBox" |
| 206 | val totalWidth = 100.dp |
| 207 | val closedDrawerContentWidth = 30.dp |
| 208 | val expectedContentWidth = totalWidth - closedDrawerContentWidth |
| 209 | rule.setContent { |
| 210 | Box(modifier = Modifier.width(totalWidth)) { |
| 211 | NavigationDrawer( |
| 212 | drawerState = remember { DrawerState(DrawerValue.Closed) }, |
| 213 | drawerContent = { |
| 214 | Box(Modifier.width(closedDrawerContentWidth)) { |
| 215 | // extra long content wrapped in a drawer-width restricting box |
| 216 | Box(Modifier.width(closedDrawerContentWidth * 10)) |
| 217 | } |
| 218 | } |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 219 | ) { Box( |
| 220 | Modifier |
| 221 | .fillMaxWidth() |
| 222 | .testTag(contentWidthBoxTag)) } |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | rule.onNodeWithTag(contentWidthBoxTag).assertWidthIsEqualTo(expectedContentWidth) |
| 227 | } |
| 228 | |
| 229 | @Test |
| 230 | fun modalNavigationDrawer_openState_widthOfDrawerIsWidthOfContent() { |
| 231 | val contentWidthBoxTag = "contentWidthBox" |
| 232 | val totalWidth = 100.dp |
| 233 | val openDrawerContentWidth = 70.dp |
| 234 | val expectedContentWidth = totalWidth - openDrawerContentWidth |
| 235 | rule.setContent { |
| 236 | Box(modifier = Modifier.width(totalWidth)) { |
| 237 | NavigationDrawer( |
| 238 | drawerState = remember { DrawerState(DrawerValue.Closed) }, |
| 239 | drawerContent = { |
| 240 | Box(Modifier.width(openDrawerContentWidth)) { |
| 241 | Box(Modifier.width(openDrawerContentWidth * 10)) |
| 242 | } |
| 243 | } |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 244 | ) { Box( |
| 245 | Modifier |
| 246 | .fillMaxWidth() |
| 247 | .testTag(contentWidthBoxTag)) } |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | rule.onNodeWithTag(contentWidthBoxTag).assertWidthIsEqualTo(expectedContentWidth) |
| 252 | } |
| 253 | |
| 254 | @Test |
| 255 | fun modalNavigationDrawer_rtl_drawerIsDrawnAtTheStart() { |
| 256 | val contentWidthBoxTag = "contentWidthBox" |
| 257 | val drawerContentBoxTag = "drawerContentBox" |
| 258 | rule.setContent { |
| 259 | CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { |
| 260 | ModalNavigationDrawer( |
| 261 | drawerState = remember { DrawerState(DrawerValue.Closed) }, |
| 262 | drawerContent = { |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 263 | Box( |
| 264 | Modifier |
| 265 | .testTag(drawerContentBoxTag) |
| 266 | .border(2.dp, Color.Red)) { |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 267 | BasicText(text = if (it == DrawerValue.Open) "Opened" else "Closed") |
| 268 | } |
| 269 | } |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 270 | ) { Box( |
| 271 | Modifier |
| 272 | .fillMaxWidth() |
| 273 | .testTag(contentWidthBoxTag)) } |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | val rightEdgeOfRoot = rule.onRoot().getUnclippedBoundsInRoot().right |
| 278 | rule.onNodeWithTag(drawerContentBoxTag).assertRightPositionInRootIsEqualTo(rightEdgeOfRoot) |
| 279 | } |
| 280 | |
| 281 | @Test |
| 282 | fun modalNavigationDrawer_rtl_drawerExpandsTowardsEnd() { |
| 283 | val contentWidthBoxTag = "contentWidthBox" |
| 284 | val drawerContentBoxTag = "drawerContentBox" |
| 285 | var drawerState: DrawerState? = null |
| 286 | rule.setContent { |
| 287 | CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { |
| 288 | drawerState = remember { DrawerState(DrawerValue.Closed) } |
| 289 | ModalNavigationDrawer( |
| 290 | drawerState = drawerState!!, |
| 291 | drawerContent = { |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 292 | Box( |
| 293 | Modifier |
| 294 | .testTag(drawerContentBoxTag) |
| 295 | .border(2.dp, Color.Red)) { |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 296 | BasicText(text = if (it == DrawerValue.Open) "Opened" else "Closed") |
| 297 | } |
| 298 | } |
Vighnesh Raut | bc81651 | 2023-03-15 16:31:27 +0530 | [diff] [blame] | 299 | ) { Box( |
| 300 | Modifier |
| 301 | .fillMaxWidth() |
| 302 | .testTag(contentWidthBoxTag)) } |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | val endPositionInClosedState = |
| 307 | rule.onNodeWithTag(drawerContentBoxTag).getUnclippedBoundsInRoot().left |
| 308 | |
| 309 | rule.runOnIdle { drawerState?.setValue(DrawerValue.Open) } |
| 310 | val endPositionInOpenState = |
| 311 | rule.onNodeWithTag(drawerContentBoxTag).getUnclippedBoundsInRoot().left |
| 312 | |
| 313 | assert(endPositionInClosedState.value > endPositionInOpenState.value) |
| 314 | } |
| 315 | |
Aditya Arora | 0aa705f | 2023-04-03 20:34:37 +0530 | [diff] [blame] | 316 | @OptIn(ExperimentalTestApi::class, ExperimentalComposeUiApi::class) |
| 317 | @Test |
| 318 | fun modalNavigationDrawer_parentContainerGainsFocus_onBackPress() { |
| 319 | val drawerFocusRequester = FocusRequester() |
| 320 | rule.setContent { |
| 321 | Box( |
| 322 | modifier = Modifier |
| 323 | .testTag("box-container") |
| 324 | .fillMaxSize() |
| 325 | .focusable() |
| 326 | ) { |
| 327 | ModalNavigationDrawer( |
| 328 | modifier = Modifier.focusRequester(drawerFocusRequester), |
| 329 | drawerState = remember { DrawerState(DrawerValue.Closed) }, |
| 330 | drawerContent = { |
| 331 | BasicText( |
| 332 | text = if (it == DrawerValue.Open) "Opened" else "Closed", |
| 333 | modifier = Modifier.focusable() |
| 334 | ) |
| 335 | } |
| 336 | ) { |
| 337 | BasicText("other content") |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | rule.onAllNodesWithText("Closed").assertAnyAreDisplayed() |
| 343 | |
| 344 | rule.runOnIdle { |
| 345 | drawerFocusRequester.requestFocus() |
| 346 | } |
| 347 | |
| 348 | rule.onAllNodesWithText("Opened").assertAnyAreDisplayed() |
| 349 | rule.onNodeWithTag("box-container").assertIsNotFocused() |
| 350 | |
| 351 | // Trigger back press |
| 352 | rule.onRoot().performKeyInput { pressKey(Key.Back) } |
| 353 | rule.waitForIdle() |
| 354 | |
| 355 | // Check if the parent container gains focus |
| 356 | rule.onNodeWithTag("box-container").assertIsFocused() |
| 357 | } |
| 358 | |
Vineet Kumar | aaed255 | 2022-08-28 17:53:07 +0530 | [diff] [blame] | 359 | private fun SemanticsNodeInteractionCollection.assertAnyAreDisplayed() { |
| 360 | val result = (0 until fetchSemanticsNodes().size).map { get(it) }.any { |
| 361 | try { |
| 362 | it.assertIsDisplayed() |
| 363 | true |
| 364 | } catch (e: AssertionError) { |
| 365 | false |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if (!result) throw AssertionError("Assert failed: None of the components are displayed!") |
| 370 | } |
| 371 | |
| 372 | private fun SemanticsNodeInteraction.assertRightPositionInRootIsEqualTo( |
| 373 | expectedRight: Dp |
| 374 | ): SemanticsNodeInteraction { |
| 375 | return withUnclippedBoundsInRoot { |
| 376 | it.right.assertIsEqualTo(expectedRight, "right") |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | private fun SemanticsNodeInteraction.withUnclippedBoundsInRoot( |
| 381 | assertion: (DpRect) -> Unit |
| 382 | ): SemanticsNodeInteraction { |
| 383 | val node = fetchSemanticsNode("Failed to retrieve bounds of the node.") |
| 384 | val bounds = with(node.layoutInfo.density) { |
| 385 | node.unclippedBoundsInRoot.let { |
| 386 | DpRect(it.left.toDp(), it.top.toDp(), it.right.toDp(), it.bottom.toDp()) |
| 387 | } |
| 388 | } |
| 389 | assertion.invoke(bounds) |
| 390 | return this |
| 391 | } |
| 392 | |
| 393 | private val SemanticsNode.unclippedBoundsInRoot: Rect |
| 394 | get() { |
| 395 | return if (layoutInfo.isPlaced) { |
| 396 | Rect(positionInRoot, size.toSize()) |
| 397 | } else { |
| 398 | Dp.Unspecified.value.let { Rect(it, it, it, it) } |
| 399 | } |
| 400 | } |
| 401 | } |