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