[go: nahoru, domu]

blob: 3d67011a3c745d5e7e62029b6557e8889c3299e4 [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
Mihai Popaca1c4b92019-10-03 19:16:06 +010021import androidx.ui.core.Layout
Adam Powell999a89b2020-03-11 09:08:07 -070022import androidx.ui.core.LayoutCoordinates
George Mount5e1d3f12020-03-09 17:28:35 -070023import androidx.ui.core.Modifier
Mihai Popa1257df22019-07-01 21:30:22 +010024import androidx.ui.core.Ref
George Mount5e1d3f12020-03-09 17:28:35 -070025import androidx.ui.core.onChildPositioned
Mihai Popa1257df22019-07-01 21:30:22 +010026import androidx.ui.layout.DpConstraints
27import androidx.ui.layout.MaxIntrinsicHeight
28import androidx.ui.layout.MaxIntrinsicWidth
29import androidx.ui.layout.MinIntrinsicHeight
30import androidx.ui.layout.MinIntrinsicWidth
Anastasia Sobolevadb738582020-03-27 16:06:33 +000031import androidx.ui.layout.Stack
George Mount842c8c12020-01-08 16:03:42 -080032import androidx.ui.unit.Dp
George Mount8baef7a2020-01-21 13:40:57 -080033import androidx.ui.unit.IntPxSize
George Mount842c8c12020-01-08 16:03:42 -080034import androidx.ui.unit.PxPosition
George Mount842c8c12020-01-08 16:03:42 -080035import androidx.ui.unit.dp
36import androidx.ui.unit.ipx
37import androidx.ui.unit.px
George Mount8baef7a2020-01-21 13:40:57 -080038import org.junit.Assert.assertEquals
Mihai Popa2554ba82020-03-13 19:03:52 +000039import org.junit.Assert.assertTrue
Mihai Popa1257df22019-07-01 21:30:22 +010040import org.junit.Test
41import org.junit.runner.RunWith
42import org.junit.runners.JUnit4
43import java.util.concurrent.CountDownLatch
44import java.util.concurrent.TimeUnit
45
46@SmallTest
47@RunWith(JUnit4::class)
48class IntrinsicTest : LayoutTest() {
49 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000050 fun testMinIntrinsicWidth() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +010051 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -080052 val minIntrinsicWidthSize = Ref<IntPxSize>()
53 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +010054 val childPosition = Ref<PxPosition>()
55 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +000056 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -070057 minIntrinsicWidthSize.value = coordinates.size
58 positionedLatch.countDown()
59 }
60 ) {
George Mount5e1d3f12020-03-09 17:28:35 -070061 MinIntrinsicWidth {
62 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -070063 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -070064 size = childSize,
65 position = childPosition,
66 positionedLatch = positionedLatch
67 )
68 ) {
Mihai Popa1257df22019-07-01 21:30:22 +010069 }
70 }
71 }
72 }
Mihai Popa2554ba82020-03-13 19:03:52 +000073 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +010074
George Mount8baef7a2020-01-21 13:40:57 -080075 assertEquals(IntPxSize(10.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
76 assertEquals(IntPxSize(10.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +010077 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
78 }
79
80 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000081 fun testMinIntrinsicHeight() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +010082 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -080083 val minIntrinsicHeightSize = Ref<IntPxSize>()
84 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +010085 val childPosition = Ref<PxPosition>()
86 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +000087 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -070088 minIntrinsicHeightSize.value = coordinates.size
89 positionedLatch.countDown()
90 }
91 ) {
George Mount5e1d3f12020-03-09 17:28:35 -070092 MinIntrinsicHeight {
93 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -070094 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -070095 size = childSize,
96 position = childPosition,
97 positionedLatch = positionedLatch
98 )
99 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100100 }
101 }
102 }
103 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000104 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100105
George Mount8baef7a2020-01-21 13:40:57 -0800106 assertEquals(IntPxSize(20.dp.toIntPx(), 40.dp.toIntPx()), minIntrinsicHeightSize.value)
107 assertEquals(IntPxSize(20.dp.toIntPx(), 40.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100108 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
109 }
110
111 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000112 fun testMaxIntrinsicWidth() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100113 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800114 val maxIntrinsicWidthSize = Ref<IntPxSize>()
115 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100116 val childPosition = Ref<PxPosition>()
117 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000118 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700119 maxIntrinsicWidthSize.value = coordinates.size
120 positionedLatch.countDown()
121 }
122 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700123 MaxIntrinsicWidth {
124 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700125 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700126 size = childSize,
127 position = childPosition,
128 positionedLatch = positionedLatch
129 )
130 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100131 }
132 }
133 }
134 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000135 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100136
George Mount8baef7a2020-01-21 13:40:57 -0800137 assertEquals(IntPxSize(30.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
138 assertEquals(IntPxSize(30.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100139 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
140 }
141
142 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000143 fun testMaxIntrinsicHeight() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100144 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800145 val maxIntrinsicHeightSize = Ref<IntPxSize>()
146 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100147 val childPosition = Ref<PxPosition>()
148 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000149 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700150 maxIntrinsicHeightSize.value = coordinates.size
151 positionedLatch.countDown()
152 }
153 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700154 MaxIntrinsicHeight {
155 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700156 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700157 size = childSize,
158 position = childPosition,
159 positionedLatch = positionedLatch
160 )
161 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100162 }
163 }
164 }
165 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000166 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100167
George Mount8baef7a2020-01-21 13:40:57 -0800168 assertEquals(IntPxSize(20.dp.toIntPx(), 60.dp.toIntPx()), maxIntrinsicHeightSize.value)
169 assertEquals(IntPxSize(20.dp.toIntPx(), 60.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100170 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
171 }
172
173 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000174 fun testMinIntrinsicWidth_respectsIncomingMaxConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100175 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800176 val minIntrinsicWidthSize = Ref<IntPxSize>()
177 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100178 val childPosition = Ref<PxPosition>()
179 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000180 Stack {
Adam Powell999a89b2020-03-11 09:08:07 -0700181 ConstrainedBox(
182 DpConstraints(maxWidth = 5.dp),
183 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100184 minIntrinsicWidthSize.value = coordinates.size
185 positionedLatch.countDown()
Adam Powell999a89b2020-03-11 09:08:07 -0700186 }
187 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700188 MinIntrinsicWidth {
189 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700190 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700191 size = childSize,
192 position = childPosition,
193 positionedLatch = positionedLatch
194 )
195 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100196 }
197 }
198 }
199 }
200 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000201 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100202
George Mount8baef7a2020-01-21 13:40:57 -0800203 assertEquals(IntPxSize(5.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
204 assertEquals(IntPxSize(5.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100205 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
206 }
207
208 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000209 fun testMinIntrinsicWidth_respectsIncomingMinConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100210 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800211 val minIntrinsicWidthSize = Ref<IntPxSize>()
212 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100213 val childPosition = Ref<PxPosition>()
214 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000215 Stack {
George Mount5e1d3f12020-03-09 17:28:35 -0700216 ConstrainedBox(
217 DpConstraints(minWidth = 15.dp),
Adam Powell999a89b2020-03-11 09:08:07 -0700218 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100219 minIntrinsicWidthSize.value = coordinates.size
220 positionedLatch.countDown()
George Mount5e1d3f12020-03-09 17:28:35 -0700221 }
222 ) {
223 MinIntrinsicWidth {
224 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700225 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700226 size = childSize,
227 position = childPosition,
228 positionedLatch = positionedLatch
229 )
230 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100231 }
232 }
233 }
234 }
235 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000236 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100237
George Mount8baef7a2020-01-21 13:40:57 -0800238 assertEquals(IntPxSize(15.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
239 assertEquals(IntPxSize(15.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100240 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
241 }
242
243 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000244 fun testMinIntrinsicHeight_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100245 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800246 val minIntrinsicHeightSize = Ref<IntPxSize>()
247 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100248 val childPosition = Ref<PxPosition>()
249 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000250 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700251 minIntrinsicHeightSize.value = coordinates.size
252 positionedLatch.countDown()
253 }
254 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700255 ConstrainedBox(DpConstraints(maxHeight = 35.dp)) {
256 MinIntrinsicHeight {
257 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700258 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700259 size = childSize,
260 position = childPosition,
261 positionedLatch = positionedLatch
262 )
263 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100264 }
265 }
266 }
267 }
268 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000269 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100270
George Mount8baef7a2020-01-21 13:40:57 -0800271 assertEquals(IntPxSize(20.dp.toIntPx(), 35.dp.toIntPx()), minIntrinsicHeightSize.value)
272 assertEquals(IntPxSize(20.dp.toIntPx(), 35.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100273 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
274 }
275
276 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000277 fun testMinIntrinsicHeight_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100278 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800279 val minIntrinsicHeightSize = Ref<IntPxSize>()
280 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100281 val childPosition = Ref<PxPosition>()
282 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000283 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100284 minIntrinsicHeightSize.value = coordinates.size
285 positionedLatch.countDown()
George Mount5e1d3f12020-03-09 17:28:35 -0700286 }
287 ) {
288 ConstrainedBox(DpConstraints(minHeight = 45.dp)) {
289 MinIntrinsicHeight {
290 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700291 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700292 size = childSize,
293 position = childPosition,
294 positionedLatch = positionedLatch
295 )
296 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100297 }
298 }
299 }
300 }
301 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000302 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100303
George Mount8baef7a2020-01-21 13:40:57 -0800304 assertEquals(IntPxSize(20.dp.toIntPx(), 45.dp.toIntPx()), minIntrinsicHeightSize.value)
305 assertEquals(IntPxSize(20.dp.toIntPx(), 45.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100306 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
307 }
308
309 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000310 fun testMaxIntrinsicWidth_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100311 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800312 val maxIntrinsicWidthSize = Ref<IntPxSize>()
313 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100314 val childPosition = Ref<PxPosition>()
315 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000316 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700317 maxIntrinsicWidthSize.value = coordinates.size
318 positionedLatch.countDown()
319 }
320 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700321 ConstrainedBox(DpConstraints(maxWidth = 25.dp)) {
322 MaxIntrinsicWidth {
323 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700324 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700325 size = childSize,
326 position = childPosition,
327 positionedLatch = positionedLatch
328 )
329 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100330 }
331 }
332 }
333 }
334 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000335 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100336
George Mount8baef7a2020-01-21 13:40:57 -0800337 assertEquals(IntPxSize(25.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
338 assertEquals(IntPxSize(25.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100339 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
340 }
341
342 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000343 fun testMaxIntrinsicWidth_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100344 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800345 val maxIntrinsicWidthSize = Ref<IntPxSize>()
346 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100347 val childPosition = Ref<PxPosition>()
348 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000349 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700350 maxIntrinsicWidthSize.value = coordinates.size
351 positionedLatch.countDown()
352 }
353 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700354 ConstrainedBox(DpConstraints(minWidth = 35.dp)) {
355 MaxIntrinsicWidth {
356 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700357 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700358 size = childSize,
359 position = childPosition,
360 positionedLatch = positionedLatch
361 )
362 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100363 }
364 }
365 }
366 }
367 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000368 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100369
George Mount8baef7a2020-01-21 13:40:57 -0800370 assertEquals(IntPxSize(35.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
371 assertEquals(IntPxSize(35.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100372 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
373 }
374
375 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000376 fun testMaxIntrinsicHeight_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100377 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800378 val maxIntrinsicHeightSize = Ref<IntPxSize>()
379 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100380 val childPosition = Ref<PxPosition>()
381 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000382 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700383 maxIntrinsicHeightSize.value = coordinates.size
384 positionedLatch.countDown()
385 }
386 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700387 ConstrainedBox(DpConstraints(maxHeight = 55.dp)) {
388 MaxIntrinsicHeight {
389 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700390 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700391 size = childSize,
392 position = childPosition,
393 positionedLatch = positionedLatch
394 )
395 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100396 }
397 }
398 }
399 }
400 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000401 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100402
George Mount8baef7a2020-01-21 13:40:57 -0800403 assertEquals(IntPxSize(20.dp.toIntPx(), 55.dp.toIntPx()), maxIntrinsicHeightSize.value)
404 assertEquals(IntPxSize(20.dp.toIntPx(), 55.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100405 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
406 }
407
408 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000409 fun testMaxIntrinsicHeight_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100410 val positionedLatch = CountDownLatch(2)
George Mount8baef7a2020-01-21 13:40:57 -0800411 val maxIntrinsicHeightSize = Ref<IntPxSize>()
412 val childSize = Ref<IntPxSize>()
Mihai Popa1257df22019-07-01 21:30:22 +0100413 val childPosition = Ref<PxPosition>()
414 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000415 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700416 maxIntrinsicHeightSize.value = coordinates.size
417 positionedLatch.countDown()
418 }
419 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700420 ConstrainedBox(DpConstraints(minHeight = 65.dp)) {
421 MaxIntrinsicHeight {
422 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp,
Adam Powell999a89b2020-03-11 09:08:07 -0700423 Modifier.saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700424 size = childSize,
425 position = childPosition,
426 positionedLatch = positionedLatch
427 )
428 ) {
Mihai Popa1257df22019-07-01 21:30:22 +0100429 }
430 }
431 }
432 }
433 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000434 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100435
George Mount8baef7a2020-01-21 13:40:57 -0800436 assertEquals(IntPxSize(20.dp.toIntPx(), 65.dp.toIntPx()), maxIntrinsicHeightSize.value)
437 assertEquals(IntPxSize(20.dp.toIntPx(), 65.dp.toIntPx()), childSize.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100438 assertEquals(PxPosition(0.px, 0.px), childPosition.value)
439 }
440
441 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000442 fun testMinIntrinsicWidth_intrinsicMeasurements() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100443 testIntrinsics(@Composable {
444 MinIntrinsicWidth {
445 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp) { }
446 }
447 }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight ->
448 assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0.ipx))
449 assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0.ipx))
450 assertEquals(10.dp.toIntPx(), maxIntrinsicWidth(0.ipx))
451 assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0.ipx))
452 }
453 }
454
455 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000456 fun testMinIntrinsicHeight_intrinsicMeasurements() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100457 testIntrinsics(@Composable {
458 MinIntrinsicHeight {
459 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp) { }
460 }
461 }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight ->
462 assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0.ipx))
463 assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0.ipx))
464 assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0.ipx))
465 assertEquals(40.dp.toIntPx(), maxIntrinsicHeight(0.ipx))
466 }
467 }
468
469 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000470 fun testMaxIntrinsicWidth_intrinsicMeasurements() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100471 testIntrinsics(@Composable {
472 MaxIntrinsicWidth {
473 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp) { }
474 }
475 }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight ->
476 assertEquals(30.dp.toIntPx(), minIntrinsicWidth(0.ipx))
477 assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0.ipx))
478 assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0.ipx))
479 assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0.ipx))
480 }
481 }
482
483 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000484 fun testMaxIntrinsicHeight_intrinsicMeasurements() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100485 testIntrinsics(@Composable {
486 MaxIntrinsicHeight {
487 FixedIntrinsicsBox(10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp) { }
488 }
489 }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight ->
490 assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0.ipx))
491 assertEquals(60.dp.toIntPx(), minIntrinsicHeight(0.ipx))
492 assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0.ipx))
493 assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0.ipx))
494 }
495 }
496}
497
498@Composable
499private fun FixedIntrinsicsBox(
500 minIntrinsicWidth: Dp,
501 width: Dp,
502 maxIntrinsicWidth: Dp,
503 minIntrinsicHeight: Dp,
George Mountec81f522019-08-14 15:30:26 -0700504 height: Dp,
505 maxIntrinsicHeight: Dp,
George Mount5e1d3f12020-03-09 17:28:35 -0700506 modifier: Modifier = Modifier.None,
Jim1fbb1c12019-07-31 12:43:29 -0700507 children: @Composable() () -> Unit
Mihai Popa1257df22019-07-01 21:30:22 +0100508) {
Mihai Popaca1c4b92019-10-03 19:16:06 +0100509 Layout(
510 children,
Anastasia Soboleva9474ff82020-02-19 19:02:15 +0000511 minIntrinsicWidthMeasureBlock = { _, _, _ -> minIntrinsicWidth.toIntPx() },
512 minIntrinsicHeightMeasureBlock = { _, _, _ -> minIntrinsicHeight.toIntPx() },
513 maxIntrinsicWidthMeasureBlock = { _, _, _ -> maxIntrinsicWidth.toIntPx() },
George Mount5e1d3f12020-03-09 17:28:35 -0700514 maxIntrinsicHeightMeasureBlock = { _, _, _ -> maxIntrinsicHeight.toIntPx() },
515 modifier = modifier
Anastasia Soboleva9474ff82020-02-19 19:02:15 +0000516 ) { _, constraints, _ ->
Mihai Popaca1c4b92019-10-03 19:16:06 +0100517 layout(
518 width.toIntPx().coerceIn(constraints.minWidth, constraints.maxWidth),
519 height.toIntPx().coerceIn(constraints.minHeight, constraints.maxHeight)
520 ) {}
Mihai Popa1257df22019-07-01 21:30:22 +0100521 }
522}