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