[go: nahoru, domu]

blob: bf214ad97aee6fe3e288c66967f31596b9348499 [file] [log] [blame]
Matvei Malkov2d37cbd2019-07-04 15:15:20 +01001/*
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
17package androidx.ui.material
18
Jelle Fresen11fc7062020-01-13 16:53:55 +000019import android.os.SystemClock.sleep
Louis Pullen-Freiliche46bcf02020-02-12 16:06:21 +000020import androidx.compose.emptyContent
Leland Richardsonfcf76b32020-05-13 16:58:59 -070021import androidx.compose.mutableStateOf
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010022import androidx.test.filters.MediumTest
Adam Powell999a89b2020-03-11 09:08:07 -070023import androidx.ui.core.LayoutCoordinates
24import androidx.ui.core.Modifier
George Mountd02af602020-03-06 16:41:40 -080025import androidx.ui.core.onPositioned
Filip Pavlis1d4d1b832020-05-27 14:45:15 +010026import androidx.ui.core.testTag
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010027import androidx.compose.foundation.Box
28import androidx.compose.foundation.background
29import androidx.compose.foundation.clickable
George Mount8f237572020-04-30 12:08:30 -070030import androidx.ui.geometry.Offset
Matvei Malkov11fcc1332020-06-25 18:03:03 +010031import androidx.ui.graphics.Color
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010032import androidx.compose.foundation.layout.fillMaxSize
33import androidx.compose.foundation.layout.rtl
Calin Tatarudcde3402020-07-08 11:44:22 +010034import androidx.ui.test.GestureScope
Filip Pavlis028c0d52020-06-29 15:07:53 +010035import androidx.ui.test.assertIsEqualTo
36import androidx.ui.test.assertLeftPositionInRootIsEqualTo
37import androidx.ui.test.assertTopPositionInRootIsEqualTo
38import androidx.ui.test.assertWidthIsEqualTo
Jelle Fresen77f44182020-07-21 16:34:29 +010039import androidx.ui.test.bottomCenter
Calin Tatarudcde3402020-07-08 11:44:22 +010040import androidx.ui.test.center
Jelle Fresen77f44182020-07-21 16:34:29 +010041import androidx.ui.test.centerLeft
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010042import androidx.ui.test.createComposeRule
Filip Pavlis659ea722020-07-13 14:14:32 +010043import androidx.ui.test.performGesture
44import androidx.ui.test.onNodeWithTag
Filip Pavlisa4d73a52020-07-14 11:30:00 +010045import androidx.ui.test.runOnIdle
Filip Pavlis6fdb2502020-04-02 14:41:32 +010046import androidx.ui.test.runOnUiThread
Filip Pavlis659ea722020-07-13 14:14:32 +010047import androidx.ui.test.click
48import androidx.ui.test.swipe
49import androidx.ui.test.swipeDown
50import androidx.ui.test.swipeLeft
51import androidx.ui.test.swipeRight
52import androidx.ui.test.swipeUp
George Mount842c8c12020-01-08 16:03:42 -080053import androidx.ui.unit.dp
Ryan Mentley7865a632019-08-20 20:10:29 -070054import androidx.ui.unit.height
Ryan Mentley7865a632019-08-20 20:10:29 -070055import androidx.ui.unit.width
Filip Pavlise63b30c2020-01-09 16:21:07 +000056import com.google.common.truth.Truth.assertThat
Matvei Malkov886ec6a2020-02-03 01:45:29 +000057import org.junit.Ignore
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010058import org.junit.Rule
59import org.junit.Test
60import org.junit.runner.RunWith
61import org.junit.runners.JUnit4
Jelle Fresen4c566312020-01-16 10:32:40 +000062import java.util.concurrent.CountDownLatch
63import java.util.concurrent.TimeUnit
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010064import kotlin.math.roundToInt
65
66@MediumTest
67@RunWith(JUnit4::class)
68class DrawerTest {
69
70 @get:Rule
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010071 val composeTestRule = createComposeRule(disableTransitions = true)
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010072
73 @Test
74 fun modalDrawer_testOffset_whenOpened() {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010075 composeTestRule.setMaterialContent {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010076 ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = {
Filip Pavlis028c0d52020-06-29 15:07:53 +010077 Box(Modifier.fillMaxSize().testTag("content"))
Louis Pullen-Freiliche46bcf02020-02-12 16:06:21 +000078 }, bodyContent = emptyContent())
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010079 }
Filip Pavlis028c0d52020-06-29 15:07:53 +010080
Filip Pavlis659ea722020-07-13 14:14:32 +010081 onNodeWithTag("content")
Filip Pavlis028c0d52020-06-29 15:07:53 +010082 .assertLeftPositionInRootIsEqualTo(0.dp)
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010083 }
84
85 @Test
86 fun modalDrawer_testOffset_whenClosed() {
Nader Jawad6df06122020-06-03 15:27:08 -070087 var position: Offset? = null
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010088 composeTestRule.setMaterialContent {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010089 ModalDrawerLayout(DrawerState.Closed, {}, drawerContent = {
Adam Powell999a89b2020-03-11 09:08:07 -070090 Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates ->
Filip Pavlis028c0d52020-06-29 15:07:53 +010091 position = coords.localToRoot(Offset.Zero)
Matvei Malkov201726d2020-03-13 14:03:54 +000092 })
Louis Pullen-Freiliche46bcf02020-02-12 16:06:21 +000093 }, bodyContent = emptyContent())
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010094 }
Filip Pavlis028c0d52020-06-29 15:07:53 +010095
96 val width = rootWidth()
Filip Pavlisa4d73a52020-07-14 11:30:00 +010097 composeTestRule.runOnIdleWithDensity {
Filip Pavlis028c0d52020-06-29 15:07:53 +010098 position!!.x.toDp().assertIsEqualTo(-width)
Filip Pavlise63b30c2020-01-09 16:21:07 +000099 }
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100100 }
101
102 @Test
103 fun modalDrawer_testEndPadding_whenOpened() {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100104 composeTestRule.setMaterialContent {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100105 ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = {
Filip Pavlis028c0d52020-06-29 15:07:53 +0100106 Box(Modifier.fillMaxSize().testTag("content"))
Louis Pullen-Freiliche46bcf02020-02-12 16:06:21 +0000107 }, bodyContent = emptyContent())
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100108 }
109
Filip Pavlis659ea722020-07-13 14:14:32 +0100110 onNodeWithTag("content")
Filip Pavlis028c0d52020-06-29 15:07:53 +0100111 .assertWidthIsEqualTo(rootWidth() - 56.dp)
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100112 }
113
114 @Test
115 fun bottomDrawer_testOffset_whenOpened() {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100116 composeTestRule.setMaterialContent {
Calin Tatarudcde3402020-07-08 11:44:22 +0100117 BottomDrawerLayout(BottomDrawerState.Opened, {}, drawerContent = {
Filip Pavlis028c0d52020-06-29 15:07:53 +0100118 Box(Modifier.fillMaxSize().testTag("content"))
Louis Pullen-Freiliche46bcf02020-02-12 16:06:21 +0000119 }, bodyContent = emptyContent())
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100120 }
Filip Pavlise63b30c2020-01-09 16:21:07 +0000121
Filip Pavlis028c0d52020-06-29 15:07:53 +0100122 val width = rootWidth()
123 val height = rootHeight()
124 val expectedHeight = if (width > height) 0.dp else (height / 2)
Filip Pavlis659ea722020-07-13 14:14:32 +0100125 onNodeWithTag("content")
Filip Pavlis028c0d52020-06-29 15:07:53 +0100126 .assertTopPositionInRootIsEqualTo(expectedHeight)
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100127 }
128
129 @Test
130 fun bottomDrawer_testOffset_whenClosed() {
Nader Jawad6df06122020-06-03 15:27:08 -0700131 var position: Offset? = null
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100132 composeTestRule.setMaterialContent {
Calin Tatarudcde3402020-07-08 11:44:22 +0100133 BottomDrawerLayout(BottomDrawerState.Closed, {}, drawerContent = {
Adam Powell999a89b2020-03-11 09:08:07 -0700134 Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates ->
Filip Pavlis028c0d52020-06-29 15:07:53 +0100135 position = coords.localToRoot(Offset.Zero)
Matvei Malkov201726d2020-03-13 14:03:54 +0000136 })
Louis Pullen-Freiliche46bcf02020-02-12 16:06:21 +0000137 }, bodyContent = emptyContent())
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100138 }
Filip Pavlis028c0d52020-06-29 15:07:53 +0100139
140 val height = rootHeight()
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100141 composeTestRule.runOnIdleWithDensity {
Filip Pavlis028c0d52020-06-29 15:07:53 +0100142 position!!.y.toDp().assertIsEqualTo(height)
Filip Pavlise63b30c2020-01-09 16:21:07 +0000143 }
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100144 }
145
146 @Test
Matvei Malkov886ec6a2020-02-03 01:45:29 +0000147 @Ignore("failing in postsubmit, fix in b/148751721")
Jelle Fresen4c566312020-01-16 10:32:40 +0000148 fun modalDrawer_openAndClose() {
George Mount8f237572020-04-30 12:08:30 -0700149 var contentWidth: Int? = null
Jelle Fresen4c566312020-01-16 10:32:40 +0000150 var openedLatch: CountDownLatch? = null
151 var closedLatch: CountDownLatch? = CountDownLatch(1)
Leland Richardsonfcf76b32020-05-13 16:58:59 -0700152 val drawerState = mutableStateOf(DrawerState.Closed)
Jelle Fresen4c566312020-01-16 10:32:40 +0000153 composeTestRule.setMaterialContent {
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100154 ModalDrawerLayout(drawerState.value, { drawerState.value = it },
155 drawerContent = {
156 Box(
157 Modifier.fillMaxSize().onPositioned { info: LayoutCoordinates ->
Nader Jawad6df06122020-06-03 15:27:08 -0700158 val pos = info.localToGlobal(Offset.Zero)
Nader Jawade6a9b332020-05-21 13:49:20 -0700159 if (pos.x == 0.0f) {
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100160 // If fully opened, mark the openedLatch if present
161 openedLatch?.countDown()
George Mount8f237572020-04-30 12:08:30 -0700162 } else if (-pos.x.roundToInt() == contentWidth) {
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100163 // If fully closed, mark the closedLatch if present
164 closedLatch?.countDown()
Matvei Malkov9f27abf2020-05-19 15:15:56 +0100165 }
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100166 }
167 )
168 },
169 bodyContent = {
170 Box(Modifier.fillMaxSize()
171 .onPositioned { contentWidth = it.size.width })
172 })
Jelle Fresen4c566312020-01-16 10:32:40 +0000173 }
174 // Drawer should start in closed state
175 assertThat(closedLatch!!.await(5, TimeUnit.SECONDS)).isTrue()
176
177 // When the drawer state is set to Opened
178 openedLatch = CountDownLatch(1)
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100179 runOnIdle {
Leland Richardsonfcf76b32020-05-13 16:58:59 -0700180 drawerState.value = DrawerState.Opened
Jelle Fresen4c566312020-01-16 10:32:40 +0000181 }
182 // Then the drawer should be opened
183 assertThat(openedLatch.await(5, TimeUnit.SECONDS)).isTrue()
184
185 // When the drawer state is set to Closed
186 closedLatch = CountDownLatch(1)
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100187 runOnIdle {
Leland Richardsonfcf76b32020-05-13 16:58:59 -0700188 drawerState.value = DrawerState.Closed
Jelle Fresen4c566312020-01-16 10:32:40 +0000189 }
190 // Then the drawer should be closed
191 assertThat(closedLatch.await(5, TimeUnit.SECONDS)).isTrue()
192 }
193
194 @Test
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100195 fun modalDrawer_bodyContent_clickable() {
196 var drawerClicks = 0
197 var bodyClicks = 0
Leland Richardsonfcf76b32020-05-13 16:58:59 -0700198 val drawerState = mutableStateOf(DrawerState.Closed)
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100199 composeTestRule.setMaterialContent {
Aurimas Liutikas7a828d32019-10-07 17:16:05 -0700200 // emulate click on the screen
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100201 ModalDrawerLayout(drawerState.value, { drawerState.value = it },
202 drawerContent = {
203 Box(
204 Modifier.fillMaxSize().clickable { drawerClicks += 1 },
205 children = emptyContent()
206 )
207 },
208 bodyContent = {
209 Box(
210 Modifier.testTag("Drawer").fillMaxSize().clickable { bodyClicks += 1 },
211 children = emptyContent()
212 )
213 })
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100214 }
215
Jelle Fresen11fc7062020-01-13 16:53:55 +0000216 // Click in the middle of the drawer (which is the middle of the body)
Filip Pavlis659ea722020-07-13 14:14:32 +0100217 onNodeWithTag("Drawer").performGesture { click() }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100218
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100219 runOnIdle {
Filip Pavlise63b30c2020-01-09 16:21:07 +0000220 assertThat(drawerClicks).isEqualTo(0)
221 assertThat(bodyClicks).isEqualTo(1)
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100222
Leland Richardsonfcf76b32020-05-13 16:58:59 -0700223 drawerState.value = DrawerState.Opened
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100224 }
Jelle Fresen11fc7062020-01-13 16:53:55 +0000225 sleep(100) // TODO(147586311): remove this sleep when opening the drawer triggers a wait
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100226
Jelle Fresen11fc7062020-01-13 16:53:55 +0000227 // Click on the left-center pixel of the drawer
Filip Pavlis659ea722020-07-13 14:14:32 +0100228 onNodeWithTag("Drawer").performGesture {
Jelle Fresen77f44182020-07-21 16:34:29 +0100229 click(centerLeft)
Jelle Fresen11fc7062020-01-13 16:53:55 +0000230 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100231
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100232 runOnIdle {
Filip Pavlise63b30c2020-01-09 16:21:07 +0000233 assertThat(drawerClicks).isEqualTo(1)
234 assertThat(bodyClicks).isEqualTo(1)
Filip Pavlis2b161e42019-11-22 17:40:08 +0000235 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100236 }
237
238 @Test
Matvei Malkov886ec6a2020-02-03 01:45:29 +0000239 @Ignore("failing in postsubmit, fix in b/148751721")
Jelle Fresen4c566312020-01-16 10:32:40 +0000240 fun bottomDrawer_openAndClose() {
George Mount8f237572020-04-30 12:08:30 -0700241 var contentHeight: Int? = null
242 var openedHeight: Int? = null
Jelle Fresen4c566312020-01-16 10:32:40 +0000243 var openedLatch: CountDownLatch? = null
244 var closedLatch: CountDownLatch? = CountDownLatch(1)
Calin Tatarudcde3402020-07-08 11:44:22 +0100245 val drawerState = mutableStateOf(BottomDrawerState.Closed)
Jelle Fresen4c566312020-01-16 10:32:40 +0000246 composeTestRule.setMaterialContent {
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100247 BottomDrawerLayout(drawerState.value, { drawerState.value = it },
248 drawerContent = {
249 Box(Modifier.fillMaxSize().onPositioned { info: LayoutCoordinates ->
Nader Jawad6df06122020-06-03 15:27:08 -0700250 val pos = info.localToGlobal(Offset.Zero)
George Mount8f237572020-04-30 12:08:30 -0700251 if (pos.y.roundToInt() == openedHeight) {
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100252 // If fully opened, mark the openedLatch if present
253 openedLatch?.countDown()
George Mount8f237572020-04-30 12:08:30 -0700254 } else if (pos.y.roundToInt() == contentHeight) {
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100255 // If fully closed, mark the closedLatch if present
256 closedLatch?.countDown()
257 }
258 })
259 },
260 bodyContent = {
261 Box(Modifier.fillMaxSize().onPositioned {
262 contentHeight = it.size.height
George Mount8f237572020-04-30 12:08:30 -0700263 openedHeight = (it.size.height * BottomDrawerOpenFraction).roundToInt()
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100264 })
265 }
266 )
Jelle Fresen4c566312020-01-16 10:32:40 +0000267 }
268 // Drawer should start in closed state
269 assertThat(closedLatch!!.await(5, TimeUnit.SECONDS)).isTrue()
270
271 // When the drawer state is set to Opened
272 openedLatch = CountDownLatch(1)
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100273 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100274 drawerState.value = BottomDrawerState.Opened
Jelle Fresen4c566312020-01-16 10:32:40 +0000275 }
276 // Then the drawer should be opened
277 assertThat(openedLatch.await(5, TimeUnit.SECONDS)).isTrue()
278
279 // When the drawer state is set to Closed
280 closedLatch = CountDownLatch(1)
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100281 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100282 drawerState.value = BottomDrawerState.Closed
Jelle Fresen4c566312020-01-16 10:32:40 +0000283 }
284 // Then the drawer should be closed
285 assertThat(closedLatch.await(5, TimeUnit.SECONDS)).isTrue()
286 }
287
288 @Test
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100289 fun bottomDrawer_bodyContent_clickable() {
290 var drawerClicks = 0
291 var bodyClicks = 0
Calin Tatarudcde3402020-07-08 11:44:22 +0100292 val drawerState = mutableStateOf(BottomDrawerState.Closed)
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100293 composeTestRule.setMaterialContent {
Aurimas Liutikas7a828d32019-10-07 17:16:05 -0700294 // emulate click on the screen
Filip Pavlis1d4d1b832020-05-27 14:45:15 +0100295 BottomDrawerLayout(drawerState.value, { drawerState.value = it },
296 drawerContent = {
297 Box(
298 Modifier.fillMaxSize().clickable { drawerClicks += 1 },
299 children = emptyContent()
300 )
301 },
302 bodyContent = {
303 Box(
304 Modifier.testTag("Drawer").fillMaxSize().clickable { bodyClicks += 1 },
305 children = emptyContent()
306 )
307 })
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100308 }
309
Jelle Fresen11fc7062020-01-13 16:53:55 +0000310 // Click in the middle of the drawer (which is the middle of the body)
Filip Pavlis659ea722020-07-13 14:14:32 +0100311 onNodeWithTag("Drawer").performGesture { click() }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100312
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100313 runOnIdle {
Filip Pavlise63b30c2020-01-09 16:21:07 +0000314 assertThat(drawerClicks).isEqualTo(0)
315 assertThat(bodyClicks).isEqualTo(1)
316 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100317
Filip Pavlis6fdb2502020-04-02 14:41:32 +0100318 runOnUiThread {
Calin Tatarudcde3402020-07-08 11:44:22 +0100319 drawerState.value = BottomDrawerState.Opened
Jelle Fresen11fc7062020-01-13 16:53:55 +0000320 }
321 sleep(100) // TODO(147586311): remove this sleep when opening the drawer triggers a wait
322
323 // Click on the bottom-center pixel of the drawer
Filip Pavlis659ea722020-07-13 14:14:32 +0100324 onNodeWithTag("Drawer").performGesture {
Jelle Fresen77f44182020-07-21 16:34:29 +0100325 click(bottomCenter)
Jelle Fresen11fc7062020-01-13 16:53:55 +0000326 }
327
328 assertThat(drawerClicks).isEqualTo(1)
329 assertThat(bodyClicks).isEqualTo(1)
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100330 }
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100331
332 @Test
333 fun modalDrawer_openBySwipe() {
334 val drawerState = mutableStateOf(DrawerState.Closed)
335 composeTestRule.setMaterialContent {
336 // emulate click on the screen
337 Box(Modifier.testTag("Drawer")) {
338 ModalDrawerLayout(drawerState.value, { drawerState.value = it },
339 drawerContent = {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100340 Box(Modifier.fillMaxSize().background(color = Color.Magenta))
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100341 },
342 bodyContent = {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100343 Box(Modifier.fillMaxSize().background(color = Color.Red))
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100344 })
345 }
346 }
347
Filip Pavlis659ea722020-07-13 14:14:32 +0100348 onNodeWithTag("Drawer")
349 .performGesture { swipeRight() }
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100350
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100351 runOnIdle {
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100352 assertThat(drawerState.value).isEqualTo(DrawerState.Opened)
353 }
354
Filip Pavlis659ea722020-07-13 14:14:32 +0100355 onNodeWithTag("Drawer")
356 .performGesture { swipeLeft() }
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100357
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100358 runOnIdle {
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100359 assertThat(drawerState.value).isEqualTo(DrawerState.Closed)
360 }
361 }
362
363 @Test
364 fun modalDrawer_openBySwipe_rtl() {
365 val drawerState = mutableStateOf(DrawerState.Closed)
366 composeTestRule.setMaterialContent {
367 // emulate click on the screen
368 Box(Modifier.testTag("Drawer").rtl) {
369 ModalDrawerLayout(drawerState.value, { drawerState.value = it },
370 drawerContent = {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100371 Box(Modifier.fillMaxSize().background(color = Color.Magenta))
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100372 },
373 bodyContent = {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100374 Box(Modifier.fillMaxSize().background(color = Color.Red))
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100375 })
376 }
377 }
378
Filip Pavlis659ea722020-07-13 14:14:32 +0100379 onNodeWithTag("Drawer")
380 .performGesture { swipeLeft() }
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100381
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100382 runOnIdle {
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100383 assertThat(drawerState.value).isEqualTo(DrawerState.Opened)
384 }
385
Filip Pavlis659ea722020-07-13 14:14:32 +0100386 onNodeWithTag("Drawer")
387 .performGesture { swipeRight() }
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100388
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100389 runOnIdle {
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100390 assertThat(drawerState.value).isEqualTo(DrawerState.Closed)
391 }
392 }
393
394 @Test
395 fun bottomDrawer_openBySwipe() {
Calin Tatarudcde3402020-07-08 11:44:22 +0100396 val drawerState = mutableStateOf(BottomDrawerState.Closed)
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100397 composeTestRule.setMaterialContent {
398 // emulate click on the screen
399 Box(Modifier.testTag("Drawer")) {
400 BottomDrawerLayout(drawerState.value, { drawerState.value = it },
401 drawerContent = {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100402 Box(Modifier.fillMaxSize().background(color = Color.Magenta))
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100403 },
404 bodyContent = {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100405 Box(Modifier.fillMaxSize().background(color = Color.Red))
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100406 })
407 }
408 }
409
Filip Pavlis659ea722020-07-13 14:14:32 +0100410 onNodeWithTag("Drawer")
411 .performGesture { swipeUp() }
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100412
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100413 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100414 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Expanded)
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100415 }
416
Filip Pavlis659ea722020-07-13 14:14:32 +0100417 onNodeWithTag("Drawer")
418 .performGesture { swipeDown() }
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100419
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100420 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100421 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Closed)
Matvei Malkov11fcc1332020-06-25 18:03:03 +0100422 }
423 }
Calin Tatarudcde3402020-07-08 11:44:22 +0100424
425 @Test
426 fun bottomDrawer_openBySwipe_thresholds() {
427 val drawerState = mutableStateOf(BottomDrawerState.Closed)
428 composeTestRule.setMaterialContent {
429 // emulate click on the screen
430 Box(Modifier.testTag("Drawer")) {
431 BottomDrawerLayout(drawerState.value, { drawerState.value = it },
432 drawerContent = {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100433 Box(Modifier.fillMaxSize().background(Color.Magenta))
Calin Tatarudcde3402020-07-08 11:44:22 +0100434 },
435 bodyContent = {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100436 Box(Modifier.fillMaxSize().background(Color.Red))
Calin Tatarudcde3402020-07-08 11:44:22 +0100437 })
438 }
439 }
440 val threshold = with (composeTestRule.density) { BottomDrawerThreshold.toPx() }
441
Filip Pavlis659ea722020-07-13 14:14:32 +0100442 onNodeWithTag("Drawer")
443 .performGesture { swipeUpBy(threshold / 2) }
Calin Tatarudcde3402020-07-08 11:44:22 +0100444
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100445 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100446 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Closed)
447 }
448
Filip Pavlis659ea722020-07-13 14:14:32 +0100449 onNodeWithTag("Drawer")
450 .performGesture { swipeUpBy(threshold) }
Calin Tatarudcde3402020-07-08 11:44:22 +0100451
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100452 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100453 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Opened)
454 }
455
Filip Pavlis659ea722020-07-13 14:14:32 +0100456 onNodeWithTag("Drawer")
457 .performGesture { swipeUpBy(threshold / 2) }
Calin Tatarudcde3402020-07-08 11:44:22 +0100458
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100459 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100460 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Opened)
461 }
462
Filip Pavlis659ea722020-07-13 14:14:32 +0100463 onNodeWithTag("Drawer")
464 .performGesture { swipeUpBy(threshold) }
Calin Tatarudcde3402020-07-08 11:44:22 +0100465
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100466 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100467 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Expanded)
468 }
469
Filip Pavlis659ea722020-07-13 14:14:32 +0100470 onNodeWithTag("Drawer")
471 .performGesture { swipeDownBy(threshold / 2) }
Calin Tatarudcde3402020-07-08 11:44:22 +0100472
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100473 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100474 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Expanded)
475 }
476
Filip Pavlis659ea722020-07-13 14:14:32 +0100477 onNodeWithTag("Drawer")
478 .performGesture { swipeDownBy(threshold) }
Calin Tatarudcde3402020-07-08 11:44:22 +0100479
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100480 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100481 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Opened)
482 }
483
Filip Pavlis659ea722020-07-13 14:14:32 +0100484 onNodeWithTag("Drawer")
485 .performGesture { swipeDownBy(threshold / 2) }
Calin Tatarudcde3402020-07-08 11:44:22 +0100486
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100487 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100488 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Opened)
489 }
490
Filip Pavlis659ea722020-07-13 14:14:32 +0100491 onNodeWithTag("Drawer")
492 .performGesture { swipeDownBy(threshold) }
Calin Tatarudcde3402020-07-08 11:44:22 +0100493
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100494 runOnIdle {
Calin Tatarudcde3402020-07-08 11:44:22 +0100495 assertThat(drawerState.value).isEqualTo(BottomDrawerState.Closed)
496 }
497 }
498
Filip Pavlis659ea722020-07-13 14:14:32 +0100499 private fun GestureScope.swipeUpBy(offset: Float) {
500 swipe(center, center.copy(y = center.y - offset))
Calin Tatarudcde3402020-07-08 11:44:22 +0100501 }
502
Filip Pavlis659ea722020-07-13 14:14:32 +0100503 private fun GestureScope.swipeDownBy(offset: Float) {
504 swipe(center, center.copy(y = center.y + offset))
Calin Tatarudcde3402020-07-08 11:44:22 +0100505 }
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100506}