Doris Liu | 881decd | 2020-06-23 15:12:40 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020 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.animation |
| 18 | |
| 19 | import androidx.animation.LinearOutSlowInEasing |
| 20 | import androidx.animation.tween |
| 21 | import androidx.compose.foundation.Box |
| 22 | import androidx.compose.foundation.layout.size |
| 23 | import androidx.compose.getValue |
| 24 | import androidx.compose.mutableStateOf |
| 25 | import androidx.compose.setValue |
| 26 | import androidx.test.filters.MediumTest |
| 27 | import androidx.ui.core.Constraints |
| 28 | import androidx.ui.core.DensityAmbient |
| 29 | import androidx.ui.core.LayoutDirection |
| 30 | import androidx.ui.core.LayoutModifier |
| 31 | import androidx.ui.core.Measurable |
| 32 | import androidx.ui.core.MeasureScope |
| 33 | import androidx.ui.core.Modifier |
| 34 | import androidx.ui.test.createComposeRule |
| 35 | import androidx.ui.test.runOnIdle |
| 36 | import androidx.ui.test.waitForIdle |
| 37 | import androidx.ui.unit.dp |
| 38 | import junit.framework.TestCase.assertEquals |
| 39 | import org.junit.Rule |
| 40 | import org.junit.Test |
| 41 | import org.junit.runner.RunWith |
| 42 | import org.junit.runners.JUnit4 |
| 43 | |
| 44 | @RunWith(JUnit4::class) |
| 45 | @MediumTest |
| 46 | class AnimationModifierTest { |
| 47 | |
| 48 | @get:Rule |
| 49 | val composeTestRule = createComposeRule() |
| 50 | |
| 51 | @Test |
| 52 | fun animateContentSizeTest() { |
| 53 | val startWidth = 100 |
| 54 | val endWidth = 150 |
| 55 | val startHeight = 400 |
| 56 | val endHeight = 200 |
| 57 | var width by mutableStateOf(startWidth) |
| 58 | var height by mutableStateOf(startHeight) |
| 59 | |
| 60 | var density = 0f |
| 61 | val testModifier by mutableStateOf(TestModifier()) |
| 62 | |
| 63 | composeTestRule.clockTestRule.pauseClock() |
| 64 | composeTestRule.setContent { |
| 65 | Box( |
| 66 | testModifier + Modifier.animateContentSize( |
| 67 | tween( |
| 68 | 200, easing = LinearOutSlowInEasing |
| 69 | ) |
| 70 | ).size( |
| 71 | width.dp, height.dp |
| 72 | ) |
| 73 | ) |
| 74 | density = DensityAmbient.current.density |
| 75 | } |
| 76 | |
| 77 | runOnIdle { |
| 78 | width = endWidth |
| 79 | height = endHeight |
| 80 | } |
| 81 | waitForIdle() |
| 82 | |
| 83 | for (i in 0..200 step 20) { |
| 84 | val fraction = LinearOutSlowInEasing.invoke(i / 200f) |
| 85 | assertEquals( |
| 86 | density * (startWidth * (1 - fraction) + endWidth * fraction), |
| 87 | testModifier.width.toFloat(), 1f |
| 88 | ) |
| 89 | |
| 90 | assertEquals( |
| 91 | density * (startHeight * (1 - fraction) + endHeight * fraction), |
| 92 | testModifier.height.toFloat(), 1f |
| 93 | ) |
| 94 | |
| 95 | composeTestRule.clockTestRule.advanceClock(20) |
| 96 | waitForIdle() |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private class TestModifier : LayoutModifier { |
| 102 | var width: Int = 0 |
| 103 | var height: Int = 0 |
| 104 | override fun MeasureScope.measure( |
| 105 | measurable: Measurable, |
| 106 | constraints: Constraints, |
| 107 | layoutDirection: LayoutDirection |
| 108 | ): MeasureScope.MeasureResult { |
| 109 | val placeable = measurable.measure(constraints) |
| 110 | width = placeable.width |
| 111 | height = placeable.height |
| 112 | return layout(width, height) { |
| 113 | placeable.place(0, 0) |
| 114 | } |
| 115 | } |
| 116 | } |