1 /*
2  * Copyright 2021 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.compose.ui.inspection
18 
19 import android.os.Build.VERSION.SDK_INT
20 import android.os.Build.VERSION_CODES.VANILLA_ICE_CREAM
21 import android.view.View
22 import android.view.ViewGroup
23 import androidx.compose.ui.ComposeUiFlags.isAdaptiveRefreshRateEnabled
24 import androidx.compose.ui.ExperimentalComposeUiApi
25 import androidx.compose.ui.inspection.rules.ComposeInspectionRule
26 import androidx.compose.ui.inspection.rules.sendCommand
27 import androidx.compose.ui.inspection.testdata.AndroidViewTestActivity
28 import androidx.compose.ui.inspection.util.GetComposablesCommand
29 import androidx.compose.ui.inspection.util.flatten
30 import androidx.compose.ui.inspection.util.toMap
31 import androidx.test.filters.LargeTest
32 import com.google.common.truth.Truth.assertThat
33 import kotlinx.coroutines.runBlocking
34 import org.junit.Rule
35 import org.junit.Test
36 
37 @LargeTest
38 class AndroidViewTest {
39     @get:Rule val rule = ComposeInspectionRule(AndroidViewTestActivity::class)
40 
41     val isArrEnabled =
42         @OptIn(ExperimentalComposeUiApi::class) isAdaptiveRefreshRateEnabled &&
43             SDK_INT >= VANILLA_ICE_CREAM
44 
45     @Test
<lambda>null46     fun androidView(): Unit = runBlocking {
47         val app =
48             rule.inspectorTester
49                 .sendCommand(GetComposablesCommand(rule.rootId, skipSystemComposables = false))
50                 .getComposablesResponse
51         val strings = app.stringsList.toMap()
52         val composeNode =
53             app.rootsList
54                 .flatMap { it.nodesList }
55                 .flatMap { it.flatten() }
56                 .filter { it.viewId != 0L }
57                 .single()
58         assertThat(strings[composeNode.name]).isEqualTo("ComposeNode")
59         val rootView = rule.rootsForTest.single().view
60         val androidViewsHandler =
61             rootView.let {
62                 if (isArrEnabled) {
63                     it.childAt(1)
64                 } else {
65                     it.childAt(0)
66                 }
67             }
68         val viewFactoryHolder = androidViewsHandler.childAt(0)
69         assertThat(composeNode.viewId).isEqualTo(viewFactoryHolder.uniqueDrawingId)
70     }
71 
childAtnull72     private fun View.childAt(index: Int): View = (this as ViewGroup).getChildAt(index)
73 }
74