[go: nahoru, domu]

blob: 4f9080f8ea616020f13803dab2588da67c0ee8ba [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
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010019import androidx.compose.Model
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010020import androidx.test.filters.MediumTest
21import androidx.ui.core.OnPositioned
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010022import androidx.ui.core.TestTag
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010023import androidx.ui.foundation.Clickable
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010024import androidx.ui.layout.Container
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010025import androidx.ui.semantics.Semantics
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010026import androidx.ui.test.createComposeRule
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010027import androidx.ui.test.doClick
28import androidx.ui.test.findByTag
George Mount842c8c12020-01-08 16:03:42 -080029import androidx.ui.unit.PxPosition
30import androidx.ui.unit.PxSize
31import androidx.ui.unit.dp
32import androidx.ui.unit.round
Filip Pavlise63b30c2020-01-09 16:21:07 +000033import com.google.common.truth.Truth.assertThat
Matvei Malkov95ca1712019-09-06 17:11:28 +010034import org.junit.Ignore
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010035import org.junit.Rule
36import org.junit.Test
37import org.junit.runner.RunWith
38import org.junit.runners.JUnit4
39import kotlin.math.roundToInt
40
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010041@Model
42data class DrawerStateHolder(var state: DrawerState)
43
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010044@MediumTest
45@RunWith(JUnit4::class)
46class DrawerTest {
47
48 @get:Rule
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010049 val composeTestRule = createComposeRule(disableTransitions = true)
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010050
51 @Test
52 fun modalDrawer_testOffset_whenOpened() {
53 var position: PxPosition? = null
54 composeTestRule.setMaterialContent {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010055 ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010056 Container(expanded = true) {
57 OnPositioned { coords ->
58 position = coords.localToGlobal(PxPosition.Origin)
59 }
60 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010061 }) {}
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010062 }
Filip Pavlise63b30c2020-01-09 16:21:07 +000063 composeTestRule.runOnIdleCompose {
64 assertThat(position!!.x.value).isEqualTo(0f)
65 }
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010066 }
67
68 @Test
69 fun modalDrawer_testOffset_whenClosed() {
70 var position: PxPosition? = null
71 composeTestRule.setMaterialContent {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010072 ModalDrawerLayout(DrawerState.Closed, {}, drawerContent = {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010073 Container(expanded = true) {
74 OnPositioned { coords ->
75 position = coords.localToGlobal(PxPosition.Origin)
76 }
77 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010078 }) {}
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010079 }
80 val width = composeTestRule.displayMetrics.widthPixels
Filip Pavlise63b30c2020-01-09 16:21:07 +000081 composeTestRule.runOnIdleCompose {
82 assertThat(position!!.x.round().value).isEqualTo(-width)
83 }
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010084 }
85
86 @Test
87 fun modalDrawer_testEndPadding_whenOpened() {
88 var size: PxSize? = null
89 composeTestRule.setMaterialContent {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010090 ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010091 Container(expanded = true) {
92 OnPositioned { coords ->
93 size = coords.size
94 }
95 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010096 }) {}
Matvei Malkov2d37cbd2019-07-04 15:15:20 +010097 }
98
99 val width = composeTestRule.displayMetrics.widthPixels
Filip Pavlise63b30c2020-01-09 16:21:07 +0000100 composeTestRule.runOnIdleComposeWithDensity {
101 assertThat(size!!.width.round().value)
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100102 .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 Malkov7aa7fd22019-08-16 16:52:59 +0100110 BottomDrawerLayout(DrawerState.Opened, {}, drawerContent = {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100111 Container(expanded = true) {
112 OnPositioned { coords ->
113 position = coords.localToGlobal(PxPosition.Origin)
114 }
115 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100116 }) {}
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100117 }
Filip Pavlise63b30c2020-01-09 16:21:07 +0000118
Matvei Malkov2270be12019-07-08 16:59:45 +0100119 val width = composeTestRule.displayMetrics.widthPixels
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100120 val height = composeTestRule.displayMetrics.heightPixels
Matvei Malkov2270be12019-07-08 16:59:45 +0100121 // temporary calculation of landscape screen
122 val expectedHeight = if (width > height) height else (height / 2f).roundToInt()
Filip Pavlise63b30c2020-01-09 16:21:07 +0000123 composeTestRule.runOnIdleCompose {
124 assertThat(position!!.y.round().value).isEqualTo(expectedHeight)
125 }
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100126 }
127
128 @Test
129 fun bottomDrawer_testOffset_whenClosed() {
130 var position: PxPosition? = null
131 composeTestRule.setMaterialContent {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100132 BottomDrawerLayout(DrawerState.Closed, {}, drawerContent = {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100133 Container(expanded = true) {
134 OnPositioned { coords ->
135 position = coords.localToGlobal(PxPosition.Origin)
136 }
137 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100138 }) {}
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100139 }
140 val height = composeTestRule.displayMetrics.heightPixels
Filip Pavlise63b30c2020-01-09 16:21:07 +0000141 composeTestRule.runOnIdleCompose {
142 assertThat(position!!.y.round().value).isEqualTo(height)
143 }
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100144 }
145
146 @Test
147 fun staticDrawer_testWidth_whenOpened() {
148 composeTestRule
Matvei Malkov804bee42019-07-16 16:24:06 +0100149 .setMaterialContentAndCollectSizes {
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100150 StaticDrawer {
151 Container(expanded = true) {}
152 }
153 }
154 .assertWidthEqualsTo(256.dp)
155 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100156
157 @Test
Matvei Malkov95ca1712019-09-06 17:11:28 +0100158 @Ignore("(malkov/mount): unignore this when b/136678145 is fixed")
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100159 fun modalDrawer_bodyContent_clickable() {
160 var drawerClicks = 0
161 var bodyClicks = 0
162 val drawerState = DrawerStateHolder(DrawerState.Closed)
163 composeTestRule.setMaterialContent {
Aurimas Liutikas7a828d32019-10-07 17:16:05 -0700164 // emulate click on the screen
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100165 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 Pavlis2b161e42019-11-22 17:40:08 +0000185 composeTestRule.runOnIdleCompose {
Filip Pavlise63b30c2020-01-09 16:21:07 +0000186 assertThat(drawerClicks).isEqualTo(0)
187 assertThat(bodyClicks).isEqualTo(1)
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100188
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100189 drawerState.state = DrawerState.Opened
190 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100191
192 findByTag("Drawer")
193 .doClick()
194
Filip Pavlis2b161e42019-11-22 17:40:08 +0000195 composeTestRule.runOnIdleCompose {
Filip Pavlise63b30c2020-01-09 16:21:07 +0000196 assertThat(drawerClicks).isEqualTo(1)
197 assertThat(bodyClicks).isEqualTo(1)
Filip Pavlis2b161e42019-11-22 17:40:08 +0000198 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100199 }
200
201 @Test
Matvei Malkov95ca1712019-09-06 17:11:28 +0100202 @Ignore("(malkov/mount): unignore this when b/136678145 is fixed")
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100203 fun bottomDrawer_bodyContent_clickable() {
204 var drawerClicks = 0
205 var bodyClicks = 0
206 val drawerState = DrawerStateHolder(DrawerState.Closed)
207 composeTestRule.setMaterialContent {
Aurimas Liutikas7a828d32019-10-07 17:16:05 -0700208 // emulate click on the screen
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100209 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 Pavlise63b30c2020-01-09 16:21:07 +0000229 composeTestRule.runOnIdleCompose {
230 assertThat(drawerClicks).isEqualTo(0)
231 assertThat(bodyClicks).isEqualTo(1)
232 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100233
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 Malkov2d37cbd2019-07-04 15:15:20 +0100247}