Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +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.layout.test |
| 18 | |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 19 | import androidx.compose.Composable |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 20 | import androidx.test.filters.SmallTest |
Qingqing Deng | 8c5d038 | 2019-09-16 11:59:16 -0700 | [diff] [blame] | 21 | import androidx.ui.core.Alignment |
Mihai Popa | ca1c4b9 | 2019-10-03 19:16:06 +0100 | [diff] [blame] | 22 | import androidx.ui.core.Layout |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 23 | import androidx.ui.core.LayoutCoordinates |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 24 | import androidx.ui.core.Modifier |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 25 | import androidx.ui.core.Ref |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 26 | import androidx.ui.core.onChildPositioned |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 27 | import androidx.ui.layout.Align |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 28 | import androidx.ui.layout.DpConstraints |
| 29 | import androidx.ui.layout.MaxIntrinsicHeight |
| 30 | import androidx.ui.layout.MaxIntrinsicWidth |
| 31 | import androidx.ui.layout.MinIntrinsicHeight |
| 32 | import androidx.ui.layout.MinIntrinsicWidth |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 33 | import androidx.ui.unit.Dp |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 34 | import androidx.ui.unit.IntPxSize |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 35 | import androidx.ui.unit.PxPosition |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 36 | import androidx.ui.unit.dp |
| 37 | import androidx.ui.unit.ipx |
| 38 | import androidx.ui.unit.px |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 39 | import org.junit.Assert.assertEquals |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 40 | import org.junit.Assert.assertTrue |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 41 | import org.junit.Test |
| 42 | import org.junit.runner.RunWith |
| 43 | import org.junit.runners.JUnit4 |
| 44 | import java.util.concurrent.CountDownLatch |
| 45 | import java.util.concurrent.TimeUnit |
| 46 | |
| 47 | @SmallTest |
| 48 | @RunWith(JUnit4::class) |
| 49 | class IntrinsicTest : LayoutTest() { |
| 50 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 51 | fun testMinIntrinsicWidth() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 52 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 53 | val minIntrinsicWidthSize = Ref<IntPxSize>() |
| 54 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 55 | val childPosition = Ref<PxPosition>() |
| 56 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 57 | Align( |
| 58 | alignment = Alignment.TopStart, |
| 59 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 60 | minIntrinsicWidthSize.value = coordinates.size |
| 61 | positionedLatch.countDown() |
| 62 | } |
| 63 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 64 | MinIntrinsicWidth { |
| 65 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 66 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 67 | size = childSize, |
| 68 | position = childPosition, |
| 69 | positionedLatch = positionedLatch |
| 70 | ) |
| 71 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 76 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 77 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 78 | assertEquals(IntPxSize(10.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value) |
| 79 | assertEquals(IntPxSize(10.dp.toIntPx(), 50.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 80 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 81 | } |
| 82 | |
| 83 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 84 | fun testMinIntrinsicHeight() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 85 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 86 | val minIntrinsicHeightSize = Ref<IntPxSize>() |
| 87 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 88 | val childPosition = Ref<PxPosition>() |
| 89 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 90 | Align( |
| 91 | alignment = Alignment.TopStart, |
| 92 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 93 | minIntrinsicHeightSize.value = coordinates.size |
| 94 | positionedLatch.countDown() |
| 95 | } |
| 96 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 97 | MinIntrinsicHeight { |
| 98 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 99 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 100 | size = childSize, |
| 101 | position = childPosition, |
| 102 | positionedLatch = positionedLatch |
| 103 | ) |
| 104 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 109 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 110 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 111 | assertEquals(IntPxSize(20.dp.toIntPx(), 40.dp.toIntPx()), minIntrinsicHeightSize.value) |
| 112 | assertEquals(IntPxSize(20.dp.toIntPx(), 40.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 113 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 114 | } |
| 115 | |
| 116 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 117 | fun testMaxIntrinsicWidth() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 118 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 119 | val maxIntrinsicWidthSize = Ref<IntPxSize>() |
| 120 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 121 | val childPosition = Ref<PxPosition>() |
| 122 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 123 | Align( |
| 124 | alignment = Alignment.TopStart, |
| 125 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 126 | maxIntrinsicWidthSize.value = coordinates.size |
| 127 | positionedLatch.countDown() |
| 128 | } |
| 129 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 130 | MaxIntrinsicWidth { |
| 131 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 132 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 133 | size = childSize, |
| 134 | position = childPosition, |
| 135 | positionedLatch = positionedLatch |
| 136 | ) |
| 137 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 142 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 143 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 144 | assertEquals(IntPxSize(30.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value) |
| 145 | assertEquals(IntPxSize(30.dp.toIntPx(), 50.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 146 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 147 | } |
| 148 | |
| 149 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 150 | fun testMaxIntrinsicHeight() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 151 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 152 | val maxIntrinsicHeightSize = Ref<IntPxSize>() |
| 153 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 154 | val childPosition = Ref<PxPosition>() |
| 155 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 156 | Align( |
| 157 | alignment = Alignment.TopStart, |
| 158 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 159 | maxIntrinsicHeightSize.value = coordinates.size |
| 160 | positionedLatch.countDown() |
| 161 | } |
| 162 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 163 | MaxIntrinsicHeight { |
| 164 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 165 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 166 | size = childSize, |
| 167 | position = childPosition, |
| 168 | positionedLatch = positionedLatch |
| 169 | ) |
| 170 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 175 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 176 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 177 | assertEquals(IntPxSize(20.dp.toIntPx(), 60.dp.toIntPx()), maxIntrinsicHeightSize.value) |
| 178 | assertEquals(IntPxSize(20.dp.toIntPx(), 60.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 179 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 180 | } |
| 181 | |
| 182 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 183 | fun testMinIntrinsicWidth_respectsIncomingMaxConstraints() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 184 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 185 | val minIntrinsicWidthSize = Ref<IntPxSize>() |
| 186 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 187 | val childPosition = Ref<PxPosition>() |
| 188 | show { |
Anastasia Soboleva | e5a9777 | 2020-02-18 16:05:48 +0000 | [diff] [blame] | 189 | Align(alignment = Alignment.TopStart) { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 190 | ConstrainedBox( |
| 191 | DpConstraints(maxWidth = 5.dp), |
| 192 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 193 | minIntrinsicWidthSize.value = coordinates.size |
| 194 | positionedLatch.countDown() |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 195 | } |
| 196 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 197 | MinIntrinsicWidth { |
| 198 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 199 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 200 | size = childSize, |
| 201 | position = childPosition, |
| 202 | positionedLatch = positionedLatch |
| 203 | ) |
| 204 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 210 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 211 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 212 | assertEquals(IntPxSize(5.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value) |
| 213 | assertEquals(IntPxSize(5.dp.toIntPx(), 50.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 214 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 215 | } |
| 216 | |
| 217 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 218 | fun testMinIntrinsicWidth_respectsIncomingMinConstraints() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 219 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 220 | val minIntrinsicWidthSize = Ref<IntPxSize>() |
| 221 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 222 | val childPosition = Ref<PxPosition>() |
| 223 | show { |
Anastasia Soboleva | e5a9777 | 2020-02-18 16:05:48 +0000 | [diff] [blame] | 224 | Align(alignment = Alignment.TopStart) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 225 | ConstrainedBox( |
| 226 | DpConstraints(minWidth = 15.dp), |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 227 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 228 | minIntrinsicWidthSize.value = coordinates.size |
| 229 | positionedLatch.countDown() |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 230 | } |
| 231 | ) { |
| 232 | MinIntrinsicWidth { |
| 233 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 234 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 235 | size = childSize, |
| 236 | position = childPosition, |
| 237 | positionedLatch = positionedLatch |
| 238 | ) |
| 239 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 245 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 246 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 247 | assertEquals(IntPxSize(15.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value) |
| 248 | assertEquals(IntPxSize(15.dp.toIntPx(), 50.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 249 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 250 | } |
| 251 | |
| 252 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 253 | fun testMinIntrinsicHeight_respectsMaxIncomingConstraints() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 254 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 255 | val minIntrinsicHeightSize = Ref<IntPxSize>() |
| 256 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 257 | val childPosition = Ref<PxPosition>() |
| 258 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 259 | Align( |
| 260 | alignment = Alignment.TopStart, |
| 261 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 262 | minIntrinsicHeightSize.value = coordinates.size |
| 263 | positionedLatch.countDown() |
| 264 | } |
| 265 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 266 | ConstrainedBox(DpConstraints(maxHeight = 35.dp)) { |
| 267 | MinIntrinsicHeight { |
| 268 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 269 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 270 | size = childSize, |
| 271 | position = childPosition, |
| 272 | positionedLatch = positionedLatch |
| 273 | ) |
| 274 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 280 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 281 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 282 | assertEquals(IntPxSize(20.dp.toIntPx(), 35.dp.toIntPx()), minIntrinsicHeightSize.value) |
| 283 | assertEquals(IntPxSize(20.dp.toIntPx(), 35.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 284 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 285 | } |
| 286 | |
| 287 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 288 | fun testMinIntrinsicHeight_respectsMinIncomingConstraints() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 289 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 290 | val minIntrinsicHeightSize = Ref<IntPxSize>() |
| 291 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 292 | val childPosition = Ref<PxPosition>() |
| 293 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 294 | Align( |
| 295 | alignment = Alignment.TopStart, |
| 296 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 297 | minIntrinsicHeightSize.value = coordinates.size |
| 298 | positionedLatch.countDown() |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 299 | } |
| 300 | ) { |
| 301 | ConstrainedBox(DpConstraints(minHeight = 45.dp)) { |
| 302 | MinIntrinsicHeight { |
| 303 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 304 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 305 | size = childSize, |
| 306 | position = childPosition, |
| 307 | positionedLatch = positionedLatch |
| 308 | ) |
| 309 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 315 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 316 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 317 | assertEquals(IntPxSize(20.dp.toIntPx(), 45.dp.toIntPx()), minIntrinsicHeightSize.value) |
| 318 | assertEquals(IntPxSize(20.dp.toIntPx(), 45.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 319 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 320 | } |
| 321 | |
| 322 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 323 | fun testMaxIntrinsicWidth_respectsMaxIncomingConstraints() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 324 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 325 | val maxIntrinsicWidthSize = Ref<IntPxSize>() |
| 326 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 327 | val childPosition = Ref<PxPosition>() |
| 328 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 329 | Align( |
| 330 | alignment = Alignment.TopStart, |
| 331 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 332 | maxIntrinsicWidthSize.value = coordinates.size |
| 333 | positionedLatch.countDown() |
| 334 | } |
| 335 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 336 | ConstrainedBox(DpConstraints(maxWidth = 25.dp)) { |
| 337 | MaxIntrinsicWidth { |
| 338 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 339 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 340 | size = childSize, |
| 341 | position = childPosition, |
| 342 | positionedLatch = positionedLatch |
| 343 | ) |
| 344 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 350 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 351 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 352 | assertEquals(IntPxSize(25.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value) |
| 353 | assertEquals(IntPxSize(25.dp.toIntPx(), 50.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 354 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 355 | } |
| 356 | |
| 357 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 358 | fun testMaxIntrinsicWidth_respectsMinIncomingConstraints() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 359 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 360 | val maxIntrinsicWidthSize = Ref<IntPxSize>() |
| 361 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 362 | val childPosition = Ref<PxPosition>() |
| 363 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 364 | Align( |
| 365 | alignment = Alignment.TopStart, |
| 366 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 367 | maxIntrinsicWidthSize.value = coordinates.size |
| 368 | positionedLatch.countDown() |
| 369 | } |
| 370 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 371 | ConstrainedBox(DpConstraints(minWidth = 35.dp)) { |
| 372 | MaxIntrinsicWidth { |
| 373 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 374 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 375 | size = childSize, |
| 376 | position = childPosition, |
| 377 | positionedLatch = positionedLatch |
| 378 | ) |
| 379 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 385 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 386 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 387 | assertEquals(IntPxSize(35.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value) |
| 388 | assertEquals(IntPxSize(35.dp.toIntPx(), 50.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 389 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 390 | } |
| 391 | |
| 392 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 393 | fun testMaxIntrinsicHeight_respectsMaxIncomingConstraints() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 394 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 395 | val maxIntrinsicHeightSize = Ref<IntPxSize>() |
| 396 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 397 | val childPosition = Ref<PxPosition>() |
| 398 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 399 | Align( |
| 400 | alignment = Alignment.TopStart, |
| 401 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 402 | maxIntrinsicHeightSize.value = coordinates.size |
| 403 | positionedLatch.countDown() |
| 404 | } |
| 405 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 406 | ConstrainedBox(DpConstraints(maxHeight = 55.dp)) { |
| 407 | MaxIntrinsicHeight { |
| 408 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 409 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 410 | size = childSize, |
| 411 | position = childPosition, |
| 412 | positionedLatch = positionedLatch |
| 413 | ) |
| 414 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 420 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 421 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 422 | assertEquals(IntPxSize(20.dp.toIntPx(), 55.dp.toIntPx()), maxIntrinsicHeightSize.value) |
| 423 | assertEquals(IntPxSize(20.dp.toIntPx(), 55.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 424 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 425 | } |
| 426 | |
| 427 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 428 | fun testMaxIntrinsicHeight_respectsMinIncomingConstraints() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 429 | val positionedLatch = CountDownLatch(2) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 430 | val maxIntrinsicHeightSize = Ref<IntPxSize>() |
| 431 | val childSize = Ref<IntPxSize>() |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 432 | val childPosition = Ref<PxPosition>() |
| 433 | show { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 434 | Align( |
| 435 | alignment = Alignment.TopStart, |
| 436 | modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates -> |
| 437 | maxIntrinsicHeightSize.value = coordinates.size |
| 438 | positionedLatch.countDown() |
| 439 | } |
| 440 | ) { |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 441 | ConstrainedBox(DpConstraints(minHeight = 65.dp)) { |
| 442 | MaxIntrinsicHeight { |
| 443 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame^] | 444 | Modifier.saveLayoutInfo( |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 445 | size = childSize, |
| 446 | position = childPosition, |
| 447 | positionedLatch = positionedLatch |
| 448 | ) |
| 449 | ) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 455 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 456 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 457 | assertEquals(IntPxSize(20.dp.toIntPx(), 65.dp.toIntPx()), maxIntrinsicHeightSize.value) |
| 458 | assertEquals(IntPxSize(20.dp.toIntPx(), 65.dp.toIntPx()), childSize.value) |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 459 | assertEquals(PxPosition(0.px, 0.px), childPosition.value) |
| 460 | } |
| 461 | |
| 462 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 463 | fun testMinIntrinsicWidth_intrinsicMeasurements() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 464 | testIntrinsics(@Composable { |
| 465 | MinIntrinsicWidth { |
| 466 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp) { } |
| 467 | } |
| 468 | }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight -> |
| 469 | assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0.ipx)) |
| 470 | assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0.ipx)) |
| 471 | assertEquals(10.dp.toIntPx(), maxIntrinsicWidth(0.ipx)) |
| 472 | assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0.ipx)) |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 477 | fun testMinIntrinsicHeight_intrinsicMeasurements() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 478 | testIntrinsics(@Composable { |
| 479 | MinIntrinsicHeight { |
| 480 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp) { } |
| 481 | } |
| 482 | }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight -> |
| 483 | assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0.ipx)) |
| 484 | assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0.ipx)) |
| 485 | assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0.ipx)) |
| 486 | assertEquals(40.dp.toIntPx(), maxIntrinsicHeight(0.ipx)) |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 491 | fun testMaxIntrinsicWidth_intrinsicMeasurements() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 492 | testIntrinsics(@Composable { |
| 493 | MaxIntrinsicWidth { |
| 494 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp) { } |
| 495 | } |
| 496 | }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight -> |
| 497 | assertEquals(30.dp.toIntPx(), minIntrinsicWidth(0.ipx)) |
| 498 | assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0.ipx)) |
| 499 | assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0.ipx)) |
| 500 | assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0.ipx)) |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 505 | fun testMaxIntrinsicHeight_intrinsicMeasurements() = with(density) { |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 506 | testIntrinsics(@Composable { |
| 507 | MaxIntrinsicHeight { |
| 508 | FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp) { } |
| 509 | } |
| 510 | }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight -> |
| 511 | assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0.ipx)) |
| 512 | assertEquals(60.dp.toIntPx(), minIntrinsicHeight(0.ipx)) |
| 513 | assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0.ipx)) |
| 514 | assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0.ipx)) |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | @Composable |
| 520 | private fun FixedIntrinsicsBox( |
| 521 | minIntrinsicWidth: Dp, |
| 522 | width: Dp, |
| 523 | maxIntrinsicWidth: Dp, |
| 524 | minIntrinsicHeight: Dp, |
George Mount | ec81f52 | 2019-08-14 15:30:26 -0700 | [diff] [blame] | 525 | height: Dp, |
| 526 | maxIntrinsicHeight: Dp, |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 527 | modifier: Modifier = Modifier.None, |
Jim | 1fbb1c1 | 2019-07-31 12:43:29 -0700 | [diff] [blame] | 528 | children: @Composable() () -> Unit |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 529 | ) { |
Mihai Popa | ca1c4b9 | 2019-10-03 19:16:06 +0100 | [diff] [blame] | 530 | Layout( |
| 531 | children, |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 532 | minIntrinsicWidthMeasureBlock = { _, _, _ -> minIntrinsicWidth.toIntPx() }, |
| 533 | minIntrinsicHeightMeasureBlock = { _, _, _ -> minIntrinsicHeight.toIntPx() }, |
| 534 | maxIntrinsicWidthMeasureBlock = { _, _, _ -> maxIntrinsicWidth.toIntPx() }, |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 535 | maxIntrinsicHeightMeasureBlock = { _, _, _ -> maxIntrinsicHeight.toIntPx() }, |
| 536 | modifier = modifier |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 537 | ) { _, constraints, _ -> |
Mihai Popa | ca1c4b9 | 2019-10-03 19:16:06 +0100 | [diff] [blame] | 538 | layout( |
| 539 | width.toIntPx().coerceIn(constraints.minWidth, constraints.maxWidth), |
| 540 | height.toIntPx().coerceIn(constraints.minHeight, constraints.maxHeight) |
| 541 | ) {} |
Mihai Popa | 1257df2 | 2019-07-01 21:30:22 +0100 | [diff] [blame] | 542 | } |
| 543 | } |