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 | |
| 19 | import androidx.compose.composer |
| 20 | import androidx.test.filters.MediumTest |
| 21 | import androidx.ui.core.OnPositioned |
| 22 | import androidx.ui.core.PxPosition |
| 23 | import androidx.ui.core.PxSize |
| 24 | import androidx.ui.core.dp |
| 25 | import androidx.ui.core.round |
| 26 | import androidx.ui.core.withDensity |
| 27 | import androidx.ui.layout.Container |
| 28 | import androidx.ui.test.createComposeRule |
| 29 | import com.google.common.truth.Truth |
| 30 | import org.junit.Rule |
| 31 | import org.junit.Test |
| 32 | import org.junit.runner.RunWith |
| 33 | import org.junit.runners.JUnit4 |
| 34 | import kotlin.math.roundToInt |
| 35 | |
| 36 | @MediumTest |
| 37 | @RunWith(JUnit4::class) |
| 38 | class 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 Malkov | 2270be1 | 2019-07-08 16:59:45 +0100 | [diff] [blame^] | 106 | val width = composeTestRule.displayMetrics.widthPixels |
Matvei Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 107 | val height = composeTestRule.displayMetrics.heightPixels |
Matvei Malkov | 2270be1 | 2019-07-08 16:59:45 +0100 | [diff] [blame^] | 108 | // 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 Malkov | 2d37cbd | 2019-07-04 15:15:20 +0100 | [diff] [blame] | 111 | } |
| 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 | } |