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