[go: nahoru, domu]

blob: 70974e15228fbbdd5025b79d72bf216beb6ee10c [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 Popa8b5b06b2020-03-27 14:34:39 +000020import androidx.compose.emptyContent
Mihai Popa1257df22019-07-01 21:30:22 +010021import androidx.test.filters.SmallTest
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 Mount8f237572020-04-30 12:08:30 -070026import androidx.ui.core.constrainHeight
27import androidx.ui.core.constrainWidth
George Mount5e1d3f12020-03-09 17:28:35 -070028import androidx.ui.core.onChildPositioned
George Mount8f237572020-04-30 12:08:30 -070029import androidx.ui.geometry.Offset
Mihai Popa1257df22019-07-01 21:30:22 +010030import androidx.ui.layout.DpConstraints
Mihai Popaa22885d2020-05-26 18:31:21 +010031import androidx.ui.layout.ExperimentalLayout
Anastasia Sobolevadb738582020-03-27 16:06:33 +000032import androidx.ui.layout.Stack
Mihai Popa8b5b06b2020-03-27 14:34:39 +000033import androidx.ui.layout.IntrinsicSize
34import androidx.ui.layout.preferredHeight
35import androidx.ui.layout.preferredWidth
George Mount842c8c12020-01-08 16:03:42 -080036import androidx.ui.unit.Dp
George Mount8f237572020-04-30 12:08:30 -070037import androidx.ui.unit.IntSize
George Mount842c8c12020-01-08 16:03:42 -080038import androidx.ui.unit.dp
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)
Mihai Popaa22885d2020-05-26 18:31:21 +010049@OptIn(ExperimentalLayout::class)
Mihai Popa1257df22019-07-01 21:30:22 +010050class IntrinsicTest : LayoutTest() {
51 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000052 fun testMinIntrinsicWidth() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +010053 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -070054 val minIntrinsicWidthSize = Ref<IntSize>()
55 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -070056 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +010057 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +000058 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -070059 minIntrinsicWidthSize.value = coordinates.size
60 positionedLatch.countDown()
61 }
62 ) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +000063 FixedIntrinsicsBox(
64 Modifier.preferredWidth(IntrinsicSize.Min).saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -070065 size = childSize,
66 position = childPosition,
67 positionedLatch = positionedLatch
Mihai Popa8b5b06b2020-03-27 14:34:39 +000068 ),
69 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
70 )
Mihai Popa1257df22019-07-01 21:30:22 +010071 }
72 }
Mihai Popa2554ba82020-03-13 19:03:52 +000073 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +010074
George Mount8f237572020-04-30 12:08:30 -070075 assertEquals(IntSize(10.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
76 assertEquals(IntSize(10.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -070077 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +010078 }
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 Mount8f237572020-04-30 12:08:30 -070083 val minIntrinsicHeightSize = Ref<IntSize>()
84 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -070085 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +010086 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 ) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +000092 FixedIntrinsicsBox(
93 Modifier.preferredHeight(IntrinsicSize.Min).saveLayoutInfo(
94 size = childSize,
95 position = childPosition,
96 positionedLatch = positionedLatch
97 ),
98 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
99 )
Mihai Popa1257df22019-07-01 21:30:22 +0100100 }
101 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000102 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100103
George Mount8f237572020-04-30 12:08:30 -0700104 assertEquals(IntSize(20.dp.toIntPx(), 40.dp.toIntPx()), minIntrinsicHeightSize.value)
105 assertEquals(IntSize(20.dp.toIntPx(), 40.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700106 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100107 }
108
109 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000110 fun testMaxIntrinsicWidth() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100111 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700112 val maxIntrinsicWidthSize = Ref<IntSize>()
113 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700114 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100115 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000116 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700117 maxIntrinsicWidthSize.value = coordinates.size
118 positionedLatch.countDown()
119 }
120 ) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000121 FixedIntrinsicsBox(
122 Modifier.preferredWidth(IntrinsicSize.Max).saveLayoutInfo(
123 size = childSize,
124 position = childPosition,
125 positionedLatch = positionedLatch
126 ),
127 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
128 )
Mihai Popa1257df22019-07-01 21:30:22 +0100129 }
130 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000131 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100132
George Mount8f237572020-04-30 12:08:30 -0700133 assertEquals(IntSize(30.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
134 assertEquals(IntSize(30.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700135 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100136 }
137
138 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000139 fun testMaxIntrinsicHeight() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100140 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700141 val maxIntrinsicHeightSize = Ref<IntSize>()
142 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700143 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100144 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000145 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700146 maxIntrinsicHeightSize.value = coordinates.size
147 positionedLatch.countDown()
148 }
149 ) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000150 FixedIntrinsicsBox(
151 Modifier.preferredHeight(IntrinsicSize.Max).saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700152 size = childSize,
153 position = childPosition,
154 positionedLatch = positionedLatch
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000155 ),
156 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
157 )
Mihai Popa1257df22019-07-01 21:30:22 +0100158 }
159 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000160 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100161
George Mount8f237572020-04-30 12:08:30 -0700162 assertEquals(IntSize(20.dp.toIntPx(), 60.dp.toIntPx()), maxIntrinsicHeightSize.value)
163 assertEquals(IntSize(20.dp.toIntPx(), 60.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700164 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100165 }
166
167 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000168 fun testMinIntrinsicWidth_respectsIncomingMaxConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100169 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700170 val minIntrinsicWidthSize = Ref<IntSize>()
171 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700172 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100173 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000174 Stack {
Adam Powell999a89b2020-03-11 09:08:07 -0700175 ConstrainedBox(
176 DpConstraints(maxWidth = 5.dp),
177 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100178 minIntrinsicWidthSize.value = coordinates.size
179 positionedLatch.countDown()
Adam Powell999a89b2020-03-11 09:08:07 -0700180 }
181 ) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000182 FixedIntrinsicsBox(
183 Modifier.preferredWidth(IntrinsicSize.Min).saveLayoutInfo(
184 size = childSize,
185 position = childPosition,
186 positionedLatch = positionedLatch
187 ),
188 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
189 )
Mihai Popa1257df22019-07-01 21:30:22 +0100190 }
191 }
192 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000193 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100194
George Mount8f237572020-04-30 12:08:30 -0700195 assertEquals(IntSize(5.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
196 assertEquals(IntSize(5.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700197 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100198 }
199
200 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000201 fun testMinIntrinsicWidth_respectsIncomingMinConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100202 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700203 val minIntrinsicWidthSize = Ref<IntSize>()
204 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700205 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100206 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000207 Stack {
George Mount5e1d3f12020-03-09 17:28:35 -0700208 ConstrainedBox(
209 DpConstraints(minWidth = 15.dp),
Adam Powell999a89b2020-03-11 09:08:07 -0700210 modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100211 minIntrinsicWidthSize.value = coordinates.size
212 positionedLatch.countDown()
George Mount5e1d3f12020-03-09 17:28:35 -0700213 }
214 ) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000215 FixedIntrinsicsBox(
216 Modifier.preferredWidth(IntrinsicSize.Min).saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700217 size = childSize,
218 position = childPosition,
219 positionedLatch = positionedLatch
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000220 ),
221 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
222 )
Mihai Popa1257df22019-07-01 21:30:22 +0100223 }
224 }
225 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000226 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100227
George Mount8f237572020-04-30 12:08:30 -0700228 assertEquals(IntSize(15.dp.toIntPx(), 50.dp.toIntPx()), minIntrinsicWidthSize.value)
229 assertEquals(IntSize(15.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700230 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100231 }
232
233 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000234 fun testMinIntrinsicHeight_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100235 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700236 val minIntrinsicHeightSize = Ref<IntSize>()
237 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700238 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100239 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000240 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700241 minIntrinsicHeightSize.value = coordinates.size
242 positionedLatch.countDown()
243 }
244 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700245 ConstrainedBox(DpConstraints(maxHeight = 35.dp)) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000246 FixedIntrinsicsBox(
247 Modifier.preferredHeight(IntrinsicSize.Min).saveLayoutInfo(
248 size = childSize,
249 position = childPosition,
250 positionedLatch = positionedLatch
251 ),
252 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
253 )
Mihai Popa1257df22019-07-01 21:30:22 +0100254 }
255 }
256 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000257 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100258
George Mount8f237572020-04-30 12:08:30 -0700259 assertEquals(IntSize(20.dp.toIntPx(), 35.dp.toIntPx()), minIntrinsicHeightSize.value)
260 assertEquals(IntSize(20.dp.toIntPx(), 35.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700261 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100262 }
263
264 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000265 fun testMinIntrinsicHeight_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100266 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700267 val minIntrinsicHeightSize = Ref<IntSize>()
268 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700269 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100270 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000271 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Mihai Popa1257df22019-07-01 21:30:22 +0100272 minIntrinsicHeightSize.value = coordinates.size
273 positionedLatch.countDown()
George Mount5e1d3f12020-03-09 17:28:35 -0700274 }
275 ) {
276 ConstrainedBox(DpConstraints(minHeight = 45.dp)) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000277 FixedIntrinsicsBox(
278 Modifier.preferredHeight(IntrinsicSize.Min).saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700279 size = childSize,
280 position = childPosition,
281 positionedLatch = positionedLatch
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000282 ),
283 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
284 )
Mihai Popa1257df22019-07-01 21:30:22 +0100285 }
286 }
287 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000288 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100289
George Mount8f237572020-04-30 12:08:30 -0700290 assertEquals(IntSize(20.dp.toIntPx(), 45.dp.toIntPx()), minIntrinsicHeightSize.value)
291 assertEquals(IntSize(20.dp.toIntPx(), 45.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700292 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100293 }
294
295 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000296 fun testMaxIntrinsicWidth_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100297 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700298 val maxIntrinsicWidthSize = Ref<IntSize>()
299 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700300 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100301 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000302 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700303 maxIntrinsicWidthSize.value = coordinates.size
304 positionedLatch.countDown()
305 }
306 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700307 ConstrainedBox(DpConstraints(maxWidth = 25.dp)) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000308 FixedIntrinsicsBox(
309 Modifier.preferredWidth(IntrinsicSize.Max).saveLayoutInfo(
310 size = childSize,
311 position = childPosition,
312 positionedLatch = positionedLatch
313 ),
314 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
315 )
Mihai Popa1257df22019-07-01 21:30:22 +0100316 }
317 }
318 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000319 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100320
George Mount8f237572020-04-30 12:08:30 -0700321 assertEquals(IntSize(25.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
322 assertEquals(IntSize(25.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700323 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100324 }
325
326 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000327 fun testMaxIntrinsicWidth_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100328 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700329 val maxIntrinsicWidthSize = Ref<IntSize>()
330 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700331 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100332 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000333 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700334 maxIntrinsicWidthSize.value = coordinates.size
335 positionedLatch.countDown()
336 }
337 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700338 ConstrainedBox(DpConstraints(minWidth = 35.dp)) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000339 FixedIntrinsicsBox(
340 Modifier.preferredWidth(IntrinsicSize.Max).saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700341 size = childSize,
342 position = childPosition,
343 positionedLatch = positionedLatch
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000344 ),
345 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
346 )
Mihai Popa1257df22019-07-01 21:30:22 +0100347 }
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 Mount8f237572020-04-30 12:08:30 -0700352 assertEquals(IntSize(35.dp.toIntPx(), 50.dp.toIntPx()), maxIntrinsicWidthSize.value)
353 assertEquals(IntSize(35.dp.toIntPx(), 50.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700354 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100355 }
356
357 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000358 fun testMaxIntrinsicHeight_respectsMaxIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100359 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700360 val maxIntrinsicHeightSize = Ref<IntSize>()
361 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700362 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100363 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000364 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700365 maxIntrinsicHeightSize.value = coordinates.size
366 positionedLatch.countDown()
367 }
368 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700369 ConstrainedBox(DpConstraints(maxHeight = 55.dp)) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000370 FixedIntrinsicsBox(
371 Modifier.preferredHeight(IntrinsicSize.Max).saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700372 size = childSize,
373 position = childPosition,
374 positionedLatch = positionedLatch
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000375 ),
376 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
377 )
Mihai Popa1257df22019-07-01 21:30:22 +0100378 }
379 }
380 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000381 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100382
George Mount8f237572020-04-30 12:08:30 -0700383 assertEquals(IntSize(20.dp.toIntPx(), 55.dp.toIntPx()), maxIntrinsicHeightSize.value)
384 assertEquals(IntSize(20.dp.toIntPx(), 55.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700385 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100386 }
387
388 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000389 fun testMaxIntrinsicHeight_respectsMinIncomingConstraints() = with(density) {
Mihai Popa1257df22019-07-01 21:30:22 +0100390 val positionedLatch = CountDownLatch(2)
George Mount8f237572020-04-30 12:08:30 -0700391 val maxIntrinsicHeightSize = Ref<IntSize>()
392 val childSize = Ref<IntSize>()
Nader Jawad6df06122020-06-03 15:27:08 -0700393 val childPosition = Ref<Offset>()
Mihai Popa1257df22019-07-01 21:30:22 +0100394 show {
Anastasia Sobolevadb738582020-03-27 16:06:33 +0000395 Stack(modifier = Modifier.onChildPositioned { coordinates: LayoutCoordinates ->
Adam Powell999a89b2020-03-11 09:08:07 -0700396 maxIntrinsicHeightSize.value = coordinates.size
397 positionedLatch.countDown()
398 }
399 ) {
George Mount5e1d3f12020-03-09 17:28:35 -0700400 ConstrainedBox(DpConstraints(minHeight = 65.dp)) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000401 FixedIntrinsicsBox(
402 Modifier.preferredHeight(IntrinsicSize.Max).saveLayoutInfo(
George Mount5e1d3f12020-03-09 17:28:35 -0700403 size = childSize,
404 position = childPosition,
405 positionedLatch = positionedLatch
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000406 ),
407 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
408 )
Mihai Popa1257df22019-07-01 21:30:22 +0100409 }
410 }
411 }
Mihai Popa2554ba82020-03-13 19:03:52 +0000412 assertTrue(positionedLatch.await(1, TimeUnit.SECONDS))
Mihai Popa1257df22019-07-01 21:30:22 +0100413
George Mount8f237572020-04-30 12:08:30 -0700414 assertEquals(IntSize(20.dp.toIntPx(), 65.dp.toIntPx()), maxIntrinsicHeightSize.value)
415 assertEquals(IntSize(20.dp.toIntPx(), 65.dp.toIntPx()), childSize.value)
Nader Jawad6df06122020-06-03 15:27:08 -0700416 assertEquals(Offset(0f, 0f), childPosition.value)
Mihai Popa1257df22019-07-01 21:30:22 +0100417 }
418
419 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000420 fun testMinIntrinsicWidth_intrinsicMeasurements() = with(density) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000421 testIntrinsics({
422 FixedIntrinsicsBox(
423 Modifier.preferredWidth(IntrinsicSize.Min), 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
424 )
Mihai Popa1257df22019-07-01 21:30:22 +0100425 }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight ->
George Mount8f237572020-04-30 12:08:30 -0700426 assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0))
427 assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0))
428 assertEquals(10.dp.toIntPx(), maxIntrinsicWidth(0))
429 assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0))
Mihai Popa1257df22019-07-01 21:30:22 +0100430 }
431 }
432
433 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000434 fun testMinIntrinsicHeight_intrinsicMeasurements() = with(density) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000435 testIntrinsics({
436 FixedIntrinsicsBox(
437 Modifier.preferredHeight(IntrinsicSize.Min),
438 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
439 )
Mihai Popa1257df22019-07-01 21:30:22 +0100440 }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight ->
George Mount8f237572020-04-30 12:08:30 -0700441 assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0))
442 assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0))
443 assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0))
444 assertEquals(40.dp.toIntPx(), maxIntrinsicHeight(0))
Mihai Popa1257df22019-07-01 21:30:22 +0100445 }
446 }
447
448 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000449 fun testMaxIntrinsicWidth_intrinsicMeasurements() = with(density) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000450 testIntrinsics({
451 FixedIntrinsicsBox(
452 Modifier.preferredWidth(IntrinsicSize.Max), 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
453 )
Mihai Popa1257df22019-07-01 21:30:22 +0100454 }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight ->
George Mount8f237572020-04-30 12:08:30 -0700455 assertEquals(30.dp.toIntPx(), minIntrinsicWidth(0))
456 assertEquals(40.dp.toIntPx(), minIntrinsicHeight(0))
457 assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0))
458 assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0))
Mihai Popa1257df22019-07-01 21:30:22 +0100459 }
460 }
461
462 @Test
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000463 fun testMaxIntrinsicHeight_intrinsicMeasurements() = with(density) {
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000464 testIntrinsics({
465 FixedIntrinsicsBox(
466 Modifier.preferredHeight(IntrinsicSize.Max),
467 10.dp, 20.dp, 30.dp, 40.dp, 50.dp, 60.dp
468 )
Mihai Popa1257df22019-07-01 21:30:22 +0100469 }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight ->
George Mount8f237572020-04-30 12:08:30 -0700470 assertEquals(10.dp.toIntPx(), minIntrinsicWidth(0))
471 assertEquals(60.dp.toIntPx(), minIntrinsicHeight(0))
472 assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(0))
473 assertEquals(60.dp.toIntPx(), maxIntrinsicHeight(0))
Mihai Popa1257df22019-07-01 21:30:22 +0100474 }
475 }
476}
477
478@Composable
479private fun FixedIntrinsicsBox(
Adam Powellb6d8db22020-04-02 12:40:03 -0700480 modifier: Modifier = Modifier,
Mihai Popa1257df22019-07-01 21:30:22 +0100481 minIntrinsicWidth: Dp,
482 width: Dp,
483 maxIntrinsicWidth: Dp,
484 minIntrinsicHeight: Dp,
George Mountec81f522019-08-14 15:30:26 -0700485 height: Dp,
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000486 maxIntrinsicHeight: Dp
Mihai Popa1257df22019-07-01 21:30:22 +0100487) {
Mihai Popaca1c4b92019-10-03 19:16:06 +0100488 Layout(
Mihai Popa8b5b06b2020-03-27 14:34:39 +0000489 emptyContent(),
Anastasia Soboleva5e382dd2020-06-17 21:51:38 +0100490 minIntrinsicWidthMeasureBlock = { _, _ -> minIntrinsicWidth.toIntPx() },
491 minIntrinsicHeightMeasureBlock = { _, _ -> minIntrinsicHeight.toIntPx() },
492 maxIntrinsicWidthMeasureBlock = { _, _ -> maxIntrinsicWidth.toIntPx() },
493 maxIntrinsicHeightMeasureBlock = { _, _ -> maxIntrinsicHeight.toIntPx() },
George Mount5e1d3f12020-03-09 17:28:35 -0700494 modifier = modifier
Anastasia Soboleva5e382dd2020-06-17 21:51:38 +0100495 ) { _, constraints ->
Mihai Popaca1c4b92019-10-03 19:16:06 +0100496 layout(
George Mount8f237572020-04-30 12:08:30 -0700497 constraints.constrainWidth(width.toIntPx()),
498 constraints.constrainHeight(height.toIntPx())
Mihai Popaca1c4b92019-10-03 19:16:06 +0100499 ) {}
Mihai Popa1257df22019-07-01 21:30:22 +0100500 }
501}