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