Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +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 | |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 17 | package androidx.compose.foundation.layout |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 18 | |
Leland Richardson | a5396b4 | 2019-10-11 17:34:55 -0700 | [diff] [blame] | 19 | import androidx.compose.Composable |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 20 | import androidx.test.filters.SmallTest |
| 21 | import androidx.ui.core.Constraints |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 22 | import androidx.ui.core.Layout |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 23 | import androidx.ui.core.Modifier |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 24 | import androidx.ui.core.Ref |
Louis Pullen-Freilich | f434a13 | 2020-07-22 14:19:24 +0100 | [diff] [blame] | 25 | import androidx.compose.ui.geometry.Offset |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 26 | import androidx.ui.unit.IntSize |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 27 | import androidx.ui.unit.dp |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame] | 28 | import org.junit.Assert.assertEquals |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 29 | import org.junit.Assert.assertTrue |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 30 | import org.junit.Test |
| 31 | import org.junit.runner.RunWith |
| 32 | import org.junit.runners.JUnit4 |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 33 | import java.util.concurrent.CountDownLatch |
| 34 | import java.util.concurrent.TimeUnit |
| 35 | |
| 36 | @SmallTest |
| 37 | @RunWith(JUnit4::class) |
Mihai Popa | c9af288 | 2020-03-16 17:55:00 +0000 | [diff] [blame] | 38 | class LayoutAspectRatioTest : LayoutTest() { |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 39 | @Test |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 40 | fun testAspectRatioModifier_intrinsicDimensions() = with(density) { |
Leland Richardson | a5396b4 | 2019-10-11 17:34:55 -0700 | [diff] [blame] | 41 | testIntrinsics(@Composable { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 42 | Container(modifier = Modifier.aspectRatio(2f), width = 30.dp, height = 40.dp) { } |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 43 | }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight -> |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 44 | assertEquals(40, minIntrinsicWidth(20)) |
| 45 | assertEquals(40, maxIntrinsicWidth(20)) |
| 46 | assertEquals(20, minIntrinsicHeight(40)) |
| 47 | assertEquals(20, maxIntrinsicHeight(40)) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 48 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 49 | assertEquals(30.dp.toIntPx(), minIntrinsicWidth(Constraints.Infinity)) |
| 50 | assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(Constraints.Infinity)) |
| 51 | assertEquals(40.dp.toIntPx(), minIntrinsicHeight(Constraints.Infinity)) |
| 52 | assertEquals(40.dp.toIntPx(), maxIntrinsicHeight(Constraints.Infinity)) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | @Test(expected = IllegalArgumentException::class) |
| 57 | fun testAspectRatioModifier_zeroRatio() { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 58 | Modifier.aspectRatio(0f) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Test(expected = IllegalArgumentException::class) |
| 62 | fun testAspectRatioModifier_negativeRatio() { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 63 | Modifier.aspectRatio(-2f) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test |
| 67 | fun testAspectRatio_sizesCorrectly() { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 68 | assertEquals(IntSize(30, 30), getSize(1f, Constraints(maxWidth = 30))) |
| 69 | assertEquals(IntSize(30, 15), getSize(2f, Constraints(maxWidth = 30))) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 70 | assertEquals( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 71 | IntSize(10, 10), |
| 72 | getSize(1f, Constraints(maxWidth = 30, maxHeight = 10)) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 73 | ) |
| 74 | assertEquals( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 75 | IntSize(20, 10), |
| 76 | getSize(2f, Constraints(maxWidth = 30, maxHeight = 10)) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 77 | ) |
| 78 | assertEquals( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 79 | IntSize(10, 5), |
| 80 | getSize(2f, Constraints(minWidth = 10, minHeight = 5)) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 81 | ) |
| 82 | assertEquals( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 83 | IntSize(20, 10), |
| 84 | getSize(2f, Constraints(minWidth = 5, minHeight = 10)) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 85 | ) |
| 86 | } |
| 87 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 88 | private fun getSize(aspectRatio: Float, childContraints: Constraints): IntSize { |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 89 | val positionedLatch = CountDownLatch(1) |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 90 | val size = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 91 | val position = Ref<Offset>() |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 92 | show { |
Leland Richardson | a5396b4 | 2019-10-11 17:34:55 -0700 | [diff] [blame] | 93 | Layout(@Composable { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 94 | Container( |
| 95 | Modifier.aspectRatio(aspectRatio) + |
| 96 | Modifier.saveLayoutInfo(size, position, positionedLatch) |
George Mount | 5e1d3f1 | 2020-03-09 17:28:35 -0700 | [diff] [blame] | 97 | ) { |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 98 | } |
Anastasia Soboleva | 5e382dd | 2020-06-17 21:51:38 +0100 | [diff] [blame] | 99 | }) { measurables, incomingConstraints -> |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 100 | require(measurables.isNotEmpty()) |
| 101 | val placeable = measurables.first().measure(childContraints) |
| 102 | layout(incomingConstraints.maxWidth, incomingConstraints.maxHeight) { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 103 | placeable.place(0, 0) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
Mihai Popa | 2554ba8 | 2020-03-13 19:03:52 +0000 | [diff] [blame] | 107 | assertTrue(positionedLatch.await(1, TimeUnit.SECONDS)) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 108 | return size.value!! |
| 109 | } |
| 110 | } |