[go: nahoru, domu]

blob: 25cadc93b175301564ac31125ad8d4756c34cf67 [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
19import androidx.compose.composer
20import androidx.test.filters.MediumTest
21import androidx.ui.core.OnPositioned
22import androidx.ui.core.PxPosition
23import androidx.ui.core.PxSize
24import androidx.ui.core.dp
25import androidx.ui.core.round
26import androidx.ui.core.withDensity
27import androidx.ui.layout.Container
28import androidx.ui.test.createComposeRule
29import com.google.common.truth.Truth
30import org.junit.Rule
31import org.junit.Test
32import org.junit.runner.RunWith
33import org.junit.runners.JUnit4
34import kotlin.math.roundToInt
35
36@MediumTest
37@RunWith(JUnit4::class)
38class DrawerTest {
39
40 @get:Rule
41 val composeTestRule = createComposeRule()
42
43 @Test
44 fun modalDrawer_testOffset_whenOpened() {
45 var position: PxPosition? = null
46 composeTestRule.setMaterialContent {
47 ModalDrawer(DrawerState.Opened, {}) {
48 Container(expanded = true) {
49 OnPositioned { coords ->
50 position = coords.localToGlobal(PxPosition.Origin)
51 }
52 }
53 }
54 }
55 Truth.assertThat(position!!.x.value).isEqualTo(0f)
56 }
57
58 @Test
59 fun modalDrawer_testOffset_whenClosed() {
60 var position: PxPosition? = null
61 composeTestRule.setMaterialContent {
62 ModalDrawer(DrawerState.Closed, {}) {
63 Container(expanded = true) {
64 OnPositioned { coords ->
65 position = coords.localToGlobal(PxPosition.Origin)
66 }
67 }
68 }
69 }
70 val width = composeTestRule.displayMetrics.widthPixels
71 Truth.assertThat(position!!.x.round().value).isEqualTo(-width)
72 }
73
74 @Test
75 fun modalDrawer_testEndPadding_whenOpened() {
76 var size: PxSize? = null
77 composeTestRule.setMaterialContent {
78 ModalDrawer(DrawerState.Opened, {}) {
79 Container(expanded = true) {
80 OnPositioned { coords ->
81 size = coords.size
82 }
83 }
84 }
85 }
86
87 val width = composeTestRule.displayMetrics.widthPixels
88 withDensity(composeTestRule.density) {
89 Truth.assertThat(size!!.width.round().value)
90 .isEqualTo(width - 56.dp.toPx().round().value)
91 }
92 }
93
94 @Test
95 fun bottomDrawer_testOffset_whenOpened() {
96 var position: PxPosition? = null
97 composeTestRule.setMaterialContent {
98 BottomDrawer(DrawerState.Opened, {}) {
99 Container(expanded = true) {
100 OnPositioned { coords ->
101 position = coords.localToGlobal(PxPosition.Origin)
102 }
103 }
104 }
105 }
106 val height = composeTestRule.displayMetrics.heightPixels
107 Truth.assertThat(position!!.y.round().value).isEqualTo((height / 2f).roundToInt())
108 }
109
110 @Test
111 fun bottomDrawer_testOffset_whenClosed() {
112 var position: PxPosition? = null
113 composeTestRule.setMaterialContent {
114 BottomDrawer(DrawerState.Closed, {}) {
115 Container(expanded = true) {
116 OnPositioned { coords ->
117 position = coords.localToGlobal(PxPosition.Origin)
118 }
119 }
120 }
121 }
122 val height = composeTestRule.displayMetrics.heightPixels
123 Truth.assertThat(position!!.y.round().value).isEqualTo(height)
124 }
125
126 @Test
127 fun staticDrawer_testWidth_whenOpened() {
128 composeTestRule
129 .setMaterialContentAndTestSizes {
130 StaticDrawer {
131 Container(expanded = true) {}
132 }
133 }
134 .assertWidthEqualsTo(256.dp)
135 }
136}