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