[go: nahoru, domu]

blob: bcb828b7315bd1c77c955253018daaf083bded2b [file] [log] [blame]
Mihai Popa1257df22019-07-01 21:30:22 +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.layout.test
18
Mihai Popa1257df22019-07-01 21:30:22 +010019import androidx.compose.Composable
Mihai Popa1257df22019-07-01 21:30:22 +010020import androidx.test.filters.SmallTest
Qingqing Deng8c5d0382019-09-16 11:59:16 -070021import androidx.ui.core.Alignment
Mihai Popaca1c4b92019-10-03 19:16:06 +010022import androidx.ui.core.Layout
Adam Powell999a89b2020-03-11 09:08:07 -070023import androidx.ui.core.LayoutCoordinates
George Mount5e1d3f12020-03-09 17:28:35 -070024import androidx.ui.core.Modifier
Mihai Popa1257df22019-07-01 21:30:22 +010025import androidx.ui.core.Ref
George Mount5e1d3f12020-03-09 17:28:35 -070026import androidx.ui.core.onChildPositioned
Mihai Popa1257df22019-07-01 21:30:22 +010027import androidx.ui.layout.Align
Mihai Popa1257df22019-07-01 21:30:22 +010028import androidx.ui.layout.DpConstraints
29import androidx.ui.layout.MaxIntrinsicHeight
30import androidx.ui.layout.MaxIntrinsicWidth
31import androidx.ui.layout.MinIntrinsicHeight
32import androidx.ui.layout.MinIntrinsicWidth
George Mount842c8c12020-01-08 16:03:42 -080033import androidx.ui.unit.Dp
George Mount8baef7a2020-01-21 13:40:57 -080034import androidx.ui.unit.IntPxSize
George Mount842c8c12020-01-08 16:03:42 -080035import androidx.ui.unit.PxPosition
George Mount842c8c12020-01-08 16:03:42 -080036import androidx.ui.unit.dp
37import androidx.ui.unit.ipx
38import androidx.ui.unit.px
George Mount8baef7a2020-01-21 13:40:57 -080039import org.junit.Assert.assertEquals
Mihai Popa2554ba82020-03-13 19:03:52 +000040import org.junit.Assert.assertTrue
Mihai Popa1257df22019-07-01 21:30:22 +010041import org.junit.Test
42import org.junit.runner.RunWith
43import org.junit.runners.JUnit4
44import java.util.concurrent.CountDownLatch
45import java.util.concurrent.TimeUnit
46
47@SmallTest
48@RunWith(JUnit4::class)
49class IntrinsicTest : LayoutTest() {
50 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000051 fun testMinIntrinsicWidth() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +010052 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -080053 val minIntrinsicWidthSize = Ref<IntPxSize>()
54 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +010055 val childPosition = Ref<PxPosition>()
56 show {
Adam Powell999a89b2020-03-11 09:08:07 -070057 Align(
58 alignment = Alignment.TopStart,
59 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
60 minIntrinsicWidthSize.value = coordinates.size
61 positionedLatch.countDown()
62 }
63 ) {
George Mount5e1d3f12020-03-09 17:28:35 -070064 MinIntrinsicWidth {
65 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -070066 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -070067 size = childSize,
68 position = childPosition,
69 positionedLatch = positionedLatch
70 )
71 ) {
Mihai Popa1257df22019-07-01 21:30:22 +010072 }
73 }
74 }
75 }
Mihai Popa2554ba82020-03-13 19:03:52 +000076 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +010077
George Mount8baef7a2020-01-21 13:40:57 -080078 assertEquals(IntPxSize(10.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
79 assertEquals(IntPxSize(10.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +010080 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
81 }
82
83 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000084 fun testMinIntrinsicHeight() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +010085 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -080086 val minIntrinsicHeightSize = Ref<IntPxSize>()
87 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +010088 val childPosition = Ref<PxPosition>()
89 show {
Adam Powell999a89b2020-03-11 09:08:07 -070090 Align(
91 alignment = Alignment.TopStart,
92 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
93 minIntrinsicHeightSize.value = coordinates.size
94 positionedLatch.countDown()
95 }
96 ) {
George Mount5e1d3f12020-03-09 17:28:35 -070097 MinIntrinsicHeight {
98 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -070099 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700100 size = childSize,
101 position = childPosition,
102 positionedLatch = positionedLatch
103 )
104 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100105 }
106 }
107 }
108 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000109 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100110
George Mount8baef7a2020-01-21 13:40:57 -0800111 assertEquals(IntPxSize(20.dp.toIntPx(), 40.dp.toIntPx()), minIntrinsicHeightSize.value)
112 assertEquals(IntPxSize(20.dp.toIntPx(), 40.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100113 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
114 }
115
116 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000117 fun testMaxIntrinsicWidth() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100118 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800119 val maxIntrinsicWidthSize = Ref<IntPxSize>()
120 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100121 val childPosition = Ref<PxPosition>()
122 show {
Adam Powell999a89b2020-03-11 09:08:07 -0700123 Align(
124 alignment = Alignment.TopStart,
125 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
126 maxIntrinsicWidthSize.value = coordinates.size
127 positionedLatch.countDown()
128 }
129 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700130 MaxIntrinsicWidth {
131 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700132 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700133 size = childSize,
134 position = childPosition,
135 positionedLatch = positionedLatch
136 )
137 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100138 }
139 }
140 }
141 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000142 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100143
George Mount8baef7a2020-01-21 13:40:57 -0800144 assertEquals(IntPxSize(30.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
145 assertEquals(IntPxSize(30.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100146 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
147 }
148
149 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000150 fun testMaxIntrinsicHeight() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100151 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800152 val maxIntrinsicHeightSize = Ref<IntPxSize>()
153 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100154 val childPosition = Ref<PxPosition>()
155 show {
Adam Powell999a89b2020-03-11 09:08:07 -0700156 Align(
157 alignment = Alignment.TopStart,
158 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
159 maxIntrinsicHeightSize.value = coordinates.size
160 positionedLatch.countDown()
161 }
162 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700163 MaxIntrinsicHeight {
164 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700165 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700166 size = childSize,
167 position = childPosition,
168 positionedLatch = positionedLatch
169 )
170 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100171 }
172 }
173 }
174 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000175 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100176
George Mount8baef7a2020-01-21 13:40:57 -0800177 assertEquals(IntPxSize(20.dp.toIntPx(), 60.dp.toIntPx()), maxIntrinsicHeightSize.value)
178 assertEquals(IntPxSize(20.dp.toIntPx(), 60.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100179 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
180 }
181
182 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000183 fun testMinIntrinsicWidth_respectsIncomingMaxConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100184 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800185 val minIntrinsicWidthSize = Ref<IntPxSize>()
186 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100187 val childPosition = Ref<PxPosition>()
188 show {
Anastasia Sobolevae5a97772020-02-18 16:05:48 +0000189 Align(alignment = Alignment.TopStart) {
Adam Powell999a89b2020-03-11 09:08:07 -0700190 ConstrainedBox(
191 DpConstraints(maxWidth = 5.dp),
192 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100193 minIntrinsicWidthSize.value = coordinates.size
194 positionedLatch.countDown()
Adam Powell999a89b2020-03-11 09:08:07 -0700195 }
196 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700197 MinIntrinsicWidth {
198 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700199 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700200 size = childSize,
201 position = childPosition,
202 positionedLatch = positionedLatch
203 )
204 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100205 }
206 }
207 }
208 }
209 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000210 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100211
George Mount8baef7a2020-01-21 13:40:57 -0800212 assertEquals(IntPxSize(5.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
213 assertEquals(IntPxSize(5.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100214 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
215 }
216
217 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000218 fun testMinIntrinsicWidth_respectsIncomingMinConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100219 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800220 val minIntrinsicWidthSize = Ref<IntPxSize>()
221 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100222 val childPosition = Ref<PxPosition>()
223 show {
Anastasia Sobolevae5a97772020-02-18 16:05:48 +0000224 Align(alignment = Alignment.TopStart) {
George Mount5e1d3f12020-03-09 17:28:35 -0700225 ConstrainedBox(
226 DpConstraints(minWidth = 15.dp),
Adam Powell999a89b2020-03-11 09:08:07 -0700227 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100228 minIntrinsicWidthSize.value = coordinates.size
229 positionedLatch.countDown()
George Mount5e1d3f12020-03-09 17:28:35 -0700230 }
231 ) {
232 MinIntrinsicWidth {
233 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700234 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700235 size = childSize,
236 position = childPosition,
237 positionedLatch = positionedLatch
238 )
239 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100240 }
241 }
242 }
243 }
244 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000245 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100246
George Mount8baef7a2020-01-21 13:40:57 -0800247 assertEquals(IntPxSize(15.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
248 assertEquals(IntPxSize(15.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100249 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
250 }
251
252 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000253 fun testMinIntrinsicHeight_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100254 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800255 val minIntrinsicHeightSize = Ref<IntPxSize>()
256 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100257 val childPosition = Ref<PxPosition>()
258 show {
Adam Powell999a89b2020-03-11 09:08:07 -0700259 Align(
260 alignment = Alignment.TopStart,
261 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
262 minIntrinsicHeightSize.value = coordinates.size
263 positionedLatch.countDown()
264 }
265 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700266 ConstrainedBox(DpConstraints(maxHeight = 35.dp)) {
267 MinIntrinsicHeight {
268 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700269 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700270 size = childSize,
271 position = childPosition,
272 positionedLatch = positionedLatch
273 )
274 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100275 }
276 }
277 }
278 }
279 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000280 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100281
George Mount8baef7a2020-01-21 13:40:57 -0800282 assertEquals(IntPxSize(20.dp.toIntPx(), 35.dp.toIntPx()), minIntrinsicHeightSize.value)
283 assertEquals(IntPxSize(20.dp.toIntPx(), 35.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100284 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
285 }
286
287 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000288 fun testMinIntrinsicHeight_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100289 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800290 val minIntrinsicHeightSize = Ref<IntPxSize>()
291 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100292 val childPosition = Ref<PxPosition>()
293 show {
Adam Powell999a89b2020-03-11 09:08:07 -0700294 Align(
295 alignment = Alignment.TopStart,
296 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100297 minIntrinsicHeightSize.value = coordinates.size
298 positionedLatch.countDown()
George Mount5e1d3f12020-03-09 17:28:35 -0700299 }
300 ) {
301 ConstrainedBox(DpConstraints(minHeight = 45.dp)) {
302 MinIntrinsicHeight {
303 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700304 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700305 size = childSize,
306 position = childPosition,
307 positionedLatch = positionedLatch
308 )
309 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100310 }
311 }
312 }
313 }
314 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000315 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100316
George Mount8baef7a2020-01-21 13:40:57 -0800317 assertEquals(IntPxSize(20.dp.toIntPx(), 45.dp.toIntPx()), minIntrinsicHeightSize.value)
318 assertEquals(IntPxSize(20.dp.toIntPx(), 45.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100319 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
320 }
321
322 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000323 fun testMaxIntrinsicWidth_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100324 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800325 val maxIntrinsicWidthSize = Ref<IntPxSize>()
326 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100327 val childPosition = Ref<PxPosition>()
328 show {
Adam Powell999a89b2020-03-11 09:08:07 -0700329 Align(
330 alignment = Alignment.TopStart,
331 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
332 maxIntrinsicWidthSize.value = coordinates.size
333 positionedLatch.countDown()
334 }
335 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700336 ConstrainedBox(DpConstraints(maxWidth = 25.dp)) {
337 MaxIntrinsicWidth {
338 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700339 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700340 size = childSize,
341 position = childPosition,
342 positionedLatch = positionedLatch
343 )
344 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100345 }
346 }
347 }
348 }
349 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000350 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100351
George Mount8baef7a2020-01-21 13:40:57 -0800352 assertEquals(IntPxSize(25.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
353 assertEquals(IntPxSize(25.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100354 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
355 }
356
357 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000358 fun testMaxIntrinsicWidth_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100359 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800360 val maxIntrinsicWidthSize = Ref<IntPxSize>()
361 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100362 val childPosition = Ref<PxPosition>()
363 show {
Adam Powell999a89b2020-03-11 09:08:07 -0700364 Align(
365 alignment = Alignment.TopStart,
366 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
367 maxIntrinsicWidthSize.value = coordinates.size
368 positionedLatch.countDown()
369 }
370 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700371 ConstrainedBox(DpConstraints(minWidth = 35.dp)) {
372 MaxIntrinsicWidth {
373 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700374 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700375 size = childSize,
376 position = childPosition,
377 positionedLatch = positionedLatch
378 )
379 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100380 }
381 }
382 }
383 }
384 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000385 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100386
George Mount8baef7a2020-01-21 13:40:57 -0800387 assertEquals(IntPxSize(35.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
388 assertEquals(IntPxSize(35.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100389 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
390 }
391
392 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000393 fun testMaxIntrinsicHeight_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100394 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800395 val maxIntrinsicHeightSize = Ref<IntPxSize>()
396 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100397 val childPosition = Ref<PxPosition>()
398 show {
Adam Powell999a89b2020-03-11 09:08:07 -0700399 Align(
400 alignment = Alignment.TopStart,
401 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
402 maxIntrinsicHeightSize.value = coordinates.size
403 positionedLatch.countDown()
404 }
405 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700406 ConstrainedBox(DpConstraints(maxHeight = 55.dp)) {
407 MaxIntrinsicHeight {
408 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700409 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700410 size = childSize,
411 position = childPosition,
412 positionedLatch = positionedLatch
413 )
414 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100415 }
416 }
417 }
418 }
419 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000420 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100421
George Mount8baef7a2020-01-21 13:40:57 -0800422 assertEquals(IntPxSize(20.dp.toIntPx(), 55.dp.toIntPx()), maxIntrinsicHeightSize.value)
423 assertEquals(IntPxSize(20.dp.toIntPx(), 55.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100424 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
425 }
426
427 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000428 fun testMaxIntrinsicHeight_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100429 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800430 val maxIntrinsicHeightSize = Ref<IntPxSize>()
431 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100432 val childPosition = Ref<PxPosition>()
433 show {
Adam Powell999a89b2020-03-11 09:08:07 -0700434 Align(
435 alignment = Alignment.TopStart,
436 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
437 maxIntrinsicHeightSize.value = coordinates.size
438 positionedLatch.countDown()
439 }
440 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700441 ConstrainedBox(DpConstraints(minHeight = 65.dp)) {
442 MaxIntrinsicHeight {
443 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700444 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700445 size = childSize,
446 position = childPosition,
447 positionedLatch = positionedLatch
448 )
449 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100450 }
451 }
452 }
453 }
454 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000455 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100456
George Mount8baef7a2020-01-21 13:40:57 -0800457 assertEquals(IntPxSize(20.dp.toIntPx(), 65.dp.toIntPx()), maxIntrinsicHeightSize.value)
458 assertEquals(IntPxSize(20.dp.toIntPx(), 65.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100459 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
460 }
461
462 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000463 fun testMinIntrinsicWidth_intrinsicMeasurements() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100464 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 Kulikov47f0e1a2020-01-28 13:52:46 +0000477 fun testMinIntrinsicHeight_intrinsicMeasurements() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100478 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 Kulikov47f0e1a2020-01-28 13:52:46 +0000491 fun testMaxIntrinsicWidth_intrinsicMeasurements() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100492 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 Kulikov47f0e1a2020-01-28 13:52:46 +0000505 fun testMaxIntrinsicHeight_intrinsicMeasurements() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100506 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
520private fun FixedIntrinsicsBox(
521 minIntrinsicWidth: Dp,
522 width: Dp,
523 maxIntrinsicWidth: Dp,
524 minIntrinsicHeight: Dp,
George Mountec81f522019-08-14 15:30:26 -0700525 height: Dp,
526 maxIntrinsicHeight: Dp,
George Mount5e1d3f12020-03-09 17:28:35 -0700527 modifier: Modifier = Modifier.None,
Jim1fbb1c12019-07-31 12:43:29 -0700528 children: @Composable() () -> Unit
Mihai Popa1257df22019-07-01 21:30:22 +0100529) {
Mihai Popaca1c4b92019-10-03 19:16:06 +0100530 Layout(
531 children,
Anastasia Soboleva9474ff82020-02-19 19:02:15 +0000532 minIntrinsicWidthMeasureBlock = { _, _, _ -> minIntrinsicWidth.toIntPx() },
533 minIntrinsicHeightMeasureBlock = { _, _, _ -> minIntrinsicHeight.toIntPx() },
534 maxIntrinsicWidthMeasureBlock = { _, _, _ -> maxIntrinsicWidth.toIntPx() },
George Mount5e1d3f12020-03-09 17:28:35 -0700535 maxIntrinsicHeightMeasureBlock = { _, _, _ -> maxIntrinsicHeight.toIntPx() },
536 modifier = modifier
Anastasia Soboleva9474ff82020-02-19 19:02:15 +0000537 ) { _, constraints, _ ->
Mihai Popaca1c4b92019-10-03 19:16:06 +0100538 layout(
539 width.toIntPx().coerceIn(constraints.minWidth, constraints.maxWidth),
540 height.toIntPx().coerceIn(constraints.minHeight, constraints.maxHeight)
541 ) {}
Mihai Popa1257df22019-07-01 21:30:22 +0100542 }
543}