[go: nahoru, domu]

blob: f285b86ae48eaa2a741d0210c32b9b7dd76616f5 [file] [log] [blame]
Cătălin Tudor37adfc72019-09-04 17:58:58 +01001/*
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
17package androidx.ui.test
18
19import androidx.compose.Composable
Jelle Fresenb4c281a2020-05-11 15:15:43 +010020import androidx.compose.getValue
21import androidx.compose.mutableStateOf
22import androidx.compose.setValue
Cătălin Tudor37adfc72019-09-04 17:58:58 +010023import androidx.test.filters.MediumTest
24import androidx.ui.core.Layout
Adam Powell999a89b2020-03-11 09:08:07 -070025import androidx.ui.core.Modifier
Jelle Fresenb4c281a2020-05-11 15:15:43 +010026import androidx.ui.foundation.Box
Cătălin Tudor37adfc72019-09-04 17:58:58 +010027import androidx.ui.foundation.VerticalScroller
Jelle Fresenb4c281a2020-05-11 15:15:43 +010028import androidx.ui.graphics.Color
Cătălin Tudor37adfc72019-09-04 17:58:58 +010029import androidx.ui.layout.Column
Cătălin Tudor37adfc72019-09-04 17:58:58 +010030import androidx.ui.layout.Row
Anastasia Soboleva22015a62020-03-12 12:48:37 +000031import androidx.ui.layout.Stack
Jelle Fresenb4c281a2020-05-11 15:15:43 +010032import androidx.ui.layout.fillMaxHeight
33import androidx.ui.layout.fillMaxWidth
34import androidx.ui.layout.height
35import androidx.ui.layout.size
36import androidx.ui.layout.width
37import androidx.ui.test.util.BoundaryNode
38import androidx.ui.unit.Dp
George Mount842c8c12020-01-08 16:03:42 -080039import androidx.ui.unit.dp
40import androidx.ui.unit.ipx
George Mount842c8c12020-01-08 16:03:42 -080041import org.junit.Rule
42import org.junit.Test
43import org.junit.runner.RunWith
44import org.junit.runners.JUnit4
Cătălin Tudor37adfc72019-09-04 17:58:58 +010045
46@MediumTest
47@RunWith(JUnit4::class)
Jelle Fresen247ddb9cb2020-05-11 16:03:37 +010048class IsDisplayedTest {
Cătălin Tudor37adfc72019-09-04 17:58:58 +010049
50 @get:Rule
51 val composeTestRule = createComposeRule(disableTransitions = true)
52
Jelle Fresenb4c281a2020-05-11 15:15:43 +010053 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 Tudor37adfc72019-09-04 17:58:58 +010066 @Test
67 fun componentInScrollable_isDisplayed() {
Jelle Fresenb4c281a2020-05-11 15:15:43 +010068 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 Tudor37adfc72019-09-04 17:58:58 +010075
Jelle Fresenb4c281a2020-05-11 15:15:43 +010076 findByTag("item0")
Cătălin Tudor37adfc72019-09-04 17:58:58 +010077 .assertIsDisplayed()
78 }
79
80 @Test
81 fun componentInScrollable_isNotDisplayed() {
Cătălin Tudor37adfc72019-09-04 17:58:58 +010082 composeTestRule.setContent {
Jelle Fresenb4c281a2020-05-11 15:15:43 +010083 VerticalScroller(modifier = Modifier.size(100.dp)) {
Anastasia Soboleva875d9572020-02-11 14:53:47 +000084 Column {
Jelle Fresenb4c281a2020-05-11 15:15:43 +010085 repeat(10) { Item(it, height = 30.dp) }
Cătălin Tudor37adfc72019-09-04 17:58:58 +010086 }
87 }
88 }
Jelle Fresenb4c281a2020-05-11 15:15:43 +010089
90 findByTag("item4")
91 .assertIsNotDisplayed()
Cătălin Tudor37adfc72019-09-04 17:58:58 +010092 }
93
94 @Test
95 fun toggleParentVisibility() {
Jelle Fresenb4c281a2020-05-11 15:15:43 +010096 var place by mutableStateOf(true)
Cătălin Tudor37adfc72019-09-04 17:58:58 +010097
Jelle Fresenb4c281a2020-05-11 15:15:43 +010098 // Place `Stack { Item(0) }` or not, based on the value of [place]
Cătălin Tudor37adfc72019-09-04 17:58:58 +010099 composeTestRule.setContent {
Jelle Fresenb4c281a2020-05-11 15:15:43 +0100100 Layout({
Anastasia Soboleva22015a62020-03-12 12:48:37 +0000101 Stack {
Jelle Fresenb4c281a2020-05-11 15:15:43 +0100102 // 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 Mentley7865a632019-08-20 20:10:29 -0700110 }
Jelle Fresenb4c281a2020-05-11 15:15:43 +0100111 } else {
112 layout(0.ipx, 0.ipx) {}
Cătălin Tudor37adfc72019-09-04 17:58:58 +0100113 }
114 }
Cătălin Tudor37adfc72019-09-04 17:58:58 +0100115 }
116
Jelle Fresenb4c281a2020-05-11 15:15:43 +0100117 findByTag("item0")
Cătălin Tudor37adfc72019-09-04 17:58:58 +0100118 .assertIsDisplayed()
119
Jelle Fresenb4c281a2020-05-11 15:15:43 +0100120 runOnIdleCompose {
121 place = false
Cătălin Tudor37adfc72019-09-04 17:58:58 +0100122 }
123
Jelle Fresenb4c281a2020-05-11 15:15:43 +0100124 findByTag("item0")
Cătălin Tudor37adfc72019-09-04 17:58:58 +0100125 .assertIsNotDisplayed()
126 }
127
128 @Test
129 fun rowTooSmall() {
130 composeTestRule.setContent {
Jelle Fresenb4c281a2020-05-11 15:15:43 +0100131 Row(modifier = Modifier.size(100.dp)) {
132 repeat(10) { Item(it, width = 30.dp) }
Cătălin Tudor37adfc72019-09-04 17:58:58 +0100133 }
134 }
135
Jelle Fresenb4c281a2020-05-11 15:15:43 +0100136 findByTag("item9")
Cătălin Tudor37adfc72019-09-04 17:58:58 +0100137 .assertIsNotDisplayed()
138 }
Cătălin Tudor37adfc72019-09-04 17:58:58 +0100139}