Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +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 | */ |
Siyamed Sinir | fc05929 | 2019-11-21 13:39:17 -0800 | [diff] [blame] | 16 | package androidx.ui.core |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 17 | |
| 18 | import android.view.View |
Siyamed Sinir | fc05929 | 2019-11-21 13:39:17 -0800 | [diff] [blame] | 19 | import androidx.compose.Composable |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame^] | 20 | import androidx.compose.emptyContent |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 21 | import androidx.test.espresso.Espresso |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 22 | import androidx.test.espresso.Root |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 23 | import androidx.test.espresso.assertion.ViewAssertions.matches |
| 24 | import androidx.test.espresso.matcher.BoundedMatcher |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 25 | import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
| 26 | import androidx.test.filters.MediumTest |
Siyamed Sinir | fd8bc42 | 2019-11-21 18:23:58 -0800 | [diff] [blame] | 27 | import androidx.ui.core.selection.SimpleContainer |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 28 | import androidx.ui.test.createComposeRule |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 29 | import androidx.ui.unit.IntPx |
| 30 | import androidx.ui.unit.IntPxPosition |
| 31 | import androidx.ui.unit.IntPxSize |
Mihai Popa | d9d53507 | 2020-01-15 12:58:15 +0000 | [diff] [blame] | 32 | import androidx.ui.unit.ipx |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 33 | import androidx.ui.unit.isFinite |
| 34 | import androidx.ui.unit.toPxPosition |
| 35 | import androidx.ui.unit.toPxSize |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 36 | import com.google.common.truth.Truth |
| 37 | import org.hamcrest.CoreMatchers.instanceOf |
| 38 | import org.hamcrest.Description |
| 39 | import org.hamcrest.Matcher |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 40 | import org.hamcrest.TypeSafeMatcher |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 41 | import org.junit.Rule |
| 42 | import org.junit.Test |
| 43 | import org.junit.runner.RunWith |
| 44 | import org.junit.runners.JUnit4 |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 45 | import java.util.concurrent.CountDownLatch |
| 46 | import java.util.concurrent.TimeUnit |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 47 | |
| 48 | @MediumTest |
| 49 | @RunWith(JUnit4::class) |
| 50 | class PopupTest { |
| 51 | @get:Rule |
| 52 | val composeTestRule = createComposeRule(disableTransitions = true) |
| 53 | private val popupText = "popupText" |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 54 | private val testTag = "testedPopup" |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 55 | |
| 56 | private val parentGlobalPosition = IntPxPosition(IntPx(50), IntPx(50)) |
| 57 | private val offset = IntPxPosition(IntPx(10), IntPx(10)) |
| 58 | private val parentSize = IntPxSize(IntPx(100), IntPx(100)) |
| 59 | private val popupSize = IntPxSize(IntPx(40), IntPx(20)) |
| 60 | |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 61 | private var composeViewAbsolutePosition = IntPxPosition(IntPx(0), IntPx(0)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 62 | |
| 63 | // TODO(b/140215440): Some tests are calling the OnChildPosition method inside the Popup too |
| 64 | // many times |
| 65 | private fun createPopupWithAlignmentRule(alignment: Alignment, measureLatch: CountDownLatch) { |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 66 | with(composeTestRule.density) { |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 67 | val popupWidthDp = popupSize.width.toDp() |
| 68 | val popupHeightDp = popupSize.height.toDp() |
| 69 | val parentWidthDp = parentSize.width.toDp() |
| 70 | val parentHeightDp = parentSize.height.toDp() |
| 71 | |
| 72 | composeTestRule.setContent { |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 73 | // Get the compose view position on screen |
Chuck Jazdzewski | fb6db65 | 2019-11-25 08:30:51 -0800 | [diff] [blame] | 74 | val composeView = AndroidComposeViewAmbient.current |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 75 | val positionArray = IntArray(2) |
| 76 | composeView.getLocationOnScreen(positionArray) |
| 77 | composeViewAbsolutePosition = IntPxPosition( |
| 78 | IntPx(positionArray[0]), |
| 79 | IntPx(positionArray[1]) |
| 80 | ) |
| 81 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 82 | // Align the parent of the popup on the TopLeft corner, this results in the global |
| 83 | // position of the parent to be (0, 0) |
Siyamed Sinir | fc05929 | 2019-11-21 13:39:17 -0800 | [diff] [blame] | 84 | TestAlign { |
Siyamed Sinir | fd8bc42 | 2019-11-21 18:23:58 -0800 | [diff] [blame] | 85 | SimpleContainer(width = parentWidthDp, height = parentHeightDp) { |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 86 | PopupTestTag(testTag) { |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 87 | Popup(alignment = alignment, offset = offset) { |
| 88 | // This is called after the OnChildPosition method in Popup() which |
| 89 | // updates the popup to its final position |
| 90 | OnPositioned { |
| 91 | measureLatch.countDown() |
| 92 | } |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame^] | 93 | SimpleContainer( |
| 94 | width = popupWidthDp, |
| 95 | height = popupHeightDp, |
| 96 | children = emptyContent() |
| 97 | ) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 98 | } |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 106 | // TODO(b/139861182): Remove all of this and provide helpers on ComposeTestRule |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 107 | private fun popupMatches(viewMatcher: Matcher<in View>) { |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 108 | Espresso.onView(instanceOf(AndroidComposeView::class.java)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 109 | .inRoot(PopupLayoutMatcher()) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 110 | .check(matches(viewMatcher)) |
| 111 | } |
| 112 | |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 113 | private inner class PopupLayoutMatcher : TypeSafeMatcher<Root>() { |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 114 | override fun describeTo(description: Description?) { |
| 115 | description?.appendText("PopupLayoutMatcher") |
| 116 | } |
| 117 | |
| 118 | // TODO(b/141101446): Find a way to match the window used by the popup |
| 119 | override fun matchesSafely(item: Root?): Boolean { |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 120 | return item != null && isPopupLayout(item.decorView, testTag) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 124 | @Test |
| 125 | fun popup_isShowing() { |
| 126 | composeTestRule.setContent { |
Siyamed Sinir | fd8bc42 | 2019-11-21 18:23:58 -0800 | [diff] [blame] | 127 | SimpleContainer { |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 128 | PopupTestTag(testTag) { |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 129 | Popup(alignment = Alignment.Center) { |
| 130 | Text(popupText) |
| 131 | } |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | popupMatches(isDisplayed()) |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | fun popup_hasActualSize() { |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 141 | val popupWidthDp = with(composeTestRule.density) { |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 142 | popupSize.width.toDp() |
| 143 | } |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 144 | val popupHeightDp = with(composeTestRule.density) { |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 145 | popupSize.height.toDp() |
| 146 | } |
| 147 | |
| 148 | composeTestRule.setContent { |
Siyamed Sinir | fd8bc42 | 2019-11-21 18:23:58 -0800 | [diff] [blame] | 149 | SimpleContainer { |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 150 | PopupTestTag(testTag) { |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 151 | Popup(alignment = Alignment.Center) { |
Louis Pullen-Freilich | e46bcf0 | 2020-02-12 16:06:21 +0000 | [diff] [blame^] | 152 | SimpleContainer( |
| 153 | width = popupWidthDp, |
| 154 | height = popupHeightDp, |
| 155 | children = emptyContent() |
| 156 | ) |
Filip Pavlis | bea89ec | 2019-10-02 17:28:06 +0100 | [diff] [blame] | 157 | } |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | popupMatches(matchesSize(popupSize.width.value, popupSize.height.value)) |
| 163 | } |
| 164 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 165 | @Test |
| 166 | fun popup_correctPosition_alignmentTopLeft() { |
| 167 | /* Expected TopLeft Position |
| 168 | x = offset.x |
| 169 | y = offset.y |
| 170 | */ |
| 171 | val expectedPositionTopLeft = IntPxPosition(IntPx(10), IntPx(10)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 172 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 173 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 174 | createPopupWithAlignmentRule(alignment = Alignment.TopLeft, measureLatch = measureLatch) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 175 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 176 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 177 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionTopLeft)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 180 | @Test |
| 181 | fun popup_correctPosition_alignmentTopCenter() { |
| 182 | /* Expected TopCenter Position |
| 183 | x = offset.x + parentSize.x / 2 - popupSize.x / 2 |
| 184 | y = offset.y |
| 185 | */ |
| 186 | val expectedPositionTopCenter = IntPxPosition(IntPx(40), IntPx(10)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 187 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 188 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 189 | createPopupWithAlignmentRule(alignment = Alignment.TopCenter, measureLatch = measureLatch) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 190 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 191 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 192 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionTopCenter)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 195 | @Test |
| 196 | fun popup_correctPosition_alignmentTopRight() { |
| 197 | /* Expected TopRight Position |
| 198 | x = offset.x + parentSize.x - popupSize.x |
| 199 | y = offset.y |
| 200 | */ |
| 201 | val expectedPositionTopRight = IntPxPosition(IntPx(70), IntPx(10)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 202 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 203 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 204 | createPopupWithAlignmentRule(alignment = Alignment.TopRight, measureLatch = measureLatch) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 205 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 206 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 207 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionTopRight)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 210 | @Test |
| 211 | fun popup_correctPosition_alignmentCenterRight() { |
| 212 | /* Expected CenterRight Position |
| 213 | x = offset.x + parentSize.x - popupSize.x |
| 214 | y = offset.y + parentSize.y / 2 - popupSize.y / 2 |
| 215 | */ |
| 216 | val expectedPositionCenterRight = IntPxPosition(IntPx(70), IntPx(50)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 217 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 218 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 219 | createPopupWithAlignmentRule(alignment = Alignment.CenterRight, measureLatch = measureLatch) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 220 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 221 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 222 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionCenterRight)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 225 | @Test |
| 226 | fun popup_correctPosition_alignmentBottomRight() { |
| 227 | /* Expected BottomRight Position |
| 228 | x = offset.x + parentSize.x - popupSize.x |
| 229 | y = offset.y + parentSize.y - popupSize.y |
| 230 | */ |
| 231 | val expectedPositionBottomRight = IntPxPosition(IntPx(70), IntPx(90)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 232 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 233 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 234 | createPopupWithAlignmentRule(alignment = Alignment.BottomRight, measureLatch = measureLatch) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 235 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 236 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 237 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionBottomRight)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 240 | @Test |
| 241 | fun popup_correctPosition_alignmentBottomCenter() { |
| 242 | /* Expected BottomCenter Position |
| 243 | x = offset.x + parentSize.x / 2 - popupSize.x / 2 |
| 244 | y = offset.y + parentSize.y - popupSize.y |
| 245 | */ |
| 246 | val expectedPositionBottomCenter = IntPxPosition(IntPx(40), IntPx(90)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 247 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 248 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 249 | createPopupWithAlignmentRule( |
| 250 | alignment = Alignment.BottomCenter, |
| 251 | measureLatch = measureLatch |
| 252 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 253 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 254 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 255 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionBottomCenter)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 258 | @Test |
| 259 | fun popup_correctPosition_alignmentBottomLeft() { |
| 260 | /* Expected BottomLeft Position |
| 261 | x = offset.x |
| 262 | y = offset.y + parentSize.y - popupSize.y |
| 263 | */ |
| 264 | val expectedPositionBottomLeft = IntPxPosition(IntPx(10), IntPx(90)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 265 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 266 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 267 | createPopupWithAlignmentRule(alignment = Alignment.BottomLeft, measureLatch = measureLatch) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 268 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 269 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 270 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionBottomLeft)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 273 | @Test |
| 274 | fun popup_correctPosition_alignmentCenterLeft() { |
| 275 | /* Expected CenterLeft Position |
| 276 | x = offset.x |
| 277 | y = offset.y + parentSize.y / 2 - popupSize.y / 2 |
| 278 | */ |
| 279 | val expectedPositionCenterLeft = IntPxPosition(IntPx(10), IntPx(50)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 280 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 281 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 282 | createPopupWithAlignmentRule(alignment = Alignment.CenterLeft, measureLatch = measureLatch) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 283 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 284 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 285 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionCenterLeft)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 288 | @Test |
| 289 | fun popup_correctPosition_alignmentCenter() { |
| 290 | /* Expected Center Position |
| 291 | x = offset.x + parentSize.x / 2 - popupSize.x / 2 |
| 292 | y = offset.y + parentSize.y / 2 - popupSize.y / 2 |
| 293 | */ |
| 294 | val expectedPositionCenter = IntPxPosition(IntPx(40), IntPx(50)) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 295 | val measureLatch = CountDownLatch(1) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 296 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 297 | createPopupWithAlignmentRule(alignment = Alignment.Center, measureLatch = measureLatch) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 298 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 299 | measureLatch.await(1, TimeUnit.SECONDS) |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 300 | popupMatches(matchesPosition(composeViewAbsolutePosition + expectedPositionCenter)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Test |
| 304 | fun popup_calculateGlobalPositionTopLeft() { |
| 305 | /* Expected TopLeft Position |
| 306 | x = parentGlobalPosition.x + offset.x |
| 307 | y = parentGlobalPosition.y + offset.y |
| 308 | */ |
| 309 | val expectedPositionTopLeft = IntPxPosition(IntPx(60), IntPx(60)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 310 | val popupPositionProperties = PopupPositionProperties( |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 311 | offset = offset |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 312 | ) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 313 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 314 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 315 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 316 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 317 | val positionTopLeft = calculatePopupGlobalPosition( |
| 318 | popupPositionProperties, |
| 319 | Alignment.TopLeft |
| 320 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 321 | |
| 322 | Truth.assertThat(positionTopLeft).isEqualTo(expectedPositionTopLeft) |
| 323 | } |
| 324 | |
| 325 | @Test |
| 326 | fun popup_calculateGlobalPositionTopCenter() { |
| 327 | /* Expected TopCenter Position |
| 328 | x = parentGlobalPosition.x + offset.x + parentSize.x / 2 - popupSize.x / 2 |
| 329 | y = parentGlobalPosition.y + offset.y |
| 330 | */ |
| 331 | val expectedPositionTopCenter = IntPxPosition(IntPx(90), IntPx(60)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 332 | val popupPositionProperties = PopupPositionProperties( |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 333 | offset = offset |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 334 | ) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 335 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 336 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 337 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 338 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 339 | val positionTopCenter = calculatePopupGlobalPosition( |
| 340 | popupPositionProperties, |
| 341 | Alignment.TopCenter |
| 342 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 343 | |
| 344 | Truth.assertThat(positionTopCenter).isEqualTo(expectedPositionTopCenter) |
| 345 | } |
| 346 | |
| 347 | @Test |
| 348 | fun popup_calculateGlobalPositionTopRight() { |
| 349 | /* Expected TopRight Position |
| 350 | x = parentGlobalPosition.x + offset.x + parentSize.x - popupSize.x |
| 351 | y = parentGlobalPosition.y + offset.y |
| 352 | */ |
| 353 | val expectedPositionTopRight = IntPxPosition(IntPx(120), IntPx(60)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 354 | val popupPositionProperties = PopupPositionProperties( |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 355 | offset = offset |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 356 | ) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 357 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 358 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 359 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 360 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 361 | val positionTopRight = calculatePopupGlobalPosition( |
| 362 | popupPositionProperties, |
| 363 | Alignment.TopRight |
| 364 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 365 | |
| 366 | Truth.assertThat(positionTopRight).isEqualTo(expectedPositionTopRight) |
| 367 | } |
| 368 | |
| 369 | @Test |
| 370 | fun popup_calculateGlobalPositionCenterRight() { |
| 371 | /* Expected CenterRight Position |
| 372 | x = parentGlobalPosition.x + offset.x + parentSize.x - popupSize.x |
| 373 | y = parentGlobalPosition.y + offset.y + parentSize.y / 2 - popupSize.y / 2 |
| 374 | */ |
| 375 | val expectedPositionCenterRight = IntPxPosition(IntPx(120), IntPx(100)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 376 | val popupPositionProperties = PopupPositionProperties( |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 377 | offset = offset |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 378 | ) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 379 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 380 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 381 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 382 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 383 | val positionBottomRight = calculatePopupGlobalPosition( |
| 384 | popupPositionProperties, |
| 385 | Alignment.CenterRight |
| 386 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 387 | |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 388 | Truth.assertThat(positionBottomRight).isEqualTo(expectedPositionCenterRight) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Test |
| 392 | fun popup_calculateGlobalPositionBottomCenter() { |
| 393 | /* Expected BottomCenter Position |
| 394 | x = parentGlobalPosition.x + offset.x + parentSize.x / 2 - popupSize.x / 2 |
| 395 | y = parentGlobalPosition.y + offset.y + parentSize.y - popupSize.y |
| 396 | */ |
| 397 | val expectedPositionBottomCenter = IntPxPosition(IntPx(90), IntPx(140)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 398 | val popupPositionProperties = PopupPositionProperties( |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 399 | offset = offset |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 400 | ) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 401 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 402 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 403 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 404 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 405 | val positionBottomCenter = calculatePopupGlobalPosition( |
| 406 | popupPositionProperties, |
| 407 | Alignment.BottomCenter |
| 408 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 409 | |
| 410 | Truth.assertThat(positionBottomCenter).isEqualTo(expectedPositionBottomCenter) |
| 411 | } |
| 412 | |
| 413 | @Test |
| 414 | fun popup_calculateGlobalPositionBottomLeft() { |
| 415 | /* Expected BottomLeft Position |
| 416 | x = parentGlobalPosition.x + offset.x |
| 417 | y = parentGlobalPosition.y + offset.y + parentSize.y - popupSize.y |
| 418 | */ |
| 419 | val expectedPositionBottomLeft = IntPxPosition(IntPx(60), IntPx(140)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 420 | val popupPositionProperties = PopupPositionProperties( |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 421 | offset = offset |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 422 | ) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 423 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 424 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 425 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 426 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 427 | val positionBottomLeft = calculatePopupGlobalPosition( |
| 428 | popupPositionProperties, |
| 429 | Alignment.BottomLeft |
| 430 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 431 | |
| 432 | Truth.assertThat(positionBottomLeft).isEqualTo(expectedPositionBottomLeft) |
| 433 | } |
| 434 | |
| 435 | @Test |
| 436 | fun popup_calculateGlobalPositionCenterLeft() { |
| 437 | /* Expected CenterLeft Position |
| 438 | x = parentGlobalPosition.x + offset.x |
| 439 | y = parentGlobalPosition.y + offset.y + parentSize.y / 2 - popupSize.y / 2 |
| 440 | */ |
| 441 | val expectedPositionCenterLeft = IntPxPosition(IntPx(60), IntPx(100)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 442 | val popupPositionProperties = PopupPositionProperties( |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 443 | offset = offset |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 444 | ) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 445 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 446 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 447 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 448 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 449 | val positionCenterLeft = calculatePopupGlobalPosition( |
| 450 | popupPositionProperties, |
| 451 | Alignment.CenterLeft |
| 452 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 453 | |
| 454 | Truth.assertThat(positionCenterLeft).isEqualTo(expectedPositionCenterLeft) |
| 455 | } |
| 456 | |
| 457 | @Test |
| 458 | fun popup_calculateGlobalPositionCenter() { |
| 459 | /* Expected Center Position |
| 460 | x = parentGlobalPosition.x + offset.x + parentSize.x / 2 - popupSize.x / 2 |
| 461 | y = parentGlobalPosition.y + offset.y + parentSize.y / 2 - popupSize.y / 2 |
| 462 | */ |
| 463 | val expectedPositionCenter = IntPxPosition(IntPx(90), IntPx(100)) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 464 | val popupPositionProperties = PopupPositionProperties( |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 465 | offset = offset |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 466 | ) |
Mihai Burlaciuc | 27e8f54 | 2019-09-16 11:19:33 +0100 | [diff] [blame] | 467 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 468 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 469 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 470 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 471 | val positionCenter = calculatePopupGlobalPosition( |
| 472 | popupPositionProperties, |
| 473 | Alignment.Center |
| 474 | ) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 475 | |
| 476 | Truth.assertThat(positionCenter).isEqualTo(expectedPositionCenter) |
| 477 | } |
| 478 | |
Mihai Burlaciuc | 49000c6 | 2019-09-17 11:15:47 +0100 | [diff] [blame] | 479 | @Test |
| 480 | fun dropdownAlignment_calculateGlobalPositionLeft() { |
| 481 | /* Expected Dropdown Start Position |
| 482 | x = parentGlobalPosition.x + offset.x |
| 483 | y = parentGlobalPosition.y + offset.y + parentSize.y |
| 484 | */ |
| 485 | val expectedPositionLeft = IntPxPosition(IntPx(60), IntPx(160)) |
| 486 | val popupPositionProperties = PopupPositionProperties(offset = offset) |
| 487 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 488 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 489 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 490 | |
| 491 | val positionLeft = calculateDropdownPopupPosition( |
| 492 | popupPositionProperties, |
| 493 | DropDownAlignment.Left |
| 494 | ) |
| 495 | |
| 496 | Truth.assertThat(positionLeft).isEqualTo(expectedPositionLeft) |
| 497 | } |
| 498 | |
| 499 | @Test |
| 500 | fun dropdownAlignment_calculateGlobalPositionRight() { |
| 501 | /* Expected Dropdown End Position |
| 502 | x = parentGlobalPosition.x + offset.x + parentSize.x |
| 503 | y = parentGlobalPosition.y + offset.y + parentSize.y |
| 504 | */ |
| 505 | val expectedPositionRight = IntPxPosition(IntPx(160), IntPx(160)) |
| 506 | val popupPositionProperties = PopupPositionProperties(offset = offset) |
| 507 | popupPositionProperties.parentPosition = parentGlobalPosition.toPxPosition() |
| 508 | popupPositionProperties.parentSize = parentSize.toPxSize() |
| 509 | popupPositionProperties.childrenSize = popupSize.toPxSize() |
| 510 | |
| 511 | val positionRight = calculateDropdownPopupPosition( |
| 512 | popupPositionProperties, |
| 513 | DropDownAlignment.Right |
| 514 | ) |
| 515 | |
| 516 | Truth.assertThat(positionRight).isEqualTo(expectedPositionRight) |
| 517 | } |
| 518 | |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 519 | private fun matchesAndroidComposeView(): BoundedMatcher<View, View> { |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 520 | return object : BoundedMatcher<View, View>(View::class.java) { |
| 521 | override fun matchesSafely(item: View?): Boolean { |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 522 | return (item is AndroidComposeView) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | override fun describeTo(description: Description?) { |
George Mount | 203056e | 2019-08-30 16:17:48 -0700 | [diff] [blame] | 526 | description?.appendText("with no AndroidComposeView") |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 531 | private fun matchesSize(width: Int, height: Int): BoundedMatcher<View, View> { |
| 532 | return object : BoundedMatcher<View, View>(View::class.java) { |
| 533 | override fun matchesSafely(item: View?): Boolean { |
| 534 | return item?.width == width && item.height == height |
| 535 | } |
| 536 | |
| 537 | override fun describeTo(description: Description?) { |
| 538 | description?.appendText("with width = $width height = $height") |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | private fun matchesPosition(expectedPosition: IntPxPosition): BoundedMatcher<View, View> { |
| 544 | return object : BoundedMatcher<View, View>(View::class.java) { |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 545 | // (-1, -1) no position found |
| 546 | var positionFound = IntPxPosition(IntPx(-1), IntPx(-1)) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 547 | override fun matchesSafely(item: View?): Boolean { |
| 548 | val position = IntArray(2) |
| 549 | item?.getLocationOnScreen(position) |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 550 | positionFound = IntPxPosition(IntPx(position[0]), IntPx(position[1])) |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 551 | |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 552 | return expectedPosition == positionFound |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | override fun describeTo(description: Description?) { |
Mihai Burlaciuc | 96f747c | 2019-08-23 14:15:43 +0100 | [diff] [blame] | 556 | description?.appendText("with expected position: $expectedPosition" + |
| 557 | " but position found: $positionFound") |
Mihai Burlaciuc | 478fae6 | 2019-08-08 10:00:06 +0100 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
Siyamed Sinir | fc05929 | 2019-11-21 13:39:17 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | @Composable |
| 564 | private fun TestAlign(children: @Composable() () -> Unit) { |
| 565 | Layout(children) { measurables, constraints -> |
| 566 | val measurable = measurables.firstOrNull() |
| 567 | // The child cannot be larger than our max constraints, but we ignore min constraints. |
Mihai Popa | d9d53507 | 2020-01-15 12:58:15 +0000 | [diff] [blame] | 568 | val placeable = measurable?.measure(constraints.copy(minWidth = 0.ipx, minHeight = 0.ipx)) |
Siyamed Sinir | fc05929 | 2019-11-21 13:39:17 -0800 | [diff] [blame] | 569 | |
| 570 | // The layout is as large as possible for bounded constraints, |
| 571 | // or wrap content otherwise. |
| 572 | val layoutWidth = if (constraints.maxWidth.isFinite()) { |
| 573 | constraints.maxWidth |
| 574 | } else { |
| 575 | placeable?.width ?: constraints.minWidth |
| 576 | } |
| 577 | val layoutHeight = if (constraints.maxHeight.isFinite()) { |
| 578 | constraints.maxHeight |
| 579 | } else { |
| 580 | placeable?.height ?: constraints.minHeight |
| 581 | } |
| 582 | |
| 583 | layout(layoutWidth, layoutHeight) { |
| 584 | if (placeable != null) { |
| 585 | val position = Alignment.TopLeft.align( |
| 586 | IntPxSize(layoutWidth - placeable.width, layoutHeight - placeable.height) |
| 587 | ) |
| 588 | placeable.place(position.x, position.y) |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |