[go: nahoru, domu]

blob: b66422a6dffaf737324c09f899b8f539652e0c63 [file] [log] [blame]
Doris Liu881decd2020-06-23 15:12:40 -07001/*
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
Louis Pullen-Freilichbb77c7b2020-07-20 22:23:06 +010017package androidx.compose.animation
Doris Liu881decd2020-06-23 15:12:40 -070018
Louis Pullen-Freilich5bb0c4712020-07-20 22:01:15 +010019import androidx.compose.animation.core.LinearOutSlowInEasing
20import androidx.compose.animation.core.tween
Doris Liu881decd2020-06-23 15:12:40 -070021import androidx.compose.foundation.Box
22import androidx.compose.foundation.layout.size
23import androidx.compose.getValue
24import androidx.compose.mutableStateOf
25import androidx.compose.setValue
26import androidx.test.filters.MediumTest
27import androidx.ui.core.Constraints
28import androidx.ui.core.DensityAmbient
29import androidx.ui.core.LayoutDirection
30import androidx.ui.core.LayoutModifier
31import androidx.ui.core.Measurable
32import androidx.ui.core.MeasureScope
33import androidx.ui.core.Modifier
34import androidx.ui.test.createComposeRule
35import androidx.ui.test.runOnIdle
36import androidx.ui.test.waitForIdle
37import androidx.ui.unit.dp
38import junit.framework.TestCase.assertEquals
39import org.junit.Rule
40import org.junit.Test
41import org.junit.runner.RunWith
42import org.junit.runners.JUnit4
43
44@RunWith(JUnit4::class)
45@MediumTest
46class 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
101private 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}