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