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