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 | |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 19 | import android.view.View |
| 20 | import android.view.ViewGroup |
| 21 | import android.widget.FrameLayout |
| 22 | import androidx.activity.ComponentActivity |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 23 | import androidx.compose.Composable |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 24 | import androidx.compose.Recomposer |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 25 | import androidx.compose.getValue |
| 26 | import androidx.compose.mutableStateOf |
| 27 | import androidx.compose.setValue |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 28 | import androidx.test.espresso.Espresso.onView |
| 29 | import androidx.test.espresso.ViewInteraction |
| 30 | import androidx.test.espresso.assertion.ViewAssertions.matches |
| 31 | import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
| 32 | import androidx.test.espresso.matcher.ViewMatchers.withId |
| 33 | import androidx.test.espresso.matcher.ViewMatchers.withParent |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 34 | import androidx.test.ext.junit.rules.ActivityScenarioRule |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 35 | import androidx.test.filters.MediumTest |
| 36 | import androidx.ui.core.Layout |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 37 | import androidx.ui.core.Modifier |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 38 | import androidx.ui.core.setContent |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 39 | import androidx.compose.foundation.Box |
| 40 | import androidx.compose.foundation.ScrollableColumn |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 41 | import androidx.ui.graphics.Color |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 42 | import androidx.compose.foundation.layout.Row |
| 43 | import androidx.compose.foundation.layout.Stack |
| 44 | import androidx.compose.foundation.layout.fillMaxHeight |
| 45 | import androidx.compose.foundation.layout.fillMaxWidth |
| 46 | import androidx.compose.foundation.layout.height |
| 47 | import androidx.compose.foundation.layout.size |
| 48 | import androidx.compose.foundation.layout.width |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 49 | import androidx.ui.test.android.AndroidComposeTestRule |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 50 | import androidx.ui.test.util.BoundaryNode |
| 51 | import androidx.ui.unit.Dp |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 52 | import androidx.ui.unit.dp |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 53 | import org.hamcrest.CoreMatchers.allOf |
| 54 | import org.hamcrest.CoreMatchers.not |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 55 | import org.junit.Rule |
| 56 | import org.junit.Test |
| 57 | import org.junit.runner.RunWith |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 58 | import org.junit.runners.Parameterized |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 59 | |
| 60 | @MediumTest |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 61 | @RunWith(Parameterized::class) |
| 62 | class IsDisplayedTest(val config: BitmapCapturingTest.TestConfig) { |
| 63 | data class TestConfig( |
| 64 | val activityClass: Class<out ComponentActivity> |
| 65 | ) |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 66 | |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 67 | companion object { |
| 68 | @JvmStatic |
| 69 | @Parameterized.Parameters(name = "{0}") |
| 70 | fun createTestSet(): List<BitmapCapturingTest.TestConfig> = listOf( |
| 71 | BitmapCapturingTest.TestConfig(ComponentActivity::class.java), |
| 72 | BitmapCapturingTest.TestConfig(ActivityWithActionBar::class.java) |
| 73 | ) |
| 74 | } |
| 75 | |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 76 | @get:Rule |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 77 | val composeTestRule = AndroidComposeTestRule( |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 78 | ActivityScenarioRule(config.activityClass) |
| 79 | ) |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 80 | |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 81 | private val colors = listOf(Color.Red, Color.Green, Color.Blue) |
| 82 | |
| 83 | @Composable |
| 84 | private fun Item(i: Int, width: Dp? = null, height: Dp? = null) { |
| 85 | BoundaryNode("item$i") { |
| 86 | Box( |
| 87 | modifier = with(Modifier) { width?.let { width(it) } ?: fillMaxWidth() } + |
| 88 | with(Modifier) { height?.let { height(it) } ?: fillMaxHeight() }, |
| 89 | backgroundColor = colors[i % colors.size] |
| 90 | ) |
| 91 | } |
| 92 | } |
| 93 | |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 94 | @Composable |
| 95 | fun PlaceConditionally(place: Boolean, child: @Composable () -> Unit) { |
Anastasia Soboleva | 5e382dd | 2020-06-17 21:51:38 +0100 | [diff] [blame] | 96 | Layout(children = child) { measurables, constraints -> |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 97 | if (place) { |
| 98 | val placeable = measurables[0].measure(constraints) |
| 99 | layout(placeable.width, placeable.height) { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 100 | placeable.place(0, 0) |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 101 | } |
| 102 | } else { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 103 | layout(0, 0) {} |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 108 | @Test |
| 109 | fun componentInScrollable_isDisplayed() { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 110 | composeTestRule.setContent { |
Matvei Malkov | 235b4fa | 2020-07-07 21:14:49 +0100 | [diff] [blame] | 111 | ScrollableColumn(modifier = Modifier.size(100.dp)) { |
Matvei Malkov | 9fb6924 | 2020-05-19 14:07:14 +0100 | [diff] [blame] | 112 | repeat(10) { Item(it, height = 30.dp) } |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 113 | } |
| 114 | } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 115 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 116 | onNodeWithTag("item0") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 117 | .assertIsDisplayed() |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | fun componentInScrollable_isNotDisplayed() { |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 122 | composeTestRule.setContent { |
Matvei Malkov | 235b4fa | 2020-07-07 21:14:49 +0100 | [diff] [blame] | 123 | ScrollableColumn(modifier = Modifier.size(100.dp)) { |
Matvei Malkov | 9fb6924 | 2020-05-19 14:07:14 +0100 | [diff] [blame] | 124 | repeat(10) { Item(it, height = 30.dp) } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 127 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 128 | onNodeWithTag("item4") |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 129 | .assertIsNotDisplayed() |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 133 | fun togglePlacement() { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 134 | var place by mutableStateOf(true) |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 135 | |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 136 | composeTestRule.setContent { |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 137 | PlaceConditionally(place) { |
| 138 | // Item instead of BoundaryNode because we need non-zero size |
| 139 | Item(0) |
| 140 | } |
| 141 | } |
| 142 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 143 | onNodeWithTag("item0") |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 144 | .assertIsDisplayed() |
| 145 | |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 146 | runOnIdle { |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 147 | place = false |
| 148 | } |
| 149 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 150 | onNodeWithTag("item0") |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 151 | .assertIsNotDisplayed() |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | fun toggleParentPlacement() { |
| 156 | var place by mutableStateOf(true) |
| 157 | |
| 158 | composeTestRule.setContent { |
| 159 | PlaceConditionally(place) { |
Anastasia Soboleva | 22015a6 | 2020-03-12 12:48:37 +0000 | [diff] [blame] | 160 | Stack { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 161 | // Item instead of BoundaryNode because we need non-zero size |
| 162 | Item(0) |
| 163 | } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 164 | } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 167 | onNodeWithTag("item0") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 168 | .assertIsDisplayed() |
| 169 | |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 170 | runOnIdle { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 171 | place = false |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 174 | onNodeWithTag("item0") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 175 | .assertIsNotDisplayed() |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | fun rowTooSmall() { |
| 180 | composeTestRule.setContent { |
Jelle Fresen | b4c281a | 2020-05-11 15:15:43 +0100 | [diff] [blame] | 181 | Row(modifier = Modifier.size(100.dp)) { |
| 182 | repeat(10) { Item(it, width = 30.dp) } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 186 | onNodeWithTag("item9") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 187 | .assertIsNotDisplayed() |
| 188 | } |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 189 | |
| 190 | @Test |
| 191 | fun viewVisibility_androidComposeView() { |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 192 | lateinit var androidComposeView: View |
| 193 | composeTestRule.activityRule.scenario.onActivity { activity -> |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 194 | // FrameLayout(id=100, w=100, h=100) |
| 195 | // '- AndroidComposeView |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 196 | androidComposeView = FrameLayout(activity).apply { |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 197 | id = 100 |
| 198 | layoutParams = ViewGroup.MarginLayoutParams(100, 100) |
| 199 | activity.setContentView(this) |
| 200 | setContent(Recomposer.current()) { |
| 201 | Item(0) |
| 202 | } |
| 203 | }.getChildAt(0) |
| 204 | } |
| 205 | |
| 206 | fun onComposeView(): ViewInteraction { |
| 207 | return onView(allOf(withParent(withId(100)))) |
| 208 | } |
| 209 | |
| 210 | onComposeView().check(matches(isDisplayed())) |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 211 | onNodeWithTag("item0").assertIsDisplayed() |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 212 | |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 213 | runOnIdle { |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 214 | androidComposeView.visibility = View.GONE |
| 215 | } |
| 216 | |
| 217 | onComposeView().check(matches(not(isDisplayed()))) |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 218 | onNodeWithTag("item0").assertIsNotDisplayed() |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Test |
| 222 | fun viewVisibility_parentView() { |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 223 | lateinit var composeContainer: View |
| 224 | composeTestRule.activityRule.scenario.onActivity { activity -> |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 225 | // FrameLayout |
| 226 | // '- FrameLayout(id=100, w=100, h=100) -> composeContainer |
| 227 | // '- AndroidComposeView |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 228 | composeContainer = FrameLayout(activity).apply { |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 229 | id = 100 |
| 230 | layoutParams = ViewGroup.MarginLayoutParams(100, 100) |
| 231 | activity.setContentView(FrameLayout(activity).also { it.addView(this) }) |
| 232 | setContent(Recomposer.current()) { |
| 233 | Item(0) |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | fun onComposeView(): ViewInteraction { |
| 239 | return onView(allOf(withParent(withId(100)))) |
| 240 | } |
| 241 | |
| 242 | onComposeView().check(matches(isDisplayed())) |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 243 | onNodeWithTag("item0").assertIsDisplayed() |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 244 | |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 245 | runOnIdle { |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 246 | composeContainer.visibility = View.GONE |
| 247 | } |
| 248 | |
| 249 | onComposeView().check(matches(not(isDisplayed()))) |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 250 | onNodeWithTag("item0").assertIsNotDisplayed() |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 251 | } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 252 | } |