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 |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 20 | import androidx.compose.Model |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 21 | import androidx.test.filters.MediumTest |
| 22 | import androidx.ui.core.OnPositioned |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 23 | import androidx.ui.core.TestTag |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 24 | import androidx.ui.foundation.Clickable |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 25 | import androidx.ui.layout.Container |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 26 | import androidx.ui.semantics.Semantics |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 27 | import androidx.ui.test.createComposeRule |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 28 | import androidx.ui.test.doGesture |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 29 | import androidx.ui.test.findByTag |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 30 | import androidx.ui.test.globalBounds |
| 31 | import androidx.ui.test.sendClick |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 32 | import androidx.ui.unit.PxPosition |
| 33 | import androidx.ui.unit.PxSize |
| 34 | import androidx.ui.unit.dp |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame^] | 35 | import androidx.ui.unit.height |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 36 | import androidx.ui.unit.px |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 37 | import androidx.ui.unit.round |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame^] | 38 | import androidx.ui.unit.width |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 39 | import com.google.common.truth.Truth.assertThat |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 40 | import org.junit.Rule |
| 41 | import org.junit.Test |
| 42 | import org.junit.runner.RunWith |
| 43 | import org.junit.runners.JUnit4 |
| 44 | import kotlin.math.roundToInt |
| 45 | |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 46 | @Model |
| 47 | data class DrawerStateHolder(var state: DrawerState) |
| 48 | |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 49 | @MediumTest |
| 50 | @RunWith(JUnit4::class) |
| 51 | class DrawerTest { |
| 52 | |
| 53 | @get:Rule |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 54 | val composeTestRule = createComposeRule(disableTransitions = true) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 55 | |
| 56 | @Test |
| 57 | fun modalDrawer_testOffset_whenOpened() { |
| 58 | var position: PxPosition? = null |
| 59 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 60 | ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 61 | Container(expanded = true) { |
| 62 | OnPositioned { coords -> |
| 63 | position = coords.localToGlobal(PxPosition.Origin) |
| 64 | } |
| 65 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 66 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 67 | } |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 68 | composeTestRule.runOnIdleCompose { |
| 69 | assertThat(position!!.x.value).isEqualTo(0f) |
| 70 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
| 74 | fun modalDrawer_testOffset_whenClosed() { |
| 75 | var position: PxPosition? = null |
| 76 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 77 | ModalDrawerLayout(DrawerState.Closed, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 78 | Container(expanded = true) { |
| 79 | OnPositioned { coords -> |
| 80 | position = coords.localToGlobal(PxPosition.Origin) |
| 81 | } |
| 82 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 83 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 84 | } |
| 85 | val width = composeTestRule.displayMetrics.widthPixels |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 86 | composeTestRule.runOnIdleCompose { |
| 87 | assertThat(position!!.x.round().value).isEqualTo(-width) |
| 88 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Test |
| 92 | fun modalDrawer_testEndPadding_whenOpened() { |
| 93 | var size: PxSize? = null |
| 94 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 95 | ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 96 | Container(expanded = true) { |
| 97 | OnPositioned { coords -> |
| 98 | size = coords.size |
| 99 | } |
| 100 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 101 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | val width = composeTestRule.displayMetrics.widthPixels |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 105 | composeTestRule.runOnIdleComposeWithDensity { |
| 106 | assertThat(size!!.width.round().value) |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 107 | .isEqualTo(width - 56.dp.toPx().round().value) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | fun bottomDrawer_testOffset_whenOpened() { |
| 113 | var position: PxPosition? = null |
| 114 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 115 | BottomDrawerLayout(DrawerState.Opened, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 116 | Container(expanded = true) { |
| 117 | OnPositioned { coords -> |
| 118 | position = coords.localToGlobal(PxPosition.Origin) |
| 119 | } |
| 120 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 121 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 122 | } |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 123 | |
Matvei Malkov | 2270be1 | 2019-07-08 16:59:45 +0100 | [diff] [blame] | 124 | val width = composeTestRule.displayMetrics.widthPixels |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 125 | val height = composeTestRule.displayMetrics.heightPixels |
Matvei Malkov | 2270be1 | 2019-07-08 16:59:45 +0100 | [diff] [blame] | 126 | // temporary calculation of landscape screen |
| 127 | val expectedHeight = if (width > height) height else (height / 2f).roundToInt() |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 128 | composeTestRule.runOnIdleCompose { |
| 129 | assertThat(position!!.y.round().value).isEqualTo(expectedHeight) |
| 130 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Test |
| 134 | fun bottomDrawer_testOffset_whenClosed() { |
| 135 | var position: PxPosition? = null |
| 136 | composeTestRule.setMaterialContent { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 137 | BottomDrawerLayout(DrawerState.Closed, {}, drawerContent = { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 138 | Container(expanded = true) { |
| 139 | OnPositioned { coords -> |
| 140 | position = coords.localToGlobal(PxPosition.Origin) |
| 141 | } |
| 142 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 143 | }) {} |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 144 | } |
| 145 | val height = composeTestRule.displayMetrics.heightPixels |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 146 | composeTestRule.runOnIdleCompose { |
| 147 | assertThat(position!!.y.round().value).isEqualTo(height) |
| 148 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Test |
| 152 | fun staticDrawer_testWidth_whenOpened() { |
| 153 | composeTestRule |
Matvei Malkov | 804bee4 | 2019-07-16 16:24:06 +0100 | [diff] [blame] | 154 | .setMaterialContentAndCollectSizes { |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 155 | StaticDrawer { |
| 156 | Container(expanded = true) {} |
| 157 | } |
| 158 | } |
| 159 | .assertWidthEqualsTo(256.dp) |
| 160 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 161 | |
| 162 | @Test |
| 163 | fun modalDrawer_bodyContent_clickable() { |
| 164 | var drawerClicks = 0 |
| 165 | var bodyClicks = 0 |
| 166 | val drawerState = DrawerStateHolder(DrawerState.Closed) |
| 167 | composeTestRule.setMaterialContent { |
Aurimas Liutikas | 7a828d3 | 2019-10-07 17:16:05 -0700 | [diff] [blame] | 168 | // emulate click on the screen |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 169 | TestTag("Drawer") { |
| 170 | Semantics(container = true) { |
| 171 | ModalDrawerLayout(drawerState.state, { drawerState.state = it }, |
| 172 | drawerContent = { |
| 173 | Clickable(onClick = { drawerClicks += 1 }) { |
| 174 | Container(expanded = true) {} |
| 175 | } |
| 176 | }, |
| 177 | bodyContent = { |
| 178 | Clickable(onClick = { bodyClicks += 1 }) { |
| 179 | Container(expanded = true) {} |
| 180 | } |
| 181 | }) |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 186 | // Click in the middle of the drawer (which is the middle of the body) |
| 187 | findByTag("Drawer").doGesture { sendClick() } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 188 | |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 189 | composeTestRule.runOnIdleCompose { |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 190 | assertThat(drawerClicks).isEqualTo(0) |
| 191 | assertThat(bodyClicks).isEqualTo(1) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 192 | |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 193 | drawerState.state = DrawerState.Opened |
| 194 | } |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 195 | 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] | 196 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 197 | // Click on the left-center pixel of the drawer |
| 198 | findByTag("Drawer").doGesture { |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame^] | 199 | val left = 1.px |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 200 | val centerY = globalBounds.height / 2 |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame^] | 201 | sendClick(PxPosition(left, centerY)) |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 202 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 203 | |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 204 | composeTestRule.runOnIdleCompose { |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 205 | assertThat(drawerClicks).isEqualTo(1) |
| 206 | assertThat(bodyClicks).isEqualTo(1) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 207 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Test |
| 211 | fun bottomDrawer_bodyContent_clickable() { |
| 212 | var drawerClicks = 0 |
| 213 | var bodyClicks = 0 |
| 214 | val drawerState = DrawerStateHolder(DrawerState.Closed) |
| 215 | composeTestRule.setMaterialContent { |
Aurimas Liutikas | 7a828d3 | 2019-10-07 17:16:05 -0700 | [diff] [blame] | 216 | // emulate click on the screen |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 217 | TestTag("Drawer") { |
| 218 | Semantics(container = true) { |
| 219 | BottomDrawerLayout(drawerState.state, { drawerState.state = it }, |
| 220 | drawerContent = { |
| 221 | Clickable(onClick = { drawerClicks += 1 }) { |
| 222 | Container(expanded = true) {} |
| 223 | } |
| 224 | }, |
| 225 | bodyContent = { |
| 226 | Clickable(onClick = { bodyClicks += 1 }) { |
| 227 | Container(expanded = true) {} |
| 228 | } |
| 229 | }) |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 234 | // Click in the middle of the drawer (which is the middle of the body) |
| 235 | findByTag("Drawer").doGesture { sendClick() } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 236 | |
Filip Pavlis | e63b30c | 2020-01-09 16:21:07 +0000 | [diff] [blame] | 237 | composeTestRule.runOnIdleCompose { |
| 238 | assertThat(drawerClicks).isEqualTo(0) |
| 239 | assertThat(bodyClicks).isEqualTo(1) |
| 240 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 241 | |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 242 | composeTestRule.runOnUiThread { |
| 243 | drawerState.state = DrawerState.Opened |
| 244 | } |
| 245 | sleep(100) // TODO(147586311): remove this sleep when opening the drawer triggers a wait |
| 246 | |
| 247 | // Click on the bottom-center pixel of the drawer |
| 248 | findByTag("Drawer").doGesture { |
| 249 | val bounds = globalBounds |
| 250 | val centerX = bounds.width / 2 |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame^] | 251 | val bottom = bounds.height - 1.px |
| 252 | sendClick(PxPosition(centerX, bottom)) |
Jelle Fresen | 11fc706 | 2020-01-13 16:53:55 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | assertThat(drawerClicks).isEqualTo(1) |
| 256 | assertThat(bodyClicks).isEqualTo(1) |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 257 | } |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 258 | } |