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