1 /*
2  * Copyright (C) 2024 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 com.android.compose.animation.scene.testing
18 
19 import androidx.compose.animation.core.LinearEasing
20 import androidx.compose.animation.core.tween
21 import androidx.compose.foundation.layout.Box
22 import androidx.compose.foundation.layout.size
23 import androidx.compose.ui.Modifier
24 import androidx.compose.ui.test.junit4.createComposeRule
25 import androidx.compose.ui.unit.dp
26 import androidx.test.ext.junit.runners.AndroidJUnit4
27 import com.android.compose.animation.scene.Scale
28 import com.android.compose.animation.scene.TestElements
29 import com.android.compose.animation.scene.TestScenes.SceneA
30 import com.android.compose.animation.scene.TestScenes.SceneB
31 import com.android.compose.animation.scene.testTransition
32 import com.google.common.truth.Truth.assertThat
33 import org.junit.Rule
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 
37 @RunWith(AndroidJUnit4::class)
38 class ElementStateAccessTest {
39 
40     @get:Rule val rule = createComposeRule()
41 
42     @Test
testElementStateAccessnull43     fun testElementStateAccess() {
44         rule.testTransition(
45             fromSceneContent = { Box(Modifier.element(TestElements.Foo).size(50.dp)) },
46             toSceneContent = { Box(Modifier) },
47             transition = {
48                 spec = tween(durationMillis = 16 * 4, easing = LinearEasing)
49                 fade(TestElements.Foo)
50                 scaleDraw(TestElements.Foo, scaleX = 0f, scaleY = 0f)
51             },
52             fromScene = SceneA,
53             toScene = SceneB,
54         ) {
55             before {
56                 val semanticNode = onElement(TestElements.Foo).fetchSemanticsNode()
57                 assertThat(semanticNode.lastAlphaForTesting).isEqualTo(1f)
58                 assertThat(semanticNode.lastScaleForTesting).isEqualTo(Scale(1f, 1f))
59             }
60 
61             at(32) {
62                 val semanticNode = onElement(TestElements.Foo).fetchSemanticsNode()
63                 assertThat(semanticNode.lastAlphaForTesting).isEqualTo(0.5f)
64                 assertThat(semanticNode.lastScaleForTesting).isEqualTo(Scale(0.5f, 0.5f))
65             }
66 
67             at(64) {
68                 val semanticNode = onElement(TestElements.Foo).fetchSemanticsNode()
69                 assertThat(semanticNode.lastAlphaForTesting).isEqualTo(0f)
70                 assertThat(semanticNode.lastScaleForTesting).isEqualTo(Scale(0f, 0f))
71             }
72             after { onElement(TestElements.Foo).assertDoesNotExist() }
73         }
74     }
75 }
76