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 | |
| 17 | package androidx.ui.layout.test |
| 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 |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 23 | import androidx.ui.core.Ref |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 24 | import androidx.ui.layout.Container |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 25 | import androidx.ui.layout.LayoutAspectRatio |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 26 | import androidx.ui.unit.IntPx |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 27 | import androidx.ui.unit.IntPxSize |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 28 | import androidx.ui.unit.PxPosition |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 29 | import androidx.ui.unit.dp |
| 30 | import androidx.ui.unit.ipx |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 31 | import androidx.ui.unit.withDensity |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 32 | import org.junit.Assert.assertEquals |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 33 | import org.junit.Test |
| 34 | import org.junit.runner.RunWith |
| 35 | import org.junit.runners.JUnit4 |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 36 | import java.util.concurrent.CountDownLatch |
| 37 | import java.util.concurrent.TimeUnit |
| 38 | |
| 39 | @SmallTest |
| 40 | @RunWith(JUnit4::class) |
| 41 | class AspectRatioModifierTest : LayoutTest() { |
| 42 | @Test |
| 43 | fun testAspectRatioModifier_intrinsicDimensions() = withDensity(density) { |
Leland Richardson | a5396b4 | 2019-10-11 17:34:55 -0700 | [diff] [blame] | 44 | testIntrinsics(@Composable { |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 45 | Container(modifier = LayoutAspectRatio(2f), width = 30.dp, height = 40.dp) { } |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 46 | }) { minIntrinsicWidth, minIntrinsicHeight, maxIntrinsicWidth, maxIntrinsicHeight -> |
| 47 | assertEquals(40.ipx, minIntrinsicWidth(20.ipx)) |
| 48 | assertEquals(40.ipx, maxIntrinsicWidth(20.ipx)) |
| 49 | assertEquals(20.ipx, minIntrinsicHeight(40.ipx)) |
| 50 | assertEquals(20.ipx, maxIntrinsicHeight(40.ipx)) |
| 51 | |
| 52 | assertEquals(30.dp.toIntPx(), minIntrinsicWidth(IntPx.Infinity)) |
| 53 | assertEquals(30.dp.toIntPx(), maxIntrinsicWidth(IntPx.Infinity)) |
| 54 | assertEquals(40.dp.toIntPx(), minIntrinsicHeight(IntPx.Infinity)) |
| 55 | assertEquals(40.dp.toIntPx(), maxIntrinsicHeight(IntPx.Infinity)) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | @Test(expected = IllegalArgumentException::class) |
| 60 | fun testAspectRatioModifier_zeroRatio() { |
| 61 | show { |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 62 | Container(LayoutAspectRatio(0f)) { } |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | @Test(expected = IllegalArgumentException::class) |
| 67 | fun testAspectRatioModifier_negativeRatio() { |
| 68 | show { |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 69 | Container(LayoutAspectRatio(-2f)) { } |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | fun testAspectRatio_sizesCorrectly() { |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 75 | assertEquals(IntPxSize(30.ipx, 30.ipx), getSize(1f, Constraints(maxWidth = 30.ipx))) |
| 76 | assertEquals(IntPxSize(30.ipx, 15.ipx), getSize(2f, Constraints(maxWidth = 30.ipx))) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 77 | assertEquals( |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 78 | IntPxSize(10.ipx, 10.ipx), |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 79 | getSize(1f, Constraints(maxWidth = 30.ipx, maxHeight = 10.ipx)) |
| 80 | ) |
| 81 | assertEquals( |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 82 | IntPxSize(20.ipx, 10.ipx), |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 83 | getSize(2f, Constraints(maxWidth = 30.ipx, maxHeight = 10.ipx)) |
| 84 | ) |
| 85 | assertEquals( |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 86 | IntPxSize(10.ipx, 5.ipx), |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 87 | getSize(2f, Constraints(minWidth = 10.ipx, minHeight = 5.ipx)) |
| 88 | ) |
| 89 | assertEquals( |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 90 | IntPxSize(20.ipx, 10.ipx), |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 91 | getSize(2f, Constraints(minWidth = 5.ipx, minHeight = 10.ipx)) |
| 92 | ) |
| 93 | } |
| 94 | |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 95 | private fun getSize(aspectRatio: Float, childContraints: Constraints): IntPxSize { |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 96 | val positionedLatch = CountDownLatch(1) |
George Mount | 8baef7a | 2020-01-21 13:40:57 -0800 | [diff] [blame^] | 97 | val size = Ref<IntPxSize>() |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 98 | val position = Ref<PxPosition>() |
| 99 | show { |
Leland Richardson | a5396b4 | 2019-10-11 17:34:55 -0700 | [diff] [blame] | 100 | Layout(@Composable { |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 101 | Container(LayoutAspectRatio(aspectRatio)) { |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 102 | SaveLayoutInfo(size, position, positionedLatch) |
| 103 | } |
| 104 | }) { measurables, incomingConstraints -> |
| 105 | require(measurables.isNotEmpty()) |
| 106 | val placeable = measurables.first().measure(childContraints) |
| 107 | layout(incomingConstraints.maxWidth, incomingConstraints.maxHeight) { |
| 108 | placeable.place(0.ipx, 0.ipx) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | positionedLatch.await(1, TimeUnit.SECONDS) |
| 113 | return size.value!! |
| 114 | } |
| 115 | } |