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