Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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.ui.test |
| 18 | |
| 19 | import androidx.compose.Composable |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 20 | import androidx.compose.getValue |
| 21 | import androidx.compose.mutableStateOf |
| 22 | import androidx.compose.setValue |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 23 | import androidx.test.filters.MediumTest |
| 24 | import androidx.ui.core.Layout |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 25 | import androidx.ui.core.Modifier |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 26 | import androidx.ui.foundation.Box |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 27 | import androidx.ui.foundation.VerticalScroller |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 28 | import androidx.ui.graphics.Color |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 29 | import androidx.ui.layout.Column |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 30 | import androidx.ui.layout.Row |
Anastasia Soboleva | 22015a6 | 2020-03-12 12:48:37 +0000 | [diff] [blame] | 31 | import androidx.ui.layout.Stack |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 32 | import androidx.ui.layout.fillMaxHeight |
| 33 | import androidx.ui.layout.fillMaxWidth |
| 34 | import androidx.ui.layout.height |
| 35 | import androidx.ui.layout.size |
| 36 | import androidx.ui.layout.width |
| 37 | import androidx.ui.test.util.BoundaryNode |
| 38 | import androidx.ui.unit.Dp |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 39 | import androidx.ui.unit.dp |
| 40 | import androidx.ui.unit.ipx |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 41 | import org.junit.Rule |
| 42 | import org.junit.Test |
| 43 | import org.junit.runner.RunWith |
| 44 | import org.junit.runners.JUnit4 |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 45 | |
| 46 | @MediumTest |
| 47 | @RunWith(JUnit4::class) |
Jelle Fresen | 247ddb9cb | 2020-05-11 16:03:37 +0100 | [diff] [blame] | 48 | class IsDisplayedTest { |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 49 | |
| 50 | @get:Rule |
| 51 | val composeTestRule = createComposeRule(disableTransitions = true) |
| 52 | |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 53 | private val colors = listOf(Color.Red, Color.Green, Color.Blue) |
| 54 | |
| 55 | @Composable |
| 56 | private fun Item(i: Int, width: Dp? = null, height: Dp? = null) { |
| 57 | BoundaryNode("item$i") { |
| 58 | Box( |
| 59 | modifier = with(Modifier) { width?.let { width(it) } ?: fillMaxWidth() } + |
| 60 | with(Modifier) { height?.let { height(it) } ?: fillMaxHeight() }, |
| 61 | backgroundColor = colors[i % colors.size] |
| 62 | ) |
| 63 | } |
| 64 | } |
| 65 | |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 66 | @Test |
| 67 | fun componentInScrollable_isDisplayed() { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 68 | composeTestRule.setContent { |
| 69 | VerticalScroller(modifier = Modifier.size(100.dp)) { |
| 70 | Column { |
| 71 | repeat(10) { Item(it, height = 30.dp) } |
| 72 | } |
| 73 | } |
| 74 | } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 75 | |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 76 | findByTag("item0") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 77 | .assertIsDisplayed() |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | fun componentInScrollable_isNotDisplayed() { |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 82 | composeTestRule.setContent { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 83 | VerticalScroller(modifier = Modifier.size(100.dp)) { |
Anastasia Soboleva | 875d957 | 2020-02-11 14:53:47 +0000 | [diff] [blame] | 84 | Column { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 85 | repeat(10) { Item(it, height = 30.dp) } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 89 | |
| 90 | findByTag("item4") |
| 91 | .assertIsNotDisplayed() |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Test |
| 95 | fun toggleParentVisibility() { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 96 | var place by mutableStateOf(true) |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 97 | |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 98 | // Place `Stack { Item(0) }` or not, based on the value of [place] |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 99 | composeTestRule.setContent { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 100 | Layout({ |
Anastasia Soboleva | 22015a6 | 2020-03-12 12:48:37 +0000 | [diff] [blame] | 101 | Stack { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 102 | // Item instead of BoundaryNode because we need non-zero size |
| 103 | Item(0) |
| 104 | } |
| 105 | }) { measurables, constraints, _ -> |
| 106 | if (place) { |
| 107 | val placeable = measurables[0].measure(constraints) |
| 108 | layout(placeable.width, placeable.height) { |
| 109 | placeable.place(0.ipx, 0.ipx) |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 110 | } |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 111 | } else { |
| 112 | layout(0.ipx, 0.ipx) {} |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 113 | } |
| 114 | } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 117 | findByTag("item0") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 118 | .assertIsDisplayed() |
| 119 | |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 120 | runOnIdleCompose { |
| 121 | place = false |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 124 | findByTag("item0") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 125 | .assertIsNotDisplayed() |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | fun rowTooSmall() { |
| 130 | composeTestRule.setContent { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 131 | Row(modifier = Modifier.size(100.dp)) { |
| 132 | repeat(10) { Item(it, width = 30.dp) } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame^] | 136 | findByTag("item9") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 137 | .assertIsNotDisplayed() |
| 138 | } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 139 | } |