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 |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 34 | import androidx.ui.test.GestureScope |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 35 | import androidx.ui.test.assertIsEqualTo |
| 36 | import androidx.ui.test.assertLeftPositionInRootIsEqualTo |
| 37 | import androidx.ui.test.assertTopPositionInRootIsEqualTo |
| 38 | import androidx.ui.test.assertWidthIsEqualTo |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 39 | import androidx.ui.test.center |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 40 | import androidx.ui.test.createComposeRule |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 41 | import androidx.ui.test.performGesture |
| 42 | import androidx.ui.test.onNodeWithTag |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 43 | import androidx.ui.test.globalBounds |
Filip Pavlis | 6fdb250 | 2020-04-02 14:41:32 +0100 | [diff] [blame] | 44 | import androidx.ui.test.runOnIdleCompose |
| 45 | import androidx.ui.test.runOnUiThread |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 46 | import androidx.ui.test.click |
| 47 | import androidx.ui.test.swipe |
| 48 | import androidx.ui.test.swipeDown |
| 49 | import androidx.ui.test.swipeLeft |
| 50 | import androidx.ui.test.swipeRight |
| 51 | import androidx.ui.test.swipeUp |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 52 | import androidx.ui.unit.dp |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 53 | import androidx.ui.unit.height |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 54 | import androidx.ui.unit.width |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 55 | import com.google.common.truth.Truth.assertThat |
Matvei Malkov | 886ec6a | 2020-02-03 01:45:29 +0000 | [diff] [blame] | 56 | import org.junit.Ignore |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 57 | import org.junit.Rule |
| 58 | import org.junit.Test |
| 59 | import org.junit.runner.RunWith |
| 60 | import org.junit.runners.JUnit4 |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 61 | import java.util.concurrent.CountDownLatch |
| 62 | import java.util.concurrent.TimeUnit |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 63 | import kotlin.math.roundToInt |
| 64 | |
| 65 | @MediumTest |
| 66 | @RunWith(JUnit4::class) |
| 67 | class DrawerTest { |
| 68 | |
| 69 | @get:Rule |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 70 | val composeTestRule = createComposeRule(disableTransitions = true) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 71 | |
| 72 | @Test |
| 73 | fun modalDrawer_testOffset_whenOpened() { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 74 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 75 | ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 76 | Box(Modifier.fillMaxSize().testTag("content")) |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame] | 77 | }, bodyContent = emptyContent()) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 78 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 79 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 80 | onNodeWithTag("content") |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 81 | .assertLeftPositionInRootIsEqualTo(0.dp) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | fun modalDrawer_testOffset_whenClosed() { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 86 | var position: Offset? = null |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 87 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 88 | ModalDrawerLayout(DrawerState.Closed, {}, drawerContent = { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 89 | Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates -> |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 90 | position = coords.localToRoot(Offset.Zero) |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 91 | }) |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame] | 92 | }, bodyContent = emptyContent()) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 93 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 94 | |
| 95 | val width = rootWidth() |
| 96 | composeTestRule.runOnIdleComposeWithDensity { |
| 97 | position!!.x.toDp().assertIsEqualTo(-width) |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 98 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | fun modalDrawer_testEndPadding_whenOpened() { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 103 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 104 | ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 105 | Box(Modifier.fillMaxSize().testTag("content")) |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame] | 106 | }, bodyContent = emptyContent()) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 109 | onNodeWithTag("content") |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 110 | .assertWidthIsEqualTo(rootWidth() - 56.dp) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Test |
| 114 | fun bottomDrawer_testOffset_whenOpened() { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 115 | composeTestRule.setMaterialContent { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 116 | BottomDrawerLayout(BottomDrawerState.Opened, {}, drawerContent = { |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 117 | Box(Modifier.fillMaxSize().testTag("content")) |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame] | 118 | }, bodyContent = emptyContent()) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 119 | } |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 120 | |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 121 | val width = rootWidth() |
| 122 | val height = rootHeight() |
| 123 | val expectedHeight = if (width > height) 0.dp else (height / 2) |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 124 | onNodeWithTag("content") |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 125 | .assertTopPositionInRootIsEqualTo(expectedHeight) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
| 129 | fun bottomDrawer_testOffset_whenClosed() { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 130 | var position: Offset? = null |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 131 | composeTestRule.setMaterialContent { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 132 | BottomDrawerLayout(BottomDrawerState.Closed, {}, drawerContent = { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 133 | Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates -> |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 134 | position = coords.localToRoot(Offset.Zero) |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 135 | }) |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame] | 136 | }, bodyContent = emptyContent()) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 137 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 138 | |
| 139 | val height = rootHeight() |
| 140 | composeTestRule.runOnIdleComposeWithDensity { |
| 141 | position!!.y.toDp().assertIsEqualTo(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 | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 148 | var contentWidth: Int? = 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() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 161 | } else if (-pos.x.roundToInt() == contentWidth) { |
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) |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 216 | onNodeWithTag("Drawer").performGesture { click() } |
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 |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 227 | onNodeWithTag("Drawer").performGesture { |
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) |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 230 | click(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 | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 242 | var contentHeight: Int? = null |
| 243 | var openedHeight: Int? = null |
Jelle Fresen | 4c56631 | 2020-01-16 10:32:40 +0000 | [diff] [blame] | 244 | var openedLatch: CountDownLatch? = null |
| 245 | var closedLatch: CountDownLatch? = CountDownLatch(1) |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 246 | val drawerState = mutableStateOf(BottomDrawerState.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) |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 252 | if (pos.y.roundToInt() == openedHeight) { |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 253 | // If fully opened, mark the openedLatch if present |
| 254 | openedLatch?.countDown() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 255 | } else if (pos.y.roundToInt() == contentHeight) { |
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 |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 264 | openedHeight = (it.size.height * BottomDrawerOpenFraction).roundToInt() |
Filip Pavlis | 1d4d1b83 | 2020-05-27 14:45:15 +0100 | [diff] [blame] | 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 { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 275 | drawerState.value = BottomDrawerState.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 { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 283 | drawerState.value = BottomDrawerState.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 |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 293 | val drawerState = mutableStateOf(BottomDrawerState.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) |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 312 | onNodeWithTag("Drawer").performGesture { click() } |
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 { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 320 | drawerState.value = BottomDrawerState.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 |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 325 | onNodeWithTag("Drawer").performGesture { |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 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 |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 329 | click(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 | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 335 | |
| 336 | @Test |
| 337 | fun modalDrawer_openBySwipe() { |
| 338 | val drawerState = mutableStateOf(DrawerState.Closed) |
| 339 | composeTestRule.setMaterialContent { |
| 340 | // emulate click on the screen |
| 341 | Box(Modifier.testTag("Drawer")) { |
| 342 | ModalDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 343 | drawerContent = { |
| 344 | Box(Modifier.fillMaxSize().drawBackground(Color.Magenta)) |
| 345 | }, |
| 346 | bodyContent = { |
| 347 | Box(Modifier.fillMaxSize().drawBackground(Color.Red)) |
| 348 | }) |
| 349 | } |
| 350 | } |
| 351 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 352 | onNodeWithTag("Drawer") |
| 353 | .performGesture { swipeRight() } |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 354 | |
| 355 | runOnIdleCompose { |
| 356 | assertThat(drawerState.value).isEqualTo(DrawerState.Opened) |
| 357 | } |
| 358 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 359 | onNodeWithTag("Drawer") |
| 360 | .performGesture { swipeLeft() } |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 361 | |
| 362 | runOnIdleCompose { |
| 363 | assertThat(drawerState.value).isEqualTo(DrawerState.Closed) |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | @Test |
| 368 | fun modalDrawer_openBySwipe_rtl() { |
| 369 | val drawerState = mutableStateOf(DrawerState.Closed) |
| 370 | composeTestRule.setMaterialContent { |
| 371 | // emulate click on the screen |
| 372 | Box(Modifier.testTag("Drawer").rtl) { |
| 373 | ModalDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 374 | drawerContent = { |
| 375 | Box(Modifier.fillMaxSize().drawBackground(Color.Magenta)) |
| 376 | }, |
| 377 | bodyContent = { |
| 378 | Box(Modifier.fillMaxSize().drawBackground(Color.Red)) |
| 379 | }) |
| 380 | } |
| 381 | } |
| 382 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 383 | onNodeWithTag("Drawer") |
| 384 | .performGesture { swipeLeft() } |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 385 | |
| 386 | runOnIdleCompose { |
| 387 | assertThat(drawerState.value).isEqualTo(DrawerState.Opened) |
| 388 | } |
| 389 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 390 | onNodeWithTag("Drawer") |
| 391 | .performGesture { swipeRight() } |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 392 | |
| 393 | runOnIdleCompose { |
| 394 | assertThat(drawerState.value).isEqualTo(DrawerState.Closed) |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | @Test |
| 399 | fun bottomDrawer_openBySwipe() { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 400 | val drawerState = mutableStateOf(BottomDrawerState.Closed) |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 401 | composeTestRule.setMaterialContent { |
| 402 | // emulate click on the screen |
| 403 | Box(Modifier.testTag("Drawer")) { |
| 404 | BottomDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 405 | drawerContent = { |
| 406 | Box(Modifier.fillMaxSize().drawBackground(Color.Magenta)) |
| 407 | }, |
| 408 | bodyContent = { |
| 409 | Box(Modifier.fillMaxSize().drawBackground(Color.Red)) |
| 410 | }) |
| 411 | } |
| 412 | } |
| 413 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 414 | onNodeWithTag("Drawer") |
| 415 | .performGesture { swipeUp() } |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 416 | |
| 417 | runOnIdleCompose { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 418 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Expanded) |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 421 | onNodeWithTag("Drawer") |
| 422 | .performGesture { swipeDown() } |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 423 | |
| 424 | runOnIdleCompose { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 425 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Closed) |
Matvei Malkov | 11fcc133 | 2020-06-25 18:03:03 +0100 | [diff] [blame] | 426 | } |
| 427 | } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 428 | |
| 429 | @Test |
| 430 | fun bottomDrawer_openBySwipe_thresholds() { |
| 431 | val drawerState = mutableStateOf(BottomDrawerState.Closed) |
| 432 | composeTestRule.setMaterialContent { |
| 433 | // emulate click on the screen |
| 434 | Box(Modifier.testTag("Drawer")) { |
| 435 | BottomDrawerLayout(drawerState.value, { drawerState.value = it }, |
| 436 | drawerContent = { |
| 437 | Box(Modifier.fillMaxSize().drawBackground(Color.Magenta)) |
| 438 | }, |
| 439 | bodyContent = { |
| 440 | Box(Modifier.fillMaxSize().drawBackground(Color.Red)) |
| 441 | }) |
| 442 | } |
| 443 | } |
| 444 | val threshold = with (composeTestRule.density) { BottomDrawerThreshold.toPx() } |
| 445 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 446 | onNodeWithTag("Drawer") |
| 447 | .performGesture { swipeUpBy(threshold / 2) } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 448 | |
| 449 | runOnIdleCompose { |
| 450 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Closed) |
| 451 | } |
| 452 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 453 | onNodeWithTag("Drawer") |
| 454 | .performGesture { swipeUpBy(threshold) } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 455 | |
| 456 | runOnIdleCompose { |
| 457 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Opened) |
| 458 | } |
| 459 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 460 | onNodeWithTag("Drawer") |
| 461 | .performGesture { swipeUpBy(threshold / 2) } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 462 | |
| 463 | runOnIdleCompose { |
| 464 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Opened) |
| 465 | } |
| 466 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 467 | onNodeWithTag("Drawer") |
| 468 | .performGesture { swipeUpBy(threshold) } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 469 | |
| 470 | runOnIdleCompose { |
| 471 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Expanded) |
| 472 | } |
| 473 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 474 | onNodeWithTag("Drawer") |
| 475 | .performGesture { swipeDownBy(threshold / 2) } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 476 | |
| 477 | runOnIdleCompose { |
| 478 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Expanded) |
| 479 | } |
| 480 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 481 | onNodeWithTag("Drawer") |
| 482 | .performGesture { swipeDownBy(threshold) } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 483 | |
| 484 | runOnIdleCompose { |
| 485 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Opened) |
| 486 | } |
| 487 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 488 | onNodeWithTag("Drawer") |
| 489 | .performGesture { swipeDownBy(threshold / 2) } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 490 | |
| 491 | runOnIdleCompose { |
| 492 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Opened) |
| 493 | } |
| 494 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 495 | onNodeWithTag("Drawer") |
| 496 | .performGesture { swipeDownBy(threshold) } |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 497 | |
| 498 | runOnIdleCompose { |
| 499 | assertThat(drawerState.value).isEqualTo(BottomDrawerState.Closed) |
| 500 | } |
| 501 | } |
| 502 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 503 | private fun GestureScope.swipeUpBy(offset: Float) { |
| 504 | swipe(center, center.copy(y = center.y - offset)) |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 505 | } |
| 506 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame^] | 507 | private fun GestureScope.swipeDownBy(offset: Float) { |
| 508 | swipe(center, center.copy(y = center.y + offset)) |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 509 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 510 | } |