[go: nahoru, domu]

blob: fa3267c6a52cdcd8baf729145e6edcec6ccabaf7 [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 }
Matvei Malkov2270be12019-07-08 16:59:45 +0100106 val width = composeTestRule.displayMetrics.widthPixels
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100107 val height = composeTestRule.displayMetrics.heightPixels
Matvei Malkov2270be12019-07-08 16:59:45 +0100108 // temporary calculation of landscape screen
109 val expectedHeight = if (width > height) height else (height / 2f).roundToInt()
110 Truth.assertThat(position!!.y.round().value).isEqualTo(expectedHeight)
Matvei Malkov2d37cbd2019-07-04 15:15:20 +0100111 }
112
113 @Test
114 fun bottomDrawer_testOffset_whenClosed() {
115 var position: PxPosition? = null
116 composeTestRule.setMaterialContent {
117 BottomDrawer(DrawerState.Closed, {}) {
118 Container(expanded = true) {
119 OnPositioned { coords ->
120 position = coords.localToGlobal(PxPosition.Origin)
121 }
122 }
123 }
124 }
125 val height = composeTestRule.displayMetrics.heightPixels
126 Truth.assertThat(position!!.y.round().value).isEqualTo(height)
127 }
128
129 @Test
130 fun staticDrawer_testWidth_whenOpened() {
131 composeTestRule
132 .setMaterialContentAndTestSizes {
133 StaticDrawer {
134 Container(expanded = true) {}
135 }
136 }
137 .assertWidthEqualsTo(256.dp)
138 }
139}