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