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.ui.semantics.SemanticsNode
20 import com.android.compose.animation.scene.Element
21 import com.android.compose.animation.scene.Element.Companion.AlphaUnspecified
22 import com.android.compose.animation.scene.ElementModifier
23 import com.android.compose.animation.scene.Scale
24 
25 val SemanticsNode.lastAlphaForTesting: Float?
<lambda>null26     get() = elementState.lastAlpha.takeIf { it != AlphaUnspecified }
27 
28 val SemanticsNode.lastScaleForTesting: Scale?
<lambda>null29     get() = elementState.lastScale.takeIf { it != Scale.Unspecified }
30 
31 private val SemanticsNode.elementState: Element.State
32     get() {
33         val elementModifier =
34             layoutInfo
35                 .getModifierInfo()
<lambda>null36                 .map { it.modifier }
37                 .filterIsInstance<ElementModifier>()
38                 .firstOrNull()
<lambda>null39         requireNotNull(elementModifier) {
40             "No ElementModifier found. Did you use the Modifier.element(...)?"
41         }
<lambda>null42         return with(elementModifier) {
43             val element =
44                 requireNotNull(layoutImpl.elements[key]) {
45                     "No element found in STL layout with key: ${key.testTag}"
46                 }
47             requireNotNull(element.stateByContent[content.key]) {
48                 "No state found for given content key: ${content.key.testTag}"
49             }
50         }
51     }
52