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 |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 29 | import androidx.ui.foundation.drawBackground |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 30 | import androidx.ui.geometry.Offset |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 31 | import androidx.ui.graphics.Color |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 32 | import androidx.ui.layout.fillMaxSize |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 33 | import androidx.ui.layout.rtl |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 34 | import androidx.ui.test.assertIsEqualTo |
| 35 | import androidx.ui.test.assertLeftPositionInRootIsEqualTo |
| 36 | import androidx.ui.test.assertTopPositionInRootIsEqualTo |
| 37 | import androidx.ui.test.assertWidthIsEqualTo |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 38 | import androidx.ui.test.createComposeRule |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 39 | import androidx.ui.test.doGesture |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 40 | import androidx.ui.test.findByTag |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 41 | import androidx.ui.test.globalBounds |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 42 | import androidx.ui.test.runOnIdleCompose |
| 43 | import androidx.ui.test.runOnUiThread |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 44 | import androidx.ui.test.sendClick |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 45 | import androidx.ui.test.sendSwipeDown |
| 46 | import androidx.ui.test.sendSwipeLeft |
| 47 | import androidx.ui.test.sendSwipeRight |
| 48 | import androidx.ui.test.sendSwipeUp |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 49 | import androidx.ui.unit.dp |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 50 | import androidx.ui.unit.height |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 51 | import androidx.ui.unit.width |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 52 | import com.google.common.truth.Truth.assertThat |
Matvei Malkov | 886ec6a | 2020-02-03 01:45:29 +0000 | [diff] [blame] | 53 | import org.junit.Ignore |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 54 | import org.junit.Rule |
| 55 | import org.junit.Test |
| 56 | import org.junit.runner.RunWith |
| 57 | import org.junit.runners.JUnit4 |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 58 | import java.util.concurrent.CountDownLatch |
| 59 | import java.util.concurrent.TimeUnit |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 60 | import kotlin.math.roundToInt |
| 61 | |
| 62 | @MediumTest |
| 63 | @RunWith(JUnit4::class) |
| 64 | class DrawerTest { |
| 65 | |
| 66 | @get:Rule |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 67 | val composeTestRule = createComposeRule(disableTransitions = true) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 68 | |
| 69 | @Test |
| 70 | fun modalDrawer_testOffset_whenOpened() { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 71 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 72 | ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 73 | Box(Modifier.fillMaxSize().testTag("content")) |
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 | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 76 | |
| 77 | findByTag("content") |
| 78 | .assertLeftPositionInRootIsEqualTo(0.dp) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | fun modalDrawer_testOffset_whenClosed() { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 83 | var position: Offset? = null |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 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 -> |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 87 | position = coords.localToRoot(Offset.Zero) |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 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 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 91 | |
| 92 | val width = rootWidth() |
| 93 | composeTestRule.runOnIdleComposeWithDensity { |
| 94 | position!!.x.toDp().assertIsEqualTo(-width) |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 95 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | fun modalDrawer_testEndPadding_whenOpened() { |
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 = { |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 102 | Box(Modifier.fillMaxSize().testTag("content")) |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame] | 103 | }, bodyContent = emptyContent()) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 106 | findByTag("content") |
| 107 | .assertWidthIsEqualTo(rootWidth() - 56.dp) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | fun bottomDrawer_testOffset_whenOpened() { |
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 = { |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 114 | Box(Modifier.fillMaxSize().testTag("content")) |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame] | 115 | }, bodyContent = emptyContent()) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 116 | } |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 117 | |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 118 | val width = rootWidth() |
| 119 | val height = rootHeight() |
| 120 | val expectedHeight = if (width > height) 0.dp else (height / 2) |
| 121 | findByTag("content") |
| 122 | .assertTopPositionInRootIsEqualTo(expectedHeight) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Test |
| 126 | fun bottomDrawer_testOffset_whenClosed() { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 127 | var position: Offset? = null |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 128 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 129 | BottomDrawerLayout(DrawerState.Closed, {}, drawerContent = { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 130 | Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates -> |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 131 | position = coords.localToRoot(Offset.Zero) |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 132 | }) |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame] | 133 | }, bodyContent = emptyContent()) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 134 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame^] | 135 | |
| 136 | val height = rootHeight() |
| 137 | composeTestRule.runOnIdleComposeWithDensity { |
| 138 | position!!.y.toDp().assertIsEqualTo(height) |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 139 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
Matvei Malkov | 886ec6a | 2020-02-03 01:45:29 +0000 | [diff] [blame] | 143 | @Ignore("failing in postsubmit, fix in b/148751721") |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 144 | fun modalDrawer_openAndClose() { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 145 | var contentWidth: Int? = null |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 146 | var openedLatch: CountDownLatch? = null |
| 147 | var closedLatch: CountDownLatch? = CountDownLatch(1) |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 148 | val drawerState = mutableStateOf(DrawerState.Closed) |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 149 | composeTestRule.setMaterialContent { |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 150 | ModalDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 151 | drawerContent = { |
| 152 | Box( |
| 153 | Modifier.fillMaxSize().onPositioned { info: LayoutCoordinates -> |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 154 | val pos = info.localToGlobal(Offset.Zero) |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 155 | if (pos.x == 0.0f) { |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 156 | // If fully opened, mark the openedLatch if present |
| 157 | openedLatch?.countDown() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 158 | } else if (-pos.x.roundToInt() == contentWidth) { |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 159 | // If fully closed, mark the closedLatch if present |
| 160 | closedLatch?.countDown() |
Matvei Malkov | 9f27abf | 2020-05-19 15:15:56 +0100 | [diff] [blame] | 161 | } |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 162 | } |
| 163 | ) |
| 164 | }, |
| 165 | bodyContent = { |
| 166 | Box(Modifier.fillMaxSize() |
| 167 | .onPositioned { contentWidth = it.size.width }) |
| 168 | }) |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 169 | } |
| 170 | // Drawer should start in closed state |
| 171 | assertThat(closedLatch!!.await(5, TimeUnit.SECONDS)).isTrue() |
| 172 | |
| 173 | // When the drawer state is set to Opened |
| 174 | openedLatch = CountDownLatch(1) |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 175 | runOnIdleCompose { |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 176 | drawerState.value = DrawerState.Opened |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 177 | } |
| 178 | // Then the drawer should be opened |
| 179 | assertThat(openedLatch.await(5, TimeUnit.SECONDS)).isTrue() |
| 180 | |
| 181 | // When the drawer state is set to Closed |
| 182 | closedLatch = CountDownLatch(1) |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 183 | runOnIdleCompose { |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 184 | drawerState.value = DrawerState.Closed |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 185 | } |
| 186 | // Then the drawer should be closed |
| 187 | assertThat(closedLatch.await(5, TimeUnit.SECONDS)).isTrue() |
| 188 | } |
| 189 | |
| 190 | @Test |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 191 | fun modalDrawer_bodyContent_clickable() { |
| 192 | var drawerClicks = 0 |
| 193 | var bodyClicks = 0 |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 194 | val drawerState = mutableStateOf(DrawerState.Closed) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 195 | composeTestRule.setMaterialContent { |
Aurimas Liutikas | 7a828d3 | 2019-10-07 17:16:05 -0700 | [diff] [blame] | 196 | // emulate click on the screen |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 197 | ModalDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 198 | drawerContent = { |
| 199 | Box( |
| 200 | Modifier.fillMaxSize().clickable { drawerClicks += 1 }, |
| 201 | children = emptyContent() |
| 202 | ) |
| 203 | }, |
| 204 | bodyContent = { |
| 205 | Box( |
| 206 | Modifier.testTag("Drawer").fillMaxSize().clickable { bodyClicks += 1 }, |
| 207 | children = emptyContent() |
| 208 | ) |
| 209 | }) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 212 | // Click in the middle of the drawer (which is the middle of the body) |
| 213 | findByTag("Drawer").doGesture { sendClick() } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 214 | |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 215 | runOnIdleCompose { |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 216 | assertThat(drawerClicks).isEqualTo(0) |
| 217 | assertThat(bodyClicks).isEqualTo(1) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 218 | |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 219 | drawerState.value = DrawerState.Opened |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 220 | } |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 221 | 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] | 222 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 223 | // Click on the left-center pixel of the drawer |
| 224 | findByTag("Drawer").doGesture { |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 225 | val left = 1.0f |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 226 | val centerY = (globalBounds.height / 2) |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 227 | sendClick(Offset(left, centerY)) |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 228 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 229 | |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 230 | runOnIdleCompose { |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 231 | assertThat(drawerClicks).isEqualTo(1) |
| 232 | assertThat(bodyClicks).isEqualTo(1) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 233 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Test |
Matvei Malkov | 886ec6a | 2020-02-03 01:45:29 +0000 | [diff] [blame] | 237 | @Ignore("failing in postsubmit, fix in b/148751721") |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 238 | fun bottomDrawer_openAndClose() { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 239 | var contentHeight: Int? = null |
| 240 | var openedHeight: Int? = null |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 241 | var openedLatch: CountDownLatch? = null |
| 242 | var closedLatch: CountDownLatch? = CountDownLatch(1) |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 243 | val drawerState = mutableStateOf(DrawerState.Closed) |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 244 | composeTestRule.setMaterialContent { |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 245 | BottomDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 246 | drawerContent = { |
| 247 | Box(Modifier.fillMaxSize().onPositioned { info: LayoutCoordinates -> |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 248 | val pos = info.localToGlobal(Offset.Zero) |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 249 | if (pos.y.roundToInt() == openedHeight) { |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 250 | // If fully opened, mark the openedLatch if present |
| 251 | openedLatch?.countDown() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 252 | } else if (pos.y.roundToInt() == contentHeight) { |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 253 | // If fully closed, mark the closedLatch if present |
| 254 | closedLatch?.countDown() |
| 255 | } |
| 256 | }) |
| 257 | }, |
| 258 | bodyContent = { |
| 259 | Box(Modifier.fillMaxSize().onPositioned { |
| 260 | contentHeight = it.size.height |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 261 | openedHeight = (it.size.height * BottomDrawerOpenFraction).roundToInt() |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 262 | }) |
| 263 | } |
| 264 | ) |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 265 | } |
| 266 | // Drawer should start in closed state |
| 267 | assertThat(closedLatch!!.await(5, TimeUnit.SECONDS)).isTrue() |
| 268 | |
| 269 | // When the drawer state is set to Opened |
| 270 | openedLatch = CountDownLatch(1) |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 271 | runOnIdleCompose { |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 272 | drawerState.value = DrawerState.Opened |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 273 | } |
| 274 | // Then the drawer should be opened |
| 275 | assertThat(openedLatch.await(5, TimeUnit.SECONDS)).isTrue() |
| 276 | |
| 277 | // When the drawer state is set to Closed |
| 278 | closedLatch = CountDownLatch(1) |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 279 | runOnIdleCompose { |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 280 | drawerState.value = DrawerState.Closed |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 281 | } |
| 282 | // Then the drawer should be closed |
| 283 | assertThat(closedLatch.await(5, TimeUnit.SECONDS)).isTrue() |
| 284 | } |
| 285 | |
| 286 | @Test |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 287 | fun bottomDrawer_bodyContent_clickable() { |
| 288 | var drawerClicks = 0 |
| 289 | var bodyClicks = 0 |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 290 | val drawerState = mutableStateOf(DrawerState.Closed) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 291 | composeTestRule.setMaterialContent { |
Aurimas Liutikas | 7a828d3 | 2019-10-07 17:16:05 -0700 | [diff] [blame] | 292 | // emulate click on the screen |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 293 | BottomDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 294 | drawerContent = { |
| 295 | Box( |
| 296 | Modifier.fillMaxSize().clickable { drawerClicks += 1 }, |
| 297 | children = emptyContent() |
| 298 | ) |
| 299 | }, |
| 300 | bodyContent = { |
| 301 | Box( |
| 302 | Modifier.testTag("Drawer").fillMaxSize().clickable { bodyClicks += 1 }, |
| 303 | children = emptyContent() |
| 304 | ) |
| 305 | }) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 308 | // Click in the middle of the drawer (which is the middle of the body) |
| 309 | findByTag("Drawer").doGesture { sendClick() } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 310 | |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 311 | runOnIdleCompose { |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 312 | assertThat(drawerClicks).isEqualTo(0) |
| 313 | assertThat(bodyClicks).isEqualTo(1) |
| 314 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 315 | |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 316 | runOnUiThread { |
Leland Richardson | fcf76b3 | 2020-05-13 16:58:59 -0700 | [diff] [blame] | 317 | drawerState.value = DrawerState.Opened |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 318 | } |
| 319 | sleep(100) // TODO(147586311): remove this sleep when opening the drawer triggers a wait |
| 320 | |
| 321 | // Click on the bottom-center pixel of the drawer |
| 322 | findByTag("Drawer").doGesture { |
| 323 | val bounds = globalBounds |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 324 | val centerX = bounds.width / 2 |
| 325 | val bottom = bounds.height - 1.0f |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 326 | sendClick(Offset(centerX, bottom)) |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | assertThat(drawerClicks).isEqualTo(1) |
| 330 | assertThat(bodyClicks).isEqualTo(1) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 331 | } |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 332 | |
| 333 | @Test |
| 334 | fun modalDrawer_openBySwipe() { |
| 335 | val drawerState = mutableStateOf(DrawerState.Closed) |
| 336 | composeTestRule.setMaterialContent { |
| 337 | // emulate click on the screen |
| 338 | Box(Modifier.testTag("Drawer")) { |
| 339 | ModalDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 340 | drawerContent = { |
| 341 | Box(Modifier.fillMaxSize().drawBackground(Color.Magenta)) |
| 342 | }, |
| 343 | bodyContent = { |
| 344 | Box(Modifier.fillMaxSize().drawBackground(Color.Red)) |
| 345 | }) |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | findByTag("Drawer") |
| 350 | .doGesture { sendSwipeRight() } |
| 351 | |
| 352 | runOnIdleCompose { |
| 353 | assertThat(drawerState.value).isEqualTo(DrawerState.Opened) |
| 354 | } |
| 355 | |
| 356 | findByTag("Drawer") |
| 357 | .doGesture { sendSwipeLeft() } |
| 358 | |
| 359 | runOnIdleCompose { |
| 360 | assertThat(drawerState.value).isEqualTo(DrawerState.Closed) |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | @Test |
| 365 | fun modalDrawer_openBySwipe_rtl() { |
| 366 | val drawerState = mutableStateOf(DrawerState.Closed) |
| 367 | composeTestRule.setMaterialContent { |
| 368 | // emulate click on the screen |
| 369 | Box(Modifier.testTag("Drawer").rtl) { |
| 370 | ModalDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 371 | drawerContent = { |
| 372 | Box(Modifier.fillMaxSize().drawBackground(Color.Magenta)) |
| 373 | }, |
| 374 | bodyContent = { |
| 375 | Box(Modifier.fillMaxSize().drawBackground(Color.Red)) |
| 376 | }) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | findByTag("Drawer") |
| 381 | .doGesture { sendSwipeLeft() } |
| 382 | |
| 383 | runOnIdleCompose { |
| 384 | assertThat(drawerState.value).isEqualTo(DrawerState.Opened) |
| 385 | } |
| 386 | |
| 387 | findByTag("Drawer") |
| 388 | .doGesture { sendSwipeRight() } |
| 389 | |
| 390 | runOnIdleCompose { |
| 391 | assertThat(drawerState.value).isEqualTo(DrawerState.Closed) |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | @Test |
| 396 | fun bottomDrawer_openBySwipe() { |
| 397 | val drawerState = mutableStateOf(DrawerState.Closed) |
| 398 | composeTestRule.setMaterialContent { |
| 399 | // emulate click on the screen |
| 400 | Box(Modifier.testTag("Drawer")) { |
| 401 | BottomDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 402 | drawerContent = { |
| 403 | Box(Modifier.fillMaxSize().drawBackground(Color.Magenta)) |
| 404 | }, |
| 405 | bodyContent = { |
| 406 | Box(Modifier.fillMaxSize().drawBackground(Color.Red)) |
| 407 | }) |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | findByTag("Drawer") |
| 412 | .doGesture { sendSwipeUp() } |
| 413 | |
| 414 | runOnIdleCompose { |
| 415 | assertThat(drawerState.value).isEqualTo(DrawerState.Opened) |
| 416 | } |
| 417 | |
| 418 | findByTag("Drawer") |
| 419 | .doGesture { sendSwipeDown() } |
| 420 | |
| 421 | runOnIdleCompose { |
| 422 | assertThat(drawerState.value).isEqualTo(DrawerState.Closed) |
| 423 | } |
| 424 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 425 | } |