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