Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +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.material |
| 18 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 19 | import android.os.SystemClock.sleep |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 20 | import androidx.compose.Model |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 21 | import androidx.test.filters.MediumTest |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 22 | import androidx.ui.core.OnChildPositioned |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 23 | import androidx.ui.core.OnPositioned |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 24 | import androidx.ui.core.TestTag |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 25 | import androidx.ui.foundation.Clickable |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 26 | import androidx.ui.layout.Container |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 27 | import androidx.ui.semantics.Semantics |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 28 | import androidx.ui.test.createComposeRule |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 29 | import androidx.ui.test.doGesture |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 30 | import androidx.ui.test.findByTag |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 31 | import androidx.ui.test.globalBounds |
| 32 | import androidx.ui.test.sendClick |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 33 | import androidx.ui.unit.IntPx |
| 34 | import androidx.ui.unit.IntPxSize |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 35 | import androidx.ui.unit.PxPosition |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 36 | import androidx.ui.unit.dp |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 37 | import androidx.ui.unit.height |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 38 | import androidx.ui.unit.px |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 39 | import androidx.ui.unit.round |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 40 | import androidx.ui.unit.width |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 41 | import com.google.common.truth.Truth.assertThat |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 42 | import org.junit.Rule |
| 43 | import org.junit.Test |
| 44 | import org.junit.runner.RunWith |
| 45 | import org.junit.runners.JUnit4 |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 46 | import java.util.concurrent.CountDownLatch |
| 47 | import java.util.concurrent.TimeUnit |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 48 | import kotlin.math.roundToInt |
| 49 | |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 50 | @Model |
| 51 | data class DrawerStateHolder(var state: DrawerState) |
| 52 | |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 53 | @MediumTest |
| 54 | @RunWith(JUnit4::class) |
| 55 | class DrawerTest { |
| 56 | |
| 57 | @get:Rule |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 58 | val composeTestRule = createComposeRule(disableTransitions = true) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 59 | |
| 60 | @Test |
| 61 | fun modalDrawer_testOffset_whenOpened() { |
| 62 | var position: PxPosition? = null |
| 63 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 64 | ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 65 | Container(expanded = true) { |
| 66 | OnPositioned { coords -> |
| 67 | position = coords.localToGlobal(PxPosition.Origin) |
| 68 | } |
| 69 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 70 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 71 | } |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 72 | composeTestRule.runOnIdleCompose { |
| 73 | assertThat(position!!.x.value).isEqualTo(0f) |
| 74 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun modalDrawer_testOffset_whenClosed() { |
| 79 | var position: PxPosition? = null |
| 80 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 81 | ModalDrawerLayout(DrawerState.Closed, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 82 | Container(expanded = true) { |
| 83 | OnPositioned { coords -> |
| 84 | position = coords.localToGlobal(PxPosition.Origin) |
| 85 | } |
| 86 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 87 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 88 | } |
| 89 | val width = composeTestRule.displayMetrics.widthPixels |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 90 | composeTestRule.runOnIdleCompose { |
| 91 | assertThat(position!!.x.round().value).isEqualTo(-width) |
| 92 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Test |
| 96 | fun modalDrawer_testEndPadding_whenOpened() { |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 97 | var size: IntPxSize? = null |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 98 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 99 | ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 100 | Container(expanded = true) { |
| 101 | OnPositioned { coords -> |
| 102 | size = coords.size |
| 103 | } |
| 104 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 105 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | val width = composeTestRule.displayMetrics.widthPixels |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 109 | composeTestRule.runOnIdleComposeWithDensity { |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 110 | assertThat(size!!.width.value) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 111 | .isEqualTo(width - 56.dp.toPx().round().value) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | fun bottomDrawer_testOffset_whenOpened() { |
| 117 | var position: PxPosition? = null |
| 118 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 119 | BottomDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 120 | Container(expanded = true) { |
| 121 | OnPositioned { coords -> |
| 122 | position = coords.localToGlobal(PxPosition.Origin) |
| 123 | } |
| 124 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 125 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 126 | } |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 127 | |
Matvei Malkov | 2270be1 | 2019-07-08 16:59:45 +0100 | [diff] [blame] | 128 | val width = composeTestRule.displayMetrics.widthPixels |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 129 | val height = composeTestRule.displayMetrics.heightPixels |
Matvei Malkov | 2270be1 | 2019-07-08 16:59:45 +0100 | [diff] [blame] | 130 | // temporary calculation of landscape screen |
| 131 | val expectedHeight = if (width > height) height else (height / 2f).roundToInt() |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 132 | composeTestRule.runOnIdleCompose { |
| 133 | assertThat(position!!.y.round().value).isEqualTo(expectedHeight) |
| 134 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Test |
| 138 | fun bottomDrawer_testOffset_whenClosed() { |
| 139 | var position: PxPosition? = null |
| 140 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 141 | BottomDrawerLayout(DrawerState.Closed, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 142 | Container(expanded = true) { |
| 143 | OnPositioned { coords -> |
| 144 | position = coords.localToGlobal(PxPosition.Origin) |
| 145 | } |
| 146 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 147 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 148 | } |
| 149 | val height = composeTestRule.displayMetrics.heightPixels |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 150 | composeTestRule.runOnIdleCompose { |
| 151 | assertThat(position!!.y.round().value).isEqualTo(height) |
| 152 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
| 156 | fun staticDrawer_testWidth_whenOpened() { |
| 157 | composeTestRule |
Matvei Malkov | 804bee4 | 2019-07-16 16:24:06 +0100 | [diff] [blame] | 158 | .setMaterialContentAndCollectSizes { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 159 | StaticDrawer { |
| 160 | Container(expanded = true) {} |
| 161 | } |
| 162 | } |
| 163 | .assertWidthEqualsTo(256.dp) |
| 164 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 165 | |
| 166 | @Test |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 167 | fun modalDrawer_openAndClose() { |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 168 | var contentWidth: IntPx? = null |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 169 | var openedLatch: CountDownLatch? = null |
| 170 | var closedLatch: CountDownLatch? = CountDownLatch(1) |
| 171 | val drawerState = DrawerStateHolder(DrawerState.Closed) |
| 172 | composeTestRule.setMaterialContent { |
| 173 | TestTag("Drawer") { |
| 174 | Semantics(container = true) { |
| 175 | ModalDrawerLayout(drawerState.state, { drawerState.state = it }, |
| 176 | drawerContent = { |
| 177 | OnChildPositioned({ info -> |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 178 | val pos = info.localToGlobal(PxPosition.Origin) |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 179 | if (pos.x == 0.px) { |
| 180 | // If fully opened, mark the openedLatch if present |
| 181 | openedLatch?.countDown() |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 182 | } else if (-pos.x.round() == contentWidth) { |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 183 | // If fully closed, mark the closedLatch if present |
| 184 | closedLatch?.countDown() |
| 185 | } |
| 186 | }) { |
| 187 | Container(expanded = true) {} |
| 188 | } |
| 189 | }, |
| 190 | bodyContent = { |
| 191 | OnChildPositioned({ contentWidth = it.size.width }) { |
| 192 | Container(expanded = true) {} |
| 193 | } |
| 194 | }) |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | // Drawer should start in closed state |
| 199 | assertThat(closedLatch!!.await(5, TimeUnit.SECONDS)).isTrue() |
| 200 | |
| 201 | // When the drawer state is set to Opened |
| 202 | openedLatch = CountDownLatch(1) |
| 203 | composeTestRule.runOnIdleCompose { |
| 204 | drawerState.state = DrawerState.Opened |
| 205 | } |
| 206 | // Then the drawer should be opened |
| 207 | assertThat(openedLatch.await(5, TimeUnit.SECONDS)).isTrue() |
| 208 | |
| 209 | // When the drawer state is set to Closed |
| 210 | closedLatch = CountDownLatch(1) |
| 211 | composeTestRule.runOnIdleCompose { |
| 212 | drawerState.state = DrawerState.Closed |
| 213 | } |
| 214 | // Then the drawer should be closed |
| 215 | assertThat(closedLatch.await(5, TimeUnit.SECONDS)).isTrue() |
| 216 | } |
| 217 | |
| 218 | @Test |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 219 | fun modalDrawer_bodyContent_clickable() { |
| 220 | var drawerClicks = 0 |
| 221 | var bodyClicks = 0 |
| 222 | val drawerState = DrawerStateHolder(DrawerState.Closed) |
| 223 | composeTestRule.setMaterialContent { |
Aurimas Liutikas | 7a828d3 | 2019-10-07 17:16:05 -0700 | [diff] [blame] | 224 | // emulate click on the screen |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 225 | TestTag("Drawer") { |
| 226 | Semantics(container = true) { |
| 227 | ModalDrawerLayout(drawerState.state, { drawerState.state = it }, |
| 228 | drawerContent = { |
| 229 | Clickable(onClick = { drawerClicks += 1 }) { |
| 230 | Container(expanded = true) {} |
| 231 | } |
| 232 | }, |
| 233 | bodyContent = { |
| 234 | Clickable(onClick = { bodyClicks += 1 }) { |
| 235 | Container(expanded = true) {} |
| 236 | } |
| 237 | }) |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 242 | // Click in the middle of the drawer (which is the middle of the body) |
| 243 | findByTag("Drawer").doGesture { sendClick() } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 244 | |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 245 | composeTestRule.runOnIdleCompose { |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 246 | assertThat(drawerClicks).isEqualTo(0) |
| 247 | assertThat(bodyClicks).isEqualTo(1) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 248 | |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 249 | drawerState.state = DrawerState.Opened |
| 250 | } |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 251 | sleep(100) // TODO(147586311): remove this sleep when opening the drawer triggers a wait |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 252 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 253 | // Click on the left-center pixel of the drawer |
| 254 | findByTag("Drawer").doGesture { |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 255 | val left = 1.px |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 256 | val centerY = globalBounds.height / 2 |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 257 | sendClick(PxPosition(left, centerY)) |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 258 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 259 | |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 260 | composeTestRule.runOnIdleCompose { |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 261 | assertThat(drawerClicks).isEqualTo(1) |
| 262 | assertThat(bodyClicks).isEqualTo(1) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 263 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Test |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 267 | fun bottomDrawer_openAndClose() { |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 268 | var contentHeight: IntPx? = null |
| 269 | var openedHeight: IntPx? = null |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 270 | var openedLatch: CountDownLatch? = null |
| 271 | var closedLatch: CountDownLatch? = CountDownLatch(1) |
| 272 | val drawerState = DrawerStateHolder(DrawerState.Closed) |
| 273 | composeTestRule.setMaterialContent { |
| 274 | TestTag("Drawer") { |
| 275 | Semantics(container = true) { |
| 276 | BottomDrawerLayout(drawerState.state, { drawerState.state = it }, |
| 277 | drawerContent = { |
| 278 | OnChildPositioned({ info -> |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 279 | val pos = info.localToGlobal(PxPosition.Origin) |
| 280 | if (pos.y.round() == openedHeight) { |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 281 | // If fully opened, mark the openedLatch if present |
| 282 | openedLatch?.countDown() |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 283 | } else if (pos.y.round() == contentHeight) { |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 284 | // If fully closed, mark the closedLatch if present |
| 285 | closedLatch?.countDown() |
| 286 | } |
| 287 | }) { |
| 288 | Container(expanded = true) {} |
| 289 | } |
| 290 | }, |
| 291 | bodyContent = { |
| 292 | OnChildPositioned({ |
| 293 | contentHeight = it.size.height |
| 294 | openedHeight = it.size.height * BottomDrawerOpenFraction |
| 295 | }) { |
| 296 | Container(expanded = true) {} |
| 297 | } |
| 298 | }) |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | // Drawer should start in closed state |
| 303 | assertThat(closedLatch!!.await(5, TimeUnit.SECONDS)).isTrue() |
| 304 | |
| 305 | // When the drawer state is set to Opened |
| 306 | openedLatch = CountDownLatch(1) |
| 307 | composeTestRule.runOnIdleCompose { |
| 308 | drawerState.state = DrawerState.Opened |
| 309 | } |
| 310 | // Then the drawer should be opened |
| 311 | assertThat(openedLatch.await(5, TimeUnit.SECONDS)).isTrue() |
| 312 | |
| 313 | // When the drawer state is set to Closed |
| 314 | closedLatch = CountDownLatch(1) |
| 315 | composeTestRule.runOnIdleCompose { |
| 316 | drawerState.state = DrawerState.Closed |
| 317 | } |
| 318 | // Then the drawer should be closed |
| 319 | assertThat(closedLatch.await(5, TimeUnit.SECONDS)).isTrue() |
| 320 | } |
| 321 | |
| 322 | @Test |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 323 | fun bottomDrawer_bodyContent_clickable() { |
| 324 | var drawerClicks = 0 |
| 325 | var bodyClicks = 0 |
| 326 | val drawerState = DrawerStateHolder(DrawerState.Closed) |
| 327 | composeTestRule.setMaterialContent { |
Aurimas Liutikas | 7a828d3 | 2019-10-07 17:16:05 -0700 | [diff] [blame] | 328 | // emulate click on the screen |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 329 | TestTag("Drawer") { |
| 330 | Semantics(container = true) { |
| 331 | BottomDrawerLayout(drawerState.state, { drawerState.state = it }, |
| 332 | drawerContent = { |
| 333 | Clickable(onClick = { drawerClicks += 1 }) { |
| 334 | Container(expanded = true) {} |
| 335 | } |
| 336 | }, |
| 337 | bodyContent = { |
| 338 | Clickable(onClick = { bodyClicks += 1 }) { |
| 339 | Container(expanded = true) {} |
| 340 | } |
| 341 | }) |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 346 | // Click in the middle of the drawer (which is the middle of the body) |
| 347 | findByTag("Drawer").doGesture { sendClick() } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 348 | |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 349 | composeTestRule.runOnIdleCompose { |
| 350 | assertThat(drawerClicks).isEqualTo(0) |
| 351 | assertThat(bodyClicks).isEqualTo(1) |
| 352 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 353 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 354 | composeTestRule.runOnUiThread { |
| 355 | drawerState.state = DrawerState.Opened |
| 356 | } |
| 357 | sleep(100) // TODO(147586311): remove this sleep when opening the drawer triggers a wait |
| 358 | |
| 359 | // Click on the bottom-center pixel of the drawer |
| 360 | findByTag("Drawer").doGesture { |
| 361 | val bounds = globalBounds |
| 362 | val centerX = bounds.width / 2 |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 363 | val bottom = bounds.height - 1.px |
| 364 | sendClick(PxPosition(centerX, bottom)) |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | assertThat(drawerClicks).isEqualTo(1) |
| 368 | assertThat(bodyClicks).isEqualTo(1) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 369 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 370 | } |